From 500005811c44621cd69718d355d224a427732f5a Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Tue, 27 Oct 2020 22:15:44 +0000 Subject: [PATCH] Fix fader fidgeting volume jumps. Fixes #40 --- src/mixer/audio.ts | 4 ++++ src/mixer/state.ts | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/mixer/audio.ts b/src/mixer/audio.ts index 37bbbe2..a29b716 100644 --- a/src/mixer/audio.ts +++ b/src/mixer/audio.ts @@ -125,6 +125,10 @@ class Player extends ((PlayerEmitter as unknown) as { new (): EventEmitter }) { } } + getVolume() { + return this.volume; + } + setVolume(val: number) { this.volume = val; this._applyVolume(); diff --git a/src/mixer/state.ts b/src/mixer/state.ts index 9c4cf08..421e02e 100644 --- a/src/mixer/state.ts +++ b/src/mixer/state.ts @@ -666,7 +666,14 @@ export const setVolume = ( const state = getState().mixer.players[player]; const currentLevel = state.volume; - const currentGain = state.gain; + let currentGain = state.gain; + + // If we can, use the engine's 'real' volume gain. + // This helps when we've interupted a previous fade, so the state gain won't be correct. + if (typeof audioEngine.players[player] !== "undefined") { + currentGain = audioEngine.players[player]!.getVolume(); + } + const volumeTween = new Between(currentLevel, uiLevel) .time(FADE_TIME_SECONDS * 1000) .on("update", (val: number) => {