]> code.delx.au - pulseaudio/blob - src/pulsecore/sample-util.c
Fix length calculation in pa_silence_memblock_new() and make use of pa_assert() every...
[pulseaudio] / 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 <stdlib.h>
32
33 #include <liboil/liboilfuncs.h>
34
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.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 pa_assert(pool);
46 pa_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
56 length = (length+fs-1)/fs;
57
58 if (length <= 0)
59 length = 1;
60
61 length *= fs;
62
63 return pa_silence_memblock(pa_memblock_new(pool, length), spec);
64 }
65
66 pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) {
67 void *data;
68
69 pa_assert(b);
70 pa_assert(spec);
71
72 data = pa_memblock_acquire(b);
73 pa_silence_memory(data, pa_memblock_get_length(b), spec);
74 pa_memblock_release(b);
75 return b;
76 }
77
78 void pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec) {
79 void *data;
80
81 pa_assert(c);
82 pa_assert(c->memblock);
83 pa_assert(spec);
84
85 data = pa_memblock_acquire(c->memblock);
86 pa_silence_memory((uint8_t*) data+c->index, c->length, spec);
87 pa_memblock_release(c->memblock);
88 }
89
90 void pa_silence_memory(void *p, size_t length, const pa_sample_spec *spec) {
91 uint8_t c = 0;
92 pa_assert(p);
93 pa_assert(length > 0);
94 pa_assert(spec);
95
96 switch (spec->format) {
97 case PA_SAMPLE_U8:
98 c = 0x80;
99 break;
100 case PA_SAMPLE_S16LE:
101 case PA_SAMPLE_S16BE:
102 case PA_SAMPLE_FLOAT32:
103 case PA_SAMPLE_FLOAT32RE:
104 c = 0;
105 break;
106 case PA_SAMPLE_ALAW:
107 case PA_SAMPLE_ULAW:
108 c = 80;
109 break;
110 default:
111 pa_assert_not_reached();
112 }
113
114 memset(p, c, length);
115 }
116
117 size_t pa_mix(
118 pa_mix_info streams[],
119 unsigned nstreams,
120 void *data,
121 size_t length,
122 const pa_sample_spec *spec,
123 const pa_cvolume *volume,
124 int mute) {
125
126 pa_cvolume full_volume;
127 size_t d = 0;
128 unsigned k;
129
130 pa_assert(streams);
131 pa_assert(data);
132 pa_assert(length);
133 pa_assert(spec);
134
135 if (!volume)
136 volume = pa_cvolume_reset(&full_volume, spec->channels);
137
138 for (k = 0; k < nstreams; k++)
139 streams[k].internal = pa_memblock_acquire(streams[k].chunk.memblock);
140
141 switch (spec->format) {
142 case PA_SAMPLE_S16NE:{
143 unsigned channel = 0;
144
145 for (d = 0;; d += sizeof(int16_t)) {
146 int32_t sum = 0;
147
148 if (d >= length)
149 goto finish;
150
151 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
152 unsigned i;
153
154 for (i = 0; i < nstreams; i++) {
155 int32_t v;
156 pa_volume_t cvolume = streams[i].volume.values[channel];
157
158 if (d >= streams[i].chunk.length)
159 goto finish;
160
161 if (cvolume == PA_VOLUME_MUTED)
162 v = 0;
163 else {
164 v = *((int16_t*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d));
165
166 if (cvolume != PA_VOLUME_NORM)
167 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
168 }
169
170 sum += v;
171 }
172
173 if (volume->values[channel] != PA_VOLUME_NORM)
174 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
175
176 if (sum < -0x8000) sum = -0x8000;
177 if (sum > 0x7FFF) sum = 0x7FFF;
178
179 }
180
181 *((int16_t*) data) = sum;
182 data = (uint8_t*) data + sizeof(int16_t);
183
184 if (++channel >= spec->channels)
185 channel = 0;
186 }
187
188 break;
189 }
190
191 case PA_SAMPLE_S16RE:{
192 unsigned channel = 0;
193
194 for (d = 0;; d += sizeof(int16_t)) {
195 int32_t sum = 0;
196
197 if (d >= length)
198 goto finish;
199
200 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
201 unsigned i;
202
203 for (i = 0; i < nstreams; i++) {
204 int32_t v;
205 pa_volume_t cvolume = streams[i].volume.values[channel];
206
207 if (d >= streams[i].chunk.length)
208 goto finish;
209
210 if (cvolume == PA_VOLUME_MUTED)
211 v = 0;
212 else {
213 v = INT16_SWAP(*((int16_t*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d)));
214
215 if (cvolume != PA_VOLUME_NORM)
216 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
217 }
218
219 sum += v;
220 }
221
222 if (volume->values[channel] != PA_VOLUME_NORM)
223 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
224
225 if (sum < -0x8000) sum = -0x8000;
226 if (sum > 0x7FFF) sum = 0x7FFF;
227
228 }
229
230 *((int16_t*) data) = INT16_SWAP(sum);
231 data = (uint8_t*) data + sizeof(int16_t);
232
233 if (++channel >= spec->channels)
234 channel = 0;
235 }
236
237 break;
238 }
239
240 case PA_SAMPLE_U8: {
241 unsigned channel = 0;
242
243 for (d = 0;; d ++) {
244 int32_t sum = 0;
245
246 if (d >= length)
247 goto finish;
248
249 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
250 unsigned i;
251
252 for (i = 0; i < nstreams; i++) {
253 int32_t v;
254 pa_volume_t cvolume = streams[i].volume.values[channel];
255
256 if (d >= streams[i].chunk.length)
257 goto finish;
258
259 if (cvolume == PA_VOLUME_MUTED)
260 v = 0;
261 else {
262 v = (int32_t) *((uint8_t*) streams[i].internal + streams[i].chunk.index + d) - 0x80;
263
264 if (cvolume != PA_VOLUME_NORM)
265 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
266 }
267
268 sum += v;
269 }
270
271 if (volume->values[channel] != PA_VOLUME_NORM)
272 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
273
274 if (sum < -0x80) sum = -0x80;
275 if (sum > 0x7F) sum = 0x7F;
276
277 }
278
279 *((uint8_t*) data) = (uint8_t) (sum + 0x80);
280 data = (uint8_t*) data + 1;
281
282 if (++channel >= spec->channels)
283 channel = 0;
284 }
285
286 break;
287 }
288
289 case PA_SAMPLE_FLOAT32NE: {
290 unsigned channel = 0;
291
292 for (d = 0;; d += sizeof(float)) {
293 float sum = 0;
294
295 if (d >= length)
296 goto finish;
297
298 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
299 unsigned i;
300
301 for (i = 0; i < nstreams; i++) {
302 float v;
303 pa_volume_t cvolume = streams[i].volume.values[channel];
304
305 if (d >= streams[i].chunk.length)
306 goto finish;
307
308 if (cvolume == PA_VOLUME_MUTED)
309 v = 0;
310 else {
311 v = *((float*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d));
312
313 if (cvolume != PA_VOLUME_NORM)
314 v *= pa_sw_volume_to_linear(cvolume);
315 }
316
317 sum += v;
318 }
319
320 if (volume->values[channel] != PA_VOLUME_NORM)
321 sum *= pa_sw_volume_to_linear(volume->values[channel]);
322 }
323
324 *((float*) data) = sum;
325 data = (uint8_t*) data + sizeof(float);
326
327 if (++channel >= spec->channels)
328 channel = 0;
329 }
330
331 break;
332 }
333
334 default:
335 pa_log_error("ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec->format));
336 abort();
337 }
338
339 finish:
340
341 for (k = 0; k < nstreams; k++)
342 pa_memblock_release(streams[k].chunk.memblock);
343
344 return d;
345 }
346
347
348 void pa_volume_memchunk(
349 pa_memchunk*c,
350 const pa_sample_spec *spec,
351 const pa_cvolume *volume) {
352
353 void *ptr;
354
355 pa_assert(c);
356 pa_assert(spec);
357 pa_assert(c->length % pa_frame_size(spec) == 0);
358 pa_assert(volume);
359
360 if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_NORM))
361 return;
362
363 if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_MUTED)) {
364 pa_silence_memchunk(c, spec);
365 return;
366 }
367
368 ptr = pa_memblock_acquire(c->memblock);
369
370 switch (spec->format) {
371 case PA_SAMPLE_S16NE: {
372 int16_t *d;
373 size_t n;
374 unsigned channel;
375 double linear[PA_CHANNELS_MAX];
376
377 for (channel = 0; channel < spec->channels; channel++)
378 linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
379
380 for (channel = 0, d = (int16_t*) ((uint8_t*) ptr + c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
381 int32_t t = (int32_t)(*d);
382
383 t = (int32_t) (t * linear[channel]);
384
385 if (t < -0x8000) t = -0x8000;
386 if (t > 0x7FFF) t = 0x7FFF;
387
388 *d = (int16_t) t;
389
390 if (++channel >= spec->channels)
391 channel = 0;
392 }
393 break;
394 }
395
396 case PA_SAMPLE_S16RE: {
397 int16_t *d;
398 size_t n;
399 unsigned channel;
400 double linear[PA_CHANNELS_MAX];
401
402 for (channel = 0; channel < spec->channels; channel++)
403 linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
404
405 for (channel = 0, d = (int16_t*) ((uint8_t*) ptr + c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
406 int32_t t = (int32_t)(INT16_SWAP(*d));
407
408 t = (int32_t) (t * linear[channel]);
409
410 if (t < -0x8000) t = -0x8000;
411 if (t > 0x7FFF) t = 0x7FFF;
412
413 *d = INT16_SWAP((int16_t) t);
414
415 if (++channel >= spec->channels)
416 channel = 0;
417 }
418
419 break;
420 }
421
422 case PA_SAMPLE_U8: {
423 uint8_t *d;
424 size_t n;
425 unsigned channel = 0;
426
427 for (d = (uint8_t*) ptr + c->index, n = c->length; n > 0; d++, n--) {
428 int32_t t = (int32_t) *d - 0x80;
429
430 t = (int32_t) (t * pa_sw_volume_to_linear(volume->values[channel]));
431
432 if (t < -0x80) t = -0x80;
433 if (t > 0x7F) t = 0x7F;
434
435 *d = (uint8_t) (t + 0x80);
436
437 if (++channel >= spec->channels)
438 channel = 0;
439 }
440 break;
441 }
442
443 case PA_SAMPLE_FLOAT32NE: {
444 float *d;
445 int skip;
446 unsigned n;
447 unsigned channel;
448
449 d = (float*) ((uint8_t*) ptr + c->index);
450 skip = spec->channels * sizeof(float);
451 n = c->length/sizeof(float)/spec->channels;
452
453 for (channel = 0; channel < spec->channels ; channel ++) {
454 float v, *t;
455
456 if (volume->values[channel] == PA_VOLUME_NORM)
457 continue;
458
459 v = (float) pa_sw_volume_to_linear(volume->values[channel]);
460
461 t = d + channel;
462 oil_scalarmult_f32(t, skip, t, skip, &v, n);
463 }
464 break;
465 }
466
467 default:
468 pa_log_error("ERROR: Unable to change volume of format %s.",
469 pa_sample_format_to_string(spec->format));
470 abort();
471 }
472
473 pa_memblock_release(c->memblock);
474 }
475