Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
68d6d6a
Plan: S7, Für Elise as the third Showcase
jeany55 Nov 8, 2026
32463e7
Sampled instruments: the .samples file, samples(), loudness and a pia…
jeany55 Nov 8, 2026
c3b49c3
music: sampled instruments, and performances that are not on a grid
jeany55 Nov 8, 2026
f9e8cdf
Für Elise probe: a note timeline for pictures, and our transcription
jeany55 Nov 8, 2026
3a98370
Für Elise probe: the layout, the base map, and the run frame by frame
jeany55 Nov 8, 2026
e22ee9b
Für Elise visualizer probe: the map, its answer sheet, and pictures o…
jeany55 Nov 8, 2026
3160bb4
Plan: the S7 visualizer probe as built
jeany55 Nov 8, 2026
2d2f85e
Loudness as the game plays one channel; a re-struck key cuts the tail…
jeany55 Nov 8, 2026
b1125ca
S7: the Für Elise sound probe
jeany55 Nov 8, 2026
cec5337
S7: the sound probe as played
jeany55 Nov 9, 2026
12347b3
S7: the visualizer probe as played
jeany55 Nov 9, 2026
2500324
S7: the looks chosen after the visualizer probe
jeany55 Nov 9, 2026
abae530
music.excerpt, and together() to a loudness: a long piece rendered a …
jeany55 Nov 9, 2026
41055e4
S7: the Für Elise soundtrack, the whole piece
jeany55 Nov 9, 2026
a546d04
Merge fur-elise: the Für Elise soundtrack, into the visualizer branch
jeany55 Nov 9, 2026
a530a33
Für Elise: the soundtrack as a module without a program
jeany55 Nov 9, 2026
f368819
The game's tests of a script that imports media
jeany55 Nov 9, 2026
8aef897
note-timeline: fitKeys, a keyboard fitted to the notes within a limit
jeany55 Nov 9, 2026
e67671d
make-samples: an SFZ region with no root is middle C, not its lowest key
jeany55 Nov 9, 2026
bfb0b08
Für Elise, the showcase build
jeany55 Nov 9, 2026
f424f46
Plan: the S7 showcase build
jeany55 Nov 9, 2026
1360be3
Tuning checks, a kit from recordings, music.hits and the trailer presets
jeany55 Nov 9, 2026
13ce19e
A sampled instrument's identity in every clip name made from it
jeany55 Nov 9, 2026
c0c350f
S7: Für Elise, the cinematic arrangement
jeany55 Nov 9, 2026
743b1c0
Merge fur-elise: the cinematic arrangement, into the visualizer branch
jeany55 Nov 9, 2026
d675680
Für Elise showcase on the cinematic arrangement, with one switch
jeany55 Nov 9, 2026
105ac0f
Plan: the S7 showcase on the cinematic arrangement
jeany55 Nov 9, 2026
48a00fe
Melody first: Clip.duck, render's parts, the trance presets and music…
jeany55 Nov 9, 2026
c61523d
scripts/lead-check.mts: does the melody stand out, bar by bar
jeany55 Nov 9, 2026
7afff89
S7: Für Elise, the trance remix — the piano into a synth remix, the m…
jeany55 Nov 9, 2026
f4eb156
Merge fur-elise: the trance remix, into the visualizer branch
jeany55 Nov 9, 2026
9c4c42e
Für Elise showcase on the trance remix
jeany55 Nov 9, 2026
b2def23
Plan: the S7 showcase on the remix
jeany55 Nov 9, 2026
f5b12d4
Merge remote-tracking branch 'origin/main' into fur-elise-visual
jeany55 Nov 9, 2026
8c2c207
Plan: the Für Elise showcase played and published
jeany55 Nov 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ probes/*.video
probes/*.image
# The sound demo's imported sound: made on the spot by probes/lib/sound-demo-gen.mts.
probes/*.sound
# Sampled instruments a probe imports (probes/fur-elise-sound.ts): made from sample libraries outside the repository by scripts/make-samples.mts, never committed.
probes/*.samples
93 changes: 89 additions & 4 deletions compiler/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ ${kw}interface Clip {
readonly channels: 1 | 2;
/** The loudest sample, 0 to 1; above 1 is cut off when the map's file is written. */
readonly peak: number;
/**
* How loud it is as a whole, in LUFS (ITU-R BS.1770, the figure EBU R 128 and the streaming services use): −14 to
* −16 for music heard on its own, −23 for broadcast. One channel is measured as the game plays it, from both
* sides. Reading it works the samples out.
*/
readonly lufs: number;
/** The part from \`from\` to \`to\` seconds (the end when left out). */
trim(from: number, to?: number): Clip;
/** Louder or quieter: 0.5 is half as loud, 2 twice. The game plays it as loud as written. */
Expand Down Expand Up @@ -639,6 +645,21 @@ ${kw}interface Clip {
* \`makeup\` dB after (0). Evens a part out, or glues a mix.
*/
compress(options?: { readonly threshold?: number; readonly ratio?: number; readonly attack?: number; readonly release?: number; readonly knee?: number; readonly makeup?: number }): Clip;
/** Louder or quieter as a whole, so its loudness (\`lufs\`) is \`lufs\`: \`mix.loudness(-15).limit()\`. */
loudness(lufs: number): Clip;
/**
* Turned down while \`key\` sounds — up to \`depth\` dB (6) when the key is at \`threshold\` dB (−30) or over, less in
* proportion when it is quieter, following it over \`attack\` (0.01) and \`release\` (0.15) seconds: room made for a
* melody in what plays under it. \`pads.duck(lead, { depth: 5 })\`
*/
duck(key: Clip, options?: { readonly depth?: number; readonly threshold?: number; readonly attack?: number; readonly release?: number }): Clip;
/**
* What a piano's strings add while its sustain pedal is down: each string, A0 to E6, answers the partials of the
* notes that fall on its own and rings on, until the pedal comes up and the dampers stop it. \`pedal\`: [down, up]
* in seconds; \`amount\` how loud a string answers (0.25), \`decay\` seconds it rings (3). Two channels, the
* low strings to the left. \`music.sampled\` does this itself with its \`resonance\` option.
*/
resonance(options: { readonly pedal: readonly (readonly [number, number])[]; readonly amount?: number; readonly decay?: number }): Clip;
}
/**
* When the script is built: a tone of \`pitch\` — hertz, or a note's name: "A4" (440), "C#5", "Eb3" — for
Expand Down Expand Up @@ -676,9 +697,11 @@ ${kw}function mixdown(parts: readonly { readonly clip: Clip; readonly at: number
* over them — each turned down by one gain, so what they add up to never goes over \`peak\` (0.84, −1.5 dB), between
* the samples too: what a limiter does for a mix, for sounds the game adds up itself. Never turned up. The same clip
* given twice comes back as one. \`over\`: clips that may play on top, each at any of its times (a stinger on any bar
* line), room left for them too; they come back after the parts, turned down with them. When the script is built: \`const [a, b] = together([{ clip: x, at: 0 }, { clip: y, at: 1 }])\`
* line), room left for them too; they come back after the parts, turned down with them. \`lufs\`: instead the one gain,
* up or down, that brings them together to that loudness — sections of a piece rendered apart, kept as loud against
* one another as they were played. When the script is built: \`const [a, b] = together([{ clip: x, at: 0 }, { clip: y, at: 1 }])\`
*/
${kw}function together(parts: readonly { readonly clip: Clip; readonly at: number }[], options?: { readonly peak?: number; readonly over?: readonly { readonly clip: Clip; readonly at: readonly number[] }[] }): Clip[];
${kw}function together(parts: readonly { readonly clip: Clip; readonly at: number }[], options?: { readonly peak?: number; readonly lufs?: number; readonly over?: readonly { readonly clip: Clip; readonly at: readonly number[] }[] }): Clip[];
/** A brush of cover(): which of the shapes (0 the first), centred on tile (x, y) of the picture. */
${kw}interface Brush { readonly x: number; readonly y: number; readonly shape: number }
/**
Expand Down Expand Up @@ -1243,6 +1266,63 @@ export function beforeEach(fn: (sim: Sim) => void): void;
export function afterEach(fn: (sim: Sim) => void): void;
`;

/**
* Sampled instruments: in the full declarations, not the compact variant a language model reads (they come from a
* \`.samples\` file the editor does not make yet, or from \`samples()\`).
*/
const SAMPLED = (kw: string): string => `/**
* A sampled instrument when the script is built (\`import piano from "./piano.samples"\`, made from an SFZ
* instrument by scripts/make-samples.mts): its recordings, and the keys and strengths each plays (its zones).
* \`music.sampled(piano)\` plays it.
*/
${kw}interface Samples {
readonly name: string;
/** Who made the recordings and under what licence: what a map that plays them should say. */
readonly credit: string;
readonly rate: number;
readonly zones: readonly SampleZone[];
/** Seconds of recordings in it. */
readonly seconds: number;
}
/** One zone of a sampled instrument: a recording, the keys and strengths it plays, and how it is played. */
${kw}interface SampleZone {
readonly clip: Clip;
/** The key it was recorded at (60 is C4), and the keys it plays, lo … hi. */
readonly key: number;
readonly lo: number;
readonly hi: number;
/** The strengths it plays (velocity, 1 … 127). */
readonly vlo: number;
readonly vhi: number;
/** What starts it: a key struck (when left out), let go, the pedal pressed or lifted. */
readonly trigger?: "attack" | "release" | "pedal-down" | "pedal-up";
/** dB, 0 as recorded. */
readonly volume?: number;
/** How much the strength changes its loudness, 0 … 1 (1). */
readonly veltrack?: number;
/** Seconds a note takes to die away after it is let go. */
readonly release?: number;
/** A release zone: dB quieter each second the key was held. */
readonly decay?: number;
/** Cents it is tuned up or down. */
readonly tune?: number;
/** false: played at its own pitch whatever the key. */
readonly pitched?: boolean;
/** Zones of the same keys and strengths played in turn: this one's turn of \`turns\`. */
readonly turn?: number;
readonly turns?: number;
/** Zones of the same keys chosen by chance: this one when a number 0 … 1 falls in [from, to). */
readonly chance?: readonly [number, number];
}
/**
* When the script is built: a sampled instrument from clips the script has — recordings it imports (.sound), or ones
* it makes — for \`music.sampled\`. Each zone a clip, the key it plays at (60 or "C4"), and, when left out, every key
* and strength: a note is played from the zone whose key is nearest it.
* const piano = music.sampled(samples([{ clip: c3, key: "C3" }, { clip: c4, key: "C4" }, { clip: c5, key: "C5" }]));
*/
${kw}function samples(zones: readonly ({ readonly clip: Clip; readonly key: number | string; readonly lo?: number | string; readonly hi?: number | string } & Partial<Omit<SampleZone, "clip" | "key" | "lo" | "hi">>)[], options?: { readonly name?: string; readonly credit?: string }): Samples;
`;

/** The whole declaration file for a set of names. */
/** What importing one of the script's media files gives (`media.ts`). */
const MEDIA_MODULES = `// ── A video the script imports: import badApple from "./bad-apple.video"; ──
Expand All @@ -1260,6 +1340,11 @@ declare module "*.sound" {
const sound: Clip;
export default sound;
}
// ── A sampled instrument the script imports: import piano from "./piano.samples"; ──
declare module "*.samples" {
const samples: Samples;
export default samples;
}
// ── Notes the script imports, from a MIDI file: import tune from "./tune.notes"; ──
declare module "*.notes" {
const notes: Notes;
Expand All @@ -1271,7 +1356,7 @@ export function generateDeclarations(names: ScriptNames = defaultScriptNames(),
const compact = options.compact === true;
const globals = body("declare ", "", names, compact);
if (compact) return `${HEADER}${globals}\n${ARRAYS}`;
const module = `${body("export ", "export ", names, false)}\n${TESTING}`.split("\n").map((l) => (l ? ` ${l}` : l)).join("\n");
return `${HEADER}${globals}\n${ARRAYS}\n// ── The same names, as a module: import { trigger, units } from "${MODULE_NAME}"; ──\ndeclare module "${MODULE_NAME}" {\n${module}\n}\n${MEDIA_MODULES}`;
const module = `${body("export ", "export ", names, false)}\n${SAMPLED("export ")}\n${TESTING}`.split("\n").map((l) => (l ? ` ${l}` : l)).join("\n");
return `${HEADER}${globals}\n${SAMPLED("declare ")}\n${ARRAYS}\n// ── The same names, as a module: import { trigger, units } from "${MODULE_NAME}"; ──\ndeclare module "${MODULE_NAME}" {\n${module}\n}\n${MEDIA_MODULES}`;
}

