重新启动连续识别不侦听麦克风



我在一个电子应用程序中使用微软认知语音SDK中的IntentRecognizerSpeechSynthesizer,我在运行语音合成时使用stopContinuousRecognitionAsync,合成停止后,我再次使用startContinuousRecognitionAsync开始识别。

函数正常运行与回调,但不听任何我说的,并不断触发SpeechSDK.ResultReason.NoMatch。下面是我的代码:

const SpeechSDK = require("microsoft-cognitiveservices-speech-sdk");
const speechConfig = SpeechSDK.SpeechConfig.fromSubscription(LUIS_API_KEY, COGNITIVE_SERVICE_REGION);
class CognitiveSpeech {
constructor() {
speechConfig.speechRecognitionLanguage = "en-US"; //put language from settings later
var lm = SpeechSDK.LanguageUnderstandingModel.fromAppId(remote.process.env.LUIS_APP_ID);
var audioConfig  = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
recognizer = new SpeechSDK.IntentRecognizer(speechConfig, audioConfig);
recognizer.addAllIntents(lm);

recognizer.recognizing = (s, e) => {
console.log(`RECOGNIZING: Text=${e.result.text}`, e);
};

recognizer.recognized = (s, e) => {
console.log("(continuation) Reason: " + SpeechSDK.ResultReason[e.result.reason]);
switch (e.result.reason) {
case SpeechSDK.ResultReason.RecognizedSpeech:
console.log(" Text: " + e.result.text);
break;
case SpeechSDK.ResultReason.RecognizedIntent:
console.log(" Text: " + e.result.text + " IntentId: " + e.result.intentId);
///
DO WORK
this.synthesizeSpeech('Sure thing');
///
break;
case SpeechSDK.ResultReason.NoMatch:
var noMatchDetail = SpeechSDK.NoMatchDetails.fromResult(e.result);
if(SpeechSDK.NoMatchReason[noMatchDetail.reason] != 'InitialSilenceTimeout')
console.log(" NoMatchReason: " + SpeechSDK.NoMatchReason[noMatchDetail.reason]);
break;
case SpeechSDK.ResultReason.Canceled:
var cancelDetails = SpeechSDK.CancellationDetails.fromResult(e.result);
console.log(" CancellationReason: " + SpeechSDK.CancellationReason[cancelDetails.reason]);

if (cancelDetails.reason === SpeechSDK.CancellationReason.Error) {
console.log(": " + cancelDetails.errorDetails);
}
break;
}
};

recognizer.canceled = (s, e) => {
console.log(`CANCELED: Reason=${e.reason}`);

if (e.reason == CancellationReason.Error) {
console.log(`"CANCELED: ErrorCode=${e.errorCode}`);
console.log(`"CANCELED: ErrorDetails=${e.errorDetails}`);
console.log("CANCELED: Did you update the subscription info?");
}

recognizer.stopContinuousRecognitionAsync();
};

recognizer.sessionStopped = (s, e) => {
console.log("Session stopped event.");
recognizer.stopContinuousRecognitionAsync();
};
}
startRecognition() {
recognizer.startContinuousRecognitionAsync(()=>{
console.log('Listenning...');
}, err => {
console.log(err);
});

}
stopRecognition() {
recognizer.stopContinuousRecognitionAsync(()=>{
console.log('Stopped Speech Recognition');
}, err => {
console.log(err);
});
}
synthesizeSpeech(string) {
this.stopRecognition();
const speechSynthesisConfig = SpeechSDK.SpeechConfig.fromSubscription(COGNITIVE_API_KEY, COGNITIVE_SERVICE_REGION);
const audioSynthesisConfig = SpeechSDK.AudioConfig.fromDefaultSpeakerOutput();
const synthesizer = new SpeechSDK.SpeechSynthesizer(speechSynthesisConfig, audioSynthesisConfig);
synthesizer.speakSsmlAsync(
'<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US">
<voice name="en-US-AriaRUS">
<mstts:express-as style="chat">'
+string+
'</mstts:express-as>
</voice>
</speak>', //put voice from settings letter
result => {
if (result) {
if (result.errorDetails) {
console.error(result.errorDetails);
} else {
console.log(JSON.stringify(result));
}
this.startRecognition();
synthesizer.close();
}
},
error => {
console.log(error);
synthesizer.close();
});
}
}
谁能告诉我这幅画出了什么问题?

问题可能是您没有按照您期望的顺序获得您期望的回调。我能够得到以下工作:

<html>
<head>
<title>Microsoft Cognitive Services Speech SDK JavaScript Quickstart</title>
<meta charset="utf-8" />
</head>
<body style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:13px;">
<!-- <uidiv> -->
<div id="content">
<table width="100%">
<tr>
<td></td>
<td><h1 style="font-weight:500;">Microsoft Cognitive Services Speech SDK JavaScript Quickstart</h1></td>
</tr>
<tr>
<td></td>
<td><button id="startRecognitionBtn">Start recognition</button></td>
</tr>
<tr>
<td></td>
<td><button id="stopRecognitionBtn">Stop recognition</button></td>
</tr>
<tr>
<td align="right" valign="top">Results</td>
<td><textarea id="phraseDiv" style="display: inline-block;width:500px;height:200px"></textarea></td>
</tr>
</table>
</div>
<!-- </uidiv> -->
<!-- <speechsdkref> -->
<!-- Speech SDK reference sdk. -->
<script src="https://aka.ms/csspeech/jsbrowserpackageraw"></script>
<!-- </speechsdkref> -->
<!-- <quickstartcode> -->
<!-- Speech SDK USAGE -->
<script>
// status fields and start button in UI
var phraseDiv;
var startRecognitionBtn, stopRecognitionBtn;
var SpeechSDK;
var speechConfig;
var recognizer, synthesizer;
var textToSpeak;
document.addEventListener("DOMContentLoaded", function () {
startRecognitionBtn = document.getElementById("startRecognitionBtn");
stopRecognitionBtn = document.getElementById("stopRecognitionBtn");
phraseDiv = document.getElementById("phraseDiv");
const getSpeechConfig = () => {
if (!speechConfig) {
speechConfig = new SpeechSDK.SpeechConfig.fromSubscription("KEY", "REGION");
speechConfig.speechRecognitionLanguage = "en-US";
}
return speechConfig;
};
stopRecognitionBtn.addEventListener("click", function () {
startRecognitionBtn.disabled = false;
if(!!recognizer) {
recognizer.stopContinuousRecognitionAsync(
() => {
},
function (err) {
startRecognitionBtn.disabled = false;
stopRecognitionBtn.disabled = false;
phraseDiv.innerHTML += err;
window.console.log(err);
recognizer.close();
recognizer = undefined;
});
}
});
startRecognitionBtn.addEventListener("click", function () {
startRecognitionBtn.disabled = true;
stopRecognitionBtn.disabled = false;
phraseDiv.innerHTML = "";
var sc = getSpeechConfig();
var lm = SpeechSDK.LanguageUnderstandingModel.fromAppId("APP_ID");
var audioConfig  = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
recognizer = new SpeechSDK.IntentRecognizer(sc, audioConfig);
recognizer.addAllIntents(lm);
recognizer.canceled = function (_, event) {
window.console.log(event);
if (event.reason === sdk.CancellationReason.Error) {
window.console.log(event.errorDetails);
}
};
recognizer.recognizing = (_, event) => {
console.log('(recognizing)', { text: event.result.text });
//textToSpeak = event.result.text;
textToSpeak = "Sure thing";
phraseDiv.innerHTML = event.result.text;
};
recognizer.recognized = (_, event) => {
console.log(`(recognized)  Reason: ${sdk.ResultReason[event.result.reason]} Text: ${event.result.text}`);
startRecognitionBtn.disabled = false;
startRecognitionBtn.disabled = false;
phraseDiv.innerHTML = event.result.text;
};
recognizer.sessionStarted = (_, event) => {
console.log(`(sessionStarted) SessionId: ${event.sessionId}`);
textToSpeak = "";
};
recognizer.sessionStopped = (_, event) => {
console.log(`(sessionStopped) SessionId: ${event.sessionId}`);
startRecognitionBtn.disabled = false;
};
recognizer.speechStartDetected = (_, event) => {
console.log(`(speechStartDetected) SessionId: ${event.sessionId}`);
};
recognizer.speechEndDetected = (_, event) => {
console.log(`(speechEndDetected) SessionId: ${event.sessionId}`);
startRecognitionBtn.disabled = false;
recognizer.stopContinuousRecognitionAsync(
() => {},
function (err) {
startRecognitionBtn.disabled = false;
stopRecognitionBtn.disabled = false;
phraseDiv.innerHTML += err;
window.console.log(err);
recognizer.close();
recognizer = undefined;
});
startSynthesis();
};
startRecognition();
});
const startRecognition = () => {
recognizer?.startContinuousRecognitionAsync(
undefined,
function (err) {
startRecognitionBtn.disabled = false;
phraseDiv.innerHTML += err;
window.console.log(err);
recognizer.close();
recognizer = undefined;
});
};
const startSynthesis = () => {
if(!synthesizer) {
var sc2 = getSpeechConfig();

var player = new SpeechSDK.SpeakerAudioDestination();
player.onAudioEnd = function (_) {
window.console.log("playback finished");
phraseDiv.innerHTML += "playback finished" + "rn";
};
var audioOutConfig  = SpeechSDK.AudioConfig.fromSpeakerOutput(player);
synthesizer = new SpeechSDK.SpeechSynthesizer(sc2, audioOutConfig);
}
phraseDiv.innerHTML += `detected spoken phrase: ${textToSpeak}rn`;
synthesizer.speakSsmlAsync(
`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US">
<voice name="en-US-AriaRUS">
<mstts:express-as style="chat">${textToSpeak}</mstts:express-as>
</voice>
</speak>`, //put voice from settings letter,
(result) => {          
if (result.reason === SpeechSDK.ResultReason.SynthesizingAudioCompleted) {
phraseDiv.innerHTML += "synthesis finished" + "rn";
} else if (result.reason === SpeechSDK.ResultReason.Canceled) {
phraseDiv.innerHTML += "synthesis failed. Error detail: " + result.errorDetails;
}
window.console.log(result);
closeSynth();
},
(err) => {
window.console.log(err);
closeSynth();
});
};
const closeSynth = () => {
synthesizer.close();
synthesizer = undefined;
};
if (!!window.SpeechSDK) {
SpeechSDK = window.SpeechSDK;
startRecognitionBtn.disabled = false;
}
});
</script>
<!-- </quickstartcode> -->
</body>
</html>

最新更新