fertig-classic-games/src/games/checkers/CheckersSound.js

194 lines
7.8 KiB
JavaScript

// CheckersSound.js — little Web Audio synths for the back room. No audio
// files; silent without Web Audio or when muted.
//
// clack(scene, { k }) a checker landing on the board; the k-th hop of a
// multi-jump rings a step higher
// pick(scene) lifting a checker
// deny(scene) that one can't move
// flick(scene) a captured checker skittering off into the tray
// crown(scene) a checker stacked on top — the double clack
// sting(scene, kind) 'win' (a barbershop-quartet chord), 'lose', 'draw'
// Ambience rain on the window, and now and then a far-off
// radio murmur and a percolator burble
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 * 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, vib = 0 } = {}) {
const o = ac.createOscillator(); o.type = type;
o.frequency.setValueAtTime(f, t0);
if (glide !== 1) o.frequency.exponentialRampToValueAtTime(f * glide, t0 + d);
if (vib) {
const lfo = ac.createOscillator(); lfo.frequency.value = 5.2;
const lg = ac.createGain(); lg.gain.value = f * vib;
lfo.connect(lg); lg.connect(o.frequency); lfo.start(t0); lfo.stop(t0 + d + 0.05);
}
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, q = 3) {
const n = ac.createBufferSource(); n.buffer = noise(ac);
const bp = ac.createBiquadFilter(); bp.type = 'bandpass'; bp.frequency.value = f; bp.Q.value = q;
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 */ } };
const semi = (f, n) => f * Math.pow(2, n / 12);
const MAJOR = [0, 2, 4, 5, 7, 9, 11, 12, 14, 16];
export function clack(scene, { k = 0, vol = 0.12 } = {}) {
const ac = ctxOf(scene); if (!ac) return;
safe(() => {
const dest = out(scene, ac, vol), t = ac.currentTime + 0.003;
// hard composition on painted wood: a dull thock and a bright knock
tone(ac, dest, t, semi(170, MAJOR[Math.min(k, MAJOR.length - 1)]), { a: 0.9, d: 0.09, glide: 0.7 });
tick(ac, dest, t, 1800 + k * 160, 0.9, 0.035, 2);
tick(ac, dest, t + 0.002, 4200, 0.35, 0.015);
});
}
export function pick(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, 3200, 0.8, 0.02);
tone(ac, dest, t, 520, { a: 0.3, d: 0.04, glide: 1.3 });
});
}
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, 150, { a: 0.8, d: 0.12, glide: 0.7 });
tick(ac, dest, t, 700, 0.5);
});
}
export function flick(scene) {
const ac = ctxOf(scene); if (!ac) return;
safe(() => {
const dest = out(scene, ac, 0.07), t = ac.currentTime + 0.003;
// skitter across the barrel head, then a drop into the pile
for (let i = 0; i < 4; i++) tick(ac, dest, t + i * 0.045, 2400 - i * 250, 0.6 - i * 0.12, 0.02);
tone(ac, dest, t + 0.22, 260, { a: 0.6, d: 0.08, glide: 0.75 });
tick(ac, dest, t + 0.22, 1500, 0.6, 0.03);
});
}
export function crown(scene) {
const ac = ctxOf(scene); if (!ac) return;
safe(() => {
const dest = out(scene, ac, 0.13), t = ac.currentTime + 0.003;
tone(ac, dest, t, 220, { a: 0.9, d: 0.08, glide: 0.75 }); tick(ac, dest, t, 2000, 0.9, 0.03);
tone(ac, dest, t + 0.11, 260, { a: 0.9, d: 0.1, glide: 0.75 }); tick(ac, dest, t + 0.11, 2300, 0.9, 0.03);
// a little bell from the shop door
tone(ac, dest, t + 0.16, 1568, { type: 'triangle', a: 0.25, d: 0.9 });
tone(ac, dest, t + 0.16, 1568 * 2.76, { a: 0.06, d: 0.5 });
});
}
export function sting(scene, kind = 'win') {
const ac = ctxOf(scene); if (!ac) return;
safe(() => {
const dest = out(scene, ac, 0.07), t = ac.currentTime + 0.02;
const voice = (f, at, d) => {
tone(ac, dest, t + at, f, { type: 'sawtooth', a: 0.12, d, attack: 0.08, vib: 0.006 });
tone(ac, dest, t + at, f, { type: 'triangle', a: 0.5, d, attack: 0.06, vib: 0.006 });
};
if (kind === 'win') {
// the barbershop "ring": a dominant seventh resolving to the tonic
[196, 247, 294, 349].forEach((f) => voice(f, 0, 0.9));
[196, 262, 330, 392].forEach((f) => voice(f, 0.75, 1.8));
} else if (kind === 'lose') {
[220, 262, 330].forEach((f) => voice(f, 0, 0.7));
[196, 233, 294].forEach((f) => voice(f, 0.6, 1.4));
} else {
[220, 277, 330].forEach((f) => voice(f, 0, 0.6));
[220, 294, 370].forEach((f) => voice(f, 0.5, 1.2));
}
});
}
export class Ambience {
constructor(scene) { this.scene = scene; this.on = false; this.timer = null; this.rain = null; }
start() {
if (this.on) return;
this.on = true;
const ac = ctxOf(this.scene);
if (ac) {
safe(() => {
// steady rain: band-limited noise, slowly breathing
const src = ac.createBufferSource(); src.buffer = noise(ac); src.loop = true;
const hp = ac.createBiquadFilter(); hp.type = 'highpass'; hp.frequency.value = 900;
const lp = ac.createBiquadFilter(); lp.type = 'lowpass'; lp.frequency.value = 5200;
const g = out(this.scene, ac, 0.0);
g.gain.setValueAtTime(0, ac.currentTime);
g.gain.linearRampToValueAtTime(0.018, ac.currentTime + 2.5);
const lfo = ac.createOscillator(); lfo.frequency.value = 0.07;
const lg = ac.createGain(); lg.gain.value = 0.006;
lfo.connect(lg); lg.connect(g.gain);
src.connect(hp); hp.connect(lp); lp.connect(g);
src.start(); lfo.start();
this.rain = { src, lfo, g };
});
}
const event = () => {
this.timer = setTimeout(event, 6000 + Math.random() * 9000);
const a = ctxOf(this.scene); if (!a) return;
safe(() => {
const dest = out(this.scene, a, 0.02), t = a.currentTime + 0.02;
if (Math.random() < 0.5) {
// a heavy drop off the awning
tone(a, dest, t, 900 + Math.random() * 600, { a: 0.6, d: 0.08, glide: 0.5 });
} else {
// the percolator burbles
for (let i = 0; i < 5; i++) tone(a, dest, t + i * 0.09 + Math.random() * 0.04, 300 + Math.random() * 200, { a: 0.4, d: 0.06, glide: 1.6 });
}
});
};
this.timer = setTimeout(event, 3000);
}
stop() {
this.on = false;
clearTimeout(this.timer); this.timer = null;
if (this.rain) {
const { src, lfo, g } = this.rain;
safe(() => {
const ac = src.context;
g.gain.cancelScheduledValues(ac.currentTime);
g.gain.setValueAtTime(g.gain.value, ac.currentTime);
g.gain.linearRampToValueAtTime(0, ac.currentTime + 0.6);
src.stop(ac.currentTime + 0.7); lfo.stop(ac.currentTime + 0.7);
});
this.rain = null;
}
}
setOn(on) { if (on) this.start(); else this.stop(); }
}