From 5f9cca928935547ec0d4d449f4864c5eca1e7cf8 Mon Sep 17 00:00:00 2001 From: Marks Polakovs Date: Thu, 16 Apr 2020 11:06:21 +0200 Subject: [PATCH] Delay turning mic off to compensate for WebAudio latency (fixes #72) --- src/mixer/state.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/mixer/state.ts b/src/mixer/state.ts index f41d720..768ba77 100644 --- a/src/mixer/state.ts +++ b/src/mixer/state.ts @@ -719,9 +719,20 @@ export const setMicVolume = ( ): AppThunk => dispatch => { // no tween fuckery here, just cut the level const levelVal = level === "full" ? 1 : 0; - dispatch( - mixerState.actions.setMicLevels({ volume: levelVal, gain: levelVal }) - ); + // actually, that's a lie - if we're turning it off we delay it a little to compensate for + // processing latency + if (levelVal !== 0) { + dispatch( + mixerState.actions.setMicLevels({ volume: levelVal, gain: levelVal }) + ); + } else { + window.setTimeout(() => { + dispatch( + mixerState.actions.setMicLevels({ volume: levelVal, gain: levelVal }) + ); + // latency, plus a little buffer + }, audioContext.baseLatency * 1000 + 150); + } }; let analyser: AnalyserNode | null = null;