2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-29 20:37:24 +00:00
|
|
|
#include <stdio.h>
|
2004-06-23 23:17:30 +00:00
|
|
|
#include <string.h>
|
2004-11-20 16:23:53 +00:00
|
|
|
#include <stdlib.h>
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
#include <liboil/liboilfuncs.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <liboil/liboil.h>
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2008-08-28 17:53:01 +02:00
|
|
|
#include <pulse/timeval.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/log.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2007-11-09 17:11:45 +00:00
|
|
|
#include <pulsecore/g711.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
#include "sample-util.h"
|
2006-06-17 23:37:07 +00:00
|
|
|
#include "endianmacros.h"
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#define PA_SILENCE_MAX (PA_PAGE_SIZE*16)
|
2007-05-23 16:59:03 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) {
|
2007-10-28 19:13:50 +00:00
|
|
|
void *data;
|
|
|
|
|
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
pa_assert(spec);
|
|
|
|
|
|
|
|
|
|
data = pa_memblock_acquire(b);
|
|
|
|
|
pa_silence_memory(data, pa_memblock_get_length(b), spec);
|
|
|
|
|
pa_memblock_release(b);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_memchunk* pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec) {
|
2007-10-28 19:13:50 +00:00
|
|
|
void *data;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(c->memblock);
|
|
|
|
|
pa_assert(spec);
|
|
|
|
|
|
|
|
|
|
data = pa_memblock_acquire(c->memblock);
|
|
|
|
|
pa_silence_memory((uint8_t*) data+c->index, c->length, spec);
|
|
|
|
|
pa_memblock_release(c->memblock);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
return c;
|
|
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
static uint8_t silence_byte(pa_sample_format_t format) {
|
|
|
|
|
switch (format) {
|
2004-06-29 16:48:37 +00:00
|
|
|
case PA_SAMPLE_U8:
|
2008-05-15 23:34:41 +00:00
|
|
|
return 0x80;
|
2004-06-29 16:48:37 +00:00
|
|
|
case PA_SAMPLE_S16LE:
|
|
|
|
|
case PA_SAMPLE_S16BE:
|
2007-11-09 02:45:07 +00:00
|
|
|
case PA_SAMPLE_S32LE:
|
|
|
|
|
case PA_SAMPLE_S32BE:
|
2008-05-15 23:34:41 +00:00
|
|
|
case PA_SAMPLE_FLOAT32LE:
|
|
|
|
|
case PA_SAMPLE_FLOAT32BE:
|
|
|
|
|
return 0;
|
2004-06-29 16:48:37 +00:00
|
|
|
case PA_SAMPLE_ALAW:
|
2008-05-15 23:34:41 +00:00
|
|
|
return 0xd5;
|
2004-06-29 16:48:37 +00:00
|
|
|
case PA_SAMPLE_ULAW:
|
2008-05-15 23:34:41 +00:00
|
|
|
return 0xff;
|
2004-06-29 16:48:37 +00:00
|
|
|
default:
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert_not_reached();
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
void* pa_silence_memory(void *p, size_t length, const pa_sample_spec *spec) {
|
|
|
|
|
pa_assert(p);
|
|
|
|
|
pa_assert(length > 0);
|
|
|
|
|
pa_assert(spec);
|
|
|
|
|
|
|
|
|
|
memset(p, silence_byte(spec->format), length);
|
|
|
|
|
return p;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
static void calc_linear_integer_volume(int32_t linear[], const pa_cvolume *volume) {
|
|
|
|
|
unsigned channel;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
pa_assert(linear);
|
|
|
|
|
pa_assert(volume);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
for (channel = 0; channel < volume->channels; channel++)
|
2008-10-03 02:34:59 +02:00
|
|
|
linear[channel] = lrint(pa_sw_volume_to_linear(volume->values[channel]) * 0x10000);
|
2007-11-09 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
static void calc_linear_float_volume(float linear[], const pa_cvolume *volume) {
|
2007-11-09 17:11:45 +00:00
|
|
|
unsigned channel;
|
|
|
|
|
|
|
|
|
|
pa_assert(linear);
|
|
|
|
|
pa_assert(volume);
|
|
|
|
|
|
|
|
|
|
for (channel = 0; channel < volume->channels; channel++)
|
2008-10-03 01:53:34 +02:00
|
|
|
linear[channel] = (float) pa_sw_volume_to_linear(volume->values[channel]);
|
2007-11-09 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
static void calc_linear_integer_stream_volumes(pa_mix_info streams[], unsigned nstreams, const pa_cvolume *volume, const pa_sample_spec *spec) {
|
|
|
|
|
unsigned k, channel;
|
|
|
|
|
float linear[PA_CHANNELS_MAX];
|
2007-11-09 17:11:45 +00:00
|
|
|
|
|
|
|
|
pa_assert(streams);
|
|
|
|
|
pa_assert(spec);
|
2008-10-03 01:53:34 +02:00
|
|
|
pa_assert(volume);
|
|
|
|
|
|
|
|
|
|
calc_linear_float_volume(linear, volume);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
|
|
|
|
for (k = 0; k < nstreams; k++) {
|
|
|
|
|
|
|
|
|
|
for (channel = 0; channel < spec->channels; channel++) {
|
|
|
|
|
pa_mix_info *m = streams + k;
|
2008-10-03 02:34:59 +02:00
|
|
|
m->linear[channel].i = lrint(pa_sw_volume_to_linear(m->volume.values[channel]) * linear[channel] * 0x10000);
|
2007-11-09 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
static void calc_linear_float_stream_volumes(pa_mix_info streams[], unsigned nstreams, const pa_cvolume *volume, const pa_sample_spec *spec) {
|
|
|
|
|
unsigned k, channel;
|
|
|
|
|
float linear[PA_CHANNELS_MAX];
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
pa_assert(streams);
|
|
|
|
|
pa_assert(spec);
|
2007-11-09 17:11:45 +00:00
|
|
|
pa_assert(volume);
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_float_volume(linear, volume);
|
|
|
|
|
|
|
|
|
|
for (k = 0; k < nstreams; k++) {
|
|
|
|
|
|
|
|
|
|
for (channel = 0; channel < spec->channels; channel++) {
|
|
|
|
|
pa_mix_info *m = streams + k;
|
|
|
|
|
m->linear[channel].f = (float) (pa_sw_volume_to_linear(m->volume.values[channel]) * linear[channel]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-11-09 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
size_t pa_mix(
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_mix_info streams[],
|
|
|
|
|
unsigned nstreams,
|
|
|
|
|
void *data,
|
|
|
|
|
size_t length,
|
|
|
|
|
const pa_sample_spec *spec,
|
|
|
|
|
const pa_cvolume *volume,
|
2007-11-09 17:11:45 +00:00
|
|
|
pa_bool_t mute) {
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_cvolume full_volume;
|
|
|
|
|
unsigned k;
|
2008-08-11 18:34:38 +02:00
|
|
|
unsigned z;
|
|
|
|
|
void *end;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(streams);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
pa_assert(length);
|
|
|
|
|
pa_assert(spec);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!volume)
|
|
|
|
|
volume = pa_cvolume_reset(&full_volume, spec->channels);
|
|
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
if (mute || pa_cvolume_is_muted(volume) || nstreams <= 0) {
|
|
|
|
|
pa_silence_memory(data, length, spec);
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
for (k = 0; k < nstreams; k++)
|
2007-11-09 17:11:45 +00:00
|
|
|
streams[k].ptr = (uint8_t*) pa_memblock_acquire(streams[k].chunk.memblock) + streams[k].chunk.index;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
for (z = 0; z < nstreams; z++)
|
|
|
|
|
if (length > streams[z].chunk.length)
|
|
|
|
|
length = streams[z].chunk.length;
|
|
|
|
|
|
|
|
|
|
end = (uint8_t*) data + length;
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
switch (spec->format) {
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
case PA_SAMPLE_S16NE:{
|
|
|
|
|
unsigned channel = 0;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_integer_stream_volumes(streams, nstreams, volume, spec);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2006-01-27 16:25:31 +00:00
|
|
|
int32_t sum = 0;
|
2007-11-09 17:11:45 +00:00
|
|
|
unsigned i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
int32_t v, cv = m->linear[channel].i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = *((int16_t*) m->ptr);
|
|
|
|
|
v = (v * cv) / 0x10000;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + sizeof(int16_t);
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
|
2007-10-28 19:13:50 +00:00
|
|
|
*((int16_t*) data) = (int16_t) sum;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
data = (uint8_t*) data + sizeof(int16_t);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
2006-01-27 16:25:31 +00:00
|
|
|
channel = 0;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
break;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
2006-06-17 23:37:07 +00:00
|
|
|
|
|
|
|
|
case PA_SAMPLE_S16RE:{
|
|
|
|
|
unsigned channel = 0;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_integer_stream_volumes(streams, nstreams, volume, spec);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2006-06-17 23:37:07 +00:00
|
|
|
int32_t sum = 0;
|
2007-11-09 17:11:45 +00:00
|
|
|
unsigned i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
int32_t v, cv = m->linear[channel].i;
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = PA_INT16_SWAP(*((int16_t*) m->ptr));
|
|
|
|
|
v = (v * cv) / 0x10000;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + sizeof(int16_t);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
|
2007-11-09 17:11:45 +00:00
|
|
|
*((int16_t*) data) = PA_INT16_SWAP((int16_t) sum);
|
|
|
|
|
|
|
|
|
|
data = (uint8_t*) data + sizeof(int16_t);
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_SAMPLE_S32NE:{
|
|
|
|
|
unsigned channel = 0;
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_integer_stream_volumes(streams, nstreams, volume, spec);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2007-11-09 17:11:45 +00:00
|
|
|
int64_t sum = 0;
|
|
|
|
|
unsigned i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
int32_t cv = m->linear[channel].i;
|
2008-10-03 01:53:34 +02:00
|
|
|
int64_t v;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = *((int32_t*) m->ptr);
|
|
|
|
|
v = (v * cv) / 0x10000;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + sizeof(int32_t);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
sum = PA_CLAMP_UNLIKELY(sum, -0x80000000LL, 0x7FFFFFFFLL);
|
2007-11-09 17:11:45 +00:00
|
|
|
*((int32_t*) data) = (int32_t) sum;
|
|
|
|
|
|
|
|
|
|
data = (uint8_t*) data + sizeof(int32_t);
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_SAMPLE_S32RE:{
|
|
|
|
|
unsigned channel = 0;
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_integer_stream_volumes(streams, nstreams, volume, spec);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2007-11-09 17:11:45 +00:00
|
|
|
int64_t sum = 0;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
int32_t cv = m->linear[channel].i;
|
2008-10-03 01:53:34 +02:00
|
|
|
int64_t v;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2006-06-17 23:37:07 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = PA_INT32_SWAP(*((int32_t*) m->ptr));
|
|
|
|
|
v = (v * cv) / 0x10000;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + sizeof(int32_t);
|
2006-06-17 23:37:07 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
sum = PA_CLAMP_UNLIKELY(sum, -0x80000000LL, 0x7FFFFFFFLL);
|
2007-11-09 17:11:45 +00:00
|
|
|
*((int32_t*) data) = PA_INT32_SWAP((int32_t) sum);
|
|
|
|
|
|
|
|
|
|
data = (uint8_t*) data + sizeof(int32_t);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
2006-06-17 23:37:07 +00:00
|
|
|
channel = 0;
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
break;
|
2006-06-17 23:37:07 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
case PA_SAMPLE_U8: {
|
|
|
|
|
unsigned channel = 0;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_integer_stream_volumes(streams, nstreams, volume, spec);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2006-01-27 16:25:31 +00:00
|
|
|
int32_t sum = 0;
|
2007-11-09 17:11:45 +00:00
|
|
|
unsigned i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
int32_t v, cv = m->linear[channel].i;
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = (int32_t) *((uint8_t*) m->ptr) - 0x80;
|
|
|
|
|
v = (v * cv) / 0x10000;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sum = PA_CLAMP_UNLIKELY(sum, -0x80, 0x7F);
|
|
|
|
|
*((uint8_t*) data) = (uint8_t) (sum + 0x80);
|
|
|
|
|
|
|
|
|
|
data = (uint8_t*) data + 1;
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_SAMPLE_ULAW: {
|
|
|
|
|
unsigned channel = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_integer_stream_volumes(streams, nstreams, volume, spec);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2007-11-09 17:11:45 +00:00
|
|
|
int32_t sum = 0;
|
|
|
|
|
unsigned i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
int32_t v, cv = m->linear[channel].i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = (int32_t) st_ulaw2linear16(*((uint8_t*) m->ptr));
|
|
|
|
|
v = (v * cv) / 0x10000;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
|
2008-08-19 22:39:54 +02:00
|
|
|
*((uint8_t*) data) = (uint8_t) st_14linear2ulaw((int16_t) sum >> 2);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
|
|
|
|
data = (uint8_t*) data + 1;
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_SAMPLE_ALAW: {
|
|
|
|
|
unsigned channel = 0;
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_integer_stream_volumes(streams, nstreams, volume, spec);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2007-11-09 17:11:45 +00:00
|
|
|
int32_t sum = 0;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
int32_t v, cv = m->linear[channel].i;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = (int32_t) st_alaw2linear16(*((uint8_t*) m->ptr));
|
|
|
|
|
v = (v * cv) / 0x10000;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + 1;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
|
2008-08-19 22:39:54 +02:00
|
|
|
*((uint8_t*) data) = (uint8_t) st_13linear2alaw((int16_t) sum >> 3);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
data = (uint8_t*) data + 1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
2006-01-27 16:25:31 +00:00
|
|
|
channel = 0;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
break;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
case PA_SAMPLE_FLOAT32NE: {
|
|
|
|
|
unsigned channel = 0;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_float_stream_volumes(streams, nstreams, volume, spec);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2006-01-27 16:25:31 +00:00
|
|
|
float sum = 0;
|
2007-11-09 17:11:45 +00:00
|
|
|
unsigned i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
float v, cv = m->linear[channel].f;
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = *((float*) m->ptr);
|
|
|
|
|
v *= cv;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + sizeof(float);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*((float*) data) = sum;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
data = (uint8_t*) data + sizeof(float);
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_SAMPLE_FLOAT32RE: {
|
|
|
|
|
unsigned channel = 0;
|
|
|
|
|
float linear[PA_CHANNELS_MAX];
|
|
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
calc_linear_float_stream_volumes(streams, nstreams, volume, spec);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
while (data < end) {
|
2007-11-09 17:11:45 +00:00
|
|
|
float sum = 0;
|
|
|
|
|
unsigned i;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
for (i = 0; i < nstreams; i++) {
|
|
|
|
|
pa_mix_info *m = streams + i;
|
|
|
|
|
float v, cv = m->linear[channel].f;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
if (PA_UNLIKELY(cv <= 0))
|
|
|
|
|
continue;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-10-03 01:53:34 +02:00
|
|
|
v = PA_FLOAT32_SWAP(*(float*) m->ptr);
|
|
|
|
|
v *= cv;
|
2007-11-09 17:11:45 +00:00
|
|
|
sum += v;
|
2008-10-03 01:53:34 +02:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
m->ptr = (uint8_t*) m->ptr + sizeof(float);
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 15:17:30 +02:00
|
|
|
*((float*) data) = PA_FLOAT32_SWAP(sum);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
data = (uint8_t*) data + sizeof(float);
|
|
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
2006-01-27 16:25:31 +00:00
|
|
|
channel = 0;
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
break;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
default:
|
2008-08-11 18:34:38 +02:00
|
|
|
pa_log_error("Unable to mix audio data of format %s.", pa_sample_format_to_string(spec->format));
|
2007-11-09 17:11:45 +00:00
|
|
|
pa_assert_not_reached();
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
for (k = 0; k < nstreams; k++)
|
|
|
|
|
pa_memblock_release(streams[k].chunk.memblock);
|
|
|
|
|
|
2008-08-11 18:34:38 +02:00
|
|
|
return length;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2004-06-29 20:37:24 +00:00
|
|
|
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void pa_volume_memchunk(
|
|
|
|
|
pa_memchunk*c,
|
|
|
|
|
const pa_sample_spec *spec,
|
|
|
|
|
const pa_cvolume *volume) {
|
|
|
|
|
|
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(spec);
|
|
|
|
|
pa_assert(c->length % pa_frame_size(spec) == 0);
|
|
|
|
|
pa_assert(volume);
|
2004-06-29 20:37:24 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (pa_memblock_is_silence(c->memblock))
|
|
|
|
|
return;
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_NORM))
|
2004-06-29 20:37:24 +00:00
|
|
|
return;
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_MUTED)) {
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_silence_memchunk(c, spec);
|
2004-06-29 20:37:24 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
ptr = (uint8_t*) pa_memblock_acquire(c->memblock) + c->index;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
switch (spec->format) {
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
case PA_SAMPLE_S16NE: {
|
2008-08-11 15:19:52 +02:00
|
|
|
int16_t *d, *e;
|
2006-06-17 23:37:07 +00:00
|
|
|
unsigned channel;
|
2007-10-28 19:13:50 +00:00
|
|
|
int32_t linear[PA_CHANNELS_MAX];
|
2006-06-17 23:37:07 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
calc_linear_integer_volume(linear, volume);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
e = (int16_t*) ptr + c->length/sizeof(int16_t);
|
|
|
|
|
|
|
|
|
|
for (channel = 0, d = ptr; d < e; d++) {
|
2007-10-28 19:13:50 +00:00
|
|
|
int32_t t;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
t = (int32_t)(*d);
|
|
|
|
|
t = (t * linear[channel]) / 0x10000;
|
2007-11-09 02:45:07 +00:00
|
|
|
t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
|
2006-01-27 16:25:31 +00:00
|
|
|
*d = (int16_t) t;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 02:45:07 +00:00
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
2006-01-27 16:25:31 +00:00
|
|
|
channel = 0;
|
|
|
|
|
}
|
2006-01-30 11:49:03 +00:00
|
|
|
break;
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
2006-06-17 23:37:07 +00:00
|
|
|
|
|
|
|
|
case PA_SAMPLE_S16RE: {
|
2008-08-11 15:19:52 +02:00
|
|
|
int16_t *d, *e;
|
2006-06-17 23:37:07 +00:00
|
|
|
unsigned channel;
|
2007-10-28 19:13:50 +00:00
|
|
|
int32_t linear[PA_CHANNELS_MAX];
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
calc_linear_integer_volume(linear, volume);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
e = (int16_t*) ptr + c->length/sizeof(int16_t);
|
|
|
|
|
|
|
|
|
|
for (channel = 0, d = ptr; d < e; d++) {
|
2007-10-28 19:13:50 +00:00
|
|
|
int32_t t;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
t = (int32_t) PA_INT16_SWAP(*d);
|
2007-10-28 19:13:50 +00:00
|
|
|
t = (t * linear[channel]) / 0x10000;
|
2007-11-09 02:45:07 +00:00
|
|
|
t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
|
2007-10-28 19:13:50 +00:00
|
|
|
*d = PA_INT16_SWAP((int16_t) t);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 02:45:07 +00:00
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_SAMPLE_S32NE: {
|
2008-08-11 15:19:52 +02:00
|
|
|
int32_t *d, *e;
|
2007-11-09 02:45:07 +00:00
|
|
|
unsigned channel;
|
|
|
|
|
int32_t linear[PA_CHANNELS_MAX];
|
|
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
calc_linear_integer_volume(linear, volume);
|
2007-11-09 02:45:07 +00:00
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
e = (int32_t*) ptr + c->length/sizeof(int32_t);
|
|
|
|
|
|
|
|
|
|
for (channel = 0, d = ptr; d < e; d++) {
|
2007-11-09 02:45:07 +00:00
|
|
|
int64_t t;
|
|
|
|
|
|
|
|
|
|
t = (int64_t)(*d);
|
|
|
|
|
t = (t * linear[channel]) / 0x10000;
|
|
|
|
|
t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
|
|
|
|
|
*d = (int32_t) t;
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_SAMPLE_S32RE: {
|
2008-08-11 15:19:52 +02:00
|
|
|
int32_t *d, *e;
|
2007-11-09 02:45:07 +00:00
|
|
|
unsigned channel;
|
|
|
|
|
int32_t linear[PA_CHANNELS_MAX];
|
|
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
calc_linear_integer_volume(linear, volume);
|
2007-11-09 02:45:07 +00:00
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
e = (int32_t*) ptr + c->length/sizeof(int32_t);
|
|
|
|
|
|
|
|
|
|
for (channel = 0, d = ptr; d < e; d++) {
|
2007-11-09 02:45:07 +00:00
|
|
|
int64_t t;
|
|
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
t = (int64_t) PA_INT32_SWAP(*d);
|
2007-11-09 02:45:07 +00:00
|
|
|
t = (t * linear[channel]) / 0x10000;
|
|
|
|
|
t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
|
|
|
|
|
*d = PA_INT32_SWAP((int32_t) t);
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
2006-06-17 23:37:07 +00:00
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
case PA_SAMPLE_U8: {
|
2008-08-11 15:19:52 +02:00
|
|
|
uint8_t *d, *e;
|
2007-10-28 19:13:50 +00:00
|
|
|
unsigned channel;
|
|
|
|
|
int32_t linear[PA_CHANNELS_MAX];
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
calc_linear_integer_volume(linear, volume);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
e = (uint8_t*) ptr + c->length;
|
|
|
|
|
|
|
|
|
|
for (channel = 0, d = ptr; d < e; d++) {
|
2007-10-28 19:13:50 +00:00
|
|
|
int32_t t;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
t = (int32_t) *d - 0x80;
|
|
|
|
|
t = (t * linear[channel]) / 0x10000;
|
2007-11-09 02:45:07 +00:00
|
|
|
t = PA_CLAMP_UNLIKELY(t, -0x80, 0x7F);
|
2006-01-27 16:25:31 +00:00
|
|
|
*d = (uint8_t) (t + 0x80);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 02:45:07 +00:00
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
2006-01-27 16:25:31 +00:00
|
|
|
channel = 0;
|
|
|
|
|
}
|
2006-01-30 11:49:03 +00:00
|
|
|
break;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
case PA_SAMPLE_ULAW: {
|
2008-08-11 15:19:52 +02:00
|
|
|
uint8_t *d, *e;
|
2007-11-09 17:11:45 +00:00
|
|
|
unsigned channel;
|
|
|
|
|
int32_t linear[PA_CHANNELS_MAX];
|
|
|
|
|
|
|
|
|
|
calc_linear_integer_volume(linear, volume);
|
|
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
e = (uint8_t*) ptr + c->length;
|
|
|
|
|
|
|
|
|
|
for (channel = 0, d = ptr; d < e; d++) {
|
2007-11-09 17:11:45 +00:00
|
|
|
int32_t t;
|
|
|
|
|
|
|
|
|
|
t = (int32_t) st_ulaw2linear16(*d);
|
|
|
|
|
t = (t * linear[channel]) / 0x10000;
|
|
|
|
|
t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
|
2008-08-19 22:39:54 +02:00
|
|
|
*d = (uint8_t) st_14linear2ulaw((int16_t) t >> 2);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_SAMPLE_ALAW: {
|
2008-08-11 15:19:52 +02:00
|
|
|
uint8_t *d, *e;
|
2007-11-09 17:11:45 +00:00
|
|
|
unsigned channel;
|
|
|
|
|
int32_t linear[PA_CHANNELS_MAX];
|
|
|
|
|
|
|
|
|
|
calc_linear_integer_volume(linear, volume);
|
|
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
e = (uint8_t*) ptr + c->length;
|
|
|
|
|
|
|
|
|
|
for (channel = 0, d = ptr; d < e; d++) {
|
2007-11-09 17:11:45 +00:00
|
|
|
int32_t t;
|
|
|
|
|
|
|
|
|
|
t = (int32_t) st_alaw2linear16(*d);
|
|
|
|
|
t = (t * linear[channel]) / 0x10000;
|
|
|
|
|
t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
|
2008-08-19 22:39:54 +02:00
|
|
|
*d = (uint8_t) st_13linear2alaw((int16_t) t >> 3);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
case PA_SAMPLE_FLOAT32NE: {
|
|
|
|
|
float *d;
|
|
|
|
|
int skip;
|
|
|
|
|
unsigned n;
|
|
|
|
|
unsigned channel;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
d = ptr;
|
2008-08-19 22:39:54 +02:00
|
|
|
skip = (int) (spec->channels * sizeof(float));
|
|
|
|
|
n = (unsigned) (c->length/sizeof(float)/spec->channels);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
for (channel = 0; channel < spec->channels; channel ++) {
|
2006-01-27 16:25:31 +00:00
|
|
|
float v, *t;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
if (PA_UNLIKELY(volume->values[channel] == PA_VOLUME_NORM))
|
2006-01-27 16:25:31 +00:00
|
|
|
continue;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-04-08 00:09:28 +00:00
|
|
|
v = (float) pa_sw_volume_to_linear(volume->values[channel]);
|
2006-01-27 16:25:31 +00:00
|
|
|
t = d + channel;
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_scalarmult_f32(t, skip, t, skip, &v, (int) n);
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
2006-01-30 11:49:03 +00:00
|
|
|
break;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-11-09 17:11:45 +00:00
|
|
|
case PA_SAMPLE_FLOAT32RE: {
|
2008-08-11 15:19:52 +02:00
|
|
|
float *d, *e;
|
2007-11-09 17:11:45 +00:00
|
|
|
unsigned channel;
|
|
|
|
|
float linear[PA_CHANNELS_MAX];
|
|
|
|
|
|
|
|
|
|
calc_linear_float_volume(linear, volume);
|
|
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
e = (float*) ptr + c->length/sizeof(float);
|
|
|
|
|
|
|
|
|
|
for (channel = 0, d = ptr; d < e; d++) {
|
2007-11-09 17:11:45 +00:00
|
|
|
float t;
|
|
|
|
|
|
2008-08-11 15:19:52 +02:00
|
|
|
t = PA_FLOAT32_SWAP(*d);
|
2007-11-09 17:11:45 +00:00
|
|
|
t *= linear[channel];
|
2008-08-11 15:19:52 +02:00
|
|
|
*d = PA_FLOAT32_SWAP(t);
|
2007-11-09 17:11:45 +00:00
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(++channel >= spec->channels))
|
|
|
|
|
channel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
default:
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_warn(" Unable to change volume of format %s.", pa_sample_format_to_string(spec->format));
|
|
|
|
|
/* If we cannot change the volume, we just don't do it */
|
2004-06-29 20:37:24 +00:00
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_memblock_release(c->memblock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t pa_frame_align(size_t l, const pa_sample_spec *ss) {
|
|
|
|
|
size_t fs;
|
|
|
|
|
|
|
|
|
|
pa_assert(ss);
|
|
|
|
|
|
|
|
|
|
fs = pa_frame_size(ss);
|
|
|
|
|
|
|
|
|
|
return (l/fs) * fs;
|
2004-06-29 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
int pa_frame_aligned(size_t l, const pa_sample_spec *ss) {
|
|
|
|
|
size_t fs;
|
|
|
|
|
|
|
|
|
|
pa_assert(ss);
|
|
|
|
|
|
|
|
|
|
fs = pa_frame_size(ss);
|
|
|
|
|
|
|
|
|
|
return l % fs == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_interleave(const void *src[], unsigned channels, void *dst, size_t ss, unsigned n) {
|
|
|
|
|
unsigned c;
|
|
|
|
|
size_t fs;
|
|
|
|
|
|
|
|
|
|
pa_assert(src);
|
|
|
|
|
pa_assert(channels > 0);
|
|
|
|
|
pa_assert(dst);
|
|
|
|
|
pa_assert(ss > 0);
|
|
|
|
|
pa_assert(n > 0);
|
|
|
|
|
|
|
|
|
|
fs = ss * channels;
|
|
|
|
|
|
|
|
|
|
for (c = 0; c < channels; c++) {
|
|
|
|
|
unsigned j;
|
|
|
|
|
void *d;
|
|
|
|
|
const void *s;
|
|
|
|
|
|
|
|
|
|
s = src[c];
|
|
|
|
|
d = (uint8_t*) dst + c * ss;
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < n; j ++) {
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_memcpy(d, s, (int) ss);
|
2007-10-28 19:13:50 +00:00
|
|
|
s = (uint8_t*) s + ss;
|
|
|
|
|
d = (uint8_t*) d + fs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_deinterleave(const void *src, void *dst[], unsigned channels, size_t ss, unsigned n) {
|
|
|
|
|
size_t fs;
|
|
|
|
|
unsigned c;
|
|
|
|
|
|
|
|
|
|
pa_assert(src);
|
|
|
|
|
pa_assert(dst);
|
|
|
|
|
pa_assert(channels > 0);
|
|
|
|
|
pa_assert(ss > 0);
|
|
|
|
|
pa_assert(n > 0);
|
|
|
|
|
|
|
|
|
|
fs = ss * channels;
|
|
|
|
|
|
|
|
|
|
for (c = 0; c < channels; c++) {
|
|
|
|
|
unsigned j;
|
|
|
|
|
const void *s;
|
|
|
|
|
void *d;
|
|
|
|
|
|
|
|
|
|
s = (uint8_t*) src + c * ss;
|
|
|
|
|
d = dst[c];
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < n; j ++) {
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_memcpy(d, s, (int) ss);
|
2007-10-28 19:13:50 +00:00
|
|
|
s = (uint8_t*) s + fs;
|
|
|
|
|
d = (uint8_t*) d + ss;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
static pa_memblock *silence_memblock_new(pa_mempool *pool, uint8_t c) {
|
|
|
|
|
pa_memblock *b;
|
|
|
|
|
size_t length;
|
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
|
|
pa_assert(pool);
|
|
|
|
|
|
|
|
|
|
length = PA_MIN(pa_mempool_block_size_max(pool), PA_SILENCE_MAX);
|
|
|
|
|
|
|
|
|
|
b = pa_memblock_new(pool, length);
|
|
|
|
|
|
|
|
|
|
data = pa_memblock_acquire(b);
|
|
|
|
|
memset(data, c, length);
|
|
|
|
|
pa_memblock_release(b);
|
|
|
|
|
|
|
|
|
|
pa_memblock_set_is_silence(b, TRUE);
|
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_silence_cache_init(pa_silence_cache *cache) {
|
|
|
|
|
pa_assert(cache);
|
|
|
|
|
|
|
|
|
|
memset(cache, 0, sizeof(pa_silence_cache));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_silence_cache_done(pa_silence_cache *cache) {
|
|
|
|
|
pa_sample_format_t f;
|
|
|
|
|
pa_assert(cache);
|
|
|
|
|
|
|
|
|
|
for (f = 0; f < PA_SAMPLE_MAX; f++)
|
|
|
|
|
if (cache->blocks[f])
|
|
|
|
|
pa_memblock_unref(cache->blocks[f]);
|
|
|
|
|
|
|
|
|
|
memset(cache, 0, sizeof(pa_silence_cache));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_memchunk* pa_silence_memchunk_get(pa_silence_cache *cache, pa_mempool *pool, pa_memchunk* ret, const pa_sample_spec *spec, size_t length) {
|
|
|
|
|
pa_memblock *b;
|
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
|
|
pa_assert(cache);
|
|
|
|
|
pa_assert(pa_sample_spec_valid(spec));
|
|
|
|
|
|
|
|
|
|
if (!(b = cache->blocks[spec->format]))
|
|
|
|
|
|
|
|
|
|
switch (spec->format) {
|
|
|
|
|
case PA_SAMPLE_U8:
|
|
|
|
|
cache->blocks[PA_SAMPLE_U8] = b = silence_memblock_new(pool, 0x80);
|
|
|
|
|
break;
|
|
|
|
|
case PA_SAMPLE_S16LE:
|
|
|
|
|
case PA_SAMPLE_S16BE:
|
|
|
|
|
case PA_SAMPLE_S32LE:
|
|
|
|
|
case PA_SAMPLE_S32BE:
|
|
|
|
|
case PA_SAMPLE_FLOAT32LE:
|
|
|
|
|
case PA_SAMPLE_FLOAT32BE:
|
|
|
|
|
cache->blocks[PA_SAMPLE_S16LE] = b = silence_memblock_new(pool, 0);
|
|
|
|
|
cache->blocks[PA_SAMPLE_S16BE] = pa_memblock_ref(b);
|
|
|
|
|
cache->blocks[PA_SAMPLE_S32LE] = pa_memblock_ref(b);
|
|
|
|
|
cache->blocks[PA_SAMPLE_S32BE] = pa_memblock_ref(b);
|
|
|
|
|
cache->blocks[PA_SAMPLE_FLOAT32LE] = pa_memblock_ref(b);
|
|
|
|
|
cache->blocks[PA_SAMPLE_FLOAT32BE] = pa_memblock_ref(b);
|
|
|
|
|
break;
|
|
|
|
|
case PA_SAMPLE_ALAW:
|
|
|
|
|
cache->blocks[PA_SAMPLE_ALAW] = b = silence_memblock_new(pool, 0xd5);
|
|
|
|
|
break;
|
|
|
|
|
case PA_SAMPLE_ULAW:
|
|
|
|
|
cache->blocks[PA_SAMPLE_ULAW] = b = silence_memblock_new(pool, 0xff);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pa_assert_not_reached();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
|
|
|
|
ret->memblock = pa_memblock_ref(b);
|
|
|
|
|
|
|
|
|
|
l = pa_memblock_get_length(b);
|
|
|
|
|
if (length > l || length == 0)
|
|
|
|
|
length = l;
|
|
|
|
|
|
|
|
|
|
ret->length = pa_frame_align(length, spec);
|
|
|
|
|
ret->index = 0;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_sample_clamp(pa_sample_format_t format, void *dst, size_t dstr, const void *src, size_t sstr, unsigned n) {
|
|
|
|
|
const float *s;
|
|
|
|
|
float *d;
|
|
|
|
|
|
|
|
|
|
s = src; d = dst;
|
|
|
|
|
|
|
|
|
|
if (format == PA_SAMPLE_FLOAT32NE) {
|
|
|
|
|
|
|
|
|
|
float minus_one = -1.0, plus_one = 1.0;
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_clip_f32(d, (int) dstr, s, (int) sstr, (int) n, &minus_one, &plus_one);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
pa_assert(format == PA_SAMPLE_FLOAT32RE);
|
|
|
|
|
|
|
|
|
|
for (; n > 0; n--) {
|
|
|
|
|
float f;
|
|
|
|
|
|
|
|
|
|
f = PA_FLOAT32_SWAP(*s);
|
2008-08-19 22:39:54 +02:00
|
|
|
f = PA_CLAMP_UNLIKELY(f, -1.0f, 1.0f);
|
2008-05-15 23:34:41 +00:00
|
|
|
*d = PA_FLOAT32_SWAP(f);
|
|
|
|
|
|
|
|
|
|
s = (const float*) ((const uint8_t*) s + sstr);
|
|
|
|
|
d = (float*) ((uint8_t*) d + dstr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-08-28 17:53:01 +02:00
|
|
|
|
|
|
|
|
/* Similar to pa_bytes_to_usec() but rounds up, not down */
|
|
|
|
|
|
|
|
|
|
pa_usec_t pa_bytes_to_usec_round_up(uint64_t length, const pa_sample_spec *spec) {
|
|
|
|
|
size_t fs;
|
|
|
|
|
pa_usec_t usec;
|
|
|
|
|
|
|
|
|
|
pa_assert(spec);
|
|
|
|
|
|
|
|
|
|
fs = pa_frame_size(spec);
|
|
|
|
|
length = (length + fs - 1) / fs;
|
|
|
|
|
|
|
|
|
|
usec = (pa_usec_t) length * PA_USEC_PER_SEC;
|
|
|
|
|
|
|
|
|
|
return (usec + spec->rate - 1) / spec->rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Similar to pa_usec_to_bytes() but rounds up, not down */
|
|
|
|
|
|
|
|
|
|
size_t pa_usec_to_bytes_round_up(pa_usec_t t, const pa_sample_spec *spec) {
|
|
|
|
|
uint64_t u;
|
|
|
|
|
pa_assert(spec);
|
|
|
|
|
|
|
|
|
|
u = (uint64_t) t * (uint64_t) spec->rate;
|
|
|
|
|
|
|
|
|
|
u = (u + PA_USEC_PER_SEC - 1) / PA_USEC_PER_SEC;
|
|
|
|
|
|
|
|
|
|
u *= pa_frame_size(spec);
|
|
|
|
|
|
|
|
|
|
return (size_t) u;
|
|
|
|
|
}
|