210 lines
9.0 KiB
JavaScript
210 lines
9.0 KiB
JavaScript
// MexicanTrainSound.js — tiny Web Audio synths for the kitchen table. No audio
|
|
// files; silent without Web Audio or when the game is muted.
|
|
//
|
|
// clack(scene, {k}) a domino set down on the oak (k varies the pitch a hair)
|
|
// rackTick(scene) a tile nudged on the wooden rack
|
|
// wash(scene, secs) the face-down tiles swirled ("washing the bones")
|
|
// whistle(scene) the little toy-train toot (a train opens / closes)
|
|
// knockKnock(scene) two knuckle raps on the table
|
|
// pencil(scene, secs) a pencil scratching on the score pad
|
|
// chime(scene) someone goes out
|
|
// Ambience birds outside, the kitchen clock, a far-off mower
|
|
|
|
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.004 } = {}) {
|
|
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 burst(ac, dest, t0, f, { q = 2, vol = 1, d = 0.03, type = 'bandpass' } = {}) {
|
|
const n = ac.createBufferSource(); n.buffer = noise(ac);
|
|
const bp = ac.createBiquadFilter(); bp.type = type; 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 */ } };
|
|
|
|
export function clack(scene, { k = 0, vol = 0.22 } = {}) {
|
|
const ac = ctxOf(scene); if (!ac) return;
|
|
safe(() => {
|
|
const dest = out(scene, ac, vol), t = ac.currentTime + 0.003;
|
|
const f = 2300 + ((k * 137) % 5) * 140 + Math.random() * 120;
|
|
burst(ac, dest, t, f, { q: 3.5, vol: 1, d: 0.045 });
|
|
burst(ac, dest, t + 0.012, f * 1.6, { q: 5, vol: 0.4, d: 0.03 });
|
|
tone(ac, dest, t, 180, { type: 'sine', a: 0.6, d: 0.07, glide: 0.6 }); // the wood underneath
|
|
});
|
|
}
|
|
|
|
export function rackTick(scene, { vol = 0.1 } = {}) {
|
|
const ac = ctxOf(scene); if (!ac) return;
|
|
safe(() => {
|
|
const dest = out(scene, ac, vol), t = ac.currentTime + 0.003;
|
|
burst(ac, dest, t, 3200 + Math.random() * 400, { q: 4, d: 0.025 });
|
|
tone(ac, dest, t, 300, { a: 0.3, d: 0.04, glide: 0.7 });
|
|
});
|
|
}
|
|
|
|
export function wash(scene, secs = 1.6, { vol = 0.16 } = {}) {
|
|
const ac = ctxOf(scene); if (!ac) return;
|
|
safe(() => {
|
|
const dest = out(scene, ac, vol), t0 = ac.currentTime + 0.01;
|
|
// a bed of sliding hiss…
|
|
const n = ac.createBufferSource(); n.buffer = noise(ac); n.loop = true;
|
|
const bp = ac.createBiquadFilter(); bp.type = 'bandpass'; bp.frequency.value = 1800; bp.Q.value = 0.8;
|
|
const g = ac.createGain();
|
|
g.gain.setValueAtTime(0, t0);
|
|
g.gain.linearRampToValueAtTime(0.25, t0 + 0.2);
|
|
g.gain.setValueAtTime(0.25, t0 + secs - 0.3);
|
|
g.gain.linearRampToValueAtTime(0, t0 + secs);
|
|
n.connect(bp); bp.connect(g); g.connect(dest); n.start(t0); n.stop(t0 + secs + 0.05);
|
|
// …and the tiles knocking into each other
|
|
const clicks = Math.round(secs * 34);
|
|
for (let i = 0; i < clicks; i++) {
|
|
const t = t0 + Math.random() * secs;
|
|
burst(ac, dest, t, 2000 + Math.random() * 2400, { q: 4, vol: 0.25 + Math.random() * 0.5, d: 0.025 });
|
|
}
|
|
});
|
|
}
|
|
|
|
export function whistle(scene, { vol = 0.06 } = {}) {
|
|
const ac = ctxOf(scene); if (!ac) return;
|
|
safe(() => {
|
|
const dest = out(scene, ac, vol), t = ac.currentTime + 0.01;
|
|
for (const [f, dt, d] of [[880, 0, 0.18], [1108, 0, 0.18], [880, 0.22, 0.3], [1108, 0.22, 0.3]]) {
|
|
const o = ac.createOscillator(); o.type = 'triangle';
|
|
o.frequency.setValueAtTime(f * 0.97, t + dt); o.frequency.linearRampToValueAtTime(f, t + dt + 0.05);
|
|
const lfo = ac.createOscillator(); lfo.frequency.value = 7;
|
|
const lg = ac.createGain(); lg.gain.value = 6;
|
|
lfo.connect(lg); lg.connect(o.frequency);
|
|
const g = ac.createGain();
|
|
g.gain.setValueAtTime(0, t + dt); g.gain.linearRampToValueAtTime(0.5, t + dt + 0.03);
|
|
g.gain.setValueAtTime(0.5, t + dt + d - 0.05); g.gain.linearRampToValueAtTime(0, t + dt + d);
|
|
o.connect(g); g.connect(dest); o.start(t + dt); lfo.start(t + dt); o.stop(t + dt + d + 0.02); lfo.stop(t + dt + d + 0.02);
|
|
}
|
|
burst(ac, dest, t, 4000, { q: 0.6, vol: 0.15, d: 0.5, type: 'highpass' });
|
|
});
|
|
}
|
|
|
|
export function knockKnock(scene, { vol = 0.35 } = {}) {
|
|
const ac = ctxOf(scene); if (!ac) return;
|
|
safe(() => {
|
|
const dest = out(scene, ac, vol), t = ac.currentTime + 0.01;
|
|
for (const dt of [0, 0.17]) {
|
|
tone(ac, dest, t + dt, 150, { a: 1, d: 0.12, glide: 0.7 });
|
|
burst(ac, dest, t + dt, 900, { q: 1.5, vol: 0.7, d: 0.05 });
|
|
}
|
|
});
|
|
}
|
|
|
|
export function pencil(scene, secs = 0.6, { vol = 0.08 } = {}) {
|
|
const ac = ctxOf(scene); if (!ac) return;
|
|
safe(() => {
|
|
const dest = out(scene, ac, vol), t0 = ac.currentTime + 0.01;
|
|
const strokes = Math.max(2, Math.round(secs * 7));
|
|
for (let i = 0; i < strokes; i++) {
|
|
const t = t0 + (i / strokes) * secs;
|
|
burst(ac, dest, t, 5200 + Math.random() * 1600, { q: 1.2, vol: 0.8, d: 0.08 + Math.random() * 0.05 });
|
|
}
|
|
});
|
|
}
|
|
|
|
export function chime(scene, { vol = 0.07 } = {}) {
|
|
const ac = ctxOf(scene); if (!ac) return;
|
|
safe(() => {
|
|
const dest = out(scene, ac, vol), t = ac.currentTime + 0.01;
|
|
for (const [f, a, d, dt] of [[660, 1, 1.4, 0], [831, 0.8, 1.2, 0.1], [990, 0.7, 1.1, 0.2], [1320, 0.4, 1.0, 0.3]]) tone(ac, dest, t + dt, f, { a, d });
|
|
});
|
|
}
|
|
|
|
export function thud(scene, { vol = 0.25 } = {}) {
|
|
const ac = ctxOf(scene); if (!ac) return;
|
|
safe(() => {
|
|
const dest = out(scene, ac, vol), t = ac.currentTime + 0.005;
|
|
tone(ac, dest, t, 120, { a: 1, d: 0.4, glide: 0.5 });
|
|
});
|
|
}
|
|
|
|
/** Afternoon sounds through the open window. Each layer can be switched. */
|
|
export class Ambience {
|
|
constructor(scene) { this.scene = scene; this.timers = []; this.mower = null; this.on = { birds: false, clock: false }; }
|
|
set(layer, on) {
|
|
if (this.on[layer] === on) return;
|
|
this.on[layer] = on;
|
|
this.restart();
|
|
}
|
|
restart() {
|
|
this.stop();
|
|
const s = this.scene;
|
|
if (this.on.clock) {
|
|
let tock = false;
|
|
this.timers.push(setInterval(() => {
|
|
const ac = ctxOf(s); if (!ac) return;
|
|
safe(() => { const d = out(s, ac, 0.018), t = ac.currentTime + 0.01; burst(ac, d, t, tock ? 2600 : 3300, { q: 6, d: 0.03 }); tock = !tock; });
|
|
}, 1000));
|
|
}
|
|
if (this.on.birds) {
|
|
const chirp = () => {
|
|
const ac = ctxOf(s); if (!ac) return;
|
|
safe(() => {
|
|
const d = out(s, ac, 0.02), t = ac.currentTime + 0.02;
|
|
const base = 2600 + Math.random() * 1800, notes = 2 + Math.floor(Math.random() * 4);
|
|
for (let i = 0; i < notes; i++) tone(ac, d, t + i * 0.09, base * (1 + (Math.random() - 0.5) * 0.25), { a: 0.8, d: 0.07, glide: 1.3 + Math.random() * 0.4 });
|
|
});
|
|
};
|
|
this.timers.push(setInterval(() => { if (Math.random() < 0.45) chirp(); }, 1900));
|
|
this.startMower();
|
|
}
|
|
}
|
|
startMower() {
|
|
const ac = ctxOf(this.scene); if (!ac) return;
|
|
try {
|
|
const src = ac.createBufferSource(); src.buffer = noise(ac); src.loop = true;
|
|
const lp = ac.createBiquadFilter(); lp.type = 'lowpass'; lp.frequency.value = 320; lp.Q.value = 3;
|
|
const g = ac.createGain(); g.gain.value = 0;
|
|
const lfo = ac.createOscillator(); lfo.frequency.value = 0.05; // comes and goes
|
|
const lg = ac.createGain(); lg.gain.value = 0.006;
|
|
const bias = ac.createConstantSource ? ac.createConstantSource() : null;
|
|
lfo.connect(lg); lg.connect(g.gain);
|
|
if (bias) { bias.offset.value = 0.006; bias.connect(g.gain); bias.start(); }
|
|
const base = out(this.scene, ac, 1);
|
|
src.connect(lp); lp.connect(g); g.connect(base);
|
|
src.start(); lfo.start();
|
|
this.mower = { src, lfo, bias, base };
|
|
} catch (_) { this.mower = null; }
|
|
}
|
|
stop() {
|
|
this.timers.forEach(clearInterval);
|
|
this.timers = [];
|
|
if (this.mower) {
|
|
const m = this.mower; this.mower = null;
|
|
try { m.src.stop(); m.lfo.stop(); m.bias?.stop(); m.base.disconnect(); } catch (_) { /* gone */ }
|
|
}
|
|
}
|
|
}
|