2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-23 20:03:14 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-10-23 20:03:14 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-31 20:33:26 +01:00
|
|
|
#include "PcmChannels.hxx"
|
2013-07-29 08:10:10 +02:00
|
|
|
#include "PcmBuffer.hxx"
|
2013-12-02 09:36:56 +01:00
|
|
|
#include "Traits.hxx"
|
|
|
|
#include "AudioFormat.hxx"
|
2013-12-02 09:15:54 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2008-12-29 17:28:49 +01:00
|
|
|
|
2008-10-23 20:03:14 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2013-01-31 21:37:03 +01:00
|
|
|
template<typename D, typename S>
|
2008-10-23 20:03:14 +02:00
|
|
|
static void
|
2013-01-31 21:37:03 +01:00
|
|
|
MonoToStereo(D dest, S src, S end)
|
2008-10-23 20:03:14 +02:00
|
|
|
{
|
2013-01-31 21:37:03 +01:00
|
|
|
while (src != end) {
|
|
|
|
const auto value = *src++;
|
2008-10-23 20:03:14 +02:00
|
|
|
|
|
|
|
*dest++ = value;
|
|
|
|
*dest++ = value;
|
|
|
|
}
|
2013-01-31 21:37:03 +01:00
|
|
|
|
2008-10-23 20:03:14 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
template<SampleFormat F, class Traits=SampleTraits<F>>
|
|
|
|
static typename Traits::value_type
|
|
|
|
StereoToMono(typename Traits::value_type _a,
|
|
|
|
typename Traits::value_type _b)
|
2008-10-23 20:03:14 +02:00
|
|
|
{
|
2013-12-02 11:45:03 +01:00
|
|
|
typename Traits::sum_type a(_a);
|
|
|
|
typename Traits::sum_type b(_b);
|
2008-10-23 20:03:14 +02:00
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
return typename Traits::value_type((a + b) / 2);
|
2008-10-23 20:03:14 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
template<SampleFormat F, class Traits=SampleTraits<F>>
|
|
|
|
static typename Traits::pointer_type
|
|
|
|
StereoToMono(typename Traits::pointer_type dest,
|
|
|
|
typename Traits::const_pointer_type src,
|
|
|
|
typename Traits::const_pointer_type end)
|
2008-10-23 20:03:14 +02:00
|
|
|
{
|
2013-12-02 09:36:56 +01:00
|
|
|
while (src != end) {
|
|
|
|
const auto a = *src++;
|
|
|
|
const auto b = *src++;
|
2008-10-23 20:03:14 +02:00
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
*dest++ = StereoToMono<F, Traits>(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
2008-10-23 20:03:14 +02:00
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
template<SampleFormat F, class Traits=SampleTraits<F>>
|
|
|
|
static typename Traits::pointer_type
|
|
|
|
NToStereo(typename Traits::pointer_type dest,
|
|
|
|
unsigned src_channels,
|
|
|
|
typename Traits::const_pointer_type src,
|
|
|
|
typename Traits::const_pointer_type end)
|
|
|
|
{
|
|
|
|
assert((end - src) % src_channels == 0);
|
2008-10-23 20:03:14 +02:00
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
while (src != end) {
|
2013-12-02 11:45:03 +01:00
|
|
|
typename Traits::sum_type sum = *src++;
|
2013-12-02 09:36:56 +01:00
|
|
|
for (unsigned c = 1; c < src_channels; ++c)
|
2008-10-23 20:03:14 +02:00
|
|
|
sum += *src++;
|
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
typename Traits::value_type value(sum / int(src_channels));
|
|
|
|
|
|
|
|
/* TODO: this is actually only mono ... */
|
2008-10-23 20:03:14 +02:00
|
|
|
*dest++ = value;
|
|
|
|
*dest++ = value;
|
|
|
|
}
|
2013-12-02 09:36:56 +01:00
|
|
|
|
|
|
|
return dest;
|
2008-10-23 20:03:14 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 11:42:19 +01:00
|
|
|
template<SampleFormat F, class Traits=SampleTraits<F>>
|
|
|
|
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) {
|
2013-12-02 11:45:03 +01:00
|
|
|
typename Traits::sum_type sum = *src++;
|
2013-12-02 11:42:19 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
template<SampleFormat F, class Traits=SampleTraits<F>>
|
|
|
|
static ConstBuffer<typename Traits::value_type>
|
|
|
|
ConvertChannels(PcmBuffer &buffer,
|
|
|
|
unsigned dest_channels,
|
|
|
|
unsigned src_channels,
|
|
|
|
ConstBuffer<typename Traits::value_type> src)
|
2008-10-23 20:03:14 +02:00
|
|
|
{
|
2013-12-02 09:15:54 +01:00
|
|
|
assert(src.size % src_channels == 0);
|
2011-10-19 21:56:41 +02:00
|
|
|
|
2013-12-02 09:15:54 +01:00
|
|
|
const size_t dest_size = src.size / src_channels * dest_channels;
|
2013-12-02 09:36:56 +01:00
|
|
|
auto dest = buffer.GetT<typename Traits::value_type>(dest_size);
|
2011-10-09 19:32:46 +02:00
|
|
|
|
2008-10-23 20:03:14 +02:00
|
|
|
if (src_channels == 1 && dest_channels == 2)
|
2013-12-02 09:15:54 +01:00
|
|
|
MonoToStereo(dest, src.begin(), src.end());
|
2008-10-23 20:03:14 +02:00
|
|
|
else if (src_channels == 2 && dest_channels == 1)
|
2013-12-02 09:36:56 +01:00
|
|
|
StereoToMono<F>(dest, src.begin(), src.end());
|
2008-10-23 20:03:14 +02:00
|
|
|
else if (dest_channels == 2)
|
2013-12-02 09:36:56 +01:00
|
|
|
NToStereo<F>(dest, src_channels, src.begin(), src.end());
|
2009-07-23 12:01:03 +02:00
|
|
|
else
|
2013-12-02 11:42:19 +01:00
|
|
|
NToM<F>(dest, dest_channels,
|
|
|
|
src_channels, src.begin(), src.end());
|
2008-10-23 20:03:14 +02:00
|
|
|
|
2013-12-02 09:15:54 +01:00
|
|
|
return { dest, dest_size };
|
2008-10-23 20:03:14 +02:00
|
|
|
}
|
2008-10-23 20:04:37 +02:00
|
|
|
|
2013-12-02 09:36:56 +01:00
|
|
|
ConstBuffer<int16_t>
|
|
|
|
pcm_convert_channels_16(PcmBuffer &buffer,
|
|
|
|
unsigned dest_channels,
|
|
|
|
unsigned src_channels,
|
|
|
|
ConstBuffer<int16_t> src)
|
2008-10-23 20:04:37 +02:00
|
|
|
{
|
2013-12-02 09:36:56 +01:00
|
|
|
return ConvertChannels<SampleFormat::S16>(buffer, dest_channels,
|
|
|
|
src_channels, src);
|
2008-10-23 20:04:37 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 09:15:54 +01:00
|
|
|
ConstBuffer<int32_t>
|
2013-07-29 08:10:10 +02:00
|
|
|
pcm_convert_channels_24(PcmBuffer &buffer,
|
2011-10-19 22:06:39 +02:00
|
|
|
unsigned dest_channels,
|
2013-12-02 09:15:54 +01:00
|
|
|
unsigned src_channels,
|
|
|
|
ConstBuffer<int32_t> src)
|
2008-10-23 20:04:37 +02:00
|
|
|
{
|
2013-12-02 09:36:56 +01:00
|
|
|
return ConvertChannels<SampleFormat::S24_P32>(buffer, dest_channels,
|
|
|
|
src_channels, src);
|
2009-03-02 16:36:49 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 09:15:54 +01:00
|
|
|
ConstBuffer<int32_t>
|
2013-07-29 08:10:10 +02:00
|
|
|
pcm_convert_channels_32(PcmBuffer &buffer,
|
2011-10-19 22:06:39 +02:00
|
|
|
unsigned dest_channels,
|
2013-12-02 09:15:54 +01:00
|
|
|
unsigned src_channels,
|
|
|
|
ConstBuffer<int32_t> src)
|
2009-03-02 16:36:49 +01:00
|
|
|
{
|
2013-12-02 09:36:56 +01:00
|
|
|
return ConvertChannels<SampleFormat::S32>(buffer, dest_channels,
|
|
|
|
src_channels, src);
|
2012-10-02 08:29:52 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 09:15:54 +01:00
|
|
|
ConstBuffer<float>
|
2013-07-29 08:10:10 +02:00
|
|
|
pcm_convert_channels_float(PcmBuffer &buffer,
|
2012-10-02 08:29:52 +02:00
|
|
|
unsigned dest_channels,
|
2013-12-02 09:15:54 +01:00
|
|
|
unsigned src_channels,
|
|
|
|
ConstBuffer<float> src)
|
2012-10-02 08:29:52 +02:00
|
|
|
{
|
2013-12-02 09:36:56 +01:00
|
|
|
return ConvertChannels<SampleFormat::FLOAT>(buffer, dest_channels,
|
|
|
|
src_channels, src);
|
2012-10-02 08:29:52 +02:00
|
|
|
}
|