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.
// Smooth jazz, made by the browser's sound maker (Web Audio). No music files needed.
// Every note is scheduled on an exact beat, so each song loops with no gap or hiccup.
type Chord = { root: number; type: 'maj9' | 'm9' | '13' };
/** [beat in the bar, note length in beats] */
type Hit = [number, number];
/** [beat in the bar, semitones above the chord's root, length in beats] */
type BassNote = [number, number, number];
/** [beat in the bar, which chord note (0 = lowest, 4+ = an octave up), length in beats] */
type MelodyNote = [number, number, number];
type Song = {
name: string;
bpm: number;
beatsPerBar: number;
/** One chord per bar. The song loops after the last bar. */
chords: Chord[];
comp: Hit[];
/** Long soft chords instead of short piano hits. */
pads: boolean;
bass: BassNote[];
/** One melody pattern per bar, used in turn. */
melody: MelodyNote[][];
/** Drum beat, in beats within the bar. */
kick: number[];
snare: number[];
hat: number[];
};
// Note numbers: 60 is middle C, +1 is the next key up.
// Smooth jazz chords have extra "color" notes (9ths and 13ths) for that silky sound.
const SHAPES = { maj9: [0, 4, 7, 11, 14], m9: [0, 3, 7, 10, 14], '13': [0, 4, 10, 14, 21] };
const c = (root: number, type: Chord['type']): Chord => ({ root, type });
/** A laid-back groove: kick on 1 and the "and" of 3, snare on 2 and 4, soft hi-hats. */
const GROOVE = { kick: [0, 2.5], snare: [1, 3], hat: [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] };
export const SONGS: Song[] = [
{
name: 'Sunset Sax',
bpm: 92,
beatsPerBar: 4,
chords: [c(50, 'maj9'), c(47, 'm9'), c(52, 'm9'), c(45, '13'), c(54, 'm9'), c(47, 'm9'), c(52, 'm9'), c(45, '13')],
comp: [[0, 1.2], [1.5, 0.4], [2.75, 1]],
pads: false,
bass: [[0, 0, 0.75], [0.75, 0, 0.25], [1.5, 7, 0.5], [2.5, 12, 0.5], [3, 10, 0.5], [3.5, 7, 0.5]],
melody: [
[[0.5, 5, 1], [1.5, 6, 0.5], [2, 7, 2]],
[[0, 8, 0.75], [0.75, 7, 0.75], [1.5, 6, 2.5]],
[[0.5, 4, 0.5], [1, 5, 0.5], [1.5, 7, 1], [2.5, 6, 1.5]],
[[0, 5, 3.5]],
],
...GROOVE,
},
{
name: 'City Lights',
bpm: 100,
beatsPerBar: 4,
chords: [c(55, 'm9'), c(48, '13'), c(53, 'maj9'), c(50, 'm9'), c(55, 'm9'), c(48, '13'), c(57, 'm9'), c(50, '13')],
comp: [[0, 0.4], [0.75, 0.6], [2, 0.4], [2.75, 1]],
pads: false,
bass: [[0, 0, 0.5], [0.75, 12, 0.25], [1, 10, 0.5], [2, 7, 0.75], [2.75, 0, 0.25], [3.5, 5, 0.5]],
melody: [
[[0, 7, 0.5], [0.5, 8, 0.5], [1, 7, 1], [2.5, 5, 1.5]],
[[0.5, 6, 0.5], [1, 5, 1], [2, 4, 2]],
[[0, 9, 1.5], [1.5, 8, 0.5], [2, 7, 0.5], [2.5, 6, 1.5]],
[[0, 7, 2], [2.5, 5, 1.5]],
],
kick: [0, 1.75, 2.5],
snare: [1, 3],
hat: [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.25, 3.5],
},
{
name: 'Late Night Lounge',
bpm: 80,
beatsPerBar: 4,
chords: [c(56, 'maj9'), c(53, 'm9'), c(49, 'maj9'), c(51, '13'), c(48, 'm9'), c(53, 'm9'), c(46, 'm9'), c(51, '13')],
comp: [[0, 3.8]],
pads: true,
bass: [[0, 0, 1.5], [1.5, 7, 0.5], [2, 10, 1], [3, 7, 1]],
melody: [
[[0, 7, 3], [3, 6, 1]],
[[0, 5, 1.5], [1.5, 6, 0.5], [2, 8, 2]],
[[0.5, 7, 1], [1.5, 6, 1], [2.5, 5, 1.5]],
[[0, 6, 4]],
],
...GROOVE,
},
];
type Event = { beat: number; play: (time: number, beatSeconds: number) => void };
const freq = (note: number) => 440 * Math.pow(2, (note - 69) / 12);
export function createMusic() {
let ctx: AudioContext | null = null;
let master: GainNode;
let noise: AudioBuffer;
let muted = false;
let paused = true;
/** The song playing now, with its own volume knob so we can fade it out. */
let current: { song: Song; out: GainNode; events: Event[]; next: number; loopStart: number } | null = null;
let timer = 0;
function setup(): void {
if (ctx) return;
ctx = new AudioContext();
master = ctx.createGain();
master.gain.value = muted ? 0 : 0.2;
// A gentle low filter keeps everything soft, like music from a ceiling speaker.
const soft = ctx.createBiquadFilter();
soft.type = 'lowpass';
soft.frequency.value = 3500;
master.connect(soft).connect(ctx.destination);
noise = ctx.createBuffer(1, ctx.sampleRate, ctx.sampleRate);
const data = noise.getChannelData(0);
for (let i = 0; i < data.length; i++) data[i] = Math.random() * 2 - 1;
}
/** Electric piano: a soft tone with a bell-like shimmer that fades away. */
function piano(out: AudioNode, note: number, time: number, length: number, volume: number): void {
const a = ctx!;
const env = a.createGain();
env.gain.setValueAtTime(0, time);
env.gain.linearRampToValueAtTime(volume, time + 0.01);
env.gain.exponentialRampToValueAtTime(volume * 0.3, time + 0.4);
env.gain.setTargetAtTime(0.0001, time + length, 0.15);
env.connect(out);
for (const [mult, level] of [[1, 1], [2, 0.25], [3.01, 0.08]] as const) {
const osc = a.createOscillator();
osc.frequency.value = freq(note) * mult;
const g = a.createGain();
g.gain.value = level;
osc.connect(g).connect(env);
osc.start(time);
osc.stop(time + length + 1);
}
}
/** Soft string pad that swells in and out. */
function pad(out: AudioNode, note: number, time: number, length: number, volume: number): void {
const a = ctx!;
const env = a.createGain();
env.gain.setValueAtTime(0, time);
env.gain.linearRampToValueAtTime(volume, time + length * 0.3);
env.gain.linearRampToValueAtTime(volume * 0.7, time + length * 0.8);
env.gain.linearRampToValueAtTime(0, time + length + 0.2);
env.connect(out);
for (const detune of [-6, 6]) {
const osc = a.createOscillator();
osc.type = 'triangle';
osc.frequency.value = freq(note);
osc.detune.value = detune;
osc.connect(env);
osc.start(time);
osc.stop(time + length + 0.3);
}
}
/** Round, plucky bass. */
function bass(out: AudioNode, note: number, time: number, length: number): void {
const a = ctx!;
const osc = a.createOscillator();
osc.type = 'triangle';
osc.frequency.value = freq(note);
const env = a.createGain();
env.gain.setValueAtTime(0, time);
env.gain.linearRampToValueAtTime(0.5, time + 0.01);
env.gain.setTargetAtTime(0.0001, time + length * 0.9, 0.06);
osc.connect(env).connect(out);
osc.start(time);
osc.stop(time + length + 0.5);
}
/**
* Tenor saxophone: low and warm. No slide into the note and a dark tone, so it never sounds
* like a squeak. A gentle wobble fades in on long notes, like a real player.
*/
function sax(out: AudioNode, note: number, time: number, length: number): void {
const a = ctx!;
const pitch = freq(note - 12);
const tone = a.createBiquadFilter();
tone.type = 'lowpass';
tone.Q.value = 0.7;
tone.frequency.value = 1300;
const env = a.createGain();
env.gain.setValueAtTime(0, time);
env.gain.linearRampToValueAtTime(0.1, time + 0.09);
env.gain.setValueAtTime(0.09, time + Math.max(0.1, length - 0.1));
env.gain.linearRampToValueAtTime(0, time + length + 0.08);
tone.connect(env).connect(out);
const wobble = a.createOscillator();
wobble.frequency.value = 4.8;
const wobbleAmount = a.createGain();
wobbleAmount.gain.setValueAtTime(0, time);
wobbleAmount.gain.linearRampToValueAtTime(0, time + 0.3);
wobbleAmount.gain.linearRampToValueAtTime(8, time + Math.max(0.31, Math.min(0.9, length)));
// A soft, round tone mixed with a little reedy buzz.
for (const [type, level] of [['triangle', 0.8], ['sawtooth', 0.25]] as const) {
const osc = a.createOscillator();
osc.type = type;
osc.frequency.value = pitch;
wobble.connect(wobbleAmount).connect(osc.detune);
const g = a.createGain();
g.gain.value = level;
osc.connect(g).connect(tone);
osc.start(time);
osc.stop(time + length + 0.15);
}
wobble.start(time);
wobble.stop(time + length + 0.15);
}
/** Kick drum: a quick low thump. */
function kick(out: AudioNode, time: number): void {
const a = ctx!;
const osc = a.createOscillator();
osc.frequency.setValueAtTime(120, time);
osc.frequency.exponentialRampToValueAtTime(45, time + 0.12);
const env = a.createGain();
env.gain.setValueAtTime(0.5, time);
env.gain.exponentialRampToValueAtTime(0.0001, time + 0.3);
osc.connect(env).connect(out);
osc.start(time);
osc.stop(time + 0.32);
}
/** Snare: a soft "rim click" with a little noise, smooth-jazz style. */
function snare(out: AudioNode, time: number): void {
const a = ctx!;
const src = a.createBufferSource();
src.buffer = noise;
const band = a.createBiquadFilter();
band.type = 'bandpass';
band.frequency.value = 1800;
const env = a.createGain();
env.gain.setValueAtTime(0.12, time);
env.gain.exponentialRampToValueAtTime(0.0001, time + 0.15);
src.connect(band).connect(env).connect(out);
src.start(time, Math.random() * 0.5);
src.stop(time + 0.18);
const click = a.createOscillator();
click.frequency.value = 900;
const clickEnv = a.createGain();
clickEnv.gain.setValueAtTime(0.08, time);
clickEnv.gain.exponentialRampToValueAtTime(0.0001, time + 0.03);
click.connect(clickEnv).connect(out);
click.start(time);
click.stop(time + 0.05);
}
/** Hi-hat: a tiny high "tss". */
function hat(out: AudioNode, time: number, accent: boolean): void {
const a = ctx!;
const src = a.createBufferSource();
src.buffer = noise;
const hp = a.createBiquadFilter();
hp.type = 'bandpass';
hp.frequency.value = 5000;
const env = a.createGain();
env.gain.setValueAtTime(accent ? 0.07 : 0.035, time);
env.gain.exponentialRampToValueAtTime(0.0001, time + 0.12);
src.connect(hp).connect(env).connect(out);
src.start(time, Math.random() * 0.5);
src.stop(time + 0.15);
}
/** Turn a song into a list of notes for one trip through it, sorted by beat. */
function buildEvents(song: Song, out: GainNode): Event[] {
const events: Event[] = [];
song.chords.forEach((chord, bar) => {
const start = bar * song.beatsPerBar;
const tones = SHAPES[chord.type].map((n) => chord.root + n);
// Keep the chord around middle C so it sounds smooth from one chord to the next.
const voiced = tones.map((n) => (n < 55 ? n + 12 : n)).sort((x, y) => x - y);
for (const [beat, length] of song.comp) {
events.push({
beat: start + beat,
play: (t, s) => voiced.forEach((n) => (song.pads ? pad(out, n, t, length * s, 0.05) : piano(out, n, t, length * s, 0.07))),
});
}
for (const [beat, step, length] of song.bass) {
events.push({ beat: start + beat, play: (t, s) => bass(out, chord.root - 12 + step, t, length * s) });
}
const melodyTones = [...voiced, ...voiced.map((n) => n + 12)];
for (const [beat, which, length] of song.melody[bar % song.melody.length]) {
events.push({ beat: start + beat, play: (t, s) => sax(out, melodyTones[which], t, length * s) });
}
for (const beat of song.kick) events.push({ beat: start + beat, play: (t) => kick(out, t) });
for (const beat of song.snare) events.push({ beat: start + beat, play: (t) => snare(out, t) });
for (const beat of song.hat) events.push({ beat: start + beat, play: (t) => hat(out, t, beat % 1 === 0) });
});
return events.sort((a, b) => a.beat - b.beat);
}
/** Every little while, line up the notes coming in the next moment. */
function schedule(): void {
if (!ctx || !current || paused) return;
const { song, events } = current;
const beatSeconds = 60 / song.bpm;
const loopSeconds = song.chords.length * song.beatsPerBar * beatSeconds;
const until = ctx.currentTime + 0.2;
for (;;) {
const event = events[current.next];
const time = current.loopStart + event.beat * beatSeconds;
if (time > until) break;
if (time >= ctx.currentTime - 0.05) event.play(Math.max(time, ctx.currentTime), beatSeconds);
current.next++;
if (current.next >= events.length) {
// End of the song: start the next trip exactly one song-length later. That's the perfect loop.
current.next = 0;
current.loopStart += loopSeconds;
}
}
}
return {
/** Play song number `index` (it wraps around), fading out whatever was playing. */
play(index: number): void {
setup();
const a = ctx!;
const song = SONGS[((index % SONGS.length) + SONGS.length) % SONGS.length];
if (current?.song === song) return;
if (current) {
const old = current.out;
old.gain.setTargetAtTime(0, a.currentTime, 0.3);
setTimeout(() => old.disconnect(), 2000);
}
const out = a.createGain();
out.connect(master);
current = { song, out, events: buildEvents(song, out), next: 0, loopStart: a.currentTime + 0.1 };
if (!timer) timer = window.setInterval(schedule, 50);
},
pause(): void {
paused = true;
void ctx?.suspend();
},
resume(): void {
paused = false;
void ctx?.resume();
},
/** Turn the music down for a moment (when a gun goes bang), then back up. */
duck(): void {
if (!ctx || muted) return;
const t = ctx.currentTime;
master.gain.cancelScheduledValues(t);
master.gain.setTargetAtTime(0.06, t, 0.01);
master.gain.setTargetAtTime(0.2, t + 0.25, 0.25);
},
toggleMute(): boolean {
muted = !muted;
if (ctx) {
master.gain.cancelScheduledValues(ctx.currentTime);
master.gain.setTargetAtTime(muted ? 0 : 0.2, ctx.currentTime, 0.05);
}
return muted;
},
};
}