124 changes: 124 additions & 0 deletions compiler/dsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,127 @@ export function pluckVoice(hz: number, rate: number, n: number, brightness: numb
}
return out;
}

/* ── Loudness, and strings that ring in sympathy ── */

/** A biquad's coefficients, b over a (a0 = 1). */
type Biquad = readonly [b0: number, b1: number, b2: number, a1: number, a2: number];
function biquad(x: Float32Array, [b0, b1, b2, a1, a2]: Biquad): Float32Array {
const out = new Float32Array(x.length);
let x1 = 0, x2 = 0, y1 = 0, y2 = 0;
for (let i = 0; i < x.length; i++) {
const y = b0 * x[i] + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
x2 = x1; x1 = x[i]; y2 = y1; y1 = y; out[i] = y;
}
return out;
}

/**
* Integrated loudness in LUFS, as ITU-R BS.1770-4 measures it (EBU R 128's figure): each channel through the K-weighting
* (a high shelf, +4 dB above 1.7 kHz, then a high-pass at 38 Hz, made for the clip's rate as libebur128 makes them),
* its power over 400 ms blocks three-quarters overlapping, the blocks under −70 LUFS left out, then those more than
* 10 LU under the mean of the rest. −Infinity for silence. A clip of one channel is measured as the game plays it, from
* both sides (a full-scale 997 Hz sine: 0 LUFS, where BS.1770 has −3.01 for it on one channel alone).
*/
export function loudness(chans: Channels, rate: number): number {
if (chans.length === 1) chans = [chans[0], chans[0]];
let K = Math.tan((Math.PI * 1681.974450955533) / rate);
const Vh = 10 ** (3.999843853973347 / 20), Vb = Vh ** 0.4996667741545416, Q1 = 0.7071752369554196;
let a0 = 1 + K / Q1 + K * K;
const shelf: Biquad = [(Vh + (Vb * K) / Q1 + K * K) / a0, (2 * (K * K - Vh)) / a0, (Vh - (Vb * K) / Q1 + K * K) / a0, (2 * (K * K - 1)) / a0, (1 - K / Q1 + K * K) / a0];
K = Math.tan((Math.PI * 38.13547087602444) / rate);
const Q2 = 0.5003270373238773;
a0 = 1 + K / Q2 + K * K;
const high: Biquad = [1, -2, 1, (2 * (K * K - 1)) / a0, (1 - K / Q2 + K * K) / a0];
const weighted = chans.map((c) => biquad(biquad(c, shelf), high));
const n = chans[0].length, block = Math.round(0.4 * rate), step = Math.round(0.1 * rate);
if (n < block) return -Infinity;
// The power of each block, the channels added (each weighs 1: left and right, or the one).
const powers: number[] = [];
const prefix = weighted.map((w) => { const p = new Float64Array(n + 1); for (let i = 0; i < n; i++) p[i + 1] = p[i] + w[i] * w[i]; return p; });
for (let from = 0; from + block <= n; from += step) {
let z = 0;
for (const p of prefix) z += (p[from + block] - p[from]) / block;
powers.push(z);
}
const lufs = (z: number) => -0.691 + 10 * Math.log10(z);
const above = powers.filter((z) => z > 0 && lufs(z) > -70);
if (above.length === 0) return -Infinity;
const gate = lufs(above.reduce((a, b) => a + b, 0) / above.length) - 10;
const kept = above.filter((z) => lufs(z) > gate);
return lufs(kept.reduce((a, b) => a + b, 0) / kept.length);
}

