// 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.
///
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; manual: Anchor[] };
const sf2s = new Map>();
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 {
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) });
}
};