From 5936167ff77b543a533c7f6a5fa42d78ca5d724f Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 4 Nov 2024 23:17:43 +0100 Subject: [PATCH] Store compensation state as a boolean We don't need to store the last compensation value anymore, we just need to know if it's non-zero. --- app/src/audio_regulator.c | 6 +++--- app/src/audio_regulator.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/audio_regulator.c b/app/src/audio_regulator.c index fb0c3758..3e4f78ad 100644 --- a/app/src/audio_regulator.c +++ b/app/src/audio_regulator.c @@ -288,7 +288,7 @@ sc_audio_regulator_push(struct sc_audio_regulator *ar, const AVFrame *frame) { // Enable compensation when the difference exceeds +/- 4ms. // Disable compensation when the difference is lower than +/- 1ms. - int threshold = ar->compensation != 0 + int threshold = ar->compensation_active ? ar->sample_rate / 1000 /* 1ms */ : ar->sample_rate * 4 / 1000; /* 4ms */ @@ -314,7 +314,7 @@ sc_audio_regulator_push(struct sc_audio_regulator *ar, const AVFrame *frame) { LOGW("Resampling compensation failed: %d", ret); // not fatal } else { - ar->compensation = diff; + ar->compensation_active = diff != 0; } } @@ -390,7 +390,7 @@ sc_audio_regulator_init(struct sc_audio_regulator *ar, size_t sample_size, atomic_init(&ar->played, false); atomic_init(&ar->received, false); atomic_init(&ar->underflow, 0); - ar->compensation = 0; + ar->compensation_active = false; return true; diff --git a/app/src/audio_regulator.h b/app/src/audio_regulator.h index 7daa1b05..1c0eeb9f 100644 --- a/app/src/audio_regulator.h +++ b/app/src/audio_regulator.h @@ -44,8 +44,8 @@ struct sc_audio_regulator { // Number of silence samples inserted since the last received packet atomic_uint_least32_t underflow; - // Current applied compensation value (only used by the receiver thread) - int compensation; + // Non-zero compensation applied (only used by the receiver thread) + bool compensation_active; // Set to true the first time a sample is received atomic_bool received;