From e4571e0fe165f9c8dff42b850f8859825342522f Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Mon, 31 Jan 2022 21:13:03 -0500 Subject: At least I took care of the memory errors --- src/mixer.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/src/mixer.c b/src/mixer.c index 3fc1ac9..ba40215 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -7,7 +7,7 @@ #include #include -static void mixer_apply_int16_t_mono(int16_t *dest, +static void mixer_apply_mono_to_mono(int16_t *dest, int16_t *src, size_t count, float bias_l, @@ -19,16 +19,41 @@ static void mixer_apply_int16_t_mono(int16_t *dest, } } -static void mixer_apply_int16_t_stereo(int16_t *dest, +static void mixer_apply_mono_to_stereo(int16_t *dest, int16_t *src, size_t count, float bias_l, float bias_r) { size_t i; - for (i=0; i<2*count; i+=2) { - dest[i] += (int16_t)roundf(src[i] * bias_l); - dest[i+1] += (int16_t)roundf(src[i+1] * bias_r); + for (i=0; iinputs; - void (*mixer_apply)(int16_t *, - int16_t *, - size_t, - float, - float) = (output->channels == 2)? - mixer_apply_int16_t_stereo: - mixer_apply_int16_t_mono; - memset(mixer->buf, '\0', output->sample_size * output->channels * output->buffer_size); while (input) { + void (*mixer_apply)(int16_t *, + int16_t *, + size_t, + float, + float); + xas_mixer_input *next = input->next; int16_t *buf; ssize_t readlen; + if (output->channels == 2 && input->stream->channels == 2) { + mixer_apply = mixer_apply_stereo_to_stereo; + } else if (output->channels == 2 && input->stream->channels == 1) { + mixer_apply = mixer_apply_mono_to_stereo; + } else if (output->channels == 1 && input->stream->channels == 1) { + mixer_apply = mixer_apply_mono_to_mono; + } else if (output->channels == 1 && input->stream->channels == 2) { + mixer_apply = mixer_apply_stereo_to_mono; + } else { + goto error_invalid_input; + } + if ((readlen = xas_audio_stream_read(input->stream, (void **)&buf, count)) < 0) { @@ -83,6 +118,7 @@ static ssize_t mixer_fill(xas_mixer *mixer, return total; +error_invalid_input: error_audio_read_stream: return -1; } @@ -140,6 +176,7 @@ void xas_mixer_destroy(xas_mixer *mixer) { xas_audio_stream_destroy(mixer->output); + free(mixer->buf); free(mixer); } -- cgit v1.2.3