/**
* What the strings of a piano add while its sustain pedal is down: every damper lifted, each string answers the
* partials of the notes played that fall on its own — a comb filter a string from A0 to E6 (the keys with dampers),
* tuned to it, each fed the sound while the pedal is down and ringing `decay` seconds (to −60 dB; the low strings
* longer, the high ones shorter), and stopped in a tenth of a second when the pedal comes up and the dampers fall.
* The strings low to high are heard from left to right, as the player hears them. Only what they add comes out,
* `amount` of it (a string answering a partial held on its pitch comes back that loud); `pedal` is [down, up] in seconds.
*/
export function sympathetic(chans: Channels, rate: number, pedal: Points, amount: number, decay: number, frames: number): Float32Array[] {
const n = chans[0].length, L = new Float32Array(frames), R = new Float32Array(frames);
// Whether the dampers are up, each sample, eased over 20 ms either way.
const up = new Float32Array(frames), ease = Math.max(1, Math.round(0.02 * rate));
for (const [down, lift] of pedal) {
const a = Math.max(0, Math.round(down * rate)), b = Math.min(frames, Math.round(lift * rate));
for (let i = a; i < b; i++) up[i] = 1;
}
const open = new Float32Array(frames);
let g = 0;
for (let i = 0; i < frames; i++) { g += (up[i] - g) / ease; open[i] = g; }
const x = new Float32Array(frames);
for (let i = 0; i < Math.min(n, frames); i++) {
let s = 0;
for (const c of chans) s += c[i];
x[i] = (s / chans.length) * open[i];
}
const damped = 0.1;
for (let m = 21; m <= 88; m++) {
const hz = 440 * 2 ** ((m - 69) / 12), delay = rate / hz;
// The string's ring: the low strings half again as long as `decay`, the top ones half as long.
const t60 = decay * (1.5 - (m - 21) / 67);
const gDown = 10 ** ((-3 * delay) / (rate * t60)), gUp = 10 ** ((-3 * delay) / (rate * damped));
const size = Math.ceil(delay) + 2, buf = new Float32Array(size);
const d0 = Math.floor(delay), frac = delay - d0;
const pan = (m - 21) / 67, gl = Math.cos((pan * Math.PI) / 2), gr = Math.sin((pan * Math.PI) / 2);
let w = 0, lp = 0, energy = 0;
for (let i = 0; i < frames; i++) {
const r1 = (w - d0 + size) % size, r2 = (r1 - 1 + size) % size;
const back = buf[r1] * (1 - frac) + buf[r2] * frac;
lp = 0.75 * back + 0.25 * lp;
const gk = gUp + (gDown - gUp) * open[i];
const y = x[i] + gk * lp;
buf[w] = y;
w = (w + 1) % size;
// What comes out is the string's ring alone, scaled so that a partial held on its pitch returns as loud.
const out = (y - x[i]) * (1 - gDown) * amount;
L[i] += gl * out;
R[i] += gr * out;
energy = energy * 0.999 + Math.abs(y);
if (energy < 1e-9 && open[i] === 0 && i > n) break;
}
}
return [L, R];
}

