Skip to content

Commit 0786f14

Browse files
Fix speech recognition loop, disable broken fallback path, add failure tracking
1 parent 0bb59db commit 0786f14

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

ask.html

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,13 @@
808808
// ── Speech Recognition (browser-native, no API key needed) ──
809809
let speechRecognition = null;
810810
let recognitionFinalTranscript = '';
811+
let srFailCount = 0;
811812

812813
function startSpeechRecognition() {
813814
try {
814815
const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
815-
if (!SR) { fallbackStartRecording(); return; }
816+
if (!SR) { srFailCount++; setLiveState('idle'); return; }
817+
if (srFailCount > 3) { setLiveState('idle'); return; }
816818
speechRecognition = new SR();
817819
speechRecognition.lang = 'en-US';
818820
speechRecognition.interimResults = false;
@@ -829,36 +831,32 @@
829831
speechRecognition = null;
830832
recognitionFinalTranscript = '';
831833
if (transcript && liveActive) {
834+
srFailCount = 0;
832835
processTranscript(transcript);
833836
} else if (liveActive) {
837+
srFailCount++;
834838
startLiveLoop();
835839
}
836840
};
837841

838842
speechRecognition.onerror = (event) => {
839843
speechRecognition = null;
840-
if (event.error === 'no-speech' || event.error === 'aborted') {
841-
if (liveActive) startLiveLoop();
842-
return;
843-
}
844+
srFailCount++;
844845
console.error('Speech recognition error:', event.error);
845-
fallbackStartRecording();
846+
if (liveActive) startLiveLoop();
846847
};
847848

848849
speechRecognition.start();
849850
} catch (e) {
850-
fallbackStartRecording();
851+
srFailCount++;
852+
if (liveActive) startLiveLoop();
851853
}
852854
}
853855

854856
function stopSpeechRecognition() {
855857
try { if (speechRecognition) { speechRecognition.abort(); speechRecognition = null; } } catch (e) {}
856858
}
857859

858-
function fallbackStartRecording() {
859-
if (liveActive) startRecording();
860-
}
861-
862860
async function startRecording() {
863861
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
864862
recordingStream = stream;

0 commit comments

Comments
 (0)