simplify webaudio replaygain to reduce pop-in

This commit is contained in:
Kendall Garner 2024-08-21 23:04:37 -07:00
parent 616fd45734
commit 37b0407188
No known key found for this signature in database
GPG Key ID: 18D2767419676C87

View File

@ -277,18 +277,34 @@ export const AudioPlayer = forwardRef(
}, [audioDeviceId, webAudio]);
useEffect(() => {
if (webAudio && player1Source && player1 && currentPlayer === 1) {
const newVolume = calculateReplayGain(player1) * volume;
webAudio.gain.gain.setValueAtTime(newVolume, 0);
}
}, [calculateReplayGain, currentPlayer, player1, player1Source, volume, webAudio]);
if (!webAudio) return;
useEffect(() => {
if (webAudio && player2Source && player2 && currentPlayer === 2) {
const newVolume = calculateReplayGain(player2) * volume;
const sources = [player1Source ? player1 : null, player2Source ? player2 : null];
const current = sources[currentPlayer - 1];
// Set the current replaygain
if (current) {
const newVolume = calculateReplayGain(current) * volume;
webAudio.gain.gain.setValueAtTime(newVolume, 0);
}
}, [calculateReplayGain, currentPlayer, player2, player2Source, volume, webAudio]);
// Set the next track replaygain right before the end of this track
// Attempt to prevent pop-in for web audio.
const next = sources[3 - currentPlayer];
if (next && current) {
const newVolume = calculateReplayGain(next) * volume;
webAudio.gain.gain.setValueAtTime(newVolume, (current.duration - 1) / 1000);
}
}, [
calculateReplayGain,
currentPlayer,
player1,
player1Source,
player2,
player2Source,
volume,
webAudio,
]);
const handlePlayer1Start = useCallback(
async (player: ReactPlayer) => {