Skip to content

Instantly share code, notes, and snippets.

@onEnterFrame
Created November 26, 2017 20:34
Show Gist options
  • Save onEnterFrame/cc61792ed82edae66781c961a2257389 to your computer and use it in GitHub Desktop.
Save onEnterFrame/cc61792ed82edae66781c961a2257389 to your computer and use it in GitHub Desktop.
// SDK USAGE
// On doument load resolve the SDK dependecy
function Initialize(onComplete) {
require(["Speech.Browser.Sdk"], function(SDK) {
onComplete(SDK);
});
}
// Setup the recongizer
function RecognizerSetup(SDK, recognitionMode, language, format, subscriptionKey) {
var recognizerConfig = new SDK.RecognizerConfig(
new SDK.SpeechConfig(
new SDK.Context(
new SDK.OS(navigator.userAgent, "Browser", null),
new SDK.Device("SpeechSample", "SpeechSample", "1.0.00000"))),
recognitionMode, // SDK.RecognitionMode.Interactive (Options - Interactive/Conversation/Dictation>)
language, // Supported laguages are specific to each recognition mode. Refer to docs.
format); // SDK.SpeechResultFormat.Simple (Options - Simple/Detailed)
// Alternatively use SDK.CognitiveTokenAuthentication(fetchCallback, fetchOnExpiryCallback) for token auth
var authentication = new SDK.CognitiveSubscriptionKeyAuthentication(subscriptionKey);
return SDK.CreateRecognizer(recognizerConfig, authentication);
}
// Start the recognition
function RecognizerStart(SDK, recognizer) {
recognizer.Recognize(function(event) {
/*
Alternative syntax for typescript devs.
if (event instanceof SDK.RecognitionTriggeredEvent)
*/
switch (event.Name) {
case "RecognitionTriggeredEvent":
UpdateStatus("Initializing");
break;
case "ListeningStartedEvent":
UpdateStatus("Listening");
break;
case "RecognitionStartedEvent":
UpdateStatus("Listening_Recognizing");
break;
case "SpeechStartDetectedEvent":
UpdateStatus("Listening_DetectedSpeech_Recognizing");
console.log(JSON.stringify(event.Result)); // check console for other information in result
break;
case "SpeechHypothesisEvent":
UpdateRecognizedHypothesis(event.Result.Text);
console.log(JSON.stringify(event.Result)); // check console for other information in result
break;
case "SpeechEndDetectedEvent":
OnSpeechEndDetected();
UpdateStatus("Processing_Adding_Final_Touches");
console.log(JSON.stringify(event.Result)); // check console for other information in result
break;
case "SpeechSimplePhraseEvent":
UpdateRecognizedPhrase(JSON.stringify(event.Result, null, 3));
break;
case "SpeechDetailedPhraseEvent":
UpdateRecognizedPhrase(JSON.stringify(event.Result, null, 3));
break;
case "RecognitionEndedEvent":
OnComplete();
UpdateStatus("Idle");
console.log(JSON.stringify(event)); // Debug information
break;
}
})
.On(function() {
// The request succeeded. Nothing to do here.
},
function(error) {
console.error(error);
});
}
// Stop the Recognition.
function RecognizerStop(SDK, recognizer) {
// recognizer.AudioSource.Detach(audioNodeId) can be also used here. (audioNodeId is part of ListeningStartedEvent)
recognizer.AudioSource.TurnOff();
}
//Browser Hooks
var SDK;
var recognizer;
var previousSubscriptionKey;
document.addEventListener("DOMContentLoaded", function() {
key = "YOUR_API_KEY";
Initialize(function(speechSdk) {
SDK = speechSdk;
});
});
function Setup() {
recognizer = RecognizerSetup(SDK, SDK.RecognitionMode.Interactive, "en-US", SDK.SpeechResultFormat["detailed"], key);
}
function UpdateStatus(status) {
}
function UpdateRecognizedHypothesis(text) {
//As new results stream in from the service update the CP variable
window.cpAPIInterface.setVariableValue("hypothesis", text);
}
function OnSpeechEndDetected() {
console.log('OnSpeechEndDetected')
}
function UpdateRecognizedPhrase(json) {
console.log("phase", json)
}
function OnComplete() {
//when done trigger CP to process the results
console.log('OnComplete')
var box = $('#process_recording')[0];
cp.clickHandler(box);
}
//start and stop functions
function StartListening() {
//if first time setup and config the connection.
if (!recognizer || previousSubscriptionKey != key) {
previousSubscriptionKey = key;
Setup();
}
var box = $('#show_text')[0];
cp.clickHandler(box);
window.cpAPIInterface.setVariableValue("hypothesis", "");
RecognizerStart(SDK, recognizer);
};
function StopListening() {
RecognizerStop(SDK);
var box = $('#process_recording')[0];
cp.clickHandler(box);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment