Skip to content

Commit

Permalink
fix: Slight pauses while speaking cause reco to halt and start a new …
Browse files Browse the repository at this point in the history
…phrase. (#982)

Co-authored-by: Ross Smith <[email protected]>
Co-authored-by: Adam Dougal <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2024
1 parent ce9281a commit e10a0fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
6 changes: 5 additions & 1 deletion code/frontend/src/components/QuestionInput/QuestionInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ export const QuestionInput = ({
const [liveRecognizedText, setLiveRecognizedText] = useState<string>("");
const [microphoneIconActive, setMicrophoneIconActive] =
useState<boolean>(false);

const [isTextAreaDisabled, setIsTextAreaDisabled] = useState(false);
useEffect(() => {
if (isRecognizing) {
setLiveRecognizedText(recognizedText);
setIsTextAreaDisabled(true)
setMicrophoneIconActive(true); // Set microphone icon to active (blue)
} else {
setIsTextAreaDisabled(false)
setMicrophoneIconActive(false); // Set microphone icon to inactive
}
}, [recognizedText, isRecognizing]);
Expand Down Expand Up @@ -81,6 +83,8 @@ export const QuestionInput = ({
<Stack horizontal className={styles.questionInputContainer}>
{/* Text Input Field */}
<TextField
style={{backgroundColor: 'white'}}
disabled={isTextAreaDisabled}
className={styles.questionInputTextArea}
placeholder={placeholder}
multiline
Expand Down
31 changes: 23 additions & 8 deletions code/frontend/src/pages/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,40 @@ const Chat = () => {

return abortController.abort();
};
// Buffer to store recognized text
let recognizedTextBuffer = "";
let currentSentence = "";

const startSpeechRecognition = async () => {
if (!isRecognizing) {
setIsRecognizing(true);

recognizerRef.current = await multiLingualSpeechRecognizer(); // Store the recognizer in the ref

recognizerRef.current.recognized = (s, e) => {
if (e.result.reason === ResultReason.RecognizedSpeech) {
const recognized = e.result.text;
setUserMessage(recognized);
setRecognizedText(recognized);
let recognizedText = e.result.text.trim();
// Append current sentence to buffer if it's not empty
if (currentSentence) {
recognizedTextBuffer += ` ${currentSentence.trim()}`;
currentSentence = "";
}
// Start new sentence
currentSentence += ` ${recognizedText}`;
//set text in textarea
setUserMessage((recognizedTextBuffer + currentSentence).trim());
setRecognizedText((recognizedTextBuffer + currentSentence).trim());
}
};

recognizerRef.current.startContinuousRecognitionAsync(() => {
setIsRecognizing(true);
setIsListening(true);
});
recognizerRef.current.startContinuousRecognitionAsync(
() => {
setIsRecognizing(true);
setIsListening(true);
},
error => {
console.error(`Error starting recognition: ${error}`);
}
);
}
};

Expand Down

0 comments on commit e10a0fa

Please sign in to comment.