PcmMix.cxx 5.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "PcmMix.hxx"
22
#include "Volume.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "PcmUtils.hxx"
24
#include "AudioFormat.hxx"
25
#include "Traits.hxx"
26
#include "util/Clamp.hxx"
27

28 29
#include "PcmDither.cxx" // including the .cxx file to get inlined templates

30
#include <assert.h>
31 32
#include <math.h>

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

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

47
template<SampleFormat F, class Traits=SampleTraits<F>>
48
static void
49 50
PcmAddVolume(PcmDither &dither,
	     typename Traits::pointer_type a,
51 52
	     typename Traits::const_pointer_type b,
	     size_t n, int volume1, int volume2)
53
{
54
	for (size_t i = 0; i != n; ++i)
55 56
		a[i] = PcmAddVolume<F, Traits>(dither, a[i], b[i],
					       volume1, volume2);
57 58
}

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

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

74 75 76 77 78 79 80 81 82 83 84 85 86 87
static void
pcm_add_vol_float(float *buffer1, const float *buffer2,
		  unsigned num_samples, float volume1, float volume2)
{
	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,
91
	    SampleFormat format)
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 136 137
template<SampleFormat F, class Traits=SampleTraits<F>>
static typename Traits::value_type
PcmAdd(typename Traits::value_type _a, typename Traits::value_type _b)
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 148
PcmAdd(typename Traits::pointer_type a,
       typename Traits::const_pointer_type b,
       size_t n)
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
156
PcmAddVoid(void *a, const void *b, size_t size)
157
{
158
	constexpr size_t sample_size = Traits::SAMPLE_SIZE;
159
	assert(size % sample_size == 0);
160

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

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

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

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

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

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

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

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

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

213
bool
214
pcm_mix(PcmDither &dither, void *buffer1, const void *buffer2, size_t size,
215
	SampleFormat format, float portion1)
216
{
217 218
	float s;

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

	s = sin(M_PI_2 * portion1);
225 226
	s *= s;

227 228
	int vol1 = s * PCM_VOLUME_1S + 0.5;
	vol1 = Clamp<int>(vol1, 0, PCM_VOLUME_1S);
229

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