Ben Doggett
Ben Doggett

Reputation: 11

ToneJS scheduled sample playback gradually decreases volume

I'm using ToneJS in a React application to build a drum machine/sampler. I have been able to record the attack and release times for the sample to play back, stored in state in a sampleData object, but for some reason when playback happens, the triggered samples sound at different volumes depending on the number of times objects in the times array. One instance maintains normal volume. Two instances seems to double volume for the first sample playback and half the second, (this pattern gets more exacerbated with each additional sample playback instance.

Here's my code for scheduling he playback

  useEffect(() => {
    if (isPlaying && sampleData.times.length > 0) {
      sampleData.times.forEach(({ startTime, duration }) => {
        if (duration) {
          Tone.Transport.schedule((time) => {
            sampler.current?.triggerAttackRelease("C4", duration, time, 1);
          }, startTime);
        }
      });
    }
  }, [isPlaying, sampler, sampleData]);

I've logged the various instances of "sampler" on each playback instance to see if any data changed on subsequent playback (like velocity, volume...), but the sampler object from one trigger === the sampler object from the following one.

Where is this volume fluctuation coming from on playback?

Here’s a sandbox link for the whole project: https://codesandbox.io/p/github/bhdoggett/drum-the-jukebox/draft/sweet-mahavira

Upvotes: 1

Views: 15

Answers (0)

Related Questions