diff --git a/src/pcm/PcmChannels.cxx b/src/pcm/PcmChannels.cxx index 8f00c01be..b90e48580 100644 --- a/src/pcm/PcmChannels.cxx +++ b/src/pcm/PcmChannels.cxx @@ -90,6 +90,31 @@ NToStereo(typename Traits::pointer_type dest, return dest; } +template> +static typename Traits::pointer_type +NToM(typename Traits::pointer_type dest, + unsigned dest_channels, + unsigned src_channels, + typename Traits::const_pointer_type src, + typename Traits::const_pointer_type end) +{ + assert((end - src) % src_channels == 0); + + while (src != end) { + typename Traits::long_type sum = *src++; + for (unsigned c = 1; c < src_channels; ++c) + sum += *src++; + + typename Traits::value_type value(sum / int(src_channels)); + + /* TODO: this is actually only mono ... */ + for (unsigned c = 0; c < dest_channels; ++c) + *dest++ = value; + } + + return dest; +} + template> static ConstBuffer ConvertChannels(PcmBuffer &buffer, @@ -109,7 +134,8 @@ ConvertChannels(PcmBuffer &buffer, else if (dest_channels == 2) NToStereo(dest, src_channels, src.begin(), src.end()); else - return nullptr; + NToM(dest, dest_channels, + src_channels, src.begin(), src.end()); return { dest, dest_size }; }