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"
|
2017-01-19 10:53:41 +01:00
|
|
|
#include "Silence.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"
|
2017-01-19 10:53:41 +01:00
|
|
|
#include "util/WritableBuffer.hxx"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <algorithm>
|
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
|
2018-01-01 19:07:33 +01:00
|
|
|
MonoToStereo(D dest, S src, S end) noexcept
|
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,
|
2018-01-01 19:07:33 +01:00
|
|
|
typename Traits::value_type _b) noexcept
|
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,
|
2018-01-01 19:07:33 +01:00
|
|
|
typename Traits::const_pointer_type end) noexcept
|
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,
|
2018-01-01 19:07:33 +01:00
|
|
|
typename Traits::const_pointer_type end) noexcept
|
2013-12-02 09:36:56 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-19 10:53:41 +01:00
|
|
|
/**
|
|
|
|
* Convert stereo to N channels (where N > 2). Left and right map to
|
|
|
|
* the first two channels (front left and front right), and the
|
|
|
|
* remaining (surround) channels are filled with silence.
|
|
|
|
*/
|
|
|
|
template<SampleFormat F, class Traits=SampleTraits<F>>
|
|
|
|
static typename Traits::pointer_type
|
|
|
|
StereoToN(typename Traits::pointer_type dest,
|
|
|
|
unsigned dest_channels,
|
|
|
|
typename Traits::const_pointer_type src,
|
2018-01-01 19:07:33 +01:00
|
|
|
typename Traits::const_pointer_type end) noexcept
|
2017-01-19 10:53:41 +01:00
|
|
|
{
|
|
|
|
assert(dest_channels > 2);
|
|
|
|
assert((end - src) % 2 == 0);
|
|
|
|
|
|
|
|
std::array<typename Traits::value_type, MAX_CHANNELS - 2> silence;
|
|
|
|
PcmSilence({&silence.front(), sizeof(silence)}, F);
|
|
|
|
|
|
|
|
while (src != end) {
|
|
|
|
/* copy left/right to front-left/front-right, which is
|
|
|
|
the first two channels in all multi-channel
|
|
|
|
configurations **/
|
|
|
|
*dest++ = *src++;
|
|
|
|
*dest++ = *src++;
|
|
|
|
|
|
|
|
/* all other channels are silent */
|
|
|
|
dest = std::copy_n(silence.begin(), dest_channels - 2, dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
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,
|
2018-01-01 19:07:33 +01:00
|
|
|
typename Traits::const_pointer_type end) noexcept
|
2013-12-02 11:42:19 +01:00
|
|
|
{
|
|
|
|
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,
|
2018-01-01 19:07:33 +01:00
|
|
|
ConstBuffer<typename Traits::value_type> src) noexcept
|
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());
|
2017-01-19 10:53:41 +01:00
|
|
|
else if (src_channels == 2 && dest_channels > 2)
|
|
|
|
StereoToN<F, Traits>(dest, dest_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,
|
2018-01-01 19:07:33 +01:00
|
|
|
ConstBuffer<int16_t> src) noexcept
|
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,
|
2018-01-01 19:07:33 +01:00
|
|
|
ConstBuffer<int32_t> src) noexcept
|
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,
|
2018-01-01 19:07:33 +01:00
|
|
|
ConstBuffer<int32_t> src) noexcept
|
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,
|
2018-01-01 19:07:33 +01:00
|
|
|
ConstBuffer<float> src) noexcept
|
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
|
|
|
}
|