a99714080b97d4114e7c4a699206cfbdd4d7e5b1
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / sample-util.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <stdlib.h>
33
34 #include <liboil/liboilfuncs.h>
35
36 #include <pulsecore/log.h>
37
38 #include "sample-util.h"
39 #include "endianmacros.h"
40
41 #define PA_SILENCE_MAX (1024*1024*1)
42
43 pa_memblock *pa_silence_memblock_new(pa_mempool *pool, const pa_sample_spec *spec, size_t length) {
44     size_t fs;
45     assert(pool);
46     assert(spec);
47
48     if (length == 0)
49         length = pa_bytes_per_second(spec)/20; /* 50 ms */
50
51     if (length > PA_SILENCE_MAX)
52         length = PA_SILENCE_MAX;
53
54     fs = pa_frame_size(spec);
55     length = ((PA_SILENCE_MAX+fs-1) / fs) * fs;
56
57     if (length <= 0)
58         length = fs;
59
60     return pa_silence_memblock(pa_memblock_new(pool, length), spec);
61 }
62
63 pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) {
64     assert(b && b->data && spec);
65     pa_silence_memory(b->data, b->length, spec);
66     return b;
67 }
68
69 void pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec) {
70     assert(c && c->memblock && c->memblock->data && spec && c->length);
71
72     pa_silence_memory((uint8_t*) c->memblock->data+c->index, c->length, spec);
73 }
74
75 void pa_silence_memory(void *p, size_t length, const pa_sample_spec *spec) {
76     uint8_t c = 0;
77     assert(p && length && spec);
78
79     switch (spec->format) {
80         case PA_SAMPLE_U8:
81             c = 0x80;
82             break;
83         case PA_SAMPLE_S16LE:
84         case PA_SAMPLE_S16BE:
85         case PA_SAMPLE_FLOAT32:
86         case PA_SAMPLE_FLOAT32RE:
87             c = 0;
88             break;
89         case PA_SAMPLE_ALAW:
90         case PA_SAMPLE_ULAW:
91             c = 80;
92             break;
93         default:
94             assert(0);
95     }
96
97     memset(p, c, length);
98 }
99
100 size_t pa_mix(
101     const pa_mix_info streams[],
102     unsigned nstreams,
103     void *data,
104     size_t length,
105     const pa_sample_spec *spec,
106     const pa_cvolume *volume,
107     int mute) {
108
109     assert(streams && data && length && spec);
110
111     switch (spec->format) {
112         case PA_SAMPLE_S16NE:{
113             size_t d;
114             unsigned channel = 0;
115
116             for (d = 0;; d += sizeof(int16_t)) {
117                 int32_t sum = 0;
118
119                 if (d >= length)
120                     return d;
121
122                 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
123                     unsigned i;
124
125                     for (i = 0; i < nstreams; i++) {
126                         int32_t v;
127                         pa_volume_t cvolume = streams[i].volume.values[channel];
128
129                         if (d >= streams[i].chunk.length)
130                             return d;
131
132                         if (cvolume == PA_VOLUME_MUTED)
133                             v = 0;
134                         else {
135                             v = *((int16_t*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d));
136
137                             if (cvolume != PA_VOLUME_NORM)
138                                 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
139                         }
140
141                         sum += v;
142                     }
143
144                     if (volume->values[channel] != PA_VOLUME_NORM)
145                         sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
146
147                     if (sum < -0x8000) sum = -0x8000;
148                     if (sum > 0x7FFF) sum = 0x7FFF;
149
150                 }
151
152                 *((int16_t*) data) = sum;
153                 data = (uint8_t*) data + sizeof(int16_t);
154
155                 if (++channel >= spec->channels)
156                     channel = 0;
157             }
158         }
159
160         case PA_SAMPLE_S16RE:{
161             size_t d;
162             unsigned channel = 0;
163
164             for (d = 0;; d += sizeof(int16_t)) {
165                 int32_t sum = 0;
166
167                 if (d >= length)
168                     return d;
169
170                 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
171                     unsigned i;
172
173                     for (i = 0; i < nstreams; i++) {
174                         int32_t v;
175                         pa_volume_t cvolume = streams[i].volume.values[channel];
176
177                         if (d >= streams[i].chunk.length)
178                             return d;
179
180                         if (cvolume == PA_VOLUME_MUTED)
181                             v = 0;
182                         else {
183                             v = INT16_SWAP(*((int16_t*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d)));
184
185                             if (cvolume != PA_VOLUME_NORM)
186                                 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
187                         }
188
189                         sum += v;
190                     }
191
192                     if (volume->values[channel] != PA_VOLUME_NORM)
193                         sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
194
195                     if (sum < -0x8000) sum = -0x8000;
196                     if (sum > 0x7FFF) sum = 0x7FFF;
197
198                 }
199
200                 *((int16_t*) data) = INT16_SWAP(sum);
201                 data = (uint8_t*) data + sizeof(int16_t);
202
203                 if (++channel >= spec->channels)
204                     channel = 0;
205             }
206         }
207
208         case PA_SAMPLE_U8: {
209             size_t d;
210             unsigned channel = 0;
211
212             for (d = 0;; d ++) {
213                 int32_t sum = 0;
214
215                 if (d >= length)
216                     return d;
217
218                 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
219                     unsigned i;
220
221                     for (i = 0; i < nstreams; i++) {
222                         int32_t v;
223                         pa_volume_t cvolume = streams[i].volume.values[channel];
224
225                         if (d >= streams[i].chunk.length)
226                             return d;
227
228                         if (cvolume == PA_VOLUME_MUTED)
229                             v = 0;
230                         else {
231                             v = (int32_t) *((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d) - 0x80;
232
233                             if (cvolume != PA_VOLUME_NORM)
234                                 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
235                         }
236
237                         sum += v;
238                     }
239
240                     if (volume->values[channel] != PA_VOLUME_NORM)
241                         sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
242
243                     if (sum < -0x80) sum = -0x80;
244                     if (sum > 0x7F) sum = 0x7F;
245
246                 }
247
248                 *((uint8_t*) data) = (uint8_t) (sum + 0x80);
249                 data = (uint8_t*) data + 1;
250
251                 if (++channel >= spec->channels)
252                     channel = 0;
253             }
254         }
255
256         case PA_SAMPLE_FLOAT32NE: {
257             size_t d;
258             unsigned channel = 0;
259
260             for (d = 0;; d += sizeof(float)) {
261                 float sum = 0;
262
263                 if (d >= length)
264                     return d;
265
266                 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
267                     unsigned i;
268
269                     for (i = 0; i < nstreams; i++) {
270                         float v;
271                         pa_volume_t cvolume = streams[i].volume.values[channel];
272
273                         if (d >= streams[i].chunk.length)
274                             return d;
275
276                         if (cvolume == PA_VOLUME_MUTED)
277                             v = 0;
278                         else {
279                             v = *((float*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d));
280
281                             if (cvolume != PA_VOLUME_NORM)
282                                 v *= pa_sw_volume_to_linear(cvolume);
283                         }
284
285                         sum += v;
286                     }
287
288                     if (volume->values[channel] != PA_VOLUME_NORM)
289                         sum *= pa_sw_volume_to_linear(volume->values[channel]);
290                 }
291
292                 *((float*) data) = sum;
293                 data = (uint8_t*) data + sizeof(float);
294
295                 if (++channel >= spec->channels)
296                     channel = 0;
297             }
298         }
299
300         default:
301             pa_log_error("ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec->format));
302             abort();
303     }
304 }
305
306
307 void pa_volume_memchunk(pa_memchunk*c, const pa_sample_spec *spec, const pa_cvolume *volume) {
308     assert(c && spec && (c->length % pa_frame_size(spec) == 0));
309     assert(volume);
310
311     if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_NORM))
312         return;
313
314     if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_MUTED)) {
315         pa_silence_memchunk(c, spec);
316         return;
317     }
318
319     switch (spec->format) {
320         case PA_SAMPLE_S16NE: {
321             int16_t *d;
322             size_t n;
323             unsigned channel;
324             double linear[PA_CHANNELS_MAX];
325
326             for (channel = 0; channel < spec->channels; channel++)
327                 linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
328
329             for (channel = 0, d = (int16_t*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
330                 int32_t t = (int32_t)(*d);
331
332                 t = (int32_t) (t * linear[channel]);
333
334                 if (t < -0x8000) t = -0x8000;
335                 if (t > 0x7FFF) t = 0x7FFF;
336
337                 *d = (int16_t) t;
338
339                 if (++channel >= spec->channels)
340                     channel = 0;
341             }
342             break;
343         }
344
345         case PA_SAMPLE_S16RE: {
346             int16_t *d;
347             size_t n;
348             unsigned channel;
349             double linear[PA_CHANNELS_MAX];
350
351             for (channel = 0; channel < spec->channels; channel++)
352                 linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
353
354             for (channel = 0, d = (int16_t*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
355                 int32_t t = (int32_t)(INT16_SWAP(*d));
356
357                 t = (int32_t) (t * linear[channel]);
358
359                 if (t < -0x8000) t = -0x8000;
360                 if (t > 0x7FFF) t = 0x7FFF;
361
362                 *d = INT16_SWAP((int16_t) t);
363
364                 if (++channel >= spec->channels)
365                     channel = 0;
366             }
367
368             break;
369         }
370
371         case PA_SAMPLE_U8: {
372             uint8_t *d;
373             size_t n;
374             unsigned channel = 0;
375
376             for (d = (uint8_t*) c->memblock->data + c->index, n = c->length; n > 0; d++, n--) {
377                 int32_t t = (int32_t) *d - 0x80;
378
379                 t = (int32_t) (t * pa_sw_volume_to_linear(volume->values[channel]));
380
381                 if (t < -0x80) t = -0x80;
382                 if (t > 0x7F) t = 0x7F;
383
384                 *d = (uint8_t) (t + 0x80);
385
386                 if (++channel >= spec->channels)
387                     channel = 0;
388             }
389             break;
390         }
391
392         case PA_SAMPLE_FLOAT32NE: {
393             float *d;
394             int skip;
395             unsigned n;
396             unsigned channel;
397
398             d = (float*) ((uint8_t*) c->memblock->data + c->index);
399             skip = spec->channels * sizeof(float);
400             n = c->length/sizeof(float)/spec->channels;
401
402             for (channel = 0; channel < spec->channels ; channel ++) {
403                 float v, *t;
404
405                 if (volume->values[channel] == PA_VOLUME_NORM)
406                     continue;
407
408                 v = (float) pa_sw_volume_to_linear(volume->values[channel]);
409
410                 t = d + channel;
411                 oil_scalarmult_f32(t, skip, t, skip, &v, n);
412             }
413             break;
414         }
415
416         default:
417             pa_log_error("ERROR: Unable to change volume of format %s.",
418                 pa_sample_format_to_string(spec->format));
419             abort();
420     }
421 }
422