Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.27k stars 965 forks source link

Add loop option to Sampler #1207

Open andreasjeppsson opened 10 months ago

andreasjeppsson commented 10 months ago

It would be nice to be able to have the sources of the sampler looping. That would allow the sampler to be used for instruments like pads, organs etc.

It's a simple change allowing a loop property to be passed in the constructor options and then set that property for each source when triggering notes.

joeqj commented 2 months ago

Just came across this limitation as well, thanks for suggesting it. Figured out a good workaround for your pads - if you set the sampler attack and release to a slow enough value and then send it through enough reverb and/or delay then you can trigger the synth with a setInterval function and it sounds endless, here's my code that works well:

const reverb2 = new Tone.Reverb({
    decay: 1,
    wet: 1,
  }).toDestination();

  const filter = new Tone.Filter({
    type: "bandpass",
    frequency: params.cutoff,
    rolloff: -12,
  });

  const reverb = new Tone.Reverb({
    decay: 1,
    wet: 1,
  });

  const sampler = new Tone.Sampler({
    urls: {
      C3: "pad.wav",
    },
    baseUrl: "/",
    volume: 0,
    attack: 4,
    release: 4,

    onload: () => {
      setInterval(() => {
        sampler.triggerAttackRelease(padKeys, Infinity);
      }, 2000);
    },
  });

  sampler.chain(reverb, filter, reverb2);

I've set the interval every 2 seconds here as my pad sample is around 4 seconds.

Don't know how this would sound with an organ so let me know if you figure it out :)