Skip to content

Tone.Sampler onload doesn't fire on successful load - if you attempted an action before load. #1327

@MGSimard

Description

@MGSimard

Describe the bug

If you try to play the sampler before it's ready, you get the following error:
Uncaught Error: buffer is either not set or not loaded

This is normal, however this inherently causes onload to never fire, EVEN though the sampler does finish loading and notes can be played completely fine.

Sure, I could disable user inputs entirely until onload has fired, but that's just sweeping the issue under the rug. Clearly the Sampler does finish loading, as you can play notes, however it never actually fires "onload" in this situation. Onload should be firing here, period.

To Reproduce

  const sampler = useRef<Tone.Sampler>(
    new Tone.Sampler({
      urls: { c4: "C4.mp3" },
      baseUrl: "/",
      release: 1,
      volume: -7,
      attack: 0,
      onload: () => {
        console.log("onload fired");
        setIsLoaded(true);
      },
      onerror: (error) => console.error("Error loading sampler", error),
    }).toDestination()
  );

Try to play a note before onload fires: sampler.current.triggerAttack(noteName);

Then even though the sampler does load shortly after and starts working, onload never fires. You won't get a console log saying it fired, and the state won't update.

Codesandbox: https://codesandbox.io/p/sandbox/4zgc96

  1. Open sandbox console, refresh sandbox preview, spam click the "Play C4" button before loading has finished.
  2. Even though eventually the sampler loads and notes start playing, onload never fires. You won't get a console log, UI state won't update.

Expected behavior

I expect onload to fire when sampler has loaded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions