// ChineseCheckersSound.js — tiny Web Audio synths for the tea pavilion. No // audio files; silent without Web Audio or when muted. // // clack(scene, { k, jump }) a glass marble landing in a wooden hole; chain // landings walk up a pentatonic scale (k = hop no.) // tap(scene) picking a marble up // deny(scene) that marble can't move // chime(scene) a point filled (small bronze bell chord) // gong(scene) the game is decided // sparkle(scene) a marble reaches its target // WindChimes a soft ambient chime now and then function ctxOf(scene) { const sm = scene?.sound; if (!sm || sm.mute || !sm.context || typeof sm.context.createOscillator !== 'function') return null; return sm.context; } const noiseCache = new WeakMap(); function noise(ac) { if (noiseCache.has(ac)) return noiseCache.get(ac); const buf = ac.createBuffer(1, Math.ceil(ac.sampleRate * 1.2), ac.sampleRate); const d = buf.getChannelData(0); for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1; noiseCache.set(ac, buf); return buf; } function out(scene, ac, gain) { const sm = scene.sound; const node = sm.masterVolumeNode ?? sm.destination ?? null; const g = ac.createGain(); g.gain.value = node ? gain : gain * (sm.volume ?? 1); g.connect(node ?? ac.destination); return g; } function tone(ac, dest, t0, f, { type = 'sine', a = 1, d = 0.2, glide = 1, attack = 0.003 } = {}) { const o = ac.createOscillator(); o.type = type; o.frequency.setValueAtTime(f, t0); if (glide !== 1) o.frequency.exponentialRampToValueAtTime(f * glide, t0 + d); const g = ac.createGain(); g.gain.setValueAtTime(0, t0); g.gain.linearRampToValueAtTime(a, t0 + attack); g.gain.exponentialRampToValueAtTime(0.0005, t0 + d); o.connect(g); g.connect(dest); o.start(t0); o.stop(t0 + d + 0.03); } function tick(ac, dest, t0, f, vol = 1, d = 0.03) { const n = ac.createBufferSource(); n.buffer = noise(ac); const bp = ac.createBiquadFilter(); bp.type = 'bandpass'; bp.frequency.value = f; bp.Q.value = 3; const g = ac.createGain(); g.gain.setValueAtTime(vol, t0); g.gain.exponentialRampToValueAtTime(0.0005, t0 + d); n.connect(bp); bp.connect(g); g.connect(dest); n.start(t0, Math.random()); n.stop(t0 + d + 0.02); } const safe = (fn) => { try { fn(); } catch (_) { /* audio is decoration */ } }; // Major pentatonic steps (semitones) for chain landings. const PENTA = [0, 2, 4, 7, 9, 12, 14, 16, 19, 21, 24, 26, 28, 31, 33, 36]; const semi = (f, n) => f * Math.pow(2, n / 12); export function clack(scene, { k = 0, jump = false, vol = 0.09 } = {}) { const ac = ctxOf(scene); if (!ac) return; safe(() => { const dest = out(scene, ac, vol), t = ac.currentTime + 0.003; // the wood thock tone(ac, dest, t, 190, { type: 'sine', a: 0.7, d: 0.07, glide: 0.6 }); tick(ac, dest, t, 2600, 0.6); // the glass ring — musical on chains const f = jump ? semi(784, PENTA[Math.min(k, PENTA.length - 1)]) : 1900; tone(ac, dest, t + 0.002, f, { type: 'triangle', a: jump ? 0.45 : 0.25, d: jump ? 0.22 : 0.06 }); if (jump) tone(ac, dest, t + 0.002, f * 2.01, { a: 0.12, d: 0.14 }); }); } export function tap(scene) { const ac = ctxOf(scene); if (!ac) return; safe(() => { const dest = out(scene, ac, 0.05), t = ac.currentTime + 0.003; tick(ac, dest, t, 4200, 0.7, 0.02); tone(ac, dest, t, 2400, { type: 'triangle', a: 0.3, d: 0.05 }); }); } export function deny(scene) { const ac = ctxOf(scene); if (!ac) return; safe(() => { const dest = out(scene, ac, 0.08), t = ac.currentTime + 0.003; tone(ac, dest, t, 160, { a: 0.8, d: 0.12, glide: 0.7 }); tick(ac, dest, t, 900, 0.5); }); } export function sparkle(scene, { vol = 0.05 } = {}) { const ac = ctxOf(scene); if (!ac) return; safe(() => { const dest = out(scene, ac, vol), t = ac.currentTime + 0.01; [0, 4, 7, 12].forEach((n, i) => tone(ac, dest, t + i * 0.05, semi(1568, n), { a: 0.6, d: 0.5 })); }); } export function chime(scene, { vol = 0.08 } = {}) { const ac = ctxOf(scene); if (!ac) return; safe(() => { const dest = out(scene, ac, vol), t = ac.currentTime + 0.01; // a small bronze bell: inharmonic partials for (const [m, a, d, dt] of [[1, 1, 2.2, 0], [2.76, 0.5, 1.4, 0], [5.4, 0.25, 0.8, 0], [1.5, 0.6, 1.8, 0.18], [4.1, 0.2, 0.7, 0.18]]) { tone(ac, dest, t + dt, 523 * m, { a, d }); } }); } export function gong(scene, { vol = 0.18 } = {}) { const ac = ctxOf(scene); if (!ac) return; safe(() => { const dest = out(scene, ac, vol), t = ac.currentTime + 0.01; for (const [f, a, d] of [[98, 1, 3.4], [147.5, 0.5, 2.8], [216, 0.35, 2.2], [302, 0.2, 1.6]]) { tone(ac, dest, t, f, { a, d, attack: 0.02, glide: 0.985 }); } tick(ac, dest, t, 600, 0.6, 0.12); }); } export class WindChimes { constructor(scene) { this.scene = scene; this.timer = null; } start() { if (this.timer) return; const ring = () => { this.timer = setTimeout(ring, 5000 + Math.random() * 7000); const ac = ctxOf(this.scene); if (!ac) return; safe(() => { const dest = out(this.scene, ac, 0.022), t = ac.currentTime + 0.02; const notes = [0, 2, 4, 7, 9, 12, 14]; const n = 2 + Math.floor(Math.random() * 3); for (let i = 0; i < n; i++) { const f = semi(1046, notes[Math.floor(Math.random() * notes.length)]); tone(ac, dest, t + i * (0.12 + Math.random() * 0.25), f, { a: 0.8, d: 2.4, attack: 0.004 }); } }); }; this.timer = setTimeout(ring, 2500); } stop() { clearTimeout(this.timer); this.timer = null; } setOn(on) { if (on) this.start(); else this.stop(); } }