// Composed track definitions. Each track yields notes for a global step index. import { midi } from './context'; import { kalimba, marimba, musicBox, bell, bass, pad, horn, shaker, kick, clap } from './instruments'; import type { Track } from './types'; export interface TrackDef { bpm: number; spb: number; level: number; step(i: number, t: number, out: GainNode): void; } interface Note { d: number; len: number } const SCALE = [0, 2, 4, 7, 9]; // major pentatonic; degree 0 = C4 const degFreq = (d: number): number => midi(60 + 12 * Math.floor(d / 5) + SCALE[((d % 5) + 5) % 5]); /** Tokens: number = scale degree, '.' = rest, '_' = hold previous note, '|' ignored. */ function parse(s: string, steps: number): (Note | null)[] { const toks = s.replace(/\|/g, ' ').trim().split(/\s+/); const out: (Note | null)[] = new Array(steps).fill(null); let last: Note | null = null; for (let i = 0; i < Math.min(steps, toks.length); i++) { const tk = toks[i]; if (tk === '.') last = null; else if (tk === '_') { if (last) last.len++; } else { last = { d: Number(tk), len: 1 }; out[i] = last; } } return out; } // ---- village: bouncy, 112 bpm, 8th-note steps, 8-bar phrases, form A A B A ---- const VA = parse(`2 . 3 2 0 . 1 . | 2 . . 3 4 . 3 . | 5 . 4 3 2 . 3 . | 1 . . . 0 . 1 . | 2 . 3 5 4 . 3 . | 2 3 4 . 3 2 1 . | 1 . 2 1 0 . -1 . | 0 . . . . . . .`, 64); const VB = parse(`5 . 6 5 4 . 3 . | 4 . 5 . 7 . . . | 8 . 7 6 5 . 4 . | 3 . . 4 3 . 1 . | 5 . 6 5 4 . 3 . | 4 5 7 . 8 . 7 . | 6 . 5 4 3 . 1 . | 2 . 3 . 1 . . .`, 64); const VFORM = [VA, VA, VB, VA]; const VROOT = [48, 41, 48, 43, 48, 41, 43, 48]; const village: TrackDef = { bpm: 112, spb: 2, level: 0.35, step(i, t, out) { const n = VFORM[Math.floor(i / 64) % 4][i % 64]; if (n) (Math.floor(i / 64) % 4 === 2 ? marimba : kalimba)(out, t, degFreq(n.d), 0.3); const s = i % 8, r = VROOT[Math.floor(i / 8) % 8]; if (s === 0 || s === 3) bass(out, t, midi(r), 0.4); if (s === 4) bass(out, t, midi(r + 7), 0.3); shaker(out, t, s % 2 ? 0.06 : 0.03); }, }; // ---- title: music box waltz, 3/4, 6 eighth-steps per bar, 16 bars ---- const TA = parse(`4 . 3 . 2 . | 3 . 2 . 0 . | 4 . 5 . 4 . | 3 . . . 2 . | 5 . 4 . 3 . | 4 . 3 . 2 . | 1 . 2 . 3 . | 0 . . . . .`, 48); const TB = parse(`7 . 6 . 5 . | 6 . 5 . 3 . | 4 . 5 . 6 . | 5 . . . 3 . | 7 . 6 . 5 . | 6 . 5 . 4 . | 3 . 1 . 2 . | 0 . . . . .`, 48); const TCH: [number, number[]][] = [ [48, [60, 64, 67]], [45, [57, 60, 64]], [41, [57, 60, 65]], [48, [60, 64, 67]], [41, [57, 60, 65]], [48, [60, 64, 67]], [43, [59, 62, 67]], [48, [60, 64, 67]], ]; const titleTrack: TrackDef = { bpm: 100, spb: 2, level: 0.4, step(i, t, out) { const n = (Math.floor(i / 48) % 2 ? TB : TA)[i % 48]; if (n) musicBox(out, t, degFreq(n.d), 0.26); const s = i % 6, [root, triad] = TCH[Math.floor(i / 6) % 8]; if (s === 0) musicBox(out, t, midi(root), 0.2); if (s === 2 || s === 4) for (const m of triad) musicBox(out, t, midi(m), 0.06); }, }; // ---- garden: calm 72 bpm, warm pads + bells, 4 chords x 2 bars ---- const GCH: [number, number[]][] = [ [48, [48, 55, 59, 64]], [45, [45, 52, 57, 60, 64]], [41, [41, 48, 57, 64]], [43, [43, 50, 55, 59, 62]], ]; const GBELL = [ parse('2 . . 4 . . 3 . . . 1 . . . . .', 16), parse('4 . . 5 . . 4 . . 2 . . 3 . . .', 16), parse('5 . . 7 . . 5 . . 4 . . 3 . . .', 16), parse('6 . . 5 . . 3 . . 1 . . 2 . . 0', 16), ]; const garden: TrackDef = { bpm: 72, spb: 2, level: 0.4, step(i, t, out) { const c = Math.floor(i / 16) % 4, s = i % 16, [root, tones] = GCH[c]; const sd = 60 / 72 / 2; if (s === 0) pad(out, t, tones.map(midi), 16 * sd + 1.2, 0.05); if (s === 0 || s === 8) bass(out, t, midi(root), 0.22); const n = GBELL[c][s]; if (n) bell(out, t, degFreq(n.d + 5), 0.16); }, }; // ---- victory: 8-bar fanfare, then a happy dance groove loop (120 bpm) ---- const FAN = parse(`3 . 3 . 3 . 5 _ | _ _ _ . 4 . 5 . | 7 _ . 7 . 8 . 7 | 5 _ _ _ . . 3 . | 5 . 5 . 5 . 7 _ | _ _ _ . 6 . 7 . | 8 _ 7 _ 6 _ 5 _ | 5 _ _ _ . . . .`, 64); const FROOT = [48, 48, 41, 48, 48, 45, 43, 48]; const TRI: Record = { 48: [60, 64, 67], 41: [57, 60, 65], 45: [57, 60, 64], 43: [59, 62, 67] }; const GRV = parse(`5 . 4 3 . 4 5 . | 3 . 2 . 3 . . . | 4 . 5 . 7 . 5 . | 6 . 5 . 3 . 1 . | 5 . 4 3 . 4 5 . | 3 . 2 . 3 . 5 . | 7 . 5 . 4 . 3 . | 1 . 2 . 3 . 1 .`, 64); const GROOT = [48, 45, 41, 43, 48, 45, 41, 43]; const victory: TrackDef = { bpm: 120, spb: 2, level: 0.4, step(i, t, out) { const sd = 60 / 120 / 2; if (i < 64) { const s = i % 8, r = FROOT[Math.floor(i / 8)]; const n = FAN[i]; if (n) { horn(out, t, degFreq(n.d), n.len * sd * 0.95); bell(out, t, degFreq(n.d + 5), 0.07); } if (s === 0 || s === 4) { bass(out, t, midi(r), 0.4); for (const m of TRI[r]) marimba(out, t, midi(m), 0.09); } if (s === 0) kick(out, t, 0.25); return; } const j = (i - 64) % 64, s = j % 8, r = GROOT[Math.floor(j / 8)]; const n = GRV[j]; if (n) (j >= 32 ? marimba : kalimba)(out, t, degFreq(n.d), 0.3); if (s === 0 || s === 3 || s === 6) bass(out, t, midi(r), 0.4); if (s === 4) bass(out, t, midi(r + 7), 0.3); if (s === 0 || s === 4) kick(out, t, 0.28); if (s === 2 || s === 6) clap(out, t, 0.1); shaker(out, t, s % 2 ? 0.07 : 0.035); }, }; // ---- home: cozy sanctuary lullaby, F major, 80 bpm, 8th-note steps, form A A B A ---- const MAJ = [0, 2, 4, 5, 7, 9, 11]; // major scale; degree 0 = F4 const majFreq = (d: number): number => midi(65 + 12 * Math.floor(d / 7) + MAJ[((d % 7) + 7) % 7]); const HA = parse(`2 _ 4 . 3 _ 2 . | 1 _ 2 . 5 _ _ . | 3 _ 5 . 4 _ 3 . | 4 _ _ _ . . . . | 2 _ 4 . 7 _ 6 . | 5 _ 3 . 5 _ 4 . | 3 _ 2 . 1 _ 4 . | 2 _ _ _ 0 _ _ .`, 64); const HB = parse(`5 _ 4 . 3 _ 5 . | 4 _ 2 . 0 _ 2 . | 3 _ 4 . 5 _ 3 . | 4 _ _ _ 6 _ _ . | 6 _ 4 . 2 _ 4 . | 5 _ 7 . 5 _ 2 . | 3 _ 1 . 4 _ 6 . | 7 _ _ _ _ _ . .`, 64); const HCH: Record = { F: [41, [57, 60, 65]], Dm: [38, [57, 62, 65]], Bb: [46, [58, 62, 65]], C: [48, [55, 60, 64]], Gm: [43, [55, 58, 62]], Am: [45, [57, 60, 64]], }; // one chord per half bar const HCA = 'F F Dm Dm Bb Bb C C F F Bb Bb C C F F'.split(' '); const HCB = 'Bb Bb F F Gm Gm C C Am Am Dm Dm Gm C F F'.split(' '); const HFORM: [(Note | null)[], string[]][] = [[HA, HCA], [HA, HCA], [HB, HCB], [HA, HCA]]; const home: TrackDef = { bpm: 80, spb: 2, level: 0.4, step(i, t, out) { const [mel, chords] = HFORM[Math.floor(i / 64) % 4], j = i % 64, s = j % 8; const n = mel[j]; if (n) musicBox(out, t, majFreq(n.d), 0.24); const [root, tri] = HCH[chords[Math.floor(j / 4)]]; if (s === 0) bass(out, t, midi(root), 0.2); if (s === 4) bass(out, t, midi(root + 7), 0.12); if (s === 2) marimba(out, t, midi(tri[0]), 0.07); if (s === 3 || s === 7) marimba(out, t, midi(tri[1]), 0.06); if (s === 6) marimba(out, t, midi(tri[2]), 0.07); }, }; export const TRACKS: Record = { title: titleTrack, village, garden, victory, home };