Mix.cxx 5.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * http://www.musicpd.org
 *
 * 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
 */

20
#include "Mix.hxx"
21
#include "Volume.hxx"
22
#include "Clamp.hxx"
23
#include "Traits.hxx"
24
#include "util/Clamp.hxx"
25
#include "util/Math.hxx"
26

27
#include "Dither.cxx" // including the .cxx file to get inlined templates
28

29
#include <cassert>
Rosen Penev's avatar
Rosen Penev committed
30
#include <cmath>
31

32 33
template<SampleFormat F, class Traits=SampleTraits<F>>
static typename Traits::value_type
34 35
PcmAddVolume(PcmDither &dither,
	     typename Traits::value_type _a, typename Traits::value_type _b,
Max Kellermann's avatar
Max Kellermann committed
36
	     int volume1, int volume2) noexcept
37
{
38
	typename Traits::long_type a(_a), b(_b);
39
	typename Traits::long_type c(a * volume1 + b * volume2);
40

41 42 43
	return dither.DitherShift<typename Traits::long_type,
				  Traits::BITS + PCM_VOLUME_BITS,
				  Traits::BITS>(c);
44 45
}

46
template<SampleFormat F, class Traits=SampleTraits<F>>
47
static void
48
PcmAddVolume(PcmDither &dither,
49 50
	     typename Traits::pointer a,
	     typename Traits::const_pointer b,
Max Kellermann's avatar
Max Kellermann committed
51
	     size_t n, int volume1, int volume2) noexcept
52
{
53
	for (size_t i = 0; i != n; ++i)
54 55
		a[i] = PcmAddVolume<F, Traits>(dither, a[i], b[i],
					       volume1, volume2);
56 57
}

58
template<SampleFormat F, class Traits=SampleTraits<F>>
59
static void
60
PcmAddVolumeVoid(PcmDither &dither,
Max Kellermann's avatar
Max Kellermann committed
61 62
		 void *a, const void *b, size_t size,
		 int volume1, int volume2) noexcept
63
{
64
	constexpr size_t sample_size = Traits::SAMPLE_SIZE;
65
	assert(size % sample_size == 0);
66

67
	PcmAddVolume<F, Traits>(dither,
68 69
				typename Traits::pointer(a),
				typename Traits::const_pointer(b),
70 71
				size / sample_size,
				volume1, volume2);
72 73
}

74 75
static void
pcm_add_vol_float(float *buffer1, const float *buffer2,
Max Kellermann's avatar
Max Kellermann committed
76
		  unsigned num_samples, float volume1, float volume2) noexcept
77 78 79 80 81 82 83 84 85 86 87
{
	while (num_samples > 0) {
		float sample1 = *buffer1;
		float sample2 = *buffer2++;

		sample1 = (sample1 * volume1 + sample2 * volume2);
		*buffer1++ = sample1;
		--num_samples;
	}
}

88
static bool
89
pcm_add_vol(PcmDither &dither, void *buffer1, const void *buffer2, size_t size,
90
	    int vol1, int vol2,
Max Kellermann's avatar
Max Kellermann committed
91
	    SampleFormat format) noexcept
92
{
93
	switch (format) {
94 95
	case SampleFormat::UNDEFINED:
	case SampleFormat::DSD:
96 97 98
		/* not implemented */
		return false;

99
	case SampleFormat::S8:
100 101
		PcmAddVolumeVoid<SampleFormat::S8>(dither,
						   buffer1, buffer2, size,
102
						   vol1, vol2);
103
		return true;
104

105
	case SampleFormat::S16:
106 107
		PcmAddVolumeVoid<SampleFormat::S16>(dither,
						    buffer1, buffer2, size,
108
						    vol1, vol2);
109
		return true;
110

111
	case SampleFormat::S24_P32:
112 113
		PcmAddVolumeVoid<SampleFormat::S24_P32>(dither,
							buffer1, buffer2, size,
114
							vol1, vol2);
115
		return true;
116

117
	case SampleFormat::S32:
118 119
		PcmAddVolumeVoid<SampleFormat::S32>(dither,
						    buffer1, buffer2, size,
120
						    vol1, vol2);
121
		return true;
122

123
	case SampleFormat::FLOAT:
Max Kellermann's avatar
Max Kellermann committed
124 125
		pcm_add_vol_float((float *)buffer1, (const float *)buffer2,
				  size / 4,
126 127 128
				  pcm_volume_to_float(vol1),
				  pcm_volume_to_float(vol2));
		return true;
129
	}
130 131

	assert(false);
132
	gcc_unreachable();
133 134
}

