Skip to content

Commit

Permalink
Fix SpeechRecognition on Phone
Browse files Browse the repository at this point in the history
  • Loading branch information
hlohaus committed Apr 13, 2024
1 parent 97d3459 commit f323e88
Showing 1 changed file with 2 additions and 16 deletions.
18 changes: 2 additions & 16 deletions g4f/gui/client/static/js/chat.v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,38 +1261,27 @@ if (SpeechRecognition) {
recognition.interimResults = true;
recognition.maxAlternatives = 1;

let shouldStop = false;
function may_stop() {
if (microLabel.classList.contains("recognition")) {
shouldStop = true
recognition.stop();
}
}

let startValue;
let timeoutHandle;
let lastDebounceTranscript;
recognition.onstart = function() {
microLabel.classList.add("recognition");
startValue = messageInput.value;
lastDebounceTranscript = "";
timeoutHandle = window.setTimeout(may_stop, 10000);
};
recognition.onend = function() {
if (shouldStop) {
microLabel.classList.remove("recognition");
messageInput.focus();
} else {
recognition.start();
}
microLabel.classList.remove("recognition");
messageInput.focus();
};
recognition.onresult = function(event) {
window.clearTimeout(timeoutHandle);
timeoutHandle = window.setTimeout(may_stop, 8000);
if (!event.results) {
return;
}

let result = event.results[event.resultIndex];
let isFinal = result.isFinal && (result[0].confidence > 0);
let transcript = result[0].transcript;
Expand All @@ -1314,13 +1303,10 @@ if (SpeechRecognition) {

microLabel.addEventListener("click", () => {
if (microLabel.classList.contains("recognition")) {
window.clearTimeout(timeoutHandle);
shouldStop = true;
recognition.stop();
} else {
const lang = document.getElementById("recognition-language")?.value;
recognition.lang = lang || navigator.language;
shouldStop = false;
recognition.start();
}
});
Expand Down

0 comments on commit f323e88

Please sign in to comment.