From c0ecce54981a3c3b9cca29604b54620c63517129 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 12 Oct 2008 12:02:52 +0200 Subject: pcm_utils: support converting N channels to stereo Convert any number of channels to stereo. In fact, this isn't really stereo, it's rater mono blown up to stereo. This patch should only make it possible to play 5.1 files at all; "real" conversion to stereo should be implemented, but for now, this is better than nothing. --- src/pcm_utils.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/pcm_utils.c') diff --git a/src/pcm_utils.c b/src/pcm_utils.c index 52f9294b7..cb6f93f63 100644 --- a/src/pcm_utils.c +++ b/src/pcm_utils.c @@ -385,6 +385,29 @@ pcm_convert_channels_2_to_1(int16_t *dest, const int16_t *src, } } +static void +pcm_convert_channels_n_to_2(int16_t *dest, + unsigned src_channels, const int16_t *src, + unsigned num_frames) +{ + unsigned c; + + assert(src_channels > 0); + + while (num_frames-- > 0) { + int32_t sum = 0; + int16_t value; + + for (c = 0; c < src_channels; ++c) + sum += *src++; + value = sum / (int)src_channels; + + /* XXX this is actually only mono ... */ + *dest++ = value; + *dest++ = value; + } +} + static const int16_t * pcm_convertChannels(int8_t dest_channels, int8_t src_channels, const int16_t *src, @@ -406,6 +429,9 @@ pcm_convertChannels(int8_t dest_channels, pcm_convert_channels_1_to_2(buf, src, num_frames); else if (src_channels == 2 && dest_channels == 1) pcm_convert_channels_2_to_1(buf, src, num_frames); + else if (dest_channels == 2) + pcm_convert_channels_n_to_2(buf, src_channels, src, + num_frames); else { ERROR("conversion %u->%u channels is not supported\n", src_channels, dest_channels); -- cgit v1.2.3