diff --git a/spa/meson.build b/spa/meson.build index 50ac8204f..f79ed4053 100644 --- a/spa/meson.build +++ b/spa/meson.build @@ -15,6 +15,7 @@ cc = meson.get_compiler('c') dl_lib = cc.find_library('dl', required : true) pthread_lib = cc.find_library('pthread', required : true) +libm = cc.find_library('m', required : true) spa_inc = include_directories('include') spa_libinc = include_directories('.') diff --git a/spa/plugins/alsa/alsa-utils.c b/spa/plugins/alsa/alsa-utils.c index 07f554095..4be12d165 100644 --- a/spa/plugins/alsa/alsa-utils.c +++ b/spa/plugins/alsa/alsa-utils.c @@ -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; diff --git a/spa/plugins/audiomixer/audiomixer.c b/spa/plugins/audiomixer/audiomixer.c index c5691ec72..a0a3a3922 100644 --- a/spa/plugins/audiomixer/audiomixer.c +++ b/spa/plugins/audiomixer/audiomixer.c @@ -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; diff --git a/spa/plugins/audiotestsrc/audiotestsrc.c b/spa/plugins/audiotestsrc/audiotestsrc.c index 1298d71ef..5275dd166 100644 --- a/spa/plugins/audiotestsrc/audiotestsrc.c +++ b/spa/plugins/audiotestsrc/audiotestsrc.c @@ -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; diff --git a/spa/plugins/audiotestsrc/meson.build b/spa/plugins/audiotestsrc/meson.build index a7531611a..681bcaef6 100644 --- a/spa/plugins/audiotestsrc/meson.build +++ b/spa/plugins/audiotestsrc/meson.build @@ -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'))) diff --git a/spa/plugins/audiotestsrc/render.c b/spa/plugins/audiotestsrc/render.c new file mode 100644 index 000000000..3cd21ae33 --- /dev/null +++ b/spa/plugins/audiotestsrc/render.c @@ -0,0 +1,56 @@ +/* Spa + * Copyright (C) 2017 Wim Taymans + * + * 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 + +#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 +}; diff --git a/spa/tests/test-mixer.c b/spa/tests/test-mixer.c index 1e3bcb392..f41089503 100644 --- a/spa/tests/test-mixer.c +++ b/spa/tests/test-mixer.c @@ -42,6 +42,10 @@ typedef struct { uint32_t props; uint32_t format; uint32_t props_device; + uint32_t props_freq; + uint32_t props_volume; + uint32_t props_min_latency; + uint32_t props_live; SpaTypeMediaType media_type; SpaTypeMediaSubtype media_subtype; SpaTypeFormatAudio format_audio; @@ -57,6 +61,10 @@ init_type (Type *type, SpaTypeMap *map) type->props = spa_type_map_get_id (map, SPA_TYPE__Props); type->format = spa_type_map_get_id (map, SPA_TYPE__Format); type->props_device = spa_type_map_get_id (map, SPA_TYPE_PROPS__device); + type->props_freq = spa_type_map_get_id (map, SPA_TYPE_PROPS__frequency); + type->props_volume = spa_type_map_get_id (map, SPA_TYPE_PROPS__volume); + type->props_min_latency = spa_type_map_get_id (map, SPA_TYPE_PROPS__minLatency); + type->props_live = spa_type_map_get_id (map, SPA_TYPE_PROPS__live); spa_type_media_type_map (map, &type->media_type); spa_type_media_subtype_map (map, &type->media_subtype); spa_type_format_audio_map (map, &type->format_audio); @@ -111,6 +119,8 @@ typedef struct { unsigned int n_fds; } AppData; +#define BUFFER_SIZE 4096 + static void init_buffer (AppData *data, Buffer *b, void *ptr, size_t size) { @@ -291,8 +301,9 @@ make_nodes (AppData *data) spa_pod_builder_init (&b, buffer, sizeof (buffer)); spa_pod_builder_props (&b, &f[0], data->type.props, - SPA_POD_PROP (&f[1], data->type.props_device, 0, - SPA_POD_TYPE_STRING, 1, "hw:0")); + SPA_POD_PROP (&f[1], data->type.props_device, 0, SPA_POD_TYPE_STRING, 1, "hw:0"), + SPA_POD_PROP (&f[1], data->type.props_min_latency, 0, SPA_POD_TYPE_INT, 1, 1024), + SPA_POD_PROP (&f[1], data->type.props_live, 0, SPA_POD_TYPE_BOOL, 1, false)); props = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaProps); if ((res = spa_node_set_props (data->sink, props)) < 0) @@ -304,18 +315,40 @@ make_nodes (AppData *data) printf ("can't create audiomixer: %d\n", res); return res; } + if ((res = make_node (data, &data->source1, "build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so", "audiotestsrc", false)) < 0) { printf ("can't create audiotestsrc: %d\n", res); return res; } + + spa_pod_builder_init (&b, buffer, sizeof (buffer)); + spa_pod_builder_props (&b, &f[0], data->type.props, + SPA_POD_PROP (&f[1], data->type.props_freq, 0, SPA_POD_TYPE_DOUBLE, 1, 600.0), + SPA_POD_PROP (&f[1], data->type.props_volume, 0, SPA_POD_TYPE_DOUBLE, 1, 0.5)); + props = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaProps); + + if ((res = spa_node_set_props (data->source1, props)) < 0) + printf ("got set_props error %d\n", res); + if ((res = make_node (data, &data->source2, "build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so", "audiotestsrc", false)) < 0) { printf ("can't create audiotestsrc: %d\n", res); return res; } + + spa_pod_builder_init (&b, buffer, sizeof (buffer)); + spa_pod_builder_props (&b, &f[0], data->type.props, + SPA_POD_PROP (&f[1], data->type.props_freq, 0, SPA_POD_TYPE_DOUBLE, 1, 440.0), + SPA_POD_PROP (&f[1], data->type.props_volume, 0, SPA_POD_TYPE_DOUBLE, 1, 0.5), + SPA_POD_PROP (&f[1], data->type.props_live, 0, SPA_POD_TYPE_BOOL, 1, false)); + props = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaProps); + + if ((res = spa_node_set_props (data->source2, props)) < 0) + printf ("got set_props error %d\n", res); + return res; } @@ -359,7 +392,7 @@ negotiate_formats (AppData *data) if ((res = spa_node_port_set_format (data->mix, SPA_DIRECTION_OUTPUT, 0, 0, format)) < 0) return res; - init_buffer (data, &data->mix_buffer[0], malloc (1024), 1024); + init_buffer (data, &data->mix_buffer[0], malloc (BUFFER_SIZE), BUFFER_SIZE); data->mix_buffers[0] = &data->mix_buffer[0].buffer; if ((res = spa_node_port_use_buffers (data->sink, SPA_DIRECTION_INPUT, 0, data->mix_buffers, 1)) < 0) return res; @@ -380,7 +413,7 @@ negotiate_formats (AppData *data) if ((res = spa_node_port_set_format (data->source1, SPA_DIRECTION_OUTPUT, 0, 0, format)) < 0) return res; - init_buffer (data, &data->source1_buffer[0], malloc (1024), 1024); + init_buffer (data, &data->source1_buffer[0], malloc (BUFFER_SIZE), BUFFER_SIZE); data->source1_buffers[0] = &data->source1_buffer[0].buffer; if ((res = spa_node_port_use_buffers (data->mix, SPA_DIRECTION_INPUT, data->mix_ports[0], data->source1_buffers, 1)) < 0) return res; @@ -400,7 +433,7 @@ negotiate_formats (AppData *data) if ((res = spa_node_port_set_format (data->source2, SPA_DIRECTION_OUTPUT, 0, 0, format)) < 0) return res; - init_buffer (data, &data->source2_buffer[0], malloc (1024), 1024); + init_buffer (data, &data->source2_buffer[0], malloc (BUFFER_SIZE), BUFFER_SIZE); data->source2_buffers[0] = &data->source2_buffer[0].buffer; if ((res = spa_node_port_use_buffers (data->mix, SPA_DIRECTION_INPUT, data->mix_ports[1], data->source2_buffers, 1)) < 0) return res; @@ -489,8 +522,8 @@ run_async_sink (AppData *data) data->running = false; } - printf ("sleeping for 10 seconds\n"); - sleep (10); + printf ("sleeping for 1000 seconds\n"); + sleep (1000); if (data->running) { data->running = false;