8000 feat(sync): coarse-to-fine pipeline + per-stage progress by johannboehme · Pull Request #25 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 19 additions & 4 deletions frontend/src/local/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,25 @@ async function runCamPrep(
detail: `${cam.id} · ${cam.filename}`,
});

const result = await syncAudio({
refSource: videoMonoPcm.pcm,
querySource: studioPcm,
});
// Sync gets the 0.4 → 0.55 slice of the per-cam progress band. The
// WASM pipeline calls back per stage (envelope→chroma→onset→ncc→
// phat→consensus→drift), so the bar moves continuously through the
// ~5-s match instead of freezing at 40 %.
const result = await syncAudio(
{
refSource: videoMonoPcm.pcm,
querySource: studioPcm,
},
{
onProgress: (stage, frac) => {
void reportProgress(jobId, {
pct: opts.mapPct(0.4 + frac * 0.15),
stage: `syncing-${cam.id}`,
detail: `${cam.id} · ${cam.filename} · ${stage}`,
});
},
},
);
sync = {
offsetMs: result.offsetMs,
driftRatio: result.driftRatio,
Expand Down
18 changes: 16 additions & 2 deletions frontend/src/local/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,15 @@ async function awaitGate(): Promise<void> {
// Worker dispatch
// -----------------------------------------------------------------------------

/** Callback fired between WASM pipeline stages. `fraction` is in [0, 1]
* and monotonically non-decreasing across calls within one match. */
export type SyncProgressFn = (stage: string, fraction: number) => void;

function runMatchInWorker(
refPcm: Float32Array,
queryPcm: Float32Array,
sampleRate: number,
onProgress?: SyncProgressFn,
): Promise<RawSyncResult> {
return new Promise<RawSyncResult>((resolve, reject) => {
const worker = new Worker(new URL("./sync.worker.ts", import.meta.url), {
Expand All @@ -173,6 +178,12 @@ function runMatchInWorker(
"message",
(e: MessageEvent<SyncWorkerResponse>) => {
const msg = e.data;
if (msg.type === "progress") {
// Don't terminate the worker on progress — keep listening for
// the eventual "result" / "error" message.
onProgress?.(msg.stage, msg.fraction);
return;
}
if (msg.type === "result") {
resolve(msg.result);
} else {
Expand Down Expand Up @@ -204,12 +215,15 @@ function runMatchInWorker(
});
}

export async function syncAudio(input: SyncInput): Promise<SyncResult> {
export async function syncAudio(
input: SyncInput,
opts?: { onProgress?: SyncProgressFn },
): Promise<SyncResult> {
const [refPcm, queryPcm] = await Promise.all([
ensurePcm(input.refSource),
ensurePcm(input.querySource),
]);
await awaitGate();
const raw = await runMatchInWorker(refPcm, queryPcm, TARGET_SR);
const raw = await runMatchInWorker(refPcm, queryPcm, TARGET_SR, opts?.onProgress);
return mapWasmResult(raw);
}
18 changes: 17 additions & 1 deletion frontend/src/local/sync/sync.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
* Decoding (WebAudio → mono PCM) stays on the main thread because
* OfflineAudioContext has Safari-side caveats inside workers; we transfer
* the resulting Float32Array buffers into the worker zero-copy instead.
*
* Progress: the WASM pipeline calls `syncAudioPcmWithProgress` with a
* Rust→JS closure that fires per pipeline stage (envelope, chroma,
* onset, ncc, phat, consensus, drift). Each fire becomes a `progress`
* postMessage so the main thread can paint a smooth bar instead of a
* 5-s frozen "40%".
*/

interface RawCandidate {
Expand All @@ -36,6 +42,7 @@ export type SyncWorkerRequest = {

export type SyncWorkerResponse =
| { type: "result"; result: RawSyncResult }
| { type: "progress"; stage: string; fraction: number }
| { type: "error"; message: string };

const ctx = self as unknown as DedicatedWorkerGlobalScope;
Expand All @@ -58,10 +65,19 @@ ctx.addEventListener("message", async (e: MessageEvent<SyncWorkerRequest>) => {
if (msg.type !== "match") return;
try {
const wasm = await loadWasm();
const result = wasm.syncAudioPcm(
// Bridge WASM stage callbacks → main-thread progress events. The
// WASM call is synchronous from the worker's point of view, but
// postMessage is non-blocking so each stage update reaches the UI
// immediately even though the worker is otherwise pegged.
const string, fraction: number) => {
const evt: SyncWorkerResponse = { type: "progress", stage, fraction };
ctx.postMessage(evt);
};
const result = wasm.syncAudioPcmWithProgress(
msg.refPcm,
msg.queryPcm,
msg.sampleRate,
onStage,
) as RawSyncResult;
const evt: SyncWorkerResponse = { type: "result", result };
ctx.postMessage(evt);
Expand Down
1 change: 1 addition & 0 deletions frontend/wasm/sync-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2"
js-sys = "0.3"
realfft = "3.4"
serde = { version = "1", features = ["derive"] }
serde-wasm-bindgen = "0.6"
Expand Down
Loading
Loading
0