Smooth Talk playback and gate audio tail

This commit is contained in:
Mikei386
2026-09-03 23:19:51 +02:00
parent 65ce9642cc
commit 023c2ee40d
4 changed files with 22 additions and 10 deletions
+14 -6
View File
@@ -246,18 +246,26 @@ class AthenaTalkBridge {
this.req.onTranscript?.("assistant", text, true);
const pcm = await this.synthesize(text, controller.signal);
if (generation !== this.generation || !this.isConnected()) return;
// Feed the relay 100 ms batches slightly faster than realtime. OpenClaw
// splits these into its native 20 ms frames. The small lead gives the
// client a jitter buffer and avoids the crackling caused by timer and
// WebSocket scheduling gaps at exact 20 ms pacing.
const frameBytes = 24000 * 2 / 10;
// Feed OpenClaw in its native 20 ms frame size. Sending larger bursts
// made the relay split several frames at once; slow-client protection
// could then drop individual frames and produce audible holes.
const frameBytes = 24000 * 2 / 50;
const playbackStartedAt = Date.now();
for (let offset = 0; offset < pcm.length; offset += frameBytes) {
if (controller.signal.aborted || generation !== this.generation || !this.isConnected()) {
return;
}
this.req.onAudio?.(pcm.subarray(offset, Math.min(offset + frameBytes, pcm.length)));
await new Promise((resolve) => setTimeout(resolve, 80));
// Run just ahead of realtime so the client builds a small jitter
// buffer without flooding the WebSocket queue.
await new Promise((resolve) => setTimeout(resolve, 18));
}
// Transmission is intentionally a little faster than playback. Keep the
// microphone gated until the client has consumed that buffered tail;
// otherwise the last spoken words are transcribed again as user input.
const audioDurationMs = pcm.length / (24000 * 2) * 1000;
const remainingPlaybackMs = Math.max(0, audioDurationMs - (Date.now() - playbackStartedAt));
await new Promise((resolve) => setTimeout(resolve, remainingPlaybackMs + 300));
this.req.onResponseDone?.({ status: "completed", responseId });
this.req.onEvent?.({ direction: "server", type: "response.done", responseId });
} catch (error) {