Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): scale the MIDI master gain to peak polyphony so dense chords can't clip

[._.]/ Adam Eivyef1600da3178
diff --git a/client/src/lib/midiPlayback.js b/client/src/lib/midiPlayback.js
index 618a7bdd6d..306a6bce7a 100644
--- a/client/src/lib/midiPlayback.js
+++ b/client/src/lib/midiPlayback.js
@@ -15,9 +15,28 @@ const MIN_TONE_SEC = 0.05; // floor so zero/near-zero-length notes still tick
const TONE_PEAK = 0.16; // per-voice peak at full velocity (polyphonic sum)
// Velocity → per-tone gain peak. A floor keeps ppp notes audible; the master
-// bus (fixed, below) keeps a dense chord from clipping the sum.
+// bus (scaled to the file's peak polyphony, below) keeps a dense chord from
+// clipping the sum.
const peakFor = (velocity) => TONE_PEAK * (0.35 + 0.65 * (Number.isFinite(velocity) ? velocity : 0.8));
-const MASTER_GAIN = 0.9;
+const MASTER_CEIL = 0.9;
+
+// Peak simultaneous-note count — the worst-case amplitude sum the master bus
+// must keep under clipping. One O(n log n) sweep per player; at a time tie,
+// ends sort before starts so back-to-back notes don't double-count.
+const peakPolyphony = (list) => {
+ const events = [];
+ for (const n of list) events.push([n.startSec, 1], [n.startSec + n.durationSec, -1]);
+ events.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
+ let live = 0;
+ let peak = 0;
+ for (const [, delta] of events) { live += delta; if (live > peak) peak = live; }
+ return peak;
+};
+
+// Master level: at low polyphony the ceiling applies as-is; past ~6 voices it
+// backs off so peak-polyphony × full-velocity tone peaks sum below clipping
+// (mirrors createMultiScorePlayer's masterGainFor, sized to this TONE_PEAK).
+const masterLevelFor = (peak) => Math.min(MASTER_CEIL, MASTER_CEIL / Math.max(1e-6, peak * TONE_PEAK));
/**
* Build a synth player over a parsed MIDI view-model.
@@ -32,6 +51,7 @@ export const createMidiPlayer = (data, options = {}) => {
const { onEnded } = options;
const notes = (data?.notes || []).filter((n) => Number.isFinite(n?.midi) && Number.isFinite(n?.startSec));
const totalSec = Math.max(data?.durationSec || 0, 0);
+ const masterLevel = masterLevelFor(peakPolyphony(notes));
let playing = false;
let interval = null;
@@ -119,7 +139,7 @@ export const createMidiPlayer = (data, options = {}) => {
if (!notes.length || totalSec <= 0) { safeCall(onEnded); return; }
master = c.createGain();
- master.gain.setValueAtTime(MASTER_GAIN, c.currentTime);
+ master.gain.setValueAtTime(masterLevel, c.currentTime);
master.connect(c.destination);
playing = true;
diff --git a/client/src/lib/midiPlayback.test.js b/client/src/lib/midiPlayback.test.js
index 68ff02c84f..5462e25eca 100644
--- a/client/src/lib/midiPlayback.test.js
+++ b/client/src/lib/midiPlayback.test.js
@@ -123,6 +123,30 @@ describe('createMidiPlayer', () => {
expect(player.position()).toBe(0);
});
+ it('backs the master bus off for dense polyphony so the sum cannot clip', async () => {
+ const chord = {
+ durationSec: 1,
+ notes: Array.from({ length: 8 }, (_, i) => (
+ { id: String(i), midi: 60 + i, startSec: 0, durationSec: 1, velocity: 1, track: 0 }
+ )),
+ };
+ const player = createMidiPlayer(chord);
+ await player.play();
+ const masterLevel = audio.gains[0].gain.values[0];
+ // 8 simultaneous full-velocity tones at TONE_PEAK 0.16 each: the summed
+ // peak must stay under the 0.9 headroom ceiling (a fixed master gain
+ // would put it at 8 × 0.16 × 0.9 ≈ 1.15 — audible clipping).
+ expect(masterLevel * 8 * 0.16).toBeLessThanOrEqual(0.91);
+ player.stop();
+
+ // A sparse file keeps the full level — no needless quieting.
+ audio.reset();
+ const sparse = createMidiPlayer(DATA); // peak polyphony 2
+ await sparse.play();
+ expect(audio.gains[0].gain.values[0]).toBe(0.9);
+ sparse.stop();
+ });
+
it('reports ended immediately for an empty view-model', async () => {
const ended = vi.fn();
const player = createMidiPlayer({ durationSec: 0, notes: [] }, { onEnded: ended });