PcmChannels.cxx 5.92 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "PcmChannels.hxx"
21
#include "ChannelDefs.hxx"
22
#include "Buffer.hxx"
23
#include "Silence.hxx"
24
#include "Traits.hxx"
25
#include "util/ConstBuffer.hxx"
26 27 28 29
#include "util/WritableBuffer.hxx"

#include <array>
#include <algorithm>
30
#include <cassert>
31

32
template<typename D, typename S>
33
static void
Max Kellermann's avatar
Max Kellermann committed
34
MonoToStereo(D dest, S src, S end) noexcept
35
{
36 37
	while (src != end) {
		const auto value = *src++;
38 39 40 41

		*dest++ = value;
		*dest++ = value;
	}
42

43 44
}

45 46 47
template<SampleFormat F, class Traits=SampleTraits<F>>
static typename Traits::value_type
StereoToMono(typename Traits::value_type _a,
Max Kellermann's avatar
Max Kellermann committed
48
	     typename Traits::value_type _b) noexcept
49
{
50 51
	typename Traits::sum_type a(_a);
	typename Traits::sum_type b(_b);
52

53
	return typename Traits::value_type((a + b) / 2);
54 55
}

56
template<SampleFormat F, class Traits=SampleTraits<F>>
57 58 59 60
static typename Traits::pointer
StereoToMono(typename Traits::pointer dest,
	     typename Traits::const_pointer src,
	     typename Traits::const_pointer end) noexcept
61
{
62 63 64
	while (src != end) {
		const auto a = *src++;
		const auto b = *src++;
65

66 67 68 69 70
		*dest++ = StereoToMono<F, Traits>(a, b);
	}

	return dest;
}
71

72
template<SampleFormat F, class Traits=SampleTraits<F>>
73 74
static typename Traits::pointer
NToStereo(typename Traits::pointer dest,
75
	  unsigned src_channels,
76 77
	  typename Traits::const_pointer src,
	  typename Traits::const_pointer end) noexcept
78 79
{
	assert((end - src) % src_channels == 0);
80

81
	while (src != end) {
82
		typename Traits::sum_type sum = *src++;
83
		for (unsigned c = 1; c < src_channels; ++c)
84 85
			sum += *src++;

86 87 88
		typename Traits::value_type value(sum / int(src_channels));

		/* TODO: this is actually only mono ... */
89 90 91
		*dest++ = value;
		*dest++ = value;
	}
92 93

	return dest;
94 95
}

96 97 98 99 100 101
/**
 * 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>>
102 103
static typename Traits::pointer
StereoToN(typename Traits::pointer dest,
104
	  unsigned dest_channels,
105 106
	  typename Traits::const_pointer src,
	  typename Traits::const_pointer end) noexcept
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
	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;
}

128
template<SampleFormat F, class Traits=SampleTraits<F>>
129 130
static typename Traits::pointer
NToM(typename Traits::pointer dest,
131 132
     unsigned dest_channels,
     unsigned src_channels,
133 134
     typename Traits::const_pointer src,
     typename Traits::const_pointer end) noexcept
135 136 137 138
{
	assert((end - src) % src_channels == 0);

	while (src != end) {
139
		typename Traits::sum_type sum = *src++;
140 141 142 143 144 145 146 147 148 149 150 151 152
		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;
}

153 154 155 156 157
template<SampleFormat F, class Traits=SampleTraits<F>>
static ConstBuffer<typename Traits::value_type>
ConvertChannels(PcmBuffer &buffer,
		unsigned dest_channels,
		unsigned src_channels,
Max Kellermann's avatar
Max Kellermann committed
158
		ConstBuffer<typename Traits::value_type> src) noexcept
159
{
160
	assert(src.size % src_channels == 0);
161

162
	const size_t dest_size = src.size / src_channels * dest_channels;
163
	auto dest = buffer.GetT<typename Traits::value_type>(dest_size);
164

165
	if (src_channels == 1 && dest_channels == 2)
166
		MonoToStereo(dest, src.begin(), src.end());
167
	else if (src_channels == 2 && dest_channels == 1)
168
		StereoToMono<F>(dest, src.begin(), src.end());
169
	else if (dest_channels == 2)
170
		NToStereo<F>(dest, src_channels, src.begin(), src.end());
171 172 173
	else if (src_channels == 2 && dest_channels > 2)
		StereoToN<F, Traits>(dest, dest_channels,
				     src.begin(), src.end());
174
	else
175 176
		NToM<F>(dest, dest_channels,
			src_channels, src.begin(), src.end());
177

178
	return { dest, dest_size };
179
}
180

181 182 183 184
ConstBuffer<int16_t>
pcm_convert_channels_16(PcmBuffer &buffer,
			unsigned dest_channels,
			unsigned src_channels,
Max Kellermann's avatar
Max Kellermann committed
185
			ConstBuffer<int16_t> src) noexcept
186
{
187 188
	return ConvertChannels<SampleFormat::S16>(buffer, dest_channels,
						  src_channels, src);
189 190
}

191
ConstBuffer<int32_t>
192
pcm_convert_channels_24(PcmBuffer &buffer,
193
			unsigned dest_channels,
194
			unsigned src_channels,
Max Kellermann's avatar
Max Kellermann committed
195
			ConstBuffer<int32_t> src) noexcept
196
{
197 198
	return ConvertChannels<SampleFormat::S24_P32>(buffer, dest_channels,
						      src_channels, src);
199 200
}

201
ConstBuffer<int32_t>
202
pcm_convert_channels_32(PcmBuffer &buffer,
203
			unsigned dest_channels,
204
			unsigned src_channels,
Max Kellermann's avatar
Max Kellermann committed
205
			ConstBuffer<int32_t> src) noexcept
206
{
207 208
	return ConvertChannels<SampleFormat::S32>(buffer, dest_channels,
						  src_channels, src);
209 210
}

211
ConstBuffer<float>
212
pcm_convert_channels_float(PcmBuffer &buffer,
213
			   unsigned dest_channels,
214
			   unsigned src_channels,
Max Kellermann's avatar
Max Kellermann committed
215
			   ConstBuffer<float> src) noexcept
216
{
217 218
	return ConvertChannels<SampleFormat::FLOAT>(buffer, dest_channels,
						    src_channels, src);
219
}