sw-synth
Version:
Lightweight sound synthesizer designed for real-time user interaction using the Web Audio API
33 lines • 1.28 kB
JavaScript
import { VoiceBase } from './base.js';
export class BufferVoice extends VoiceBase {
noteOn(frequency, velocity, noteId, params, pitchBendRange) {
// This is not the ideal pattern for AudioBufferSourceNodes, but I've had bad experiences with Web Audio API garbage collection,
// so I'd like to reuse that GainNode as long as possible...
if (this.node) {
this.node.stop();
}
const node = params.factory(this.context, frequency, velocity);
this.pitchBend.connect(node.detune);
node.connect(this.envelope);
node.addEventListener('ended', () => {
this.pitchBend.disconnect(node);
node.disconnect(this.envelope);
});
const now = this.context.currentTime + params.audioDelay;
node.start(now);
this.node = node;
const noteOff = super.noteOn(frequency, velocity, noteId, params, pitchBendRange);
return () => {
noteOff();
const then = this.context.currentTime + params.audioDelay;
node.stop(then + params.releaseTime * 3);
};
}
dispose() {
if (this.node) {
this.node.stop();
}
this.envelope.disconnect();
}
}
//# sourceMappingURL=buffer.js.map