Set the component state and check if there is valid token.
constructor(props) {
super(props);
this.state = {
displayText: 'INITIALIZED: ready to test speech...'
}
}
async componentDidMount() {
// check for valid speech key/region
const tokenRes = await getTokenOrRefresh();
if (tokenRes.authToken === null) {
this.setState({
displayText: 'FATAL_ERROR: ' + tokenRes.error
});
}
}
Function to get stream from microphone and make a call for transcription. We call this function when the microphone click happens.
async sttFromMic() {
const tokenObj = await getTokenOrRefresh();
const speechConfig = speechsdk.SpeechConfig.fromAuthorizationToken(tokenObj.authToken, tokenObj.region);
speechConfig.speechRecognitionLanguage = 'en-US';
const audioConfig = speechsdk.AudioConfig.fromDefaultMicrophoneInput();
const recognizer = new speechsdk.SpeechRecognizer(speechConfig, audioConfig);
this.setState({
displayText: 'speak into your microphone...'
});
recognizer.recognizeOnceAsync(result => {
let displayText;
if (result.reason === ResultReason.RecognizedSpeech) {
displayText = `RECOGNIZED: Text=${result.text}`
} else {
displayText = 'ERROR: Speech was cancelled or could not be recognized. Ensure your microphone is working properly.';
}
this.setState({
displayText: displayText
});
});
}