audiotestsrc: implement sine wave

Fix audiomixer some more
This commit is contained in:
Wim Taymans 2017-04-03 19:23:53 +02:00
parent 5c32690cc8
commit 32368d741d
7 changed files with 173 additions and 59 deletions

View file

@ -230,9 +230,9 @@ pull_frames_queue (SpaALSAState *state,
if ((io = state->io)) {
io->flags = SPA_PORT_IO_FLAG_RANGE;
io->status = SPA_RESULT_OK;
io->range.offset = state->sample_count;
io->range.min_size = state->threshold;
io->range.max_size = frames;
io->range.offset = state->sample_count * state->frame_size;
io->range.min_size = state->threshold * state->frame_size;
io->range.max_size = frames * state->frame_size;
}
state->event_cb (&state->node, &event, state->user_data);
}
@ -431,7 +431,7 @@ alsa_on_playback_timeout_event (SpaSource *source)
int res;
SpaALSAState *state = source->data;
snd_pcm_t *hndl = state->hndl;
snd_pcm_sframes_t avail, delay;
snd_pcm_sframes_t avail;
struct itimerspec ts;
snd_pcm_uframes_t total_written = 0, filled;
const snd_pcm_channel_area_t *my_areas;
@ -448,16 +448,16 @@ alsa_on_playback_timeout_event (SpaSource *source)
}
avail = snd_pcm_status_get_avail (status);
delay = snd_pcm_status_get_delay (status);
snd_pcm_status_get_htstamp (status, &htstamp);
state->last_ticks = state->sample_count - delay;
state->last_monotonic = (int64_t)htstamp.tv_sec * SPA_NSEC_PER_SEC + (int64_t)htstamp.tv_nsec;
if (avail > state->buffer_frames)
avail = state->buffer_frames;
filled = state->buffer_frames - avail;
state->last_ticks = state->sample_count - filled;
state->last_monotonic = (int64_t)htstamp.tv_sec * SPA_NSEC_PER_SEC + (int64_t)htstamp.tv_nsec;
if (filled > state->threshold + 16) {
if (snd_pcm_state (hndl) == SND_PCM_STATE_SUSPENDED) {
spa_log_error (state->log, "suspended: try resume");
@ -505,7 +505,8 @@ alsa_on_playback_timeout_event (SpaSource *source)
calc_timeout (total_written + filled, state->threshold, state->rate, &htstamp, &ts.it_value);
// printf ("timeout %ld %ld %ld %ld\n", total_written, filled, ts.it_value.tv_sec, ts.it_value.tv_nsec);
spa_log_debug (state->log, "timeout %ld %ld %ld %ld", total_written, filled,
ts.it_value.tv_sec, ts.it_value.tv_nsec);
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;

View file

@ -36,8 +36,6 @@ typedef struct {
SpaBuffer *outbuf;
bool outstanding;
SpaMetaHeader *h;
void *ptr;
size_t size;
SpaList link;
} MixerBuffer;
@ -463,8 +461,6 @@ spa_audiomixer_node_port_use_buffers (SpaNode *node,
spa_log_error (this->log, "volume %p: invalid memory on buffer %p", this, buffers[i]);
continue;
}
b->ptr = d[0].data;
b->size = d[0].maxsize;
break;
default:
break;
@ -557,22 +553,28 @@ spa_audiomixer_node_port_send_command (SpaNode *node,
}
static void
add_port_data (SpaAudioMixer *this, MixerBuffer *out, SpaAudioMixerPort *port)
add_port_data (SpaAudioMixer *this, MixerBuffer *out, SpaAudioMixerPort *port, int layer)
{
int i;
uint8_t *op, *ip;
uint16_t *op, *ip;
size_t os, is, chunk;
MixerBuffer *b = spa_list_first (&port->queue, MixerBuffer, link);
op = out->ptr;
os = out->size;
ip = SPA_MEMBER (b->ptr, port->queued_offset + b->outbuf->datas[0].chunk->offset, void);
op = SPA_MEMBER (out->outbuf->datas[0].data, out->outbuf->datas[0].chunk->offset, void);
os = out->outbuf->datas[0].chunk->size;
ip = SPA_MEMBER (b->outbuf->datas[0].data, port->queued_offset + b->outbuf->datas[0].chunk->offset, void);
is = b->outbuf->datas[0].chunk->size - port->queued_offset;
chunk = SPA_MIN (os, is);
for (i = 0; i < chunk; i++)
op[i] += ip[i];
if (layer == 0) {
for (i = 0; i < chunk / 2; i++)
op[i] = ip[i];
}
else {
for (i = 0; i < chunk / 2; i++)
op[i] += ip[i];
}
port->queued_offset += chunk;
port->queued_bytes -= chunk;
@ -621,7 +623,7 @@ spa_audiomixer_node_process_input (SpaNode *node)
spa_log_trace (this->log, "audiomixer %p: queue buffer %d on port %p", this, b->outbuf->id, port);
spa_list_insert (port->queue.prev, &b->link);
b->outstanding = false;
port->queued_bytes += b->size;
port->queued_bytes += b->outbuf->datas[0].chunk->size;
input->buffer_id = SPA_ID_INVALID;
}
if (min_queued == -1 || port->queued_bytes < min_queued)
@ -633,6 +635,7 @@ spa_audiomixer_node_process_input (SpaNode *node)
if (min_queued != -1) {
MixerBuffer *outbuf;
SpaPortIO *output;
int j;
if (spa_list_is_empty (&outport->queue))
return SPA_RESULT_OUT_OF_BUFFERS;
@ -641,15 +644,18 @@ spa_audiomixer_node_process_input (SpaNode *node)
spa_list_remove (&outbuf->link);
spa_log_trace (this->log, "audiomixer %p: dequeue output buffer %d", this, outbuf->outbuf->id);
outbuf->outstanding = true;
outbuf->outbuf->datas[0].chunk->offset = 0;
outbuf->outbuf->datas[0].chunk->size = min_queued;
outbuf->outbuf->datas[0].chunk->stride = 0;
for (i = 0; i < MAX_PORTS; i++) {
for (j = 0, i = 0; i < MAX_PORTS; i++) {
SpaAudioMixerPort *port = &this->in_ports[i];
if (!port->valid || port->io == NULL ||
spa_list_is_empty (&port->queue))
continue;
add_port_data (this, outbuf, port);
add_port_data (this, outbuf, port, j++);
}
output = outport->io;
output->buffer_id = outbuf->outbuf->id;

View file

@ -98,11 +98,11 @@ struct _ATSBuffer {
SpaBuffer *outbuf;
bool outstanding;
SpaMetaHeader *h;
void *ptr;
size_t size;
SpaList link;
};
typedef SpaResult (*RenderFunc) (SpaAudioTestSrc *this, void *samples, size_t n_samples);
struct _SpaAudioTestSrc {
SpaHandle handle;
SpaNode node;
@ -132,6 +132,8 @@ struct _SpaAudioTestSrc {
SpaAudioInfo current_format;
uint8_t format_buffer[1024];
size_t bpf;
RenderFunc render_func;
double accumulator;
ATSBuffer buffers[MAX_BUFFERS];
uint32_t n_buffers;
@ -243,18 +245,7 @@ send_have_output (SpaAudioTestSrc *this)
return SPA_RESULT_OK;
}
static SpaResult
fill_buffer (SpaAudioTestSrc *this, ATSBuffer *b)
{
uint8_t *p = b->ptr;
size_t i;
for (i = 0; i < b->size; i++) {
p[i] = rand();
}
return SPA_RESULT_OK;
}
#include "render.c"
static void
set_timer (SpaAudioTestSrc *this, bool enabled)
@ -279,8 +270,9 @@ static SpaResult
audiotestsrc_make_buffer (SpaAudioTestSrc *this)
{
ATSBuffer *b;
SpaPortIO *io;
SpaPortIO *io = this->io;
uint64_t expirations;
int n_bytes, n_samples;
if (read (this->timer_source.fd, &expirations, sizeof (uint64_t)) < sizeof (uint64_t))
perror ("read timerfd");
@ -292,9 +284,19 @@ audiotestsrc_make_buffer (SpaAudioTestSrc *this)
b = spa_list_first (&this->empty, ATSBuffer, link);
spa_list_remove (&b->link);
b->outstanding = true;
spa_log_trace (this->log, "audiotestsrc %p: dequeue buffer %d", this, b->outbuf->id);
fill_buffer (this, b);
n_bytes = b->outbuf->datas[0].maxsize;
if (io->flags & SPA_PORT_IO_FLAG_RANGE)
n_bytes = SPA_CLAMP (n_bytes, io->range.min_size, io->range.max_size);
spa_log_trace (this->log, "audiotestsrc %p: dequeue buffer %d %d", this, b->outbuf->id, n_bytes);
n_samples = n_bytes / this->bpf;
this->render_func (this, b->outbuf->datas[0].data, n_samples);
b->outbuf->datas[0].chunk->offset = 0;
b->outbuf->datas[0].chunk->size = n_bytes;
b->outbuf->datas[0].chunk->stride = 0;
if (b->h) {
b->h->seq = this->sample_count;
@ -302,14 +304,14 @@ audiotestsrc_make_buffer (SpaAudioTestSrc *this)
b->h->dts_offset = 0;
}
this->sample_count += b->size / this->bpf;
this->sample_count += n_samples;
this->elapsed_time = SAMPLES_TO_TIME (this, this->sample_count);
set_timer (this, true);
if ((io = this->io)) {
io->buffer_id = b->outbuf->id;
io->status = SPA_RESULT_OK;
}
io->flags = 0;
io->buffer_id = b->outbuf->id;
io->status = SPA_RESULT_OK;
return SPA_RESULT_HAVE_OUTPUT;
}
@ -488,9 +490,11 @@ next:
case 0:
spa_pod_builder_format (&b, &f[0], this->type.format,
this->type.media_type.audio, this->type.media_subtype.raw,
PROP_U_EN (&f[1], this->type.format_audio.format, SPA_POD_TYPE_ID, 3, this->type.audio_format.S16,
PROP_U_EN (&f[1], this->type.format_audio.format, SPA_POD_TYPE_ID, 5, this->type.audio_format.S16,
this->type.audio_format.S16,
this->type.audio_format.S32),
this->type.audio_format.S32,
this->type.audio_format.F32,
this->type.audio_format.F64),
PROP_U_MM (&f[1], this->type.format_audio.rate, SPA_POD_TYPE_INT, 44100, 1, INT32_MAX),
PROP_U_MM (&f[1], this->type.format_audio.channels, SPA_POD_TYPE_INT, 2, 1, INT32_MAX));
break;
@ -545,6 +549,8 @@ spa_audiotestsrc_node_port_set_format (SpaNode *node,
} else {
SpaAudioInfo info = { SPA_FORMAT_MEDIA_TYPE (format),
SPA_FORMAT_MEDIA_SUBTYPE (format), };
int idx;
int sizes[4] = { 2, 4, 4, 8 };
if (info.media_type != this->type.media_type.audio ||
info.media_subtype != this->type.media_subtype.raw)
@ -553,9 +559,21 @@ spa_audiotestsrc_node_port_set_format (SpaNode *node,
if (!spa_format_audio_raw_parse (format, &info.info.raw, &this->type.format_audio))
return SPA_RESULT_INVALID_MEDIA_TYPE;
if (info.info.raw.format == this->type.audio_format.S16)
idx = 0;
else if (info.info.raw.format == this->type.audio_format.S32)
idx = 1;
else if (info.info.raw.format == this->type.audio_format.F32)
idx = 2;
else if (info.info.raw.format == this->type.audio_format.F64)
idx = 3;
else
return SPA_RESULT_INVALID_MEDIA_TYPE;
this->bpf = sizes[idx] * info.info.raw.channels;
this->current_format = info;
this->bpf = (2 * this->current_format.info.raw.channels);
this->have_format = true;
this->render_func = sine_funcs[idx];
}
if (this->have_format) {
@ -570,8 +588,8 @@ spa_audiotestsrc_node_port_set_format (SpaNode *node,
spa_pod_builder_init (&b, this->params_buffer, sizeof (this->params_buffer));
spa_pod_builder_object (&b, &f[0], 0, this->type.alloc_param_buffers.Buffers,
PROP (&f[1], this->type.alloc_param_buffers.size, SPA_POD_TYPE_INT, 1024),
PROP (&f[1], this->type.alloc_param_buffers.stride, SPA_POD_TYPE_INT, 1024),
PROP (&f[1], this->type.alloc_param_buffers.size, SPA_POD_TYPE_INT, 1024 * this->bpf),
PROP (&f[1], this->type.alloc_param_buffers.stride, SPA_POD_TYPE_INT, this->bpf),
PROP_U_MM (&f[1], this->type.alloc_param_buffers.buffers, SPA_POD_TYPE_INT, 32, 2, 32),
PROP (&f[1], this->type.alloc_param_buffers.align, SPA_POD_TYPE_INT, 16));
this->params[0] = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaAllocParam);
@ -701,8 +719,6 @@ spa_audiotestsrc_node_port_use_buffers (SpaNode *node,
spa_log_error (this->log, "audiotestsrc %p: invalid memory on buffer %p", this, buffers[i]);
continue;
}
b->ptr = d[0].data;
b->size = d[0].maxsize;
break;
default:
break;

View file

@ -3,6 +3,7 @@ audiotestsrc_sources = ['audiotestsrc.c', 'plugin.c']
audiotestsrclib = shared_library('spa-audiotestsrc',
audiotestsrc_sources,
include_directories : [spa_inc, spa_libinc],
dependencies : libm,
link_with : spalib,
install : true,
install_dir : '@0@/spa'.format(get_option('libdir')))

View file

@ -0,0 +1,56 @@
/* Spa
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <math.h>
#define M_PI_M2 ( M_PI + M_PI )
typedef SpaResult (*RenderFunc) (SpaAudioTestSrc *this, void *samples, size_t n_samples);
#define DEFINE_SINE(type,scale) \
static void \
audio_test_src_create_sine_##type (SpaAudioTestSrc *this, type * samples, size_t n_samples) \
{ \
int i, c, channels; \
double step, amp; \
\
channels = this->current_format.info.raw.channels; \
step = M_PI_M2 * this->props.freq / this->current_format.info.raw.rate; \
amp = this->props.volume * scale; \
\
for (i = 0; i < n_samples; i++) { \
this->accumulator += step; \
if (this->accumulator >= M_PI_M2) \
this->accumulator -= M_PI_M2; \
for (c = 0; c < channels; ++c) \
*samples++ = (type) (sin (this->accumulator) * amp); \
} \
}
DEFINE_SINE (int16_t, 32767.0);
DEFINE_SINE (int32_t, 2147483647.0);
DEFINE_SINE (float, 1.0);
DEFINE_SINE (double, 1.0);
static const RenderFunc sine_funcs[] = {
(RenderFunc) audio_test_src_create_sine_int16_t,
(RenderFunc) audio_test_src_create_sine_int32_t,
(RenderFunc) audio_test_src_create_sine_float,
(RenderFunc) audio_test_src_create_sine_double
};