135
template<SampleFormat F, class Traits=SampleTraits<F>>
136
static constexpr typename Traits::value_type
Max Kellermann's avatar
Max Kellermann committed
137
PcmAdd(typename Traits::value_type _a, typename Traits::value_type _b) noexcept
138
{
139
	typename Traits::sum_type a(_a), b(_b);
140

141
	return PcmClamp<F, Traits>(a + b);
142 143
}

144
template<SampleFormat F, class Traits=SampleTraits<F>>
145
static void
146 147
PcmAdd(typename Traits::pointer a,
       typename Traits::const_pointer b,
Max Kellermann's avatar
Max Kellermann committed
148
       size_t n) noexcept
149
{
150
	for (size_t i = 0; i != n; ++i)
151
		a[i] = PcmAdd<F, Traits>(a[i], b[i]);
152 153
}

154
template<SampleFormat F, class Traits=SampleTraits<F>>
155
static void
Max Kellermann's avatar
Max Kellermann committed
156
PcmAddVoid(void *a, const void *b, size_t size) noexcept
157
{
158
	constexpr size_t sample_size = Traits::SAMPLE_SIZE;
159
	assert(size % sample_size == 0);
160

161 162
	PcmAdd<F, Traits>(typename Traits::pointer(a),
			  typename Traits::const_pointer(b),
163
			  size / sample_size);
164 165
}

166
static void
Max Kellermann's avatar
Max Kellermann committed
167 168
pcm_add_float(float *buffer1, const float *buffer2,
	      unsigned num_samples) noexcept
169 170 171 172 173 174 175 176 177
{
	while (num_samples > 0) {
		float sample1 = *buffer1;
		float sample2 = *buffer2++;
		*buffer1++ = sample1 + sample2;
		--num_samples;
	}
}

178
static bool
179
pcm_add(void *buffer1, const void *buffer2, size_t size,
Max Kellermann's avatar
Max Kellermann committed
180
	SampleFormat format) noexcept
181
{
182
	switch (format) {
183 184
	case SampleFormat::UNDEFINED:
	case SampleFormat::DSD:
185 186 187
		/* not implemented */
		return false;

188
	case SampleFormat::S8:
189
		PcmAddVoid<SampleFormat::S8>(buffer1, buffer2, size);
190
		return true;
191

192
	case SampleFormat::S16:
193
		PcmAddVoid<SampleFormat::S16>(buffer1, buffer2, size);
194
		return true;
195

196
	case SampleFormat::S24_P32:
197
		PcmAddVoid<SampleFormat::S24_P32>(buffer1, buffer2, size);
198
		return true;
199

200
	case SampleFormat::S32:
201
		PcmAddVoid<SampleFormat::S32>(buffer1, buffer2, size);
202
		return true;
203

204
	case SampleFormat::FLOAT:
Max Kellermann's avatar
Max Kellermann committed
205 206
		pcm_add_float((float *)buffer1, (const float *)buffer2,
			      size / 4);
207
		return true;
208
	}
209 210

	assert(false);
211
	gcc_unreachable();
212 213
}

214
bool
215
pcm_mix(PcmDither &dither, void *buffer1, const void *buffer2, size_t size,
Max Kellermann's avatar
Max Kellermann committed
216
	SampleFormat format, float portion1) noexcept
217
{
218 219
	float s;

220
	/* portion1 is between 0.0 and 1.0 for crossfading, MixRamp uses -1
221
	 * to signal mixing rather than fading */
222
	if (portion1 < 0)
223
		return pcm_add(buffer1, buffer2, size, format);
224

Rosen Penev's avatar
Rosen Penev committed
225
	s = std::sin((float)M_PI_2 * portion1);
226 227
	s *= s;

228
	int vol1 = lround(s * PCM_VOLUME_1S);
229
	vol1 = Clamp<int>(vol1, 0, PCM_VOLUME_1S);
230

231 232
	return pcm_add_vol(dither, buffer1, buffer2, size,
			   vol1, PCM_VOLUME_1S - vol1, format);
233
}