X-Git-Url: https://code.delx.au/pulseaudio/blobdiff_plain/78a9ad336bc9f06f3995dac824fb4e1e724d3cdb..f7afaa264497e98a6f93c483ec10f22d40e17417:/src/pulsecore/sample-util.c diff --git a/src/pulsecore/sample-util.c b/src/pulsecore/sample-util.c index 248a09b6..dc5c1fd2 100644 --- a/src/pulsecore/sample-util.c +++ b/src/pulsecore/sample-util.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /*** This file is part of PulseAudio. @@ -8,7 +6,7 @@ PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2 of the License, + by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. PulseAudio is distributed in the hope that it will be useful, but @@ -26,43 +24,25 @@ #include #endif -#include #include #include +#include +#include +#include -#include +#include #include +#include #include +#include +#include +#include #include "sample-util.h" -#include "endianmacros.h" #define PA_SILENCE_MAX (PA_PAGE_SIZE*16) -pa_memblock *pa_silence_memblock_new(pa_mempool *pool, const pa_sample_spec *spec, size_t length) { - size_t fs; - pa_assert(pool); - pa_assert(spec); - - if (length <= 0) - length = pa_bytes_per_second(spec)/20; /* 50 ms */ - - if (length > PA_SILENCE_MAX) - length = PA_SILENCE_MAX; - - fs = pa_frame_size(spec); - - length = (length+fs-1)/fs; - - if (length <= 0) - length = 1; - - length *= fs; - - return pa_silence_memblock(pa_memblock_new(pool, length), spec); -} - pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) { void *data; @@ -72,10 +52,11 @@ pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) { data = pa_memblock_acquire(b); pa_silence_memory(data, pa_memblock_get_length(b), spec); pa_memblock_release(b); + return b; } -void pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec) { +pa_memchunk* pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec) { void *data; pa_assert(c); @@ -85,401 +66,342 @@ void pa_silence_memchunk(pa_memchunk *c, const pa_sample_spec *spec) { data = pa_memblock_acquire(c->memblock); pa_silence_memory((uint8_t*) data+c->index, c->length, spec); pa_memblock_release(c->memblock); -} -void pa_silence_memory(void *p, size_t length, const pa_sample_spec *spec) { - uint8_t c = 0; - pa_assert(p); - pa_assert(length > 0); - pa_assert(spec); + return c; +} - switch (spec->format) { +static uint8_t silence_byte(pa_sample_format_t format) { + switch (format) { case PA_SAMPLE_U8: - c = 0x80; - break; + return 0x80; case PA_SAMPLE_S16LE: case PA_SAMPLE_S16BE: - case PA_SAMPLE_FLOAT32: - case PA_SAMPLE_FLOAT32RE: - c = 0; - break; + case PA_SAMPLE_S32LE: + case PA_SAMPLE_S32BE: + case PA_SAMPLE_FLOAT32LE: + case PA_SAMPLE_FLOAT32BE: + case PA_SAMPLE_S24LE: + case PA_SAMPLE_S24BE: + case PA_SAMPLE_S24_32LE: + case PA_SAMPLE_S24_32BE: + return 0; case PA_SAMPLE_ALAW: + return 0xd5; case PA_SAMPLE_ULAW: - c = 80; - break; + return 0xff; default: pa_assert_not_reached(); } - - memset(p, c, length); } -size_t pa_mix( - pa_mix_info streams[], - unsigned nstreams, - void *data, - size_t length, - const pa_sample_spec *spec, - const pa_cvolume *volume, - int mute) { - - pa_cvolume full_volume; - size_t d = 0; - unsigned k; - - pa_assert(streams); - pa_assert(data); - pa_assert(length); +void* pa_silence_memory(void *p, size_t length, const pa_sample_spec *spec) { + pa_assert(p); + pa_assert(length > 0); pa_assert(spec); - if (!volume) - volume = pa_cvolume_reset(&full_volume, spec->channels); - - for (k = 0; k < nstreams; k++) - streams[k].internal = pa_memblock_acquire(streams[k].chunk.memblock); + memset(p, silence_byte(spec->format), length); + return p; +} - switch (spec->format) { - case PA_SAMPLE_S16NE:{ - unsigned channel = 0; +size_t pa_frame_align(size_t l, const pa_sample_spec *ss) { + size_t fs; - for (d = 0;; d += sizeof(int16_t)) { - int32_t sum = 0; + pa_assert(ss); - if (d >= length) - goto finish; + fs = pa_frame_size(ss); - if (!mute && volume->values[channel] != PA_VOLUME_MUTED) { - unsigned i; + return (l/fs) * fs; +} - for (i = 0; i < nstreams; i++) { - int32_t v; - pa_volume_t cvolume = streams[i].volume.values[channel]; +bool pa_frame_aligned(size_t l, const pa_sample_spec *ss) { + size_t fs; - if (d >= streams[i].chunk.length) - goto finish; + pa_assert(ss); - if (cvolume == PA_VOLUME_MUTED) - v = 0; - else { - v = *((int16_t*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d)); + fs = pa_frame_size(ss); - if (cvolume != PA_VOLUME_NORM) - v = (int32_t) (v * pa_sw_volume_to_linear(cvolume)); - } + return l % fs == 0; +} - sum += v; - } +void pa_interleave(const void *src[], unsigned channels, void *dst, size_t ss, unsigned n) { + unsigned c; + size_t fs; - if (volume->values[channel] != PA_VOLUME_NORM) - sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel])); + pa_assert(src); + pa_assert(channels > 0); + pa_assert(dst); + pa_assert(ss > 0); + pa_assert(n > 0); - sum = CLAMP(sum, -0x8000, 0x7FFF); - } + fs = ss * channels; - *((int16_t*) data) = (int16_t) sum; - data = (uint8_t*) data + sizeof(int16_t); + for (c = 0; c < channels; c++) { + unsigned j; + void *d; + const void *s; - if (++channel >= spec->channels) - channel = 0; - } + s = src[c]; + d = (uint8_t*) dst + c * ss; - break; + for (j = 0; j < n; j ++) { + memcpy(d, s, (int) ss); + s = (uint8_t*) s + ss; + d = (uint8_t*) d + fs; } + } +} - case PA_SAMPLE_S16RE:{ - unsigned channel = 0; - - for (d = 0;; d += sizeof(int16_t)) { - int32_t sum = 0; - - if (d >= length) - goto finish; - - if (!mute && volume->values[channel] != PA_VOLUME_MUTED) { - unsigned i; - - for (i = 0; i < nstreams; i++) { - int32_t v; - pa_volume_t cvolume = streams[i].volume.values[channel]; - - if (d >= streams[i].chunk.length) - goto finish; - - if (cvolume == PA_VOLUME_MUTED) - v = 0; - else { - v = INT16_SWAP(*((int16_t*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d))); - - if (cvolume != PA_VOLUME_NORM) - v = (int32_t) (v * pa_sw_volume_to_linear(cvolume)); - } - - sum += v; - } +void pa_deinterleave(const void *src, void *dst[], unsigned channels, size_t ss, unsigned n) { + size_t fs; + unsigned c; - if (volume->values[channel] != PA_VOLUME_NORM) - sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel])); + pa_assert(src); + pa_assert(dst); + pa_assert(channels > 0); + pa_assert(ss > 0); + pa_assert(n > 0); - sum = CLAMP(sum, -0x8000, 0x7FFF); - } + fs = ss * channels; - *((int16_t*) data) = INT16_SWAP((int16_t) sum); - data = (uint8_t*) data + sizeof(int16_t); + for (c = 0; c < channels; c++) { + unsigned j; + const void *s; + void *d; - if (++channel >= spec->channels) - channel = 0; - } + s = (uint8_t*) src + c * ss; + d = dst[c]; - break; + for (j = 0; j < n; j ++) { + memcpy(d, s, (int) ss); + s = (uint8_t*) s + fs; + d = (uint8_t*) d + ss; } + } +} - case PA_SAMPLE_U8: { - unsigned channel = 0; - - for (d = 0;; d ++) { - int32_t sum = 0; +static pa_memblock *silence_memblock_new(pa_mempool *pool, uint8_t c) { + pa_memblock *b; + size_t length; + void *data; - if (d >= length) - goto finish; + pa_assert(pool); - if (!mute && volume->values[channel] != PA_VOLUME_MUTED) { - unsigned i; + length = PA_MIN(pa_mempool_block_size_max(pool), PA_SILENCE_MAX); - for (i = 0; i < nstreams; i++) { - int32_t v; - pa_volume_t cvolume = streams[i].volume.values[channel]; + b = pa_memblock_new(pool, length); - if (d >= streams[i].chunk.length) - goto finish; + data = pa_memblock_acquire(b); + memset(data, c, length); + pa_memblock_release(b); - if (cvolume == PA_VOLUME_MUTED) - v = 0; - else { - v = (int32_t) *((uint8_t*) streams[i].internal + streams[i].chunk.index + d) - 0x80; + pa_memblock_set_is_silence(b, true); - if (cvolume != PA_VOLUME_NORM) - v = (int32_t) (v * pa_sw_volume_to_linear(cvolume)); - } + return b; +} - sum += v; - } +void pa_silence_cache_init(pa_silence_cache *cache) { + pa_assert(cache); - if (volume->values[channel] != PA_VOLUME_NORM) - sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel])); + memset(cache, 0, sizeof(pa_silence_cache)); +} - sum = CLAMP(sum, -0x80, 0x7F); - } +void pa_silence_cache_done(pa_silence_cache *cache) { + pa_sample_format_t f; + pa_assert(cache); - *((uint8_t*) data) = (uint8_t) (sum + 0x80); - data = (uint8_t*) data + 1; + for (f = 0; f < PA_SAMPLE_MAX; f++) + if (cache->blocks[f]) + pa_memblock_unref(cache->blocks[f]); - if (++channel >= spec->channels) - channel = 0; - } + memset(cache, 0, sizeof(pa_silence_cache)); +} - break; - } +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_S24LE: + case PA_SAMPLE_S24BE: + case PA_SAMPLE_S24_32LE: + case PA_SAMPLE_S24_32BE: + 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_S24LE] = pa_memblock_ref(b); + cache->blocks[PA_SAMPLE_S24BE] = pa_memblock_ref(b); + cache->blocks[PA_SAMPLE_S24_32LE] = pa_memblock_ref(b); + cache->blocks[PA_SAMPLE_S24_32BE] = 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(); + } - case PA_SAMPLE_FLOAT32NE: { - unsigned channel = 0; + pa_assert(b); - for (d = 0;; d += sizeof(float)) { - float sum = 0; + ret->memblock = pa_memblock_ref(b); - if (d >= length) - goto finish; + l = pa_memblock_get_length(b); + if (length > l || length == 0) + length = l; - if (!mute && volume->values[channel] != PA_VOLUME_MUTED) { - unsigned i; + ret->length = pa_frame_align(length, spec); + ret->index = 0; - for (i = 0; i < nstreams; i++) { - float v; - pa_volume_t cvolume = streams[i].volume.values[channel]; + return ret; +} - if (d >= streams[i].chunk.length) - goto finish; +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; - if (cvolume == PA_VOLUME_MUTED) - v = 0; - else { - v = *((float*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d)); + s = src; d = dst; - if (cvolume != PA_VOLUME_NORM) - v *= pa_sw_volume_to_linear(cvolume); - } + if (format == PA_SAMPLE_FLOAT32NE) { + for (; n > 0; n--) { + float f; - sum += v; - } + f = *s; + *d = PA_CLAMP_UNLIKELY(f, -1.0f, 1.0f); - if (volume->values[channel] != PA_VOLUME_NORM) - sum *= pa_sw_volume_to_linear(volume->values[channel]); - } + s = (const float*) ((const uint8_t*) s + sstr); + d = (float*) ((uint8_t*) d + dstr); + } + } else { + pa_assert(format == PA_SAMPLE_FLOAT32RE); - *((float*) data) = sum; - data = (uint8_t*) data + sizeof(float); + for (; n > 0; n--) { + float f; - if (++channel >= spec->channels) - channel = 0; - } + f = PA_FLOAT32_SWAP(*s); + f = PA_CLAMP_UNLIKELY(f, -1.0f, 1.0f); + *d = PA_FLOAT32_SWAP(f); - break; + s = (const float*) ((const uint8_t*) s + sstr); + d = (float*) ((uint8_t*) d + dstr); } - - default: - pa_log_error("ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec->format)); - abort(); } - -finish: - - for (k = 0; k < nstreams; k++) - pa_memblock_release(streams[k].chunk.memblock); - - return d; } +/* Similar to pa_bytes_to_usec() but rounds up, not down */ -void pa_volume_memchunk( - pa_memchunk*c, - const pa_sample_spec *spec, - const pa_cvolume *volume) { - - void *ptr; +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(c); pa_assert(spec); - pa_assert(c->length % pa_frame_size(spec) == 0); - pa_assert(volume); - - if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_NORM)) - return; - if (pa_cvolume_channels_equal_to(volume, PA_VOLUME_MUTED)) { - pa_silence_memchunk(c, spec); - return; - } - - ptr = pa_memblock_acquire(c->memblock); - - switch (spec->format) { + fs = pa_frame_size(spec); + length = (length + fs - 1) / fs; - case PA_SAMPLE_S16NE: { - int16_t *d; - size_t n; - unsigned channel; - int32_t linear[PA_CHANNELS_MAX]; + usec = (pa_usec_t) length * PA_USEC_PER_SEC; - for (channel = 0; channel < spec->channels; channel++) - linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000); + return (usec + spec->rate - 1) / spec->rate; +} - for (channel = 0, d = (int16_t*) ((uint8_t*) ptr + c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) { - int32_t t; +/* Similar to pa_usec_to_bytes() but rounds up, not down */ - t = (int32_t)(*d); - t = (t * linear[channel]) / 0x10000; - t = CLAMP(t, -0x8000, 0x7FFF); - *d = (int16_t) t; +size_t pa_usec_to_bytes_round_up(pa_usec_t t, const pa_sample_spec *spec) { + uint64_t u; + pa_assert(spec); - if (++channel >= spec->channels) - channel = 0; - } - break; - } + u = (uint64_t) t * (uint64_t) spec->rate; - case PA_SAMPLE_S16RE: { - int16_t *d; - size_t n; - unsigned channel; - int32_t linear[PA_CHANNELS_MAX]; + u = (u + PA_USEC_PER_SEC - 1) / PA_USEC_PER_SEC; - for (channel = 0; channel < spec->channels; channel++) - linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000); + u *= pa_frame_size(spec); - for (channel = 0, d = (int16_t*) ((uint8_t*) ptr + c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) { - int32_t t; + return (size_t) u; +} - t = (int32_t)(INT16_SWAP(*d)); - t = (t * linear[channel]) / 0x10000; - t = CLAMP(t, -0x8000, 0x7FFF); - *d = INT16_SWAP((int16_t) t); +void pa_memchunk_dump_to_file(pa_memchunk *c, const char *fn) { + FILE *f; + void *p; - if (++channel >= spec->channels) - channel = 0; - } + pa_assert(c); + pa_assert(fn); - break; - } + /* Only for debugging purposes */ - case PA_SAMPLE_U8: { - uint8_t *d; - size_t n; - unsigned channel; - int32_t linear[PA_CHANNELS_MAX]; + f = pa_fopen_cloexec(fn, "a"); - for (channel = 0; channel < spec->channels; channel++) - linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000); + if (!f) { + pa_log_warn("Failed to open '%s': %s", fn, pa_cstrerror(errno)); + return; + } - for (channel = 0, d = (uint8_t*) ptr + c->index, n = c->length; n > 0; d++, n--) { - int32_t t; + p = pa_memblock_acquire(c->memblock); - t = (int32_t) *d - 0x80; - t = (t * linear[channel]) / 0x10000; - t = CLAMP(t, -0x80, 0x7F); - *d = (uint8_t) (t + 0x80); + if (fwrite((uint8_t*) p + c->index, 1, c->length, f) != c->length) + pa_log_warn("Failed to write to '%s': %s", fn, pa_cstrerror(errno)); - if (++channel >= spec->channels) - channel = 0; - } - break; - } + pa_memblock_release(c->memblock); - case PA_SAMPLE_FLOAT32NE: { - float *d; - int skip; - unsigned n; - unsigned channel; + fclose(f); +} - d = (float*) ((uint8_t*) ptr + c->index); - skip = spec->channels * sizeof(float); - n = c->length/sizeof(float)/spec->channels; +static void calc_sine(float *f, size_t l, double freq) { + size_t i; - for (channel = 0; channel < spec->channels ; channel ++) { - float v, *t; + l /= sizeof(float); - if (volume->values[channel] == PA_VOLUME_NORM) - continue; + for (i = 0; i < l; i++) + *(f++) = (float) 0.5f * sin((double) i*M_PI*2*freq / (double) l); +} - v = (float) pa_sw_volume_to_linear(volume->values[channel]); - t = d + channel; - oil_scalarmult_f32(t, skip, t, skip, &v, n); - } - break; - } +void pa_memchunk_sine(pa_memchunk *c, pa_mempool *pool, unsigned rate, unsigned freq) { + size_t l; + unsigned gcd, n; + void *p; - default: - 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 */ - } + pa_memchunk_reset(c); - pa_memblock_release(c->memblock); -} + gcd = pa_gcd(rate, freq); + n = rate / gcd; -size_t pa_frame_align(size_t l, const pa_sample_spec *ss) { - size_t fs; + l = pa_mempool_block_size_max(pool) / sizeof(float); - pa_assert(ss); + l /= n; + if (l <= 0) l = 1; + l *= n; - fs = pa_frame_size(ss); + c->length = l * sizeof(float); + c->memblock = pa_memblock_new(pool, c->length); - return (l/fs) * fs; + p = pa_memblock_acquire(c->memblock); + calc_sine(p, c->length, freq * l / rate); + pa_memblock_release(c->memblock); } -int pa_frame_aligned(size_t l, const pa_sample_spec *ss) { - size_t fs; - - pa_assert(ss); +size_t pa_convert_size(size_t size, const pa_sample_spec *from, const pa_sample_spec *to) { + pa_usec_t usec; - fs = pa_frame_size(ss); + pa_assert(from); + pa_assert(to); - return l % fs == 0; + usec = pa_bytes_to_usec_round_up(size, from); + return pa_usec_to_bytes_round_up(usec, to); }