add envelope to soundfonts

This commit is contained in:
Felix Roos 2023-03-15 23:02:05 +01:00
parent a33ce67dd4
commit f788d347ec

View File

@ -1,5 +1,5 @@
import { toMidi } from '@strudel.cycles/core'; import { toMidi } from '@strudel.cycles/core';
import { getAudioContext, registerSound } from '@strudel.cycles/webaudio'; import { getAudioContext, registerSound, getEnvelope } from '@strudel.cycles/webaudio';
import { instruments } from './list.mjs'; import { instruments } from './list.mjs';
let loadCache = {}; let loadCache = {};
@ -122,18 +122,22 @@ export function registerSoundfonts() {
instrument, instrument,
async (time, value, onended) => { async (time, value, onended) => {
const { note, n } = value; const { note, n } = value;
const { attack = 0.001, decay = 0.001, sustain = 1, release = 0.001 } = value;
const ctx = getAudioContext(); const ctx = getAudioContext();
const bufferSource = await getFontBufferSource(instrument, note || n, ctx); const bufferSource = await getFontBufferSource(instrument, note || n, ctx);
bufferSource.start(time); bufferSource.start(time);
const stop = (time) => bufferSource.stop(time); const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 0.3, time);
const g = new GainNode(ctx, { gain: 0.3 }); bufferSource.connect(envelope);
bufferSource.connect(g); const stop = (releaseTime) => {
bufferSource.stop(releaseTime + release);
releaseEnvelope(releaseTime);
};
bufferSource.onended = () => { bufferSource.onended = () => {
bufferSource.disconnect(); bufferSource.disconnect();
g.disconnect(); envelope.disconnect();
onended(); onended();
}; };
return { node: g, stop }; return { node: envelope, stop };
}, },
{ type: 'soundfont', prebake: true }, { type: 'soundfont', prebake: true },
); );