The MIDI preview render (~2 s for a 5 min song) and auto-align ran synchronously on the server's event loop, and each preview was a 12.4 MB WAV. Renders now run as 4 parallel time chunks (2 s pre-roll, 10 ms cross-fade) on worker threads, auto-align on its own worker, and previews are mono Ogg Opus 48 kb/s via ffmpeg (1.5 MB, same timing as the WAV in Chrome). ?format=wav keeps the PCM output. Swimming: render 2.0 s -> 0.65 s + 0.6 s encode; event loop blocked 2.0 s -> <0.1 s per render. Co-Authored-By: Claude Opus 5.5 <[email protected]>
37 lines
1.8 KiB
TypeScript
37 lines
1.8 KiB
TypeScript
// Worker thread for the alignment server's heavy, synchronous jobs (MIDI preview render, automatic alignment), so the
|
|
// server's event loop keeps answering other requests (page, fit, audio) while they run. Started by ./jobs-pool.ts.
|
|
/// <reference lib="webworker" />
|
|
import { renderMidi } from "./render.ts";
|
|
import { autoAlign } from "./auto.ts";
|
|
import { makeWarp, type Anchor } from "./warp.ts";
|
|
import type { Audio, Role } from "../core/types.ts";
|
|
import type { RefTrack, Smf } from "./smf.ts";
|
|
|
|
export type Job =
|
|
/** Mono, not normalised, audio time [startSec, startSec + durationSec) -> Float32Array. */
|
|
| { type: "render"; sf2Path: string; tracks: RefTrack[]; anchors: Anchor[]; sampleRate: number; startSec: number; durationSec: number }
|
|
| { type: "auto"; sf2Path: string; audio: Audio; smf: Smf; roles: Record<string, Role | null>; manual: Anchor[] };
|
|
|
|
const sf2s = new Map<string, Promise<Uint8Array>>();
|
|
const sf2 = (p: string) => { let x = sf2s.get(p); if (!x) sf2s.set(p, (x = Bun.file(p).arrayBuffer().then((b) => new Uint8Array(b)))); return x; };
|
|
|
|
async function run(job: Job): Promise<unknown> {
|
|
if (job.type === "render") {
|
|
const a = renderMidi(job.tracks, makeWarp(job.anchors), await sf2(job.sf2Path), {
|
|
sampleRate: job.sampleRate, mono: true, startSec: job.startSec, durationSec: job.durationSec, normalize: false,
|
|
});
|
|
return a.channels[0]!; // transferred back
|
|
}
|
|
return autoAlign(job.audio, job.smf, job.roles, job.manual, await sf2(job.sf2Path));
|
|
}
|
|
|
|
self.onmessage = async (e: MessageEvent<{ id: number; job: Job }>) => {
|
|
const { id, job } = e.data;
|
|
try {
|
|
const result = await run(job);
|
|
self.postMessage({ id, result }, ArrayBuffer.isView(result) ? [result.buffer as ArrayBuffer] : []);
|
|
} catch (err) {
|
|
self.postMessage({ id, error: (err as Error).message ?? String(err) });
|
|
}
|
|
};
|