SOURCE / PINNED RELEASE
Made of little things.
Murder Mice
- Release
- 5ffb58a0d53b…
- Author-recorded commit
- ca04e97f1dd7…
- License
- LICENSE
- Author’s source reference
- nostr://npub1ny7l44q0au5xkv3mjgt6zks5f0e2ydefslq4nhh2ve2xypgc2mqqat8qx2/wss%3A%2F%2Fgit.napplet.soy%2F/n-26115e0d25b
Archive hash verified: 6db6fc213baddb85…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
// Sound effects: gunshots, explosions and mouse hits. The sounds are CC0 files listed in CREDITS.md.
import { assetBlob } from '../soy-assets.js';
export type SoundId =
| 'rifle-shot'
| 'revolver-shot'
| 'shotgun-shot'
| 'rocket-launch'
| 'explosion'
| 'beam-fire'
| 'splat'
| 'sizzle'
| 'squeak-pain'
| 'squeak-attack'
| 'squeak-death';
const ALL: SoundId[] = [
'rifle-shot',
'revolver-shot',
'shotgun-shot',
'rocket-launch',
'explosion',
'beam-fire',
'splat',
'sizzle',
'squeak-pain',
'squeak-attack',
'squeak-death',
];
/** Make a short "room" echo: quick reflections that fade away over about half a second. */
function roomEcho(ctx: AudioContext): AudioBuffer {
const seconds = 0.6;
const buffer = ctx.createBuffer(2, Math.round(ctx.sampleRate * seconds), ctx.sampleRate);
for (let c = 0; c < 2; c++) {
const data = buffer.getChannelData(c);
for (let i = 0; i < data.length; i++) {
const t = i / ctx.sampleRate;
data[i] = (Math.random() * 2 - 1) * Math.pow(1 - t / seconds, 3) * 0.5;
}
}
return buffer;
}
export function createSfx() {
let ctx: AudioContext | null = null;
let out: GainNode;
/** A little room echo, so gunshots sound like they're in a real room. */
let room: ConvolverNode;
const sounds = new Map<SoundId, AudioBuffer>();
/** When each sound last started, so lots of hits at once don't pile up into a roar. */
const lastPlayed = new Map<SoundId, number>();
/** Make the sound player and load every sound. Call this after a click or key press. */
function setup(): void {
if (ctx) return;
ctx = new AudioContext();
out = ctx.createGain();
out.gain.value = 0.6;
// A "squeezer" (compressor) makes loud bangs punchy instead of thin.
const punch = ctx.createDynamicsCompressor();
punch.threshold.value = -20;
punch.knee.value = 6;
punch.ratio.value = 6;
punch.attack.value = 0.002;
punch.release.value = 0.15;
out.connect(punch).connect(ctx.destination);
room = ctx.createConvolver();
room.buffer = roomEcho(ctx);
const roomLevel = ctx.createGain();
roomLevel.gain.value = 0.35;
room.connect(roomLevel).connect(out);
for (const id of ALL) {
assetBlob(id)
.then((blob) => blob.arrayBuffer())
.then((bytes) => ctx!.decodeAudioData(bytes))
.then((buffer) => sounds.set(id, buffer))
.catch(() => undefined); // A missing sound just stays silent.
}
}
/** Play a sound. `speed` above 1 makes it higher and faster, below 1 lower and slower. */
function play(id: SoundId, speed = 1, volume = 1, echo = false): void {
const buffer = sounds.get(id);
if (!ctx || !buffer) return;
const now = ctx.currentTime;
if (now - (lastPlayed.get(id) ?? -1) < 0.04) return;
lastPlayed.set(id, now);
const src = ctx.createBufferSource();
src.buffer = buffer;
src.playbackRate.value = speed;
const gain = ctx.createGain();
gain.gain.value = volume;
src.connect(gain).connect(out);
if (echo) gain.connect(room);
src.start();
}
/** White noise, for the sharp "crack" at the start of a shot. */
let noise: AudioBuffer | null = null;
/**
* The parts a gun recording made from far away is missing: a deep "boom" you can feel
* and a sharp "crack" right at the start. `size` is how big the gun is (shotgun biggest).
*/
function bang(size: number): void {
if (!ctx) return;
const t = ctx.currentTime;
// Boom: a low thump that drops in pitch, like a kick drum.
const boom = ctx.createOscillator();
boom.frequency.setValueAtTime(160, t);
boom.frequency.exponentialRampToValueAtTime(38, t + 0.18 * size);
const boomLevel = ctx.createGain();
boomLevel.gain.setValueAtTime(0, t);
boomLevel.gain.linearRampToValueAtTime(0.9, t + 0.003);
boomLevel.gain.exponentialRampToValueAtTime(0.0001, t + 0.35 * size);
boom.connect(boomLevel).connect(out);
boom.start(t);
boom.stop(t + 0.4 * size);
// Crack: a very short burst of hiss.
if (!noise) {
noise = ctx.createBuffer(1, Math.round(ctx.sampleRate * 0.2), ctx.sampleRate);
const d = noise.getChannelData(0);
for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1;
}
const crack = ctx.createBufferSource();
crack.buffer = noise;
const bright = ctx.createBiquadFilter();
bright.type = 'highpass';
bright.frequency.value = 1500;
const crackLevel = ctx.createGain();
crackLevel.gain.setValueAtTime(0.7, t);
crackLevel.gain.exponentialRampToValueAtTime(0.0001, t + 0.05 + 0.03 * size);
crack.connect(bright).connect(crackLevel).connect(out);
crackLevel.connect(room);
crack.start(t);
crack.stop(t + 0.2);
}
/** Real guns sound the same every shot, so gunshots only change pitch a tiny bit. */
const shotVary = () => 0.985 + Math.random() * 0.03;
/** A little random change in pitch so the same sound doesn't get boring. */
const vary = (speed: number) => speed * (0.92 + Math.random() * 0.16);
return {
setup,
pause(): void {
void ctx?.suspend();
},
resume(): void {
void ctx?.resume();
},
/** The sound each gun makes when it fires. */
shot(weapon: 'rifle' | 'revolver' | 'shotgun' | 'rocket' | 'beam'): void {
if (weapon === 'rifle') {
play('rifle-shot', shotVary(), 1, true);
bang(1);
} else if (weapon === 'revolver') {
play('revolver-shot', shotVary(), 1, true);
bang(0.8);
} else if (weapon === 'shotgun') {
play('shotgun-shot', shotVary(), 1, true);
bang(1.4);
}
else if (weapon === 'rocket') play('rocket-launch', vary(1), 0.8);
else play('beam-fire', 1, 0.7);
},
/** The sound when a mouse gets hit, different for each gun. */
hit(weapon: 'rifle' | 'revolver' | 'shotgun' | 'rocket' | 'beam'): void {
if (weapon === 'rifle') {
play('splat', vary(1));
play('squeak-pain', vary(1));
} else if (weapon === 'revolver') {
play('splat', vary(1.25), 0.8);
play('squeak-attack', vary(1.1));
} else if (weapon === 'shotgun') {
play('splat', vary(0.8));
play('squeak-death', vary(1));
} else if (weapon === 'rocket') {
play('squeak-death', vary(1.2), 0.8);
} else {
play('sizzle', vary(1.2), 0.8);
play('squeak-pain', vary(1.5));
}
},
explosion(): void {
play('explosion', vary(0.9), 1);
},
};
}
