// A soft, gliding tone whose pitch can follow something continuously (like a theremin): a sine // with a quiet octave triangle, gently low-passed, with a slow vibrato. Cheap, smooth, and // unobtrusive. export class GlideVoice { private out: GainNode; private oscs: OscillatorNode[]; private filter: BiquadFilterNode; private lfo: OscillatorNode; private lfoGain: GainNode; private ctx: AudioContext; constructor(ctx: AudioContext, dest: AudioNode) { this.ctx = ctx; this.out = ctx.createGain(); this.out.gain.value = 0; this.filter = ctx.createBiquadFilter(); this.filter.type = "lowpass"; this.filter.Q.value = 0.5; this.filter.connect(this.out).connect(dest); const sine = ctx.createOscillator(); const tri = ctx.createOscillator(); tri.type = "triangle"; const triGain = ctx.createGain(); triGain.gain.value = 0.12; sine.connect(this.filter); tri.connect(triGain).connect(this.filter); this.oscs = [sine, tri]; // vibrato (in cents, on both oscillators) this.lfo = ctx.createOscillator(); this.lfo.frequency.value = 4.8; this.lfoGain = ctx.createGain(); this.lfoGain.gain.value = 6; this.lfo.connect(this.lfoGain); for (const o of this.oscs) this.lfoGain.connect(o.detune); } start(when: number, freq: number) { this.set(when, freq, 0, 0.3, 6); for (const o of [...this.oscs, this.lfo]) o.start(when); } /** freq (Hz), level (linear), brightness 0..1 (filter), vibrato depth in cents */ set(when: number, freq: number, level: number, brightness: number, vibrato: number) { const tc = 0.03; this.oscs[0].frequency.setTargetAtTime(freq, when, tc); this.oscs[1].frequency.setTargetAtTime(2 * freq, when, tc); this.filter.frequency.setTargetAtTime(freq * (1.5 + 6 * Math.max(0, Math.min(1, brightness))), when, tc); this.out.gain.setTargetAtTime(level, when, 0.08); this.lfoGain.gain.setTargetAtTime(vibrato, when, 0.2); } stop(when: number, release: number) { this.out.gain.cancelScheduledValues(this.ctx.currentTime); this.out.gain.setTargetAtTime(0, Math.max(this.ctx.currentTime, when - release), release / 4); for (const o of [...this.oscs, this.lfo]) o.stop(when + 0.1); } dispose() { window.setTimeout(() => this.out.disconnect(), 1500); } }