2
* Copyright (C) 2003-2011 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
#include "pcm_volume.h"
23
#include "pcm_utils.h"
24
#include "audio_format.h"
31
#define G_LOG_DOMAIN "pcm"
34
pcm_add_vol_8(int8_t *buffer1, const int8_t *buffer2,
35
unsigned num_samples, int volume1, int volume2)
37
while (num_samples > 0) {
38
int32_t sample1 = *buffer1;
39
int32_t sample2 = *buffer2++;
41
sample1 = ((sample1 * volume1 + sample2 * volume2) +
42
pcm_volume_dither() + PCM_VOLUME_1 / 2)
45
*buffer1++ = pcm_range(sample1, 8);
51
pcm_add_vol_16(int16_t *buffer1, const int16_t *buffer2,
52
unsigned num_samples, int volume1, int volume2)
54
while (num_samples > 0) {
55
int32_t sample1 = *buffer1;
56
int32_t sample2 = *buffer2++;
58
sample1 = ((sample1 * volume1 + sample2 * volume2) +
59
pcm_volume_dither() + PCM_VOLUME_1 / 2)
62
*buffer1++ = pcm_range(sample1, 16);
68
pcm_add_vol_24(int32_t *buffer1, const int32_t *buffer2,
69
unsigned num_samples, unsigned volume1, unsigned volume2)
71
while (num_samples > 0) {
72
int64_t sample1 = *buffer1;
73
int64_t sample2 = *buffer2++;
75
sample1 = ((sample1 * volume1 + sample2 * volume2) +
76
pcm_volume_dither() + PCM_VOLUME_1 / 2)
79
*buffer1++ = pcm_range(sample1, 24);
85
pcm_add_vol_32(int32_t *buffer1, const int32_t *buffer2,
86
unsigned num_samples, unsigned volume1, unsigned volume2)
88
while (num_samples > 0) {
89
int64_t sample1 = *buffer1;
90
int64_t sample2 = *buffer2++;
92
sample1 = ((sample1 * volume1 + sample2 * volume2) +
93
pcm_volume_dither() + PCM_VOLUME_1 / 2)
96
*buffer1++ = pcm_range_64(sample1, 32);
102
pcm_add_vol_float(float *buffer1, const float *buffer2,
103
unsigned num_samples, float volume1, float volume2)
105
while (num_samples > 0) {
106
float sample1 = *buffer1;
107
float sample2 = *buffer2++;
109
sample1 = (sample1 * volume1 + sample2 * volume2);
110
*buffer1++ = sample1;
116
pcm_add_vol(void *buffer1, const void *buffer2, size_t size,
118
enum sample_format format)
121
case SAMPLE_FORMAT_UNDEFINED:
122
case SAMPLE_FORMAT_DSD:
123
/* not implemented */
126
case SAMPLE_FORMAT_S8:
127
pcm_add_vol_8((int8_t *)buffer1, (const int8_t *)buffer2,
131
case SAMPLE_FORMAT_S16:
132
pcm_add_vol_16((int16_t *)buffer1, (const int16_t *)buffer2,
133
size / 2, vol1, vol2);
136
case SAMPLE_FORMAT_S24_P32:
137
pcm_add_vol_24((int32_t *)buffer1, (const int32_t *)buffer2,
138
size / 4, vol1, vol2);
141
case SAMPLE_FORMAT_S32:
142
pcm_add_vol_32((int32_t *)buffer1, (const int32_t *)buffer2,
143
size / 4, vol1, vol2);
146
case SAMPLE_FORMAT_FLOAT:
147
pcm_add_vol_float(buffer1, buffer2, size / 4,
148
pcm_volume_to_float(vol1),
149
pcm_volume_to_float(vol2));
159
pcm_add_8(int8_t *buffer1, const int8_t *buffer2, unsigned num_samples)
161
while (num_samples > 0) {
162
int32_t sample1 = *buffer1;
163
int32_t sample2 = *buffer2++;
167
*buffer1++ = pcm_range(sample1, 8);
173
pcm_add_16(int16_t *buffer1, const int16_t *buffer2, unsigned num_samples)
175
while (num_samples > 0) {
176
int32_t sample1 = *buffer1;
177
int32_t sample2 = *buffer2++;
181
*buffer1++ = pcm_range(sample1, 16);
187
pcm_add_24(int32_t *buffer1, const int32_t *buffer2, unsigned num_samples)
189
while (num_samples > 0) {
190
int64_t sample1 = *buffer1;
191
int64_t sample2 = *buffer2++;
195
*buffer1++ = pcm_range(sample1, 24);
201
pcm_add_32(int32_t *buffer1, const int32_t *buffer2, unsigned num_samples)
203
while (num_samples > 0) {
204
int64_t sample1 = *buffer1;
205
int64_t sample2 = *buffer2++;
209
*buffer1++ = pcm_range_64(sample1, 32);
215
pcm_add_float(float *buffer1, const float *buffer2, unsigned num_samples)
217
while (num_samples > 0) {
218
float sample1 = *buffer1;
219
float sample2 = *buffer2++;
220
*buffer1++ = sample1 + sample2;
226
pcm_add(void *buffer1, const void *buffer2, size_t size,
227
enum sample_format format)
230
case SAMPLE_FORMAT_UNDEFINED:
231
case SAMPLE_FORMAT_DSD:
232
/* not implemented */
235
case SAMPLE_FORMAT_S8:
236
pcm_add_8((int8_t *)buffer1, (const int8_t *)buffer2, size);
239
case SAMPLE_FORMAT_S16:
240
pcm_add_16((int16_t *)buffer1, (const int16_t *)buffer2, size / 2);
243
case SAMPLE_FORMAT_S24_P32:
244
pcm_add_24((int32_t *)buffer1, (const int32_t *)buffer2, size / 4);
247
case SAMPLE_FORMAT_S32:
248
pcm_add_32((int32_t *)buffer1, (const int32_t *)buffer2, size / 4);
251
case SAMPLE_FORMAT_FLOAT:
252
pcm_add_float(buffer1, buffer2, size / 4);
262
pcm_mix(void *buffer1, const void *buffer2, size_t size,
263
enum sample_format format, float portion1)
268
/* portion1 is between 0.0 and 1.0 for crossfading, MixRamp uses NaN
269
* to signal mixing rather than fading */
271
return pcm_add(buffer1, buffer2, size, format);
273
s = sin(M_PI_2 * portion1);
276
vol1 = s * PCM_VOLUME_1 + 0.5;
277
vol1 = vol1 > PCM_VOLUME_1 ? PCM_VOLUME_1 : (vol1 < 0 ? 0 : vol1);
279
return pcm_add_vol(buffer1, buffer2, size, vol1, PCM_VOLUME_1 - vol1, format);