// Placeholder instruments: deliberately plain, to be replaced later. import { fadeCurve } from "./fade.ts"; import type { DroneInstrument, NoteEvent, NoteHandle, NoteInstrument } from "./types.ts"; /** Sine + soft octave triangle with a percussive exponential decay. */ export function placeholderTone(ctx: AudioContext, out: AudioNode): NoteInstrument { return { play({ when, freq, velocity, pan, duration }: NoteEvent): NoteHandle | null { if (velocity <= 0) return null; const t0 = Math.max(when, ctx.currentTime); const end = t0 + duration; const peak = 0.3 * velocity; const ta = t0 + 0.004; const env = ctx.createGain(); env.gain.setValueAtTime(0, t0); env.gain.linearRampToValueAtTime(peak, ta); env.gain.exponentialRampToValueAtTime(0.0001, end); const panner = ctx.createStereoPanner(); panner.pan.value = Math.max(-1, Math.min(1, pan)); env.connect(panner).connect(out); const a = ctx.createOscillator(); a.frequency.value = freq; a.connect(env); const b = ctx.createOscillator(); b.type = "triangle"; b.frequency.value = freq * 2; const bg = ctx.createGain(); bg.gain.value = 0.08 + 0.15 * velocity; // a touch brighter when louder b.connect(bg).connect(env); for (const o of [a, b]) { o.start(t0); o.stop(end + 0.05); } b.onended = () => panner.disconnect(); return { hold(from, holdLen, fadeLen) { const t = Math.max(from, ta, ctx.currentTime); if (t >= end) return; // level of the exponential decay at t const v = peak * Math.pow(0.0001 / peak, (t - ta) / (end - ta)); const g = env.gain; if (typeof g.cancelAndHoldAtTime === "function") g.cancelAndHoldAtTime(t); else g.cancelScheduledValues(t); g.setValueAtTime(v, t); g.setValueCurveAtTime(fadeCurve(v), t + Math.max(holdLen, 0.005), fadeLen); const stopAt = t + holdLen + fadeLen + 0.05; a.stop(stopAt); b.stop(stopAt); }, }; }, dispose() {}, }; } /** * Hum: sub sine + two detuned saws through a low-pass. `color` (0..1) opens the * filter and adds resonance, so the timbre brightens with energy at fixed pitch. */ export function placeholderHum(ctx: AudioContext, out: AudioNode): DroneInstrument { const gain = ctx.createGain(); gain.gain.value = 0; const filter = ctx.createBiquadFilter(); filter.type = "lowpass"; filter.frequency.value = 120; filter.Q.value = 0.7; filter.connect(gain).connect(out); const sub = ctx.createOscillator(); const subGain = ctx.createGain(); subGain.gain.value = 0.5; sub.connect(subGain).connect(gain); // sub bypasses the filter: the stable core of the hum const saws = [-6, 6].map((cents) => { const o = ctx.createOscillator(); o.type = "sawtooth"; o.detune.value = cents; const g = ctx.createGain(); g.gain.value = 0.25; o.connect(g).connect(filter); return o; }); const oscs = [sub, ...saws]; return { start(when, freq) { sub.frequency.value = freq; for (const o of saws) o.frequency.value = freq * 2; for (const o of oscs) o.start(when); }, stop(when, release) { gain.gain.cancelScheduledValues(when - release); gain.gain.setTargetAtTime(0, when - release, release / 4); for (const o of oscs) o.stop(when + 0.1); }, set(when, { level, color, freq }) { const tc = 0.08; gain.gain.setTargetAtTime(level, when, tc); sub.frequency.setTargetAtTime(freq, when, tc); for (const o of saws) o.frequency.setTargetAtTime(freq * 2, when, tc); const c = Math.max(0, Math.min(1, color)); filter.frequency.setTargetAtTime(freq * 2 * Math.pow(2, 6 * c), when, tc); filter.Q.setTargetAtTime(0.7 + 5 * c * c, when, tc); }, dispose() { gain.disconnect(); }, }; }