diff options
author | XANTRONIX Development | 2022-03-05 20:33:23 -0500 |
---|---|---|
committer | XANTRONIX Development | 2022-03-05 20:33:23 -0500 |
commit | 2894d2f9e4e2a646116836aa2b471c33fbba85f5 (patch) | |
tree | 97612113c55f704cf0f3ea3129cc523a49b0f0f2 | |
parent | 769f1c6c4087d53a2b400511baac77b510f16798 (diff) | |
download | xas-2894d2f9e4e2a646116836aa2b471c33fbba85f5.tar.gz xas-2894d2f9e4e2a646116836aa2b471c33fbba85f5.tar.bz2 xas-2894d2f9e4e2a646116836aa2b471c33fbba85f5.zip |
Implement movement in spatial objects
-rw-r--r-- | include/xas/spatial.h | 16 | ||||
-rw-r--r-- | src/spatial.c | 99 |
2 files changed, 82 insertions, 33 deletions
diff --git a/include/xas/spatial.h b/include/xas/spatial.h index ed5157a..f8591df 100644 --- a/include/xas/spatial.h +++ b/include/xas/spatial.h @@ -47,7 +47,11 @@ struct _xas_spatial_object { void *ctx; int flags; - xas_spatial_coord point; + xas_spatial_coord point, + heading; + + float speed; + xas_spatial_object *next; }; @@ -120,6 +124,16 @@ int xas_spatial_object_stop(xas_spatial_object *object); void xas_spatial_object_get_point(xas_spatial_object *object, xas_spatial_coord *point); +float xas_spatial_object_get_speed(xas_spatial_object *object); + +void xas_spatial_object_set_speed(xas_spatial_object *object, float speed); + +void xas_spatial_object_get_heading(xas_spatial_object *object, + xas_spatial_coord *heading); + +void xas_spatial_object_set_heading(xas_spatial_object *object, + xas_spatial_coord heading); + xas_audio_stream *xas_spatial_scene_stream_new(xas_spatial_scene *scene, size_t buffer_size); diff --git a/src/spatial.c b/src/spatial.c index 4d52738..7d71e37 100644 --- a/src/spatial.c +++ b/src/spatial.c @@ -181,6 +181,34 @@ static void buffer_copy(xas_spatial_buffer *buffer, } } +static void object_position(xas_spatial_scene *scene, + xas_spatial_object *object, + xas_spatial_coord point) { + size_t delta_l_old = object->delta_l, + delta_r_old = object->delta_r; + + object->point = point; + + object->distance_l = dist(scene->speaker_l, point); + object->distance_r = dist(scene->speaker_r, point); + object->delta_l = sample_delta(scene, object->distance_l); + object->delta_r = sample_delta(scene, object->distance_r); + object->shift_l = object->delta_l - delta_l_old; + object->shift_r = object->delta_r - delta_r_old; +} + +static void object_update_deltas(xas_spatial_scene *scene) { + xas_spatial_object *obj = scene->first; + + while (obj) { + xas_spatial_object *next = obj->next; + + object_position(scene, obj, obj->point); + + obj = next; + } +} + ssize_t scene_fill(xas_spatial_scene *scene, int16_t *output, size_t count, @@ -188,6 +216,8 @@ ssize_t scene_fill(xas_spatial_scene *scene, xas_spatial_buffer *buffer = scene->buffer; xas_spatial_object *obj = scene->first; + float interval = 1.0f / (float)scene->format.sample_rate; + size_t index_old = buffer->index; int16_t *dest = (int16_t *)(scene->buffer + 1); @@ -221,11 +251,22 @@ ssize_t scene_fill(xas_spatial_scene *scene, } for (i=0; i<readlen; i++) { - int16_t value_l = sample_scale(src[i], obj->distance_l), - value_r = sample_scale(src[i], obj->distance_r); + size_t value_l, index_l, + value_r, index_r; + + if (obj->speed != 0.0f) { + xas_spatial_coord point = obj->point; + + move(&point, + obj->heading, + obj->speed, + interval); - size_t index_l, - index_r; + object_position(scene, obj, point); + } + + value_l = sample_scale(src[i], obj->distance_l); + value_r = sample_scale(src[i], obj->distance_r); if (!within_cone(obj->point, cone_l)) { value_l /= 3.0f; @@ -340,34 +381,6 @@ void xas_spatial_scene_set_observer(xas_spatial_scene *scene, scene->observer.width = width; } -static void object_position(xas_spatial_scene *scene, - xas_spatial_object *object, - xas_spatial_coord point) { - size_t delta_l_old = object->delta_l, - delta_r_old = object->delta_r; - - object->point = point; - - object->distance_l = dist(scene->speaker_l, point); - object->distance_r = dist(scene->speaker_r, point); - object->delta_l = sample_delta(scene, object->distance_l); - object->delta_r = sample_delta(scene, object->distance_r); - object->shift_l = object->delta_l - delta_l_old; - object->shift_r = object->delta_r - delta_r_old; -} - -static void object_update_deltas(xas_spatial_scene *scene) { - xas_spatial_object *obj = scene->first; - - while (obj) { - xas_spatial_object *next = obj->next; - - object_position(scene, obj, obj->point); - - obj = next; - } -} - void xas_spatial_scene_set_speaker_coords(xas_spatial_scene *scene, xas_spatial_coord speaker_l, xas_spatial_coord speaker_r) { @@ -559,6 +572,28 @@ void xas_spatial_object_get_point(xas_spatial_object *object, point->z = object->point.z; } +float xas_spatial_object_get_speed(xas_spatial_object *object) { + return object->speed; +} + +void xas_spatial_object_set_speed(xas_spatial_object *object, float speed) { + object->speed = speed; +} + +void xas_spatial_object_get_heading(xas_spatial_object *object, + xas_spatial_coord *heading) { + heading->x = object->heading.x; + heading->y = object->heading.y; + heading->z = object->heading.z; +} + +void xas_spatial_object_set_heading(xas_spatial_object *object, + xas_spatial_coord heading) { + object->heading.x = heading.x; + object->heading.y = heading.y; + object->heading.z = heading.z; +} + int xas_spatial_object_start(xas_spatial_object *object) { return ((xas_object *)object->ctx)->start((xas_object *)object->ctx); } |