|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Microsoft Cognitive Services Speech SDK JavaScript Quickstart</title> |
| 5 | + <meta charset="utf-8" /> |
| 6 | +</head> |
| 7 | +<body style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:13px;"> |
| 8 | + <!-- <uidiv> --> |
| 9 | + <div id="warning"> |
| 10 | + <h1 style="font-weight:500;">Speech Recognition Speech SDK not found (microsoft.cognitiveservices.speech.sdk.bundle.js missing).</h1> |
| 11 | + </div> |
| 12 | + |
| 13 | + <div id="content" style="display:none"> |
| 14 | + <table width="100%"> |
| 15 | + <tr> |
| 16 | + <td></td> |
| 17 | + <td><h1 style="font-weight:500;">Microsoft Cognitive Services Speech SDK JavaScript Quickstart</h1></td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td align="right"><a href="https://docs.microsoft.com/azure/cognitive-services/speech-service/get-started" target="_blank">Subscription</a>:</td> |
| 21 | + <td><input id="subscriptionKey" type="text" size="40" value="subscription"></td> |
| 22 | + </tr> |
| 23 | + <tr> |
| 24 | + <td align="right">Region</td> |
| 25 | + <td><input id="serviceRegion" type="text" size="40" value="YourServiceRegion"></td> |
| 26 | + </tr> |
| 27 | + <tr> |
| 28 | + <td align="right">Source Language</td> |
| 29 | + <td><select id="languageSourceOptions"> |
| 30 | + <option value="ar-EG">Arabic - EG</option> |
| 31 | + <option selected="selected" value="de-DE">German - DE</option> |
| 32 | + <option value="en-US">English - US</option> |
| 33 | + <option value="es-ES">Spanish - ES</option> |
| 34 | + <option value="fi-FI">Finnish - FI</option> |
| 35 | + <option value="fr-FR">French - FR</option> |
| 36 | + <option value="hi-IN">Hindi - IN</option> |
| 37 | + <option value="it-IT">Italian - IT</option> |
| 38 | + <option value="ja-JP">Japanese - JP</option> |
| 39 | + <option value="ko-KR">Korean - KR</option> |
| 40 | + <option value="pl-PL">Polish - PL</option> |
| 41 | + <option value="pt-BR">Portuguese - BR</option> |
| 42 | + <option value="ru-RU">Russian - RU</option> |
| 43 | + <option value="sv-SE">Swedish - SE</option> |
| 44 | + <option value="zh-CN">Chinese - CN</option> |
| 45 | + </select></td> |
| 46 | + </tr> |
| 47 | + <tr> |
| 48 | + <td align="right">Target Language</td> |
| 49 | + <td><select id="languageTargetOptions"> |
| 50 | + <option value="ar-EG">Arabic - EG</option> |
| 51 | + <option selected="selected" value="de-DE">German - DE</option> |
| 52 | + <option value="en-US">English - US</option> |
| 53 | + <option value="es-ES">Spanish - ES</option> |
| 54 | + <option value="fi-FI">Finnish - FI</option> |
| 55 | + <option value="fr-FR">French - FR</option> |
| 56 | + <option value="hi-IN">Hindi - IN</option> |
| 57 | + <option value="it-IT">Italian - IT</option> |
| 58 | + <option value="ja-JP">Japanese - JP</option> |
| 59 | + <option value="ko-KR">Korean - KR</option> |
| 60 | + <option value="pl-PL">Polish - PL</option> |
| 61 | + <option value="pt-BR">Portuguese - BR</option> |
| 62 | + <option value="ru-RU">Russian - RU</option> |
| 63 | + <option value="sv-SE">Swedish - SE</option> |
| 64 | + <option value="zh-CN">Chinese - CN</option> |
| 65 | + </select></td> |
| 66 | + </tr> |
| 67 | + <tr> |
| 68 | + <td></td> |
| 69 | + <td><button id="startRecognizeOnceAsyncButton">Start recognition</button></td> |
| 70 | + </tr> |
| 71 | + <tr> |
| 72 | + <td align="right" valign="top">Results</td> |
| 73 | + <td><textarea id="phraseDiv" style="display: inline-block;width:500px;height:200px"></textarea></td> |
| 74 | + </tr> |
| 75 | + </table> |
| 76 | + </div> |
| 77 | + <!-- </uidiv> --> |
| 78 | + |
| 79 | + <!-- <speechsdkdiv> --> |
| 80 | + <!-- Speech SDK reference sdk. --> |
| 81 | + <script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script> |
| 82 | + <!-- </speechsdkdiv> --> |
| 83 | + |
| 84 | + <!-- <authorizationfunction> --> |
| 85 | + <!-- Speech SDK Authorization token --> |
| 86 | + <script> |
| 87 | + // Note: Replace the URL with a valid endpoint to retrieve |
| 88 | + // authorization tokens for your subscription. |
| 89 | + var authorizationEndpoint = "token.php"; |
| 90 | + |
| 91 | + function RequestAuthorizationToken() { |
| 92 | + if (authorizationEndpoint) { |
| 93 | + var a = new XMLHttpRequest(); |
| 94 | + a.open("GET", authorizationEndpoint); |
| 95 | + a.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); |
| 96 | + a.send(""); |
| 97 | + a.onload = function() { |
| 98 | + var token = JSON.parse(atob(this.responseText.split(".")[1])); |
| 99 | + serviceRegion.value = token.region; |
| 100 | + authorizationToken = this.responseText; |
| 101 | + subscriptionKey.disabled = true; |
| 102 | + subscriptionKey.value = "using authorization token (hit F5 to refresh)"; |
| 103 | + console.log("Got an authorization token: " + token); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + </script> |
| 108 | + <!-- </authorizationfunction> --> |
| 109 | + |
| 110 | + <!-- <quickstartcode> --> |
| 111 | + <!-- Speech SDK USAGE --> |
| 112 | + <script> |
| 113 | + // status fields and start button in UI |
| 114 | + var phraseDiv; |
| 115 | + var startRecognizeOnceAsyncButton; |
| 116 | + |
| 117 | + // subscription key and region for speech services. |
| 118 | + var subscriptionKey, serviceRegion, languageTargetOptions, languageSourceOptions; |
| 119 | + var authorizationToken; |
| 120 | + var SpeechSDK; |
| 121 | + var recognizer; |
| 122 | + |
| 123 | + document.addEventListener("DOMContentLoaded", function () { |
| 124 | + startRecognizeOnceAsyncButton = document.getElementById("startRecognizeOnceAsyncButton"); |
| 125 | + subscriptionKey = document.getElementById("subscriptionKey"); |
| 126 | + serviceRegion = document.getElementById("serviceRegion"); |
| 127 | + languageTargetOptions = document.getElementById("languageTargetOptions"); |
| 128 | + languageSourceOptions = document.getElementById("languageSourceOptions"); |
| 129 | + phraseDiv = document.getElementById("phraseDiv"); |
| 130 | + |
| 131 | + startRecognizeOnceAsyncButton.addEventListener("click", function () { |
| 132 | + var soundContext = undefined; |
| 133 | + try { |
| 134 | + var AudioContext = window.AudioContext || window.webkitAudioContext || false; |
| 135 | + if (AudioContext) { |
| 136 | + soundContext = new AudioContext(); |
| 137 | + } else { |
| 138 | + alert("AudioContext not supported"); |
| 139 | + } |
| 140 | + } catch (e) { |
| 141 | + window.console.log("no sound context found, no audio output. " + e); |
| 142 | + } |
| 143 | + |
| 144 | + startRecognizeOnceAsyncButton.disabled = true; |
| 145 | + phraseDiv.innerHTML = ""; |
| 146 | + |
| 147 | + // if we got an authorization token, use the token. Otherwise use the provided subscription key |
| 148 | + var speechConfig; |
| 149 | + if (authorizationToken) { |
| 150 | + speechConfig = SpeechSDK.SpeechTranslationConfig.fromAuthorizationToken(authorizationToken, serviceRegion.value); |
| 151 | + } else { |
| 152 | + if (subscriptionKey.value === "" || subscriptionKey.value === "subscription") { |
| 153 | + alert("Please enter your Microsoft Cognitive Services Speech subscription key!"); |
| 154 | + startRecognizeOnceAsyncButton.disabled = false; |
| 155 | + return; |
| 156 | + } |
| 157 | + speechConfig = SpeechSDK.SpeechTranslationConfig.fromSubscription(subscriptionKey.value, serviceRegion.value); |
| 158 | + } |
| 159 | + |
| 160 | + speechConfig.speechRecognitionLanguage = languageSourceOptions.value; |
| 161 | + var language = languageTargetOptions.value; |
| 162 | + speechConfig.addTargetLanguage(language); |
| 163 | + |
| 164 | + speechConfig.setProperty(SpeechSDK.PropertyId.SpeechServiceConnection_TranslationVoice, languageTargetOptions.value); |
| 165 | + var audioConfig = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput(); |
| 166 | + recognizer = new SpeechSDK.TranslationRecognizer(speechConfig, audioConfig); |
| 167 | + |
| 168 | + // Signals an audio payload of synthesized speech is ready for playback. |
| 169 | + // If the event result contains valid audio, it's reason will be ResultReason.SynthesizingAudio |
| 170 | + // Once a complete phrase has been synthesized, the event will be called with ResultReason.SynthesizingAudioComplete and a 0 byte audio payload. |
| 171 | + recognizer.synthesizing = function (s, e) { |
| 172 | + window.console.log(e); |
| 173 | + |
| 174 | + var audioSize = e.result.audio === undefined ? 0 : e.result.audio.byteLength; |
| 175 | + |
| 176 | + phraseDiv.innerHTML += "(synthesizing) Reason: " + SpeechSDK.ResultReason[e.result.reason] + " " + audioSize + " bytes\r\n"; |
| 177 | + |
| 178 | + if (e.result.audio && soundContext) { |
| 179 | + var source = soundContext.createBufferSource(); |
| 180 | + soundContext.decodeAudioData(e.result.audio, function (newBuffer) { |
| 181 | + source.buffer = newBuffer; |
| 182 | + source.connect(soundContext.destination); |
| 183 | + source.start(0); |
| 184 | + }); |
| 185 | + } |
| 186 | + }; |
| 187 | + recognizer.recognizeOnceAsync( |
| 188 | + function (result) { |
| 189 | + startRecognizeOnceAsyncButton.disabled = false; |
| 190 | + var languageKey = language.substring(0,2) |
| 191 | + var translation = result.translations.get(languageKey); |
| 192 | + window.console.log(translation); |
| 193 | + phraseDiv.innerHTML += translation; |
| 194 | + |
| 195 | + recognizer.close(); |
| 196 | + recognizer = undefined; |
| 197 | + }, |
| 198 | + function (err) { |
| 199 | + startRecognizeOnceAsyncButton.disabled = false; |
| 200 | + phraseDiv.innerHTML += err; |
| 201 | + window.console.log(err); |
| 202 | + |
| 203 | + recognizer.close(); |
| 204 | + recognizer = undefined; |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + if (!!window.SpeechSDK) { |
| 209 | + SpeechSDK = window.SpeechSDK; |
| 210 | + startRecognizeOnceAsyncButton.disabled = false; |
| 211 | + |
| 212 | + document.getElementById('content').style.display = 'block'; |
| 213 | + document.getElementById('warning').style.display = 'none'; |
| 214 | + |
| 215 | + // in case we have a function for getting an authorization token, call it. |
| 216 | + if (typeof RequestAuthorizationToken === "function") { |
| 217 | + RequestAuthorizationToken(); |
| 218 | + } |
| 219 | + } |
| 220 | + }); |
| 221 | + </script> |
| 222 | + <!-- </quickstartcode> --> |
| 223 | +</body> |
| 224 | +</html> |
0 commit comments