From d74aa24fd5ffcc83341a7aa7027249baeebb1881 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 7 Jan 2024 18:23:24 +0100 Subject: [PATCH] Improve audio compensation thresholds Use different threshold for enabling and disabling compensation. Concretely, enable compensation if the difference between the average and the target buffering levels exceeds 4 ms (instead of 1 ms). This avoids unnecessary compensation due to small noise in buffering level estimation. But keep a smaller threshold (1 ms) for disabling compensation, so that the buffering level is restored closer to the target value. This avoids to keep the actual level close to the compensation threshold. --- app/src/audio_player.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/audio_player.c b/app/src/audio_player.c index 728d3f2a..c70964b9 100644 --- a/app/src/audio_player.c +++ b/app/src/audio_player.c @@ -281,8 +281,15 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink, float avg = sc_average_get(&ap->avg_buffering); int diff = ap->target_buffering - avg; - if (abs(diff) < (int) ap->sample_rate / 1000) { - // Do not compensate for less than 1ms, the error is just noise + + // Enable compensation when the difference exceeds +/- 4ms. + // Disable compensation when the difference is lower than +/- 1ms. + int threshold = ap->compensation != 0 + ? ap->sample_rate / 1000 /* 1ms */ + : ap->sample_rate * 4 / 1000; /* 4ms */ + + if (abs(diff) < threshold) { + // Do not compensate for small values, the error is just noise diff = 0; } else if (diff < 0 && can_read < ap->target_buffering) { // Do not accelerate if the instant buffering level is below