// The energy hum: one drone instrument whose timbre follows the energy, with fade-in at the start // and a fade-out that ends with the piece. Shared by the compositions. import { dbToGain } from "../../core/audio.ts"; import type { Params } from "../../core/params.ts"; import { DRONE_INSTRUMENTS } from "../../instruments/index.ts"; import type { DroneInstrument } from "../../instruments/types.ts"; import { midiToFreq } from "../../music/scales.ts"; export class Hum { inst: DroneInstrument | null = null; private key = ""; /** Create the instrument (or switch it, crossfading, if the setting changed while playing). */ ensure(ctx: AudioContext, out: AudioNode, P: Params, playing: boolean) { const name = P.humInstrument as string; const key = name === "modal: custom" ? `${name}\n${P.humCustom}` : name; if (this.inst && this.key === key) return; const old = this.inst; this.key = key; const h = DRONE_INSTRUMENTS[name](ctx, out, { custom: P.humCustom as string }); this.inst = h; if (old && playing) { const t = ctx.currentTime; old.stop(t + 0.5, 0.5); window.setTimeout(() => old.dispose(), 1500); void (h.ready ?? Promise.resolve()).then(() => h === this.inst && h.start(ctx.currentTime + 0.05, midiToFreq(P.humPitch as number))); } } get ready(): Promise | undefined { return this.inst?.ready; } start(ctx: AudioContext, P: Params) { this.inst?.start(ctx.currentTime + 0.05, midiToFreq(P.humPitch as number)); } /** * e: normalized energy now; t: piece time; end: piece time everything has faded, or null while * still running (the fade-out only begins once the end is known). */ update(ctx: AudioContext, P: Params, e: number, t: number, end: number | null) { if (!this.inst) return; const peak = P.energyPeak as number; const rel = Math.max(0, Math.min(1, e / peak)); const e0 = Math.min(P.energyStart as number, 0.999 * peak); const colorBase = P.humColorMode === "log" ? Math.max(0, Math.min(1, Math.log(Math.max(e, 1e-12) / e0) / Math.log(peak / e0))) : rel; const fadeIn = P.humFadeIn as number; const fadeOut = P.humFadeOut as number; const fade = Math.min(1, fadeIn > 0 ? t / fadeIn : 1, end !== null && fadeOut > 0 ? (end - t) / fadeOut : 1); const follow = P.humFollow as number; this.inst.set(ctx.currentTime, { level: dbToGain(P.humLevel as number) * Math.max(0, fade) * (1 - follow + follow * rel), color: Math.pow(colorBase, P.humColorCurve as number), freq: midiToFreq(P.humPitch as number), normalize: P.humNormalize as boolean, timbre: [1, 2] .filter((i) => P[`humTarget${i}`] !== "none") .map((i) => ({ target: P[`humTarget${i}`] as string, from: P[`humFrom${i}`] as number, to: P[`humTo${i}`] as number })), }); } describe(): string { return this.inst?.describe?.() || "–"; } /** Release: fade out if it was sounding, then dispose. */ release(ctx: AudioContext, wasPlaying: boolean) { const h = this.inst; if (!h) return; if (wasPlaying) h.stop(ctx.currentTime + 0.3, 0.3); // a hum is only started once playing window.setTimeout(() => h.dispose(), 600); this.inst = null; this.key = ""; } }