Rework memory management to allow shared memory data transfer. The central idea
[pulseaudio.git] / src / pulsecore / sample-util.c
blob7f5d8a0206fba08fba2ab039f2ce88849d856b2c
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <stdlib.h>
31 #include <liboil/liboilfuncs.h>
33 #include <pulsecore/log.h>
35 #include "sample-util.h"
36 #include "endianmacros.h"
38 pa_memblock *pa_silence_memblock_new(pa_mempool *pool, const pa_sample_spec *spec, size_t length) {
39 assert(pool);
40 assert(spec);
42 if (length == 0)
43 length = pa_bytes_per_second(spec)/20; /* 50 ms */
45 return pa_silence_memblock(pa_memblock_new(pool, length), spec);
48 pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) {
49 assert(b && b->data && spec);
50 pa_silence_memory(b->data, b->length, spec);
51 return b;
54 void pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec) {
55 assert(c && c->memblock && c->memblock->data && spec && c->length);
57 pa_silence_memory((uint8_t*) c->memblock->data+c->index, c->length, spec);
60 void pa_silence_memory(void *p, size_t length, const pa_sample_spec *spec) {
61 uint8_t c = 0;
62 assert(p && length && spec);
64 switch (spec->format) {
65 case PA_SAMPLE_U8:
66 c = 0x80;
67 break;
68 case PA_SAMPLE_S16LE:
69 case PA_SAMPLE_S16BE:
70 case PA_SAMPLE_FLOAT32:
71 c = 0;
72 break;
73 case PA_SAMPLE_ALAW:
74 case PA_SAMPLE_ULAW:
75 c = 80;
76 break;
77 default:
78 assert(0);
81 memset(p, c, length);
84 size_t pa_mix(
85 const pa_mix_info streams[],
86 unsigned nstreams,
87 void *data,
88 size_t length,
89 const pa_sample_spec *spec,
90 const pa_cvolume *volume,
91 int mute) {
93 assert(streams && data && length && spec);
95 switch (spec->format) {
96 case PA_SAMPLE_S16NE:{
97 size_t d;
98 unsigned channel = 0;
100 for (d = 0;; d += sizeof(int16_t)) {
101 int32_t sum = 0;
103 if (d >= length)
104 return d;
106 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
107 unsigned i;
109 for (i = 0; i < nstreams; i++) {
110 int32_t v;
111 pa_volume_t cvolume = streams[i].volume.values[channel];
113 if (d >= streams[i].chunk.length)
114 return d;
116 if (cvolume == PA_VOLUME_MUTED)
117 v = 0;
118 else {
119 v = *((int16_t*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d));
121 if (cvolume != PA_VOLUME_NORM)
122 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
125 sum += v;
128 if (volume->values[channel] != PA_VOLUME_NORM)
129 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
131 if (sum < -0x8000) sum = -0x8000;
132 if (sum > 0x7FFF) sum = 0x7FFF;
136 *((int16_t*) data) = sum;
137 data = (uint8_t*) data + sizeof(int16_t);
139 if (++channel >= spec->channels)
140 channel = 0;
144 case PA_SAMPLE_S16RE:{
145 size_t d;
146 unsigned channel = 0;
148 for (d = 0;; d += sizeof(int16_t)) {
149 int32_t sum = 0;
151 if (d >= length)
152 return d;
154 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
155 unsigned i;
157 for (i = 0; i < nstreams; i++) {
158 int32_t v;
159 pa_volume_t cvolume = streams[i].volume.values[channel];
161 if (d >= streams[i].chunk.length)
162 return d;
164 if (cvolume == PA_VOLUME_MUTED)
165 v = 0;
166 else {
167 v = INT16_SWAP(*((int16_t*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d)));
169 if (cvolume != PA_VOLUME_NORM)
170 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
173 sum += v;
176 if (volume->values[channel] != PA_VOLUME_NORM)
177 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
179 if (sum < -0x8000) sum = -0x8000;
180 if (sum > 0x7FFF) sum = 0x7FFF;
184 *((int16_t*) data) = INT16_SWAP(sum);
185 data = (uint8_t*) data + sizeof(int16_t);
187 if (++channel >= spec->channels)
188 channel = 0;
192 case PA_SAMPLE_U8: {
193 size_t d;
194 unsigned channel = 0;
196 for (d = 0;; d ++) {
197 int32_t sum = 0;
199 if (d >= length)
200 return d;
202 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
203 unsigned i;
205 for (i = 0; i < nstreams; i++) {
206 int32_t v;
207 pa_volume_t cvolume = streams[i].volume.values[channel];
209 if (d >= streams[i].chunk.length)
210 return d;
212 if (cvolume == PA_VOLUME_MUTED)
213 v = 0;
214 else {
215 v = (int32_t) *((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d) - 0x80;
217 if (cvolume != PA_VOLUME_NORM)
218 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
221 sum += v;
224 if (volume->values[channel] != PA_VOLUME_NORM)
225 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
227 if (sum < -0x80) sum = -0x80;
228 if (sum > 0x7F) sum = 0x7F;
232 *((uint8_t*) data) = (uint8_t) (sum + 0x80);
233 data = (uint8_t*) data + 1;
235 if (++channel >= spec->channels)
236 channel = 0;
240 case PA_SAMPLE_FLOAT32NE: {
241 size_t d;
242 unsigned channel = 0;
244 for (d = 0;; d += sizeof(float)) {
245 float sum = 0;
247 if (d >= length)
248 return d;
250 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
251 unsigned i;
253 for (i = 0; i < nstreams; i++) {
254 float v;
255 pa_volume_t cvolume = streams[i].volume.values[channel];
257 if (d >= streams[i].chunk.length)
258 return d;
260 if (cvolume == PA_VOLUME_MUTED)
261 v = 0;
262 else {
263 v = *((float*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d));
265 if (cvolume != PA_VOLUME_NORM)
266 v *= pa_sw_volume_to_linear(cvolume);
269 sum += v;
272 if (volume->values[channel] != PA_VOLUME_NORM)
273 sum *= pa_sw_volume_to_linear(volume->values[channel]);
276 *((float*) data) = sum;
277 data = (uint8_t*) data + sizeof(float);
279 if (++channel >= spec->channels)
280 channel = 0;
284 default:
285 pa_log_error(__FILE__": ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec->format));
286 abort();
291 void pa_volume_memchunk(pa_memchunk*c, const pa_sample_spec *spec, const pa_cvolume *volume) {
292 assert(c && spec && (c->length % pa_frame_size(spec) == 0));
293 assert(volume);
295 if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_NORM))
296 return;
298 if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_MUTED)) {
299 pa_silence_memchunk(c, spec);
300 return;
303 switch (spec->format) {
304 case PA_SAMPLE_S16NE: {
305 int16_t *d;
306 size_t n;
307 unsigned channel;
308 double linear[PA_CHANNELS_MAX];
310 for (channel = 0; channel < spec->channels; channel++)
311 linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
313 for (channel = 0, d = (int16_t*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
314 int32_t t = (int32_t)(*d);
316 t = (int32_t) (t * linear[channel]);
318 if (t < -0x8000) t = -0x8000;
319 if (t > 0x7FFF) t = 0x7FFF;
321 *d = (int16_t) t;
323 if (++channel >= spec->channels)
324 channel = 0;
326 break;
329 case PA_SAMPLE_S16RE: {
330 int16_t *d;
331 size_t n;
332 unsigned channel;
333 double linear[PA_CHANNELS_MAX];
335 for (channel = 0; channel < spec->channels; channel++)
336 linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
338 for (channel = 0, d = (int16_t*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
339 int32_t t = (int32_t)(INT16_SWAP(*d));
341 t = (int32_t) (t * linear[channel]);
343 if (t < -0x8000) t = -0x8000;
344 if (t > 0x7FFF) t = 0x7FFF;
346 *d = INT16_SWAP((int16_t) t);
348 if (++channel >= spec->channels)
349 channel = 0;
352 break;
355 case PA_SAMPLE_U8: {
356 uint8_t *d;
357 size_t n;
358 unsigned channel = 0;
360 for (d = (uint8_t*) c->memblock->data + c->index, n = c->length; n > 0; d++, n--) {
361 int32_t t = (int32_t) *d - 0x80;
363 t = (int32_t) (t * pa_sw_volume_to_linear(volume->values[channel]));
365 if (t < -0x80) t = -0x80;
366 if (t > 0x7F) t = 0x7F;
368 *d = (uint8_t) (t + 0x80);
370 if (++channel >= spec->channels)
371 channel = 0;
373 break;
376 case PA_SAMPLE_FLOAT32NE: {
377 float *d;
378 int skip;
379 unsigned n;
380 unsigned channel;
382 d = (float*) ((uint8_t*) c->memblock->data + c->index);
383 skip = spec->channels * sizeof(float);
384 n = c->length/sizeof(float)/spec->channels;
386 for (channel = 0; channel < spec->channels ; channel ++) {
387 float v, *t;
389 if (volume->values[channel] == PA_VOLUME_NORM)
390 continue;
392 v = (float) pa_sw_volume_to_linear(volume->values[channel]);
394 t = d + channel;
395 oil_scalarmult_f32(t, skip, t, skip, &v, n);
397 break;
400 default:
401 pa_log_error(__FILE__": ERROR: Unable to change volume of format %s.",
402 pa_sample_format_to_string(spec->format));
403 abort();