Skip to content

Commit

Permalink
move volume scaling calculation to setGain
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarner7 committed Jan 27, 2024
1 parent 3c9f7d4 commit 3c0b2e8
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/renderer/components/audio-player/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const AudioPlayer = forwardRef(
const calculateReplayGain = useCallback(
(song: Song): number => {
if (playback.replayGainMode === 'no') {
return volume;
return 1;
}

let gain: number | undefined;
Expand All @@ -99,7 +99,7 @@ export const AudioPlayer = forwardRef(
gain = playback.replayGainFallbackDB;

if (!gain) {
return volume;
return 1;
}
}

Expand All @@ -116,14 +116,13 @@ export const AudioPlayer = forwardRef(
if (playback.replayGainClip) {
return Math.min(expectedGain, 1 / peak);
}
return expectedGain * volume;
return expectedGain;
},
[
playback.replayGainClip,
playback.replayGainFallbackDB,
playback.replayGainMode,
playback.replayGainPreampDB,
volume,
],
);

Expand Down Expand Up @@ -253,20 +252,18 @@ export const AudioPlayer = forwardRef(
}, [audioDeviceId]);

useEffect(() => {
if (webAudio && player1Source) {
if (player1 && currentPlayer === 1) {
webAudio.gain.gain.setValueAtTime(calculateReplayGain(player1), 0);
}
if (webAudio && player1Source && player1 && currentPlayer === 1) {
const newVolume = calculateReplayGain(player1) * volume;
webAudio.gain.gain.setValueAtTime(newVolume, 0);
}
}, [calculateReplayGain, currentPlayer, player1, player1Source, webAudio]);
}, [calculateReplayGain, currentPlayer, player1, player1Source, volume, webAudio]);

useEffect(() => {
if (webAudio && player2Source) {
if (player2 && currentPlayer === 2) {
webAudio.gain.gain.setValueAtTime(calculateReplayGain(player2), 0);
}
if (webAudio && player2Source && player2 && currentPlayer === 2) {
const newVolume = calculateReplayGain(player2) * volume;
webAudio.gain.gain.setValueAtTime(newVolume, 0);
}
}, [calculateReplayGain, currentPlayer, player2, player2Source, webAudio]);
}, [calculateReplayGain, currentPlayer, player2, player2Source, volume, webAudio]);

const handlePlayer1Start = useCallback(
async (player: ReactPlayer) => {
Expand Down

0 comments on commit 3c0b2e8

Please sign in to comment.