/**
* A ducker keyed from another sound: the channels turned down by up to `depth` dB while `key` sounds — the key's level
* (its two sides' louder, followed over `attack` and `release` seconds) against `threshold`: at or over it, all the
* depth; under it, as much less as the key is quieter, in proportion. What makes room for a melody in what plays under it.
*/
export function duck(chans: Channels, key: Channels, rate: number, depth: number, threshold: number, attack: number, release: number): Float32Array[] {
const n = chans[0].length, kn = key[0].length, floor = 10 ** (-depth / 20), thr = 10 ** (threshold / 20);
const up = 1 - Math.exp(-1 / Math.max(1, attack * rate)), down = 1 - Math.exp(-1 / Math.max(1, release * rate));
const gain = new Float32Array(n);
let env = 0;
for (let i = 0; i < n; i++) {
let x = 0;
if (i < kn) for (const k of key) { const a = k[i] < 0 ? -k[i] : k[i]; if (a > x) x = a; }
env += (x - env) * (x > env ? up : down);
const amount = Math.min(1, env / thr);
gain[i] = 1 - (1 - floor) * amount;
}
return chans.map((c) => { const out = new Float32Array(n); for (let i = 0; i < n; i++) out[i] = c[i] * gain[i]; return out; });
}
9 changes: 8 additions & 1 deletion compiler/gameTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { RemasteredWeigh } from "./remasteredCost";
import { EudTracer, type EudTraceHost, type EudTraceRow } from "./eudError";
import { RecordingHandle, type DebugHost, type DebugLimits, type DebugRecording } from "./gameDebug";
import { here, locate, runModules, type LinkedFile } from "./link";
import { mediaValue } from "./media";
import type { ScriptNames } from "./names";
import { Collector, createRuntime } from "./runtime";
import { isTestFile, listTests, runTests, TestRegistry, TESTING_NAMES, type TestReport, type TestResult } from "./testing";
Expand All @@ -41,6 +42,11 @@ export interface GameTestPayload {
trace?: boolean;
/** Before the tests, the map played this many frames with nobody at it (a script with no tests is traced so). */
play?: number;
/**
* The script's videos, images, sounds, notes and samples by path, as its files hold them: what an import of one gives
* when the modules run again. Absent, a script that imports one cannot run beside the game.
*/
media?: [string, string][];
}

/** What `runGameTests` returns: the report, and with `trace` asked, the run's reads and writes by line. */
Expand All @@ -55,7 +61,8 @@ function register(payload: GameTestPayload) {
registry.where = () => where(new Error()) ?? { file: ENTRY_FILE, line: 1 };
const runtime = { ...createRuntime(payload.names, collector), ...registry.library() };
collector.running = true;
const failure = runModules(linked, entryOf(linked.keys()), runtime, MODULE_NAME, { imported: TESTING_NAMES, then: [...linked.keys()].filter(isTestFile).sort() });
const media = new Map(payload.media ?? []);
const failure = runModules(linked, entryOf(linked.keys()), runtime, MODULE_NAME, { imported: TESTING_NAMES, then: [...linked.keys()].filter(isTestFile).sort(), media: (path) => { const text = media.get(path); return text === undefined ? undefined : mediaValue(path, text); } });
collector.running = false;
return { linked, registry, where, error: failure ? `${failure.file ?? ENTRY_FILE}:${failure.line ?? 1}: ${failure.message}` : null };
}
Expand Down
Loading
Loading