fix: type STT text char-by-char, increase TTS debounce to 2s

This commit is contained in:
Gan, Jimmy
2026-02-26 15:50:17 +08:00
parent af74552a01
commit 47ab062edd
+13 -4
View File
@@ -34,7 +34,7 @@ export class VoiceSession {
_onOutput() {
if (!this.ttsEnabled || this.listening) return;
clearTimeout(this._debounce);
this._debounce = setTimeout(() => this._readBuffer(), 600);
this._debounce = setTimeout(() => this._readBuffer(), 2000);
}
_readBuffer() {
@@ -84,6 +84,17 @@ export class VoiceSession {
this._notify();
}
_typeText(text) {
let i = 0;
const next = () => {
if (i < text.length && this.ws?.readyState === 1) {
this.ws.send(new TextEncoder().encode(text[i++]));
setTimeout(next, 20);
}
};
next();
}
toggleMic() {
this.listening ? this.stopListening() : this.startListening();
}
@@ -101,9 +112,7 @@ export class VoiceSession {
for (let i = e.resultIndex; i < e.results.length; i++) {
if (e.results[i].isFinal) {
const text = e.results[i][0].transcript.trim();
if (text && this.ws?.readyState === 1) {
this.ws.send(new TextEncoder().encode(text + "\r"));
}
if (text && this.ws?.readyState === 1) this._typeText(text + "\r");
}
}
};