Tone JS - filter not applying to sampler

309 Views Asked by At

I am using Tone JS to make sounds and I would like to add a filter to my Sampler instrument, my understanding from the documentation is that I can do so by using the connect method on the sampler and passing in the filter as the argument:

const filter = new Filter(20000, "highpass").toDestination();
this.sampler = new Sampler({
  urls: { C1: 'C1.wav' },
  baseUrl: "/static/samples/piano/",
})
  .connect(filter)
  .toDestination();

The above does not apply the filter effect when the sound plays and I am not sure why?

1

There are 1 best solutions below

0
On BEST ANSWER

Use the chain command to connect the sampler to the filter and then to destination. Then use the onload callback to start the sound.

Try this:

const filter = new Tone.Filter(20000, "highpass");
const sampler = new Tone.Sampler({
  urls: { C1: 'C1.wav' },
  baseUrl: "/static/samples/piano/",
  onload: () => {
    sampler.chain(filter, Tone.Destination);
    sampler.triggerAttackRelease(["D4"], 3);
});