Skip to content

Commit

Permalink
fix: randomization optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Inverseit committed Dec 6, 2023
1 parent ff2c6b0 commit 850f813
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions src/components/header/random-controller/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,37 @@ export function RandomModeButton() {

// Apply Volumes with Timeouts
function applyVolumeChanges(stepDuration) {
clearAllTimeouts() // Clears existing timeouts

soundsRef.current
.filter(sound => sound.active)
.forEach(sound => {
const targetVolume = Math.random()
const volumeSteps = calculateVolumeSteps(sound.volume, targetVolume, 5)

volumeSteps.forEach((_, index) => {
const timeoutId = setTimeout(
() => {
// Fetch the most recent value of the sound from ref
const currentSound = soundsRef.current.find(
s => s.id === sound.id
)
if (currentSound && currentSound.active) {
const updatedVolume = volumeSteps[index]
const updatedSound = { ...currentSound, volume: updatedVolume }
setSound(updatedSound)
}
},
(index + 1) * stepDuration
)

timeoutsRef.current.push(timeoutId)
})
})
}
clearAllTimeouts(); // Clears existing timeouts

soundsRef.current.filter(sound => sound.active).forEach(initialSound => {
const targetVolume = Math.random();
const volumeSteps = calculateVolumeSteps(initialSound.volume, targetVolume, NUM_STEPS);

const setVolumeStep = (sound, index) => {
if (index < volumeSteps.length) {
// Fetch the most recent value of the sound
const currentSound = soundsRef.current.find(s => s.id === sound.id);
if (currentSound && currentSound.active) {
const updatedVolume = volumeSteps[index];
const updatedSound = { ...currentSound, volume: updatedVolume };
setSound(updatedSound);
// add next timeout only if this one is successfull
const timeoutId = setTimeout(() => {
setVolumeStep(currentSound, index + 1);
}, stepDuration);

timeoutsRef.current.push(timeoutId);
}
}
};

setVolumeStep(initialSound, 0);
});
}

function randomizeVolumes() {
// Total duration for volume change
const transitionDuration = TOTAL_TRANSITION
const stepDuration = transitionDuration / NUM_STEPS

const stepDuration = TOTAL_TRANSITION / NUM_STEPS
applyVolumeChanges(stepDuration)
}

Expand Down

0 comments on commit 850f813

Please sign in to comment.