From 116676e14953a2c444f532a61c04d00a790866ed Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 21 Oct 2019 10:13:56 +0200 Subject: [PATCH] node: add active flag for rate match Add flags to the rate match io area Add flag to activate/deactivate rate match Set active flag in rate match when slaved Update rate before starting resample --- spa/include/spa/node/io.h | 2 ++ spa/plugins/alsa/alsa-pcm.c | 6 ++++-- spa/plugins/audioconvert/resample.c | 9 ++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spa/include/spa/node/io.h b/spa/include/spa/node/io.h index 7ad3078fd..145557447 100644 --- a/spa/include/spa/node/io.h +++ b/spa/include/spa/node/io.h @@ -265,6 +265,8 @@ struct spa_io_rate_match { uint32_t delay; /**< extra delay in samples for resampler */ uint32_t size; /**< requested input size for resampler */ double rate; /**< rate for resampler */ +#define SPA_IO_RATE_MATCH_FLAG_ACTIVE (1 << 0) + uint32_t flags; /**< extra flags */ }; #ifdef __cplusplus diff --git a/spa/plugins/alsa/alsa-pcm.c b/spa/plugins/alsa/alsa-pcm.c index a67b5d0f0..d6ea19307 100644 --- a/spa/plugins/alsa/alsa-pcm.c +++ b/spa/plugins/alsa/alsa-pcm.c @@ -608,7 +608,7 @@ static int get_status(struct state *state, snd_pcm_uframes_t *delay, snd_pcm_ufr *target = state->last_threshold; - if (state->rate_match) { + if (state->slaved && state->rate_match) { state->delay = state->rate_match->delay; state->read_size = state->rate_match->size; /* We try to compensate for the latency introduced by rate matching @@ -672,11 +672,13 @@ static int update_time(struct state *state, uint64_t nsec, snd_pcm_sframes_t del err, state->z1, state->z2, state->z3); } - if (slave && state->rate_match) { + if (state->rate_match) { if (state->stream == SND_PCM_STREAM_PLAYBACK) state->rate_match->rate = SPA_CLAMP(corr, 0.95, 1.05); else state->rate_match->rate = SPA_CLAMP(1.0/corr, 0.95, 1.05); + + SPA_FLAG_UPDATE(state->rate_match->flags, SPA_IO_RATE_MATCH_FLAG_ACTIVE, slave); } state->next_time += state->threshold / corr * 1e9 / state->rate; diff --git a/spa/plugins/audioconvert/resample.c b/spa/plugins/audioconvert/resample.c index 818e5af61..bfc0ea079 100644 --- a/spa/plugins/audioconvert/resample.c +++ b/spa/plugins/audioconvert/resample.c @@ -767,6 +767,14 @@ static int impl_node_process(void *object) break; } + if (this->io_rate_match) { + if (SPA_FLAG_IS_SET(this->io_rate_match->flags, SPA_IO_RATE_MATCH_FLAG_ACTIVE)) { + resample_update_rate(&this->resample, this->io_rate_match->rate); + } else { + resample_update_rate(&this->resample, 1.0); + } + } + in_len = (size - inport->offset) / sizeof(float); out_len = (maxsize - outport->offset) / sizeof(float); @@ -812,7 +820,6 @@ static int impl_node_process(void *object) } if (this->io_rate_match) { - resample_update_rate(&this->resample, this->io_rate_match->rate); this->io_rate_match->delay = resample_delay(&this->resample); this->io_rate_match->size = resample_in_len(&this->resample, max); }