diff options
-rw-r--r-- | src/pcm_convert.c | 11 | ||||
-rw-r--r-- | src/pcm_convert.h | 4 | ||||
-rw-r--r-- | src/pcm_format.c | 59 | ||||
-rw-r--r-- | src/pcm_format.h | 6 |
4 files changed, 39 insertions, 41 deletions
diff --git a/src/pcm_convert.c b/src/pcm_convert.c index 13c7405e3..e0ab4ecd3 100644 --- a/src/pcm_convert.c +++ b/src/pcm_convert.c @@ -35,11 +35,15 @@ void pcm_convert_init(struct pcm_convert_state *state) pcm_resample_init(&state->resample); pcm_dither_24_init(&state->dither); + + pcm_buffer_init(&state->format_buffer); } void pcm_convert_deinit(struct pcm_convert_state *state) { pcm_resample_deinit(&state->resample); + + pcm_buffer_deinit(&state->format_buffer); } static size_t @@ -55,8 +59,9 @@ pcm_convert_16(const struct audio_format *src_format, assert(dest_format->bits == 16); - buf = pcm_convert_to_16(&state->dither, src_format->bits, - src_buffer, src_size, &len); + buf = pcm_convert_to_16(&state->format_buffer, &state->dither, + src_format->bits, src_buffer, src_size, + &len); if (!buf) g_error("pcm_convert_to_16() failed"); @@ -95,7 +100,7 @@ pcm_convert_24(const struct audio_format *src_format, assert(dest_format->bits == 24); - buf = pcm_convert_to_24(src_format->bits, + buf = pcm_convert_to_24(&state->format_buffer, src_format->bits, src_buffer, src_size, &len); if (!buf) g_error("pcm_convert_to_24() failed"); diff --git a/src/pcm_convert.h b/src/pcm_convert.h index 16aa05576..e48315795 100644 --- a/src/pcm_convert.h +++ b/src/pcm_convert.h @@ -21,6 +21,7 @@ #include "pcm_resample.h" #include "pcm_dither.h" +#include "pcm_buffer.h" struct audio_format; @@ -28,6 +29,9 @@ struct pcm_convert_state { struct pcm_resample_state resample; struct pcm_dither_24 dither; + + /** the buffer for converting the sample format */ + struct pcm_buffer format_buffer; }; void pcm_convert_init(struct pcm_convert_state *state); diff --git a/src/pcm_format.c b/src/pcm_format.c index 16d9673d4..0e0b2dfce 100644 --- a/src/pcm_format.c +++ b/src/pcm_format.c @@ -18,6 +18,7 @@ #include "pcm_format.h" #include "pcm_dither.h" +#include "pcm_buffer.h" #include <glib.h> @@ -40,27 +41,23 @@ pcm_convert_24_to_16(struct pcm_dither_24 *dither, } const int16_t * -pcm_convert_to_16(struct pcm_dither_24 *dither, +pcm_convert_to_16(struct pcm_buffer *buffer, struct pcm_dither_24 *dither, uint8_t bits, const void *src, size_t src_size, size_t *dest_size_r) { - static int16_t *buf; - static size_t len; unsigned num_samples; + int16_t *dest; switch (bits) { case 8: num_samples = src_size; - *dest_size_r = src_size << 1; - if (*dest_size_r > len) { - len = *dest_size_r; - buf = g_realloc(buf, len); - } + *dest_size_r = src_size * sizeof(*dest); + dest = pcm_buffer_get(buffer, *dest_size_r); - pcm_convert_8_to_16((int16_t *)buf, + pcm_convert_8_to_16(dest, (const int8_t *)src, num_samples); - return buf; + return dest; case 16: *dest_size_r = src_size; @@ -68,17 +65,13 @@ pcm_convert_to_16(struct pcm_dither_24 *dither, case 24: num_samples = src_size / 4; - *dest_size_r = num_samples * 2; - if (*dest_size_r > len) { - len = *dest_size_r; - buf = g_realloc(buf, len); - } - - pcm_convert_24_to_16(dither, - (int16_t *)buf, + *dest_size_r = num_samples * sizeof(*dest); + dest = pcm_buffer_get(buffer, *dest_size_r); + + pcm_convert_24_to_16(dither, dest, (const int32_t *)src, num_samples); - return buf; + return dest; } g_warning("only 8 or 16 bits are supported for conversion!\n"); @@ -106,37 +99,31 @@ pcm_convert_16_to_24(int32_t *out, const int16_t *in, } const int32_t * -pcm_convert_to_24(uint8_t bits, const void *src, +pcm_convert_to_24(struct pcm_buffer *buffer, + uint8_t bits, const void *src, size_t src_size, size_t *dest_size_r) { - static int32_t *buf; - static size_t len; unsigned num_samples; + int32_t *dest; switch (bits) { case 8: num_samples = src_size; - *dest_size_r = src_size * 4; - if (*dest_size_r > len) { - len = *dest_size_r; - buf = g_realloc(buf, len); - } + *dest_size_r = src_size * sizeof(*dest); + dest = pcm_buffer_get(buffer, *dest_size_r); - pcm_convert_8_to_24(buf, (const int8_t *)src, + pcm_convert_8_to_24(dest, (const int8_t *)src, num_samples); - return buf; + return dest; case 16: num_samples = src_size / 2; - *dest_size_r = num_samples * 4; - if (*dest_size_r > len) { - len = *dest_size_r; - buf = g_realloc(buf, len); - } + *dest_size_r = num_samples * sizeof(*dest); + dest = pcm_buffer_get(buffer, *dest_size_r); - pcm_convert_16_to_24(buf, (const int16_t *)src, + pcm_convert_16_to_24(dest, (const int16_t *)src, num_samples); - return buf; + return dest; case 24: *dest_size_r = src_size; diff --git a/src/pcm_format.h b/src/pcm_format.h index 7304963f4..628155dd4 100644 --- a/src/pcm_format.h +++ b/src/pcm_format.h @@ -22,15 +22,17 @@ #include <stdint.h> #include <stddef.h> +struct pcm_buffer; struct pcm_dither_24; const int16_t * -pcm_convert_to_16(struct pcm_dither_24 *dither, +pcm_convert_to_16(struct pcm_buffer *buffer, struct pcm_dither_24 *dither, uint8_t bits, const void *src, size_t src_size, size_t *dest_size_r); const int32_t * -pcm_convert_to_24(uint8_t bits, const void *src, +pcm_convert_to_24(struct pcm_buffer *buffer, + uint8_t bits, const void *src, size_t src_size, size_t *dest_size_r); #endif |