Skip to content

Commit 63757c5

Browse files
authored
Merge pull request #111373 from IEvangelist/moreBasicsWithJS
Speech synthesis basics for JavaScript
2 parents b54ec81 + bc05069 commit 63757c5

File tree

3 files changed

+350
-35
lines changed

3 files changed

+350
-35
lines changed

articles/cognitive-services/Speech-Service/includes/how-to/speech-to-text-basics/speech-to-text-basics-javascript.md

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: trevorbye
33
ms.service: cognitive-services
44
ms.topic: include
5-
ms.date: 04/14/2020
5+
ms.date: 04/15/2020
66
ms.author: trbye
77
---
88

@@ -23,7 +23,15 @@ Additionally, depending on the target environment use one of the following:
2323
# [import](#tab/import)
2424

2525
```javascript
26-
import * as sdk from "microsoft-cognitiveservices-speech-sdk";
26+
import {
27+
AudioConfig,
28+
CancellationDetails,
29+
CancellationReason,
30+
PhraseListGrammar,
31+
ResultReason,
32+
SpeechConfig,
33+
SpeechRecognizer
34+
} from "microsoft-cognitiveservices-speech-sdk";
2735
```
2836

2937
For more information on `import`, see <a href="https://javascript.info/import-export" target="_blank">export and import <span class="docon docon-navigate-external x-hidden-focus"></span></a>.
@@ -39,14 +47,14 @@ For more information on `require`, see <a href="https://nodejs.org/en/knowledge/
3947

4048
# [script](#tab/script)
4149

42-
Download and extract the <a href="https://aka.ms/csspeech/jsbrowserpackage" target="_blank">JavaScript Speech SDK <span class="docon docon-navigate-external x-hidden-focus"></span></a> *microsoft.cognitiveservices.speech.sdk.bundle.js* file, and place it in a folder accessible to your HTML file.
50+
Download and extract the <a href="https://aka.ms/csspeech/jsbrowserpackage" target="_blank">JavaScript Speech SDK <span class="docon docon-navigate-external x-hidden-focus"></span></a> *microsoft.cognitiveservices.speech.bundle.js* file, and place it in a folder accessible to your HTML file.
4351

4452
```html
45-
<script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script>;
53+
<script src="microsoft.cognitiveservices.speech.bundle.js"></script>;
4654
```
4755

4856
> [!TIP]
49-
> If you're targeting a web browser, and using the `<script>` tag; the `sdk` prefix is not needed. The `sdk` prefix is an alias we use to name our `import` or `require` module.
57+
> If you're targeting a web browser, and using the `<script>` tag; the `sdk` prefix is not needed. The `sdk` prefix is an alias used to name the `require` module.
5058
5159
---
5260

@@ -67,7 +75,7 @@ There are a few ways that you can initialize a [`SpeechConfig`](https://docs.mic
6775
Let's take a look at how a [`SpeechConfig`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechconfig?view=azure-node-latest) is created using a key and region. See the [region support](https://docs.microsoft.com/azure/cognitive-services/speech-service/regions#speech-sdk) page to find your region identifier.
6876

6977
```javascript
70-
const speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
78+
const speechConfig = SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
7179
```
7280

7381
## Initialize a recognizer
@@ -77,7 +85,7 @@ After you've created a [`SpeechConfig`](https://docs.microsoft.com/javascript/ap
7785
If you're recognizing speech using your device's default microphone, here's what the [`SpeechRecognizer`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechrecognizer?view=azure-node-latest) should look like:
7886

7987
```javascript
80-
const recognizer = new sdk.SpeechRecognizer(speechConfig);
88+
const recognizer = new SpeechRecognizer(speechConfig);
8189
```
8290

8391
If you want to specify the audio input device, then you'll need to create an [`AudioConfig`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/audioconfig?view=azure-node-latest) and provide the `audioConfig` parameter when initializing your [`SpeechRecognizer`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechrecognizer?view=azure-node-latest).
@@ -88,15 +96,15 @@ If you want to specify the audio input device, then you'll need to create an [`A
8896
Reference the `AudioConfig` object as follows:
8997

9098
```javascript
91-
const audioConfig = sdk.AudioConfig.fromDefaultMicrophoneInput();
92-
const speechConfig = sdk.SpeechConfig.fromSubscription(speechConfig, audioConfig);
99+
const audioConfig = AudioConfig.fromDefaultMicrophoneInput();
100+
const speechConfig = SpeechConfig.fromSubscription(speechConfig, audioConfig);
93101
```
94102

95103
If you want to provide an audio file instead of using a microphone, you'll still need to provide an `audioConfig`. However, this can only be done when targeting **Node.js** and when you create an [`AudioConfig`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/audioconfig?view=azure-node-latest), instead of calling `fromDefaultMicrophoneInput`, you'll call `fromWavFileOutput` and pass the `filename` parameter.
96104

97105
```javascript
98-
const audioConfig = sdk.AudioConfig.fromWavFileInput("YourAudioFile.wav");
99-
const speechConfig = sdk.SpeechConfig.fromSubscription(speechConfig, audioConfig);
106+
const audioConfig = AudioConfig.fromWavFileInput("YourAudioFile.wav");
107+
const speechConfig = SpeechConfig.fromSubscription(speechConfig, audioConfig);
100108
```
101109

102110
## Recognize speech
@@ -127,20 +135,20 @@ You'll need to write some code to handle the result. This sample evaluates the [
127135

128136
```javascript
129137
switch (result.reason) {
130-
case sdk.ResultReason.RecognizedSpeech:
131-
console.log(`RECOGNIZED: Text=${result.Text}`);
138+
case ResultReason.RecognizedSpeech:
139+
console.log(`RECOGNIZED: Text=${result.text}`);
132140
console.log(" Intent not recognized.");
133141
break;
134-
case sdk.ResultReason.NoMatch:
142+
case ResultReason.NoMatch:
135143
console.log("NOMATCH: Speech could not be recognized.");
136144
break;
137-
case sdk.ResultReason.Canceled:
138-
const cancellation = sdk.CancellationDetails.fromResult(result);
139-
console.log(`CANCELED: Reason=${cancellation.Reason}`);
145+
case ResultReason.Canceled:
146+
const cancellation = CancellationDetails.fromResult(result);
147+
console.log(`CANCELED: Reason=${cancellation.reason}`);
140148

141-
if (cancellation.Reason == sdk.CancellationReason.Error) {
149+
if (cancellation.reason == CancellationReason.Error) {
142150
console.log(`CANCELED: ErrorCode=${cancellation.ErrorCode}`);
143-
console.log(`CANCELED: ErrorDetails=${cancellation.ErrorDetails}`);
151+
console.log(`CANCELED: ErrorDetails=${cancellation.errorDetails}`);
144152
console.log("CANCELED: Did you update the subscription info?");
145153
}
146154
break;
@@ -155,7 +163,7 @@ Continuous recognition is a bit more involved than single-shot recognition. It r
155163
Let's start by defining the input and initializing a [`SpeechRecognizer`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechrecognizer?view=azure-node-latest):
156164

157165
```javascript
158-
const recognizer = new sdk.SpeechRecognizer(speechConfig);
166+
const recognizer = new SpeechRecognizer(speechConfig);
159167
```
160168

161169
We'll subscribe to the events sent from the [`SpeechRecognizer`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechrecognizer?view=azure-node-latest).
@@ -166,32 +174,32 @@ We'll subscribe to the events sent from the [`SpeechRecognizer`](https://docs.mi
166174
* [`canceled`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechrecognizer?view=azure-node-latest#canceled): Signal for events containing canceled recognition results (indicating a recognition attempt that was canceled as a result or a direct cancellation request or, alternatively, a transport or protocol failure).
167175

168176
```javascript
169-
recognizer.Recognizing = (s, e) => {
170-
console.log(`RECOGNIZING: Text=${e.Result.Text}`);
177+
recognizer.recognizing = (s, e) => {
178+
console.log(`RECOGNIZING: Text=${e.result.text}`);
171179
};
172180

173181
recognizer.recognized = (s, e) => {
174-
if (e.Result.Reason == sdk.ResultReason.RecognizedSpeech) {
175-
console.log(`RECOGNIZED: Text=${e.Result.Text}`);
182+
if (e.result.reason == ResultReason.RecognizedSpeech) {
183+
console.log(`RECOGNIZED: Text=${e.result.text}`);
176184
}
177-
else if (e.Result.Reason == sdk.ResultReason.NoMatch) {
185+
else if (e.result.reason == ResultReason.NoMatch) {
178186
console.log("NOMATCH: Speech could not be recognized.");
179187
}
180188
};
181189

182-
recognizer.Canceled = (s, e) => {
183-
console.log(`CANCELED: Reason=${e.Reason}`);
190+
recognizer.canceled = (s, e) => {
191+
console.log(`CANCELED: Reason=${e.reason}`);
184192

185-
if (e.Reason == sdk.CancellationReason.Error) {
186-
console.log(`"CANCELED: ErrorCode=${e.ErrorCode}`);
187-
console.log(`"CANCELED: ErrorDetails=${e.ErrorDetails}`);
193+
if (e.reason == CancellationReason.Error) {
194+
console.log(`"CANCELED: ErrorCode=${e.errorCode}`);
195+
console.log(`"CANCELED: ErrorDetails=${e.errorDetails}`);
188196
console.log("CANCELED: Did you update the subscription info?");
189197
}
190198

191199
recognizer.stopContinuousRecognitionAsync();
192200
};
193201

194-
recognizer.SessionStopped = (s, e) => {
202+
recognizer.sessionStopped = (s, e) => {
195203
console.log("\n Session stopped event.");
196204
recognizer.stopContinuousRecognitionAsync();
197205
};
@@ -229,7 +237,7 @@ The [`speechRecognitionLanguage`](https://docs.microsoft.com/javascript/api/micr
229237

230238
## Improve recognition accuracy
231239

232-
There are a few ways to improve recognition accuracy with the Speech SDK. Let's take a look at Phrase Lists. Phrase Lists are used to identify known phrases in audio data, like a person's name or a specific location. Single words or complete phrases can be added to a Phrase List. During recognition, an entry in a phrase list is used if an exact match for the entire phrase is included in the audio. If an exact match to the phrase is not found, recognition is not assisted.
240+
There are a few ways to improve recognition accuracy with the Speech Let's take a look at Phrase Lists. Phrase Lists are used to identify known phrases in audio data, like a person's name or a specific location. Single words or complete phrases can be added to a Phrase List. During recognition, an entry in a phrase list is used if an exact match for the entire phrase is included in the audio. If an exact match to the phrase is not found, recognition is not assisted.
233241

234242
> [!IMPORTANT]
235243
> The Phrase List feature is only available in English.
@@ -239,7 +247,7 @@ To use a phrase list, first create a [`PhraseListGrammar`](https://docs.microsof
239247
Any changes to [`PhraseListGrammar`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/phraselistgrammar?view=azure-node-latest) take effect on the next recognition or after a reconnection to the Speech service.
240248

241249
```javascript
242-
const phraseList = sdk.PhraseListGrammar.fromRecognizer(recognizer);
250+
const phraseList = PhraseListGrammar.fromRecognizer(recognizer);
243251
phraseList.addPhrase("Supercalifragilisticexpialidocious");
244252
```
245253

0 commit comments

Comments
 (0)