Skip to content

Instantly share code, notes, and snippets.

@lmcarreiro
Last active July 29, 2021 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmcarreiro/4a68adea0d53379f41a9d9fb4a685709 to your computer and use it in GitHub Desktop.
Save lmcarreiro/4a68adea0d53379f41a9d9fb4a685709 to your computer and use it in GitHub Desktop.
STT+VAD article - useSpeechToText.ts - 2
// ...
const mutedRef = React.useRef<boolean>(muted);
// We use a ref to check if the microphone is muted or not, to avoid recreating
// the Azure's SpeechRecognizer instance every time we mute/unmute.
React.useEffect(() => {
mutedRef.current = muted;
}, [muted]);
// ...
// Initialize the Azure Speech to Text instance and bind the necessary events
React.useEffect(() => {
// ...
const onAudioProcess = (ev: AudioProcessingEvent) => {
const block = {
duration: ev.inputBuffer.duration,
bytes: convertFloat32ToInt16(ev.inputBuffer.getChannelData(0)),
};
if (!mutedRef.current) {
pushStream.write(block.bytes);
}
};
// ...
}, [speechToTextEnabled, newMessage, stream]);
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment