Add crunch presets, echo options, and streaming word mode
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
* them in the transcript, and runs speech to text when a provider is set.
|
||||
*/
|
||||
|
||||
import { statSync } from "node:fs";
|
||||
import { AudioManager } from "./audio.js";
|
||||
import type { CaptureSegment } from "./capture.js";
|
||||
import { CapturePipeline } from "./capture.js";
|
||||
@@ -146,6 +147,19 @@ 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. */
|
||||
private async playWhenReady(file: string): Promise<void> {
|
||||
for (let attempt = 0; attempt < 400; attempt++) {
|
||||
try {
|
||||
if (statSync(file).size > 44) break;
|
||||
} catch {
|
||||
// not created yet
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, 25));
|
||||
}
|
||||
await this.manager.play(file);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.queue.length = 0;
|
||||
this.manager.stopPlayback();
|
||||
@@ -350,9 +364,27 @@ export class AudioService {
|
||||
|
||||
this.playing = true;
|
||||
try {
|
||||
const file = await this.tts.synthesize(item.text, item.options);
|
||||
await this.applyDuck(this.shouldDuck());
|
||||
await this.manager.play(file);
|
||||
// Streaming word mode: the wrapper writes the WAV while it synthesizes, so
|
||||
// playback starts on the first word instead of after the whole line.
|
||||
let streaming: Promise<void> | null = null;
|
||||
const options: TtsOptions = { ...item.options };
|
||||
if (item.options.stream === true) {
|
||||
options.onOutputPath = path => {
|
||||
streaming = (async () => {
|
||||
await this.applyDuck(this.shouldDuck());
|
||||
await this.playWhenReady(path);
|
||||
})();
|
||||
};
|
||||
}
|
||||
|
||||
const file = await this.tts.synthesize(item.text, options);
|
||||
const pending: Promise<void> | null = streaming;
|
||||
if (pending) {
|
||||
await pending;
|
||||
} else {
|
||||
await this.applyDuck(this.shouldDuck());
|
||||
await this.manager.play(file);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[audio] speak failed:", (error as Error).message);
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user