From 45b88124dbf7dd2a9b276b32f47c1daf549f0779 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 9 Feb 2022 15:28:43 -0500 Subject: I wish I could do this to get out of my life --- src/spatial.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/spatial.c (limited to 'src') diff --git a/src/spatial.c b/src/spatial.c new file mode 100644 index 0000000..809fce1 --- /dev/null +++ b/src/spatial.c @@ -0,0 +1,88 @@ +#include +#include +#include + +#include + +static inline float dist(xas_spatial_coord a, xas_spatial_coord b) { + return powf(powf(b.x - a.x, 2.0f) + + powf(b.y - a.y, 2.0f) + + powf(b.z - a.z, 2.0f), 0.5f); +} + +static int buf_realloc(xas_spatial_scene *scene, void *buf) { + float width = scene->observer.width, + speed = scene->speed; + + size_t sample_rate = scene->format.sample_rate, + stride = scene->format.channels * scene->format.sample_size, + count = floorf(width / (speed / sample_rate)); + + if ((buf = realloc(buf, stride * count)) == NULL) { + goto error_realloc; + } + + scene->buf = buf; + scene->buflen = count; + + return 0; + +error_realloc: + return -1; +} + +xas_spatial_scene *xas_spatial_scene_new(xas_audio_format format, + xas_spatial_coord speaker_l, + xas_spatial_coord speaker_r) { + xas_spatial_scene *scene; + + if ((scene = malloc(sizeof(*scene))) == NULL) { + goto error_malloc_scene; + } + + memset(scene, '\0', sizeof(*scene)); + + scene->format = format; + scene->speaker_l = speaker_l; + scene->speaker_r = speaker_r; + scene->speed = XAS_SPATIAL_DEFAULT_SPEED; + + if (buf_realloc(scene, NULL) < 0) { + goto error_buf_realloc; + } + + return scene; + +error_buf_realloc: + free(scene); + +error_malloc_scene: + return NULL; +} + +void xas_spatial_scene_destroy(xas_spatial_scene *scene) { + free(scene->buf); + free(scene); +} + +int xas_spatial_scene_set_observer(xas_spatial_scene *scene, + xas_spatial_coord coord, + xas_spatial_rotation rotation, + float width) { + scene->observer.coord = coord; + scene->observer.rotation = rotation; + scene->observer.width = width; + + return buf_realloc(scene, scene->buf); +} + +void xas_spatial_scene_set_speaker_coords(xas_spatial_scene *scene, + xas_spatial_coord speaker_l, + xas_spatial_coord speaker_r) { + scene->speaker_l = speaker_l; + scene->speaker_r = speaker_r; +} + +void xas_spatial_scene_set_speed(xas_spatial_scene *scene, float speed) { + scene->speed = speed; +} -- cgit v1.2.3