diff options
| author | XANTRONIX Development | 2022-02-15 22:30:48 -0500 | 
|---|---|---|
| committer | XANTRONIX Development | 2022-02-15 22:30:48 -0500 | 
| commit | f494c5fd9c3d0e515124fe1f1a3ede58b966e9d9 (patch) | |
| tree | 033a75c9f66265e1527d144fb78db0cbd297c47c /src | |
| parent | e0fda7988c2cca763d92075a5d95f813a37b69f4 (diff) | |
| download | xas-f494c5fd9c3d0e515124fe1f1a3ede58b966e9d9.tar.gz xas-f494c5fd9c3d0e515124fe1f1a3ede58b966e9d9.tar.bz2 xas-f494c5fd9c3d0e515124fe1f1a3ede58b966e9d9.zip | |
I'm kind of sad and scared right now
Diffstat (limited to 'src')
| -rw-r--r-- | src/spatial.c | 57 | 
1 files changed, 36 insertions, 21 deletions
| diff --git a/src/spatial.c b/src/spatial.c index 18173aa..710726f 100644 --- a/src/spatial.c +++ b/src/spatial.c @@ -1,3 +1,4 @@ +#include <stdio.h>  #include <stdlib.h>  #include <string.h>  #include <math.h> @@ -35,10 +36,8 @@ error_realloc_buffer:      return -1;  } -static inline size_t sample_index(xas_spatial_scene *scene, float distance) { -    size_t index = floorf(distance / scene->speed / scene->format.sample_rate); - -    return (scene->buffer->index + index) % scene->buffer->size; +static inline size_t sample_delta(xas_spatial_scene *scene, float distance) { +    return floorf(distance / scene->speed / scene->format.sample_rate);  }  static inline int16_t sample_scale(int16_t value, float distance) { @@ -46,7 +45,7 @@ static inline int16_t sample_scale(int16_t value, float distance) {  }  static void buffer_zero(xas_spatial_buffer *buffer, size_t count) { -    int16_t *dest = (int16_t *)buffer + 1; +    int16_t *dest = (int16_t *)(buffer + 1);      size_t i = buffer->index;      if (count > buffer->size) { @@ -58,8 +57,8 @@ static void buffer_zero(xas_spatial_buffer *buffer, size_t count) {              i = 0;          } -        dest[i*XAS_AUDIO_STEREO]   = 0; -        dest[i*XAS_AUDIO_STEREO+1] = 0; +        dest[XAS_AUDIO_STEREO*i]   = 0; +        dest[XAS_AUDIO_STEREO*i+1] = 0;          i++;      } @@ -68,20 +67,22 @@ static void buffer_zero(xas_spatial_buffer *buffer, size_t count) {  static void buffer_copy(xas_spatial_buffer *buffer,                          int16_t *dest,                          size_t count) { -    int16_t *src = (int16_t *)buffer + 1; -    size_t i = buffer->index; +    int16_t *src = (int16_t *)(buffer + 1); + +    size_t index_i = buffer->index, +           index_o;      if (count > buffer->size) {          count = buffer->size;      } -    while (count--) { -        if (i == buffer->size) { -            i = 0; +    for (index_o=0; index_o<count; index_o++) { +        if (index_i == buffer->size) { +            index_i = 0;          } -        dest[i*XAS_AUDIO_STEREO]   = src[i*XAS_AUDIO_STEREO]; -        dest[i*XAS_AUDIO_STEREO+1] = src[i*XAS_AUDIO_STEREO+1]; +        dest[XAS_AUDIO_STEREO*index_o]   = src[XAS_AUDIO_STEREO*index_i]; +        dest[XAS_AUDIO_STEREO*index_o+1] = src[XAS_AUDIO_STEREO*index_i+1];      }  } @@ -104,14 +105,14 @@ ssize_t scene_fill(xas_spatial_scene *scene,          float distance_l = dist(scene->speaker_l, obj->coord),                distance_r = dist(scene->speaker_r, obj->coord); -        size_t index_l, -               index_r; +        size_t delta_l, +               delta_r;          ssize_t readlen,                  i;          if (distance_l > scene->radius || distance_r > scene->radius) { -            continue; +            goto next;          }          if ((readlen = xas_audio_stream_read(obj->source, @@ -124,29 +125,42 @@ ssize_t scene_fill(xas_spatial_scene *scene,              readlen = buffer->size;          } -        index_l = sample_index(scene, distance_l); -        index_r = sample_index(scene, distance_r); +        delta_l = sample_delta(scene, distance_l); +        delta_r = sample_delta(scene, distance_r);          for (i=0; i<readlen; i++) {              int16_t value_l = sample_scale(src[i], distance_l),                      value_r = sample_scale(src[i], distance_r); +            size_t index_l, +                   index_r; +              if (buffer->index == buffer->size) {                  buffer->index = 0;              } -            dest[XAS_AUDIO_STEREO*index_l+i]   += value_l; -            dest[XAS_AUDIO_STEREO*index_r+i+1] += value_r; +            index_l = buffer->index + delta_l; +            index_r = buffer->index + delta_r; + +            dest[XAS_AUDIO_STEREO*index_l]   += value_l; +            dest[XAS_AUDIO_STEREO*index_r+1] += value_r;              buffer->index++;          } +next:          buffer->index = index_old;          obj           = obj->next;      }      buffer_copy(buffer, output, count); +    buffer->index += count; + +    if (buffer->index == buffer->size) { +        buffer->index = 0; +    } +      return count;  error_audio_stream_read: @@ -167,6 +181,7 @@ xas_spatial_scene *xas_spatial_scene_new(xas_audio_format format,      scene->format    = format;      scene->speaker_l = speaker_l;      scene->speaker_r = speaker_r; +    scene->radius    = XAS_SPATIAL_DEFAULT_RADIUS;      scene->speed     = XAS_SPATIAL_DEFAULT_SPEED;      if (buffer_realloc(scene, NULL) < 0) { | 
 
    