<!DOCTYPE html>
<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="warning" style="display: none">
<h1 style="font-weight:100;">Speech Recognition Speech SDK not found (microsoft.cognitiveservices.speech.sdk.bundle.js missing).</h1>
</div>
<div id="content" style="display:none">
<table width="100%">
<tr style='visibility:collapse'>
<td align="right"><a href="https://docs.microsoft.com/azure/cognitive-services/speech-service/get-started" target="_blank">Subscription</a>:</td>
<td><input id="subscriptionKey" type="text" size="40" value="subscription"></td>
</tr>
<tr style='visibility:collapse'>
<td align="right">Region</td>
<td><input id="serviceRegion" type="text" size="40" value="YourServiceRegion"></td>
</tr>
<tr>
<td colspan="2"><button id="startRecognizeOnceAsyncButton">Start recognition</button></td>
</tr>
<tr>
<td colspan="2"><textarea id="phraseDiv" style="display: inline-block;width:100%;height:200px"></textarea></td>
</tr>
</table>
</div>
<!-- </uidiv> -->
<!-- <speechsdkref> -->
<!-- Speech SDK reference sdk. -->
<script src="../scripts/jquery.js"></script>
<script src="../scripts/json2.js"></script>
<script src="../scripts/microsoft.cognitiveservices.speech.sdk.bundle.js"></script>
<script src="../scripts/XrmServiceToolkit.min.js"></script>
<!-- </speechsdkref> -->
<!-- <quickstartcode> -->
<!-- Speech SDK USAGE -->
<script>
var adacta = adacta || {};
var Xrm = parent.Xrm;
adacta.AddNote = function (entityId, entityName, noteSubject, noteText) {
var note = new XrmServiceToolkit.Soap.BusinessEntity("annotation");
note.attributes["subject"] = noteSubject;
note.attributes["notetext"] = noteText;
note.attributes["objectid"] = { id: entityId, logicalName: entityName, type: "EntityReference" };
return XrmServiceToolkit.Soap.Create(note);
}
adacta.SaveText = function(text) {
var accountId = Xrm.Page.data.entity.getId();
var entityName = Xrm.Page.data.entity.getEntityName();
adacta.AddNote(accountId, entityName, "Recognized speech", text);
}
// status fields and start button in UI
var phraseDiv;
var startRecognizeOnceAsyncButton;
// subscription key and region for speech services.
var subscriptionKey, serviceRegion;
var authorizationToken;
var SpeechSDK;
var recognizer;
document.addEventListener("DOMContentLoaded", function () {
startRecognizeOnceAsyncButton = document.getElementById("startRecognizeOnceAsyncButton");
subscriptionKey = document.getElementById("subscriptionKey");
serviceRegion = document.getElementById("serviceRegion");
subscriptionKey.value = "a90d8fa8612a436fbaf57d42c3fab4a5";
serviceRegion.value = "westus";
phraseDiv = document.getElementById("phraseDiv");
startRecognizeOnceAsyncButton.addEventListener("click", function () {
startRecognizeOnceAsyncButton.disabled = true;
phraseDiv.innerHTML = "";
// if we got an authorization token, use the token. Otherwise use the provided subscription key
var speechConfig;
if (authorizationToken) {
speechConfig = SpeechSDK.SpeechConfig.fromAuthorizationToken(authorizationToken, serviceRegion.value);
} else {
if (subscriptionKey.value === "" || subscriptionKey.value === "subscription") {
alert("Please enter your Microsoft Cognitive Services Speech subscription key!");
return;
}
speechConfig = SpeechSDK.SpeechConfig.fromSubscription(subscriptionKey.value, serviceRegion.value);
}
speechConfig.speechRecognitionLanguage = "en-US";
var audioConfig = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, audioConfig);
recognizer.recognizeOnceAsync(
function (result) {
startRecognizeOnceAsyncButton.disabled = false;
phraseDiv.innerHTML += result.text;
window.console.log(result);
recognizer.close();
recognizer = undefined;
adacta.SaveText(result.text);
},
function (err) {
startRecognizeOnceAsyncButton.disabled = false;
phraseDiv.innerHTML += err;
window.console.log(err);
recognizer.close();
recognizer = undefined;
});
});
if (!!window.SpeechSDK) {
SpeechSDK = window.SpeechSDK;
startRecognizeOnceAsyncButton.disabled = false;
document.getElementById('content').style.display = 'block';
document.getElementById('warning').style.display = 'none';
// in case we have a function for getting an authorization token, call it.
if (typeof RequestAuthorizationToken === "function") {
RequestAuthorizationToken();
}
}
});
</script>
<!-- </quickstartcode> -->
</body>
</html>