UNPKG

sw-synth

Version:

Lightweight sound synthesizer designed for real-time user interaction using the Web Audio API

35 lines 1.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BufferVoice = void 0; const base_1 = require("./base"); class BufferVoice extends base_1.VoiceBase { noteOn(frequency, velocity, noteId, params) { // 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); node.connect(this.envelope); node.addEventListener('ended', () => { 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); 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(); } } exports.BufferVoice = BufferVoice; //# sourceMappingURL=buffer.js.map