diff options
| author | XANTRONIX Development | 2022-02-28 13:10:56 -0500 | 
|---|---|---|
| committer | XANTRONIX Development | 2022-02-28 13:12:06 -0500 | 
| commit | 1b8fe922c267844f08b1795682ff8ab7e2b2a40c (patch) | |
| tree | 952e1c133bed7a2cd7c718626829fc3193e340da /src | |
| parent | 1abc73e474f8811ade6a9573a8083fecfbfecc04 (diff) | |
| download | xas-1b8fe922c267844f08b1795682ff8ab7e2b2a40c.tar.gz xas-1b8fe922c267844f08b1795682ff8ab7e2b2a40c.tar.bz2 xas-1b8fe922c267844f08b1795682ff8ab7e2b2a40c.zip | |
Implement xas_audio_apply_gain()
Diffstat (limited to 'src')
| -rw-r--r-- | src/audio.c | 52 | 
1 files changed, 44 insertions, 8 deletions
| diff --git a/src/audio.c b/src/audio.c index bc2e04d..9caf199 100644 --- a/src/audio.c +++ b/src/audio.c @@ -1,6 +1,7 @@  #include <stdlib.h>  #include <string.h>  #include <errno.h> +#include <math.h>  #include <xas/audio.h> @@ -10,6 +11,17 @@ int xas_audio_format_eq(xas_audio_format a, xas_audio_format b) {          && a.sample_rate == b.sample_rate;  } +void xas_audio_zero(xas_audio_format format, +                      void *dest, +                      size_t index, +                      size_t count) { +    size_t stride = format.channels * format.sample_size; + +    memset((uint8_t *)dest + stride * index, +           '\0', +           stride * count); +} +  void xas_audio_copy(xas_audio_format format,                        void *dest,                        void *src, @@ -23,15 +35,39 @@ void xas_audio_copy(xas_audio_format format,             stride * count);  } -void xas_audio_zero(xas_audio_format format, -                      void *dest, -                      size_t index, -                      size_t count) { -    size_t stride = format.channels * format.sample_size; +static inline void apply_gain_mono(int16_t *samples, +                                   float gain, +                                   size_t index, +                                   size_t count) { +    size_t i; -    memset((uint8_t *)dest + stride * index, -           '\0', -           stride * count); +    for (i=index; i<count; i++) { +        samples[i] *= (int16_t)roundf(gain * samples[i]); +    } +} + +static inline void apply_gain_stereo(int16_t *samples, +                                     float gain, +                                     size_t index, +                                     size_t count) { +    size_t i; + +    for (i=index; i<count; i++) { +        samples[i*2]   *= (int16_t)roundf(gain * samples[i*2]); +        samples[i*2+1] *= (int16_t)roundf(gain * samples[i*2+1]); +    } +} + +void xas_audio_apply_gain(xas_audio_format format, +                            void *dest, +                            float gain, +                            size_t index, +                            size_t count) { +    if (format.channels == XAS_AUDIO_MONO) { +        apply_gain_mono(dest, gain, index, count); +    } else if (format.channels == XAS_AUDIO_STEREO) { +        apply_gain_stereo(dest, gain, index, count); +    }  }  static xas_audio_stream *stream_new(enum xas_audio_stream_type type, | 
 
    