Wait for the first streamed word before playing

This commit is contained in:
Fisto
2026-09-21 12:43:44 +02:00
parent baaaba3dcd
commit 7701664bc6
4 changed files with 34 additions and 5 deletions
+22 -4
View File
@@ -6,7 +6,7 @@
* them in the transcript, and runs speech to text when a provider is set.
*/
import { statSync } from "node:fs";
import { statSync, unlinkSync } from "node:fs";
import { AudioManager } from "./audio.js";
import type { CaptureSegment } from "./capture.js";
import { CapturePipeline } from "./capture.js";
@@ -147,17 +147,35 @@ export class AudioService {
return { queued: true, id, position: this.queue.length };
}
/** Wait for the wrapper's streaming file to appear, then play it while it grows. */
/**
* Wait until the wrapper's streaming file has real audio in it, then play it while it
* grows. The wrapper touches `<file>.ready` after the first word, which is the reliable
* signal; a size threshold and a timeout cover providers that do not mark readiness.
*/
private async playWhenReady(file: string): Promise<void> {
for (let attempt = 0; attempt < 400; attempt++) {
try {
if (statSync(file).size > 44) break;
if (statSync(`${file}.ready`).size >= 0) break;
} catch {
// no marker yet
}
try {
if (statSync(file).size > 100_000) break;
} catch {
// not created yet
}
await new Promise(resolve => setTimeout(resolve, 25));
}
await this.manager.play(file);
try {
await this.manager.play(file);
} finally {
try {
unlinkSync(`${file}.ready`);
} catch {
// best effort
}
}
}
stop(): void {