~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/pcm_mix.c

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include "pcm_mix.h"
22
 
#include "pcm_volume.h"
23
 
#include "pcm_utils.h"
24
 
#include "audio_format.h"
25
 
 
26
 
#include <glib.h>
27
 
 
28
 
#include <math.h>
29
 
 
30
 
#undef G_LOG_DOMAIN
31
 
#define G_LOG_DOMAIN "pcm"
32
 
 
33
 
static void
34
 
pcm_add_vol_8(int8_t *buffer1, const int8_t *buffer2,
35
 
              unsigned num_samples, int volume1, int volume2)
36
 
{
37
 
        while (num_samples > 0) {
38
 
                int32_t sample1 = *buffer1;
39
 
                int32_t sample2 = *buffer2++;
40
 
 
41
 
                sample1 = ((sample1 * volume1 + sample2 * volume2) +
42
 
                           pcm_volume_dither() + PCM_VOLUME_1 / 2)
43
 
                        / PCM_VOLUME_1;
44
 
 
45
 
                *buffer1++ = pcm_range(sample1, 8);
46
 
                --num_samples;
47
 
        }
48
 
}
49
 
 
50
 
static void
51
 
pcm_add_vol_16(int16_t *buffer1, const int16_t *buffer2,
52
 
               unsigned num_samples, int volume1, int volume2)
53
 
{
54
 
        while (num_samples > 0) {
55
 
                int32_t sample1 = *buffer1;
56
 
                int32_t sample2 = *buffer2++;
57
 
 
58
 
                sample1 = ((sample1 * volume1 + sample2 * volume2) +
59
 
                           pcm_volume_dither() + PCM_VOLUME_1 / 2)
60
 
                        / PCM_VOLUME_1;
61
 
 
62
 
                *buffer1++ = pcm_range(sample1, 16);
63
 
                --num_samples;
64
 
        }
65
 
}
66
 
 
67
 
static void
68
 
pcm_add_vol_24(int32_t *buffer1, const int32_t *buffer2,
69
 
               unsigned num_samples, unsigned volume1, unsigned volume2)
70
 
{
71
 
        while (num_samples > 0) {
72
 
                int64_t sample1 = *buffer1;
73
 
                int64_t sample2 = *buffer2++;
74
 
 
75
 
                sample1 = ((sample1 * volume1 + sample2 * volume2) +
76
 
                           pcm_volume_dither() + PCM_VOLUME_1 / 2)
77
 
                        / PCM_VOLUME_1;
78
 
 
79
 
                *buffer1++ = pcm_range(sample1, 24);
80
 
                --num_samples;
81
 
        }
82
 
}
83
 
 
84
 
static void
85
 
pcm_add_vol_32(int32_t *buffer1, const int32_t *buffer2,
86
 
               unsigned num_samples, unsigned volume1, unsigned volume2)
87
 
{
88
 
        while (num_samples > 0) {
89
 
                int64_t sample1 = *buffer1;
90
 
                int64_t sample2 = *buffer2++;
91
 
 
92
 
                sample1 = ((sample1 * volume1 + sample2 * volume2) +
93
 
                           pcm_volume_dither() + PCM_VOLUME_1 / 2)
94
 
                        / PCM_VOLUME_1;
95
 
 
96
 
                *buffer1++ = pcm_range_64(sample1, 32);
97
 
                --num_samples;
98
 
        }
99
 
}
100
 
 
101
 
static void
102
 
pcm_add_vol_float(float *buffer1, const float *buffer2,
103
 
                  unsigned num_samples, float volume1, float volume2)
104
 
{
105
 
        while (num_samples > 0) {
106
 
                float sample1 = *buffer1;
107
 
                float sample2 = *buffer2++;
108
 
 
109
 
                sample1 = (sample1 * volume1 + sample2 * volume2);
110
 
                *buffer1++ = sample1;
111
 
                --num_samples;
112
 
        }
113
 
}
114
 
 
115
 
static bool
116
 
pcm_add_vol(void *buffer1, const void *buffer2, size_t size,
117
 
            int vol1, int vol2,
118
 
            enum sample_format format)
119
 
{
120
 
        switch (format) {
121
 
        case SAMPLE_FORMAT_UNDEFINED:
122
 
        case SAMPLE_FORMAT_DSD:
123
 
                /* not implemented */
124
 
                return false;
125
 
 
126
 
        case SAMPLE_FORMAT_S8:
127
 
                pcm_add_vol_8((int8_t *)buffer1, (const int8_t *)buffer2,
128
 
                              size, vol1, vol2);
129
 
                return true;
130
 
 
131
 
        case SAMPLE_FORMAT_S16:
132
 
                pcm_add_vol_16((int16_t *)buffer1, (const int16_t *)buffer2,
133
 
                               size / 2, vol1, vol2);
134
 
                return true;
135
 
 
136
 
        case SAMPLE_FORMAT_S24_P32:
137
 
                pcm_add_vol_24((int32_t *)buffer1, (const int32_t *)buffer2,
138
 
                               size / 4, vol1, vol2);
139
 
                return true;
140
 
 
141
 
        case SAMPLE_FORMAT_S32:
142
 
                pcm_add_vol_32((int32_t *)buffer1, (const int32_t *)buffer2,
143
 
                               size / 4, vol1, vol2);
144
 
                return true;
145
 
 
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));
150
 
                return true;
151
 
        }
152
 
 
153
 
        /* unreachable */
154
 
        assert(false);
155
 
        return false;
156
 
}
157
 
 
158
 
static void
159
 
pcm_add_8(int8_t *buffer1, const int8_t *buffer2, unsigned num_samples)
160
 
{
161
 
        while (num_samples > 0) {
162
 
                int32_t sample1 = *buffer1;
163
 
                int32_t sample2 = *buffer2++;
164
 
 
165
 
                sample1 += sample2;
166
 
 
167
 
                *buffer1++ = pcm_range(sample1, 8);
168
 
                --num_samples;
169
 
        }
170
 
}
171
 
 
172
 
static void
173
 
pcm_add_16(int16_t *buffer1, const int16_t *buffer2, unsigned num_samples)
174
 
{
175
 
        while (num_samples > 0) {
176
 
                int32_t sample1 = *buffer1;
177
 
                int32_t sample2 = *buffer2++;
178
 
 
179
 
                sample1 += sample2;
180
 
 
181
 
                *buffer1++ = pcm_range(sample1, 16);
182
 
                --num_samples;
183
 
        }
184
 
}
185
 
 
186
 
static void
187
 
pcm_add_24(int32_t *buffer1, const int32_t *buffer2, unsigned num_samples)
188
 
{
189
 
        while (num_samples > 0) {
190
 
                int64_t sample1 = *buffer1;
191
 
                int64_t sample2 = *buffer2++;
192
 
 
193
 
                sample1 += sample2;
194
 
 
195
 
                *buffer1++ = pcm_range(sample1, 24);
196
 
                --num_samples;
197
 
        }
198
 
}
199
 
 
200
 
static void
201
 
pcm_add_32(int32_t *buffer1, const int32_t *buffer2, unsigned num_samples)
202
 
{
203
 
        while (num_samples > 0) {
204
 
                int64_t sample1 = *buffer1;
205
 
                int64_t sample2 = *buffer2++;
206
 
 
207
 
                sample1 += sample2;
208
 
 
209
 
                *buffer1++ = pcm_range_64(sample1, 32);
210
 
                --num_samples;
211
 
        }
212
 
}
213
 
 
214
 
static void
215
 
pcm_add_float(float *buffer1, const float *buffer2, unsigned num_samples)
216
 
{
217
 
        while (num_samples > 0) {
218
 
                float sample1 = *buffer1;
219
 
                float sample2 = *buffer2++;
220
 
                *buffer1++ = sample1 + sample2;
221
 
                --num_samples;
222
 
        }
223
 
}
224
 
 
225
 
static bool
226
 
pcm_add(void *buffer1, const void *buffer2, size_t size,
227
 
        enum sample_format format)
228
 
{
229
 
        switch (format) {
230
 
        case SAMPLE_FORMAT_UNDEFINED:
231
 
        case SAMPLE_FORMAT_DSD:
232
 
                /* not implemented */
233
 
                return false;
234
 
 
235
 
        case SAMPLE_FORMAT_S8:
236
 
                pcm_add_8((int8_t *)buffer1, (const int8_t *)buffer2, size);
237
 
                return true;
238
 
 
239
 
        case SAMPLE_FORMAT_S16:
240
 
                pcm_add_16((int16_t *)buffer1, (const int16_t *)buffer2, size / 2);
241
 
                return true;
242
 
 
243
 
        case SAMPLE_FORMAT_S24_P32:
244
 
                pcm_add_24((int32_t *)buffer1, (const int32_t *)buffer2, size / 4);
245
 
                return true;
246
 
 
247
 
        case SAMPLE_FORMAT_S32:
248
 
                pcm_add_32((int32_t *)buffer1, (const int32_t *)buffer2, size / 4);
249
 
                return true;
250
 
 
251
 
        case SAMPLE_FORMAT_FLOAT:
252
 
                pcm_add_float(buffer1, buffer2, size / 4);
253
 
                return true;
254
 
        }
255
 
 
256
 
        /* unreachable */
257
 
        assert(false);
258
 
        return false;
259
 
}
260
 
 
261
 
bool
262
 
pcm_mix(void *buffer1, const void *buffer2, size_t size,
263
 
        enum sample_format format, float portion1)
264
 
{
265
 
        int vol1;
266
 
        float s;
267
 
 
268
 
        /* portion1 is between 0.0 and 1.0 for crossfading, MixRamp uses NaN
269
 
         * to signal mixing rather than fading */
270
 
        if (isnan(portion1))
271
 
                return pcm_add(buffer1, buffer2, size, format);
272
 
 
273
 
        s = sin(M_PI_2 * portion1);
274
 
        s *= s;
275
 
 
276
 
        vol1 = s * PCM_VOLUME_1 + 0.5;
277
 
        vol1 = vol1 > PCM_VOLUME_1 ? PCM_VOLUME_1 : (vol1 < 0 ? 0 : vol1);
278
 
 
279
 
        return pcm_add_vol(buffer1, buffer2, size, vol1, PCM_VOLUME_1 - vol1, format);
280
 
}