// Formant-synthesized cute meow: saw + breath noise through gliding bandpass formants. import { getCtx, getSfxBus, getNoiseBuffer, trackVoice, canPlay, rnd, clamp } from './context'; import type { Mood, PlayOpts } from './types'; interface Unit { at: number; dur: number; gain: number; contour: [number, number][]; // [relative time 0..1, pitch multiplier] f1: [number, number]; f2: [number, number]; } const MOODS: Record = { happy: [{ at: 0, dur: 0.55, gain: 1, contour: [[0, 0.85], [0.3, 1.25], [0.7, 1.05], [1, 0.8]], f1: [380, 800], f2: [2300, 1200] }], tease: [ { at: 0, dur: 0.2, gain: 0.9, contour: [[0, 0.95], [0.5, 1.2], [1, 1.05]], f1: [450, 650], f2: [1800, 1500] }, { at: 0.27, dur: 0.22, gain: 0.85, contour: [[0, 1], [0.5, 1.3], [1, 1.1]], f1: [450, 650], f2: [1800, 1500] }, ], sleepy: [{ at: 0, dur: 1.05, gain: 0.8, contour: [[0, 0.8], [0.35, 1], [1, 0.55]], f1: [350, 650], f2: [1900, 1000] }], surprised: [{ at: 0, dur: 0.3, gain: 1.1, contour: [[0, 1], [1, 1.9]], f1: [450, 750], f2: [2400, 1900] }], proud: [{ at: 0, dur: 0.95, gain: 0.95, contour: [[0, 0.85], [0.45, 1.1], [1, 0.9]], f1: [400, 850], f2: [2200, 1100] }], }; function playUnit(f0: number, size: number, vol: number, pan: number, u: Unit): void { const c = getCtx(), bus = getSfxBus(), nb = getNoiseBuffer(); if (!c || !bus || !nb) return; const t = c.currentTime + 0.005 + u.at; const end = t + u.dur + 0.05; const nodes: AudioNode[] = []; const osc = c.createOscillator(); osc.type = 'sawtooth'; osc.frequency.setValueAtTime(f0 * u.contour[0][1], t); for (let i = 1; i < u.contour.length; i++) osc.frequency.linearRampToValueAtTime(f0 * u.contour[i][1], t + u.dur * u.contour[i][0]); const vib = c.createOscillator(); vib.frequency.value = rnd(5, 6.5); const vg = c.createGain(); vg.gain.value = 14; vib.connect(vg); vg.connect(osc.detune); const nz = c.createBufferSource(); nz.buffer = nb; nz.loop = true; const ng = c.createGain(); ng.gain.value = 0.12; nz.connect(ng); const src = c.createGain(); osc.connect(src); ng.connect(src); const sum = c.createGain(); const peak = 0.6 * vol * u.gain; sum.gain.setValueAtTime(0.0001, t); sum.gain.linearRampToValueAtTime(peak, t + Math.min(0.05, u.dur * 0.2)); sum.gain.linearRampToValueAtTime(peak * 0.7, t + u.dur * 0.75); sum.gain.exponentialRampToValueAtTime(0.0001, t + u.dur); const formants: [number, number, number, number][] = [ [u.f1[0] * size, u.f1[1] * size, 6, 1], [u.f2[0] * size, u.f2[1] * size, 8, 0.7], [2900 * size, 2900 * size, 10, 0.3], ]; for (const [a, b, q, g] of formants) { const bp = c.createBiquadFilter(); bp.type = 'bandpass'; bp.Q.value = q; bp.frequency.setValueAtTime(a, t); bp.frequency.linearRampToValueAtTime(b, t + u.dur * 0.85); const bg = c.createGain(); bg.gain.value = g; src.connect(bp); bp.connect(bg); bg.connect(sum); nodes.push(bp, bg); } let tail: AudioNode = sum; if (pan && typeof c.createStereoPanner === 'function') { const p = c.createStereoPanner(); p.pan.value = pan; sum.connect(p); tail = p; nodes.push(p); } tail.connect(bus); nodes.push(vg, ng, src, sum); trackVoice(nz, [], true); trackVoice(vib, [], true); trackVoice(osc, nodes, true); vib.start(t); nz.start(t, Math.random()); osc.start(t); vib.stop(end); nz.stop(end); osc.stop(end); } /** voice 0..1: 0 = big low cat (~300 Hz), 1 = tiny kitten (~900 Hz). */ export function playMeow(voice: number, mood: Mood = 'happy', o: PlayOpts = {}): void { const ctx = getCtx(); if (!ctx || ctx.state !== 'running' || !canPlay()) return; const v = clamp(voice, 0, 1); const f0 = 300 * Math.pow(3, v) * (o.pitch ?? 1) * (1 + (Math.random() - 0.5) * 0.06); const size = 0.9 + 0.3 * v; const vol = 0.8 * clamp(o.volume ?? 1, 0, 1); const pan = clamp(o.pan ?? 0, -1, 1); const stretch = 1 + (Math.random() - 0.5) * 0.1; for (const u of MOODS[mood]) playUnit(f0, size, vol, pan, { ...u, dur: u.dur * stretch, at: u.at * stretch }); }