Last active
July 29, 2021 21:12
-
-
Save lmcarreiro/4a68adea0d53379f41a9d9fb4a685709 to your computer and use it in GitHub Desktop.
STT+VAD article - useSpeechToText.ts - 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
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