4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
34 #include <liboil/liboilfuncs.h>
36 #include <pulsecore/log.h>
38 #include "sample-util.h"
39 #include "endianmacros.h"
41 pa_memblock *pa_silence_memblock_new(pa_mempool *pool, const pa_sample_spec *spec, size_t length) {
46 length = pa_bytes_per_second(spec)/20; /* 50 ms */
48 return pa_silence_memblock(pa_memblock_new(pool, length), spec);
51 pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) {
52 assert(b && b->data && spec);
53 pa_silence_memory(b->data, b->length, spec);
57 void pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec) {
58 assert(c && c->memblock && c->memblock->data && spec && c->length);
60 pa_silence_memory((uint8_t*) c->memblock->data+c->index, c->length, spec);
63 void pa_silence_memory(void *p, size_t length, const pa_sample_spec *spec) {
65 assert(p && length && spec);
67 switch (spec->format) {
73 case PA_SAMPLE_FLOAT32:
74 case PA_SAMPLE_FLOAT32RE:
89 const pa_mix_info streams[],
93 const pa_sample_spec *spec,
94 const pa_cvolume *volume,
97 assert(streams && data && length && spec);
99 switch (spec->format) {
100 case PA_SAMPLE_S16NE:{
102 unsigned channel = 0;
104 for (d = 0;; d += sizeof(int16_t)) {
110 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
113 for (i = 0; i < nstreams; i++) {
115 pa_volume_t cvolume = streams[i].volume.values[channel];
117 if (d >= streams[i].chunk.length)
120 if (cvolume == PA_VOLUME_MUTED)
123 v = *((int16_t*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d));
125 if (cvolume != PA_VOLUME_NORM)
126 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
132 if (volume->values[channel] != PA_VOLUME_NORM)
133 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
135 if (sum < -0x8000) sum = -0x8000;
136 if (sum > 0x7FFF) sum = 0x7FFF;
140 *((int16_t*) data) = sum;
141 data = (uint8_t*) data + sizeof(int16_t);
143 if (++channel >= spec->channels)
148 case PA_SAMPLE_S16RE:{
150 unsigned channel = 0;
152 for (d = 0;; d += sizeof(int16_t)) {
158 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
161 for (i = 0; i < nstreams; i++) {
163 pa_volume_t cvolume = streams[i].volume.values[channel];
165 if (d >= streams[i].chunk.length)
168 if (cvolume == PA_VOLUME_MUTED)
171 v = INT16_SWAP(*((int16_t*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d)));
173 if (cvolume != PA_VOLUME_NORM)
174 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
180 if (volume->values[channel] != PA_VOLUME_NORM)
181 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
183 if (sum < -0x8000) sum = -0x8000;
184 if (sum > 0x7FFF) sum = 0x7FFF;
188 *((int16_t*) data) = INT16_SWAP(sum);
189 data = (uint8_t*) data + sizeof(int16_t);
191 if (++channel >= spec->channels)
198 unsigned channel = 0;
206 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
209 for (i = 0; i < nstreams; i++) {
211 pa_volume_t cvolume = streams[i].volume.values[channel];
213 if (d >= streams[i].chunk.length)
216 if (cvolume == PA_VOLUME_MUTED)
219 v = (int32_t) *((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d) - 0x80;
221 if (cvolume != PA_VOLUME_NORM)
222 v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
228 if (volume->values[channel] != PA_VOLUME_NORM)
229 sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
231 if (sum < -0x80) sum = -0x80;
232 if (sum > 0x7F) sum = 0x7F;
236 *((uint8_t*) data) = (uint8_t) (sum + 0x80);
237 data = (uint8_t*) data + 1;
239 if (++channel >= spec->channels)
244 case PA_SAMPLE_FLOAT32NE: {
246 unsigned channel = 0;
248 for (d = 0;; d += sizeof(float)) {
254 if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
257 for (i = 0; i < nstreams; i++) {
259 pa_volume_t cvolume = streams[i].volume.values[channel];
261 if (d >= streams[i].chunk.length)
264 if (cvolume == PA_VOLUME_MUTED)
267 v = *((float*) ((uint8_t*) streams[i].chunk.memblock->data + streams[i].chunk.index + d));
269 if (cvolume != PA_VOLUME_NORM)
270 v *= pa_sw_volume_to_linear(cvolume);
276 if (volume->values[channel] != PA_VOLUME_NORM)
277 sum *= pa_sw_volume_to_linear(volume->values[channel]);
280 *((float*) data) = sum;
281 data = (uint8_t*) data + sizeof(float);
283 if (++channel >= spec->channels)
289 pa_log_error("ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec->format));
295 void pa_volume_memchunk(pa_memchunk*c, const pa_sample_spec *spec, const pa_cvolume *volume) {
296 assert(c && spec && (c->length % pa_frame_size(spec) == 0));
299 if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_NORM))
302 if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_MUTED)) {
303 pa_silence_memchunk(c, spec);
307 switch (spec->format) {
308 case PA_SAMPLE_S16NE: {
312 double linear[PA_CHANNELS_MAX];
314 for (channel = 0; channel < spec->channels; channel++)
315 linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
317 for (channel = 0, d = (int16_t*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
318 int32_t t = (int32_t)(*d);
320 t = (int32_t) (t * linear[channel]);
322 if (t < -0x8000) t = -0x8000;
323 if (t > 0x7FFF) t = 0x7FFF;
327 if (++channel >= spec->channels)
333 case PA_SAMPLE_S16RE: {
337 double linear[PA_CHANNELS_MAX];
339 for (channel = 0; channel < spec->channels; channel++)
340 linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
342 for (channel = 0, d = (int16_t*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
343 int32_t t = (int32_t)(INT16_SWAP(*d));
345 t = (int32_t) (t * linear[channel]);
347 if (t < -0x8000) t = -0x8000;
348 if (t > 0x7FFF) t = 0x7FFF;
350 *d = INT16_SWAP((int16_t) t);
352 if (++channel >= spec->channels)
362 unsigned channel = 0;
364 for (d = (uint8_t*) c->memblock->data + c->index, n = c->length; n > 0; d++, n--) {
365 int32_t t = (int32_t) *d - 0x80;
367 t = (int32_t) (t * pa_sw_volume_to_linear(volume->values[channel]));
369 if (t < -0x80) t = -0x80;
370 if (t > 0x7F) t = 0x7F;
372 *d = (uint8_t) (t + 0x80);
374 if (++channel >= spec->channels)
380 case PA_SAMPLE_FLOAT32NE: {
386 d = (float*) ((uint8_t*) c->memblock->data + c->index);
387 skip = spec->channels * sizeof(float);
388 n = c->length/sizeof(float)/spec->channels;
390 for (channel = 0; channel < spec->channels ; channel ++) {
393 if (volume->values[channel] == PA_VOLUME_NORM)
396 v = (float) pa_sw_volume_to_linear(volume->values[channel]);
399 oil_scalarmult_f32(t, skip, t, skip, &v, n);
405 pa_log_error("ERROR: Unable to change volume of format %s.",
406 pa_sample_format_to_string(spec->format));