Tonejs / Tone.js

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

Is there any way apply AmplitudeEnvelope each Note? #1195

Closed kina94 closed 5 months ago

kina94 commented 1 year ago

Hello, I want to apply an Amplitude Envelope to each note individually, but it seems that using a shared sampler causes the released sound from the previous note amplified when the next note is triggered. Creating a sampler instance for each note individually would require too much resource to resolve this issue. Is there a solution to this? or what's wrong with my code?

this is part of my code.

const ampEnv = new Tone.AmplitudeEnvelope({
    "attack": 0.1,
    "decay": 0.2,
    "sustain": 1.0,
    "release": 1
  }).toDestination()
sampler.connect(ampEnv); // my new Sampler Instance which has multiple sample ('C4', 'C3'.....).

...
...

const newPart = new Tone.Part((time, value) => {
    sampler.triggerAttack(value.note, time, value.velocity);
    ampEnv.triggerAttack(time, value.velocity);
    ampEnv.triggerRelease(time + value.duration);
    sampler.triggerRelease(value.note, time + value.duration + ampEnv.release); //after envelope release, the Sampler release
}, newNotes).start(0);
tambien commented 5 months ago

You could create a Sampler for each note, or use Player or ToneBufferSource instead (but if you do you'll have to calculate the playback rate to ensure that your samples playback at the right pitch (Tone.intervalToFrequencyRatio). Then you could connect each of those samples through an AmplitudeEnvelope and trigger it on each note of Tone.Part