diff options
-rw-r--r-- | src/pcm_channels.c | 69 | ||||
-rw-r--r-- | src/pcm_channels.h | 17 |
2 files changed, 86 insertions, 0 deletions
diff --git a/src/pcm_channels.c b/src/pcm_channels.c index a63c7f2e9..8a388110e 100644 --- a/src/pcm_channels.c +++ b/src/pcm_channels.c @@ -173,3 +173,72 @@ pcm_convert_channels_24(struct pcm_buffer *buffer, return dest; } + +static void +pcm_convert_channels_32_1_to_2(int32_t *dest, const int32_t *src, + unsigned num_frames) +{ + pcm_convert_channels_24_1_to_2(dest, src, num_frames); +} + +static void +pcm_convert_channels_32_2_to_1(int32_t *dest, const int32_t *src, + unsigned num_frames) +{ + while (num_frames-- > 0) { + int64_t a = *src++, b = *src++; + + *dest++ = (a + b) / 2; + } +} + +static void +pcm_convert_channels_32_n_to_2(int32_t *dest, + unsigned src_channels, const int32_t *src, + unsigned num_frames) +{ + unsigned c; + + assert(src_channels > 0); + + while (num_frames-- > 0) { + int64_t sum = 0; + int32_t value; + + for (c = 0; c < src_channels; ++c) + sum += *src++; + value = sum / (int64_t)src_channels; + + /* XXX this is actually only mono ... */ + *dest++ = value; + *dest++ = value; + } +} + +const int32_t * +pcm_convert_channels_32(struct pcm_buffer *buffer, + int8_t dest_channels, + int8_t src_channels, const int32_t *src, + size_t src_size, size_t *dest_size_r) +{ + unsigned num_frames = src_size / src_channels / sizeof(*src); + unsigned dest_size = num_frames * dest_channels * sizeof(*src); + int32_t *dest = pcm_buffer_get(buffer, dest_size); + + *dest_size_r = dest_size; + + if (src_channels == 1 && dest_channels == 2) + pcm_convert_channels_32_1_to_2(dest, src, num_frames); + else if (src_channels == 2 && dest_channels == 1) + pcm_convert_channels_32_2_to_1(dest, src, num_frames); + else if (dest_channels == 2) + pcm_convert_channels_32_n_to_2(dest, src_channels, src, + num_frames); + else { + g_warning("conversion %u->%u channels is not supported", + src_channels, dest_channels); + return NULL; + } + + return dest; +} diff --git a/src/pcm_channels.h b/src/pcm_channels.h index 25c13d839..bc4e4e3b0 100644 --- a/src/pcm_channels.h +++ b/src/pcm_channels.h @@ -59,4 +59,21 @@ pcm_convert_channels_24(struct pcm_buffer *buffer, int8_t src_channels, const int32_t *src, size_t src_size, size_t *dest_size_r); +/** + * Changes the number of channels in 32 bit PCM data. + * + * @param buffer the destination pcm_buffer object + * @param dest_channels the number of channels requested + * @param src_channels the number of channels in the source buffer + * @param src the source PCM buffer + * @param src_size the number of bytes in #src + * @param dest_size_r returns the number of bytes of the destination buffer + * @return the destination buffer + */ +const int32_t * +pcm_convert_channels_32(struct pcm_buffer *buffer, + int8_t dest_channels, + int8_t src_channels, const int32_t *src, + size_t src_size, size_t *dest_size_r); + #endif |