fix(align): render at 44.1k and resample; finite guard; pre-zero tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-30 06:38:31 +01:00
co-authored by Claude Sonnet 5.5
parent 63b32a488d
commit b614244e4d
2 changed files with 40 additions and 6 deletions
+13 -5
View File
@@ -1,9 +1,11 @@
import * as at from "@coderline/alphatab";
import type { Audio } from "../core/types.ts";
import { renderMidiFile } from "../audio/render.ts";
import { resample } from "../dsp/resample.ts";
import type { RefTrack } from "./smf.ts";
import type { Warp } from "./warp.ts";
const RENDER_SR = 44100; // alphaSynth yields NaN at other rates (e.g. 22050), so always render here and resample
const TPS = 1920; // division 960 at a fixed 120 bpm
const PEAK = Math.pow(10, -1 / 20);
const MELODIC_CHANNELS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15];
@@ -17,30 +19,36 @@ export function renderMidi(
tracks: RefTrack[], warp: Warp, sf2: Uint8Array,
opts: { sampleRate?: number; mono?: boolean; durationSec?: number } = {},
): Audio {
const sampleRate = opts.sampleRate ?? 44100;
const sampleRate = opts.sampleRate ?? RENDER_SR;
const midi = new at.midi.MidiFile();
midi.division = 960;
midi.addEvent(new at.midi.TempoChangeEvent(0, 500000));
const seen = new Set<number>();
let next = 0;
for (const t of tracks) {
// Beyond 15 melodic tracks channels wrap and are shared; the first track's program wins on a shared channel.
const ch = t.isDrums ? 9 : MELODIC_CHANNELS[next++ % MELODIC_CHANNELS.length]!;
if (!seen.has(ch)) { seen.add(ch); midi.addEvent(new at.midi.ProgramChangeEvent(0, 0, ch, t.isDrums ? 0 : t.program)); }
for (const n of t.notes) {
const offSec = warp.toAudio(n.offset);
if (offSec <= 0) continue;
const on = Math.round(Math.max(0, warp.toAudio(n.onset)) * TPS);
const off = Math.max(on + 1, Math.round(warp.toAudio(n.offset) * TPS));
if (warp.toAudio(n.offset) <= 0) continue;
const off = Math.max(on + 1, Math.round(offSec * TPS));
midi.addEvent(new at.midi.NoteOnEvent(0, on, ch, n.pitch, n.velocity));
midi.addEvent(new at.midi.NoteOffEvent(0, off, ch, n.pitch, 0));
}
}
const frames = opts.durationSec === undefined ? undefined : Math.round(opts.durationSec * sampleRate);
const out = renderMidiFile(midi, sf2, sampleRate, { frames });
const out = renderMidiFile(midi, sf2, RENDER_SR, { frames: opts.durationSec === undefined ? undefined : Math.round(opts.durationSec * RENDER_SR) });
let chans = out.channels;
if (sampleRate !== RENDER_SR) {
chans = chans.map((c) => resample(c, RENDER_SR, sampleRate));
if (opts.durationSec !== undefined) { const n = Math.round(opts.durationSec * sampleRate); chans = chans.map((c) => { const o = new Float32Array(n); o.set(c.subarray(0, n)); return o; }); }
}
if (opts.mono) {
const [L, R] = chans as [Float32Array, Float32Array];
chans = [L.map((v, i) => (v + R[i]!) / 2)];
}
for (const c of chans) for (let i = 0; i < c.length; i++) if (!Number.isFinite(c[i]!)) throw new Error("renderMidi: non-finite samples from synth");
let peak = 0;
for (const c of chans) for (let i = 0; i < c.length; i++) { const a = Math.abs(c[i]!); if (a > peak) peak = a; }
if (peak > 0) { const g = PEAK / peak; for (const c of chans) for (let i = 0; i < c.length; i++) c[i]! *= g; }