fix: finally repair envelopes

This commit is contained in:
Felix Roos 2023-12-12 21:36:32 +01:00
parent 73fd801907
commit b0bbd58f66
4 changed files with 19 additions and 11 deletions

View File

@ -139,8 +139,8 @@ export function registerSoundfonts() {
const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 0.3, time);
bufferSource.connect(envelope);
const stop = (releaseTime) => {
bufferSource.stop(releaseTime + release);
releaseEnvelope(releaseTime);
const silentAt = releaseEnvelope(releaseTime);
bufferSource.stop(silentAt);
};
bufferSource.onended = () => {
bufferSource.disconnect();

View File

@ -10,21 +10,30 @@ export function gainNode(value) {
// alternative to getADSR returning the gain node and a stop handle to trigger the release anytime in the future
export const getEnvelope = (attack, decay, sustain, release, velocity, begin) => {
const gainNode = getAudioContext().createGain();
let phase = begin;
gainNode.gain.setValueAtTime(0, begin);
gainNode.gain.linearRampToValueAtTime(velocity, begin + attack); // attack
gainNode.gain.linearRampToValueAtTime(sustain * velocity, begin + attack + decay); // sustain start
phase += attack;
gainNode.gain.linearRampToValueAtTime(velocity, phase); // attack
phase += decay;
let sustainLevel = sustain * velocity;
gainNode.gain.linearRampToValueAtTime(sustainLevel, phase); // decay / sustain
// sustain end
return {
node: gainNode,
stop: (t) => {
// to make sure the release won't begin before sustain is reached
//phase = Math.max(t, phase + 0.001);
phase = Math.max(t, phase);
// see https://github.com/tidalcycles/strudel/issues/522
//if (typeof gainNode.gain.cancelAndHoldAtTime === 'function') {
// gainNode.gain.cancelAndHoldAtTime(t); // this seems to release instantly....
// see https://discord.com/channels/779427371270275082/937365093082079272/1086053607360712735
//} else {
// firefox: this will glitch when the sustain has not been reached yet at the time of release
gainNode.gain.setValueAtTime(sustain * velocity, t);
gainNode.gain.setValueAtTime(sustainLevel, phase);
//}
gainNode.gain.linearRampToValueAtTime(0, t + release);
gainNode.gain.linearRampToValueAtTime(0, phase + release); // release
return phase + release;
},
};
};

View File

@ -312,8 +312,8 @@ export async function onTriggerSample(t, value, onended, bank, resolveUrl) {
const bufferDuration = bufferSource.buffer.duration / bufferSource.playbackRate.value;
releaseTime = t + (end - begin) * bufferDuration;
}
bufferSource.stop(releaseTime + release);
releaseEnvelope(releaseTime);
const silentAt = releaseEnvelope(releaseTime);
bufferSource.stop(silentAt);
};
const handle = { node: out, bufferSource, stop };

View File

@ -56,10 +56,9 @@ export function registerSynthSounds() {
return {
node: o.connect(g).connect(envelope),
stop: (releaseTime) => {
releaseEnvelope(releaseTime);
const silentAt = releaseEnvelope(releaseTime);
triggerRelease?.(releaseTime);
let end = releaseTime + release;
stop(end);
stop(silentAt);
},
};
},