Skip to content

fix: Slight pauses while speaking cause reco to halt and start a new phrase. #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 23 additions & 9 deletions code/frontend/src/pages/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,39 @@ 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
Loading