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) => {