Skip to content

Commit fb20d55

Browse files
committed
Minor fixes
1 parent 9a46873 commit fb20d55

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ There are a few ways that you can initialize a [`SpeechConfig`](https://docs.mic
6767
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.
6868

6969
```javascript
70-
const speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
70+
const speechConfig = SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
7171
```
7272

7373
## Initialize a recognizer
@@ -128,19 +128,19 @@ You'll need to write some code to handle the result. This sample evaluates the [
128128
```javascript
129129
switch (result.reason) {
130130
case sdk.ResultReason.RecognizedSpeech:
131-
console.log(`RECOGNIZED: Text=${result.Text}`);
131+
console.log(`RECOGNIZED: Text=${result.text}`);
132132
console.log(" Intent not recognized.");
133133
break;
134134
case sdk.ResultReason.NoMatch:
135135
console.log("NOMATCH: Speech could not be recognized.");
136136
break;
137137
case sdk.ResultReason.Canceled:
138138
const cancellation = sdk.CancellationDetails.fromResult(result);
139-
console.log(`CANCELED: Reason=${cancellation.Reason}`);
139+
console.log(`CANCELED: Reason=${cancellation.reason}`);
140140

141-
if (cancellation.Reason == sdk.CancellationReason.Error) {
141+
if (cancellation.reason == sdk.CancellationReason.Error) {
142142
console.log(`CANCELED: ErrorCode=${cancellation.ErrorCode}`);
143-
console.log(`CANCELED: ErrorDetails=${cancellation.ErrorDetails}`);
143+
console.log(`CANCELED: ErrorDetails=${cancellation.errorDetails}`);
144144
console.log("CANCELED: Did you update the subscription info?");
145145
}
146146
break;
@@ -167,23 +167,23 @@ We'll subscribe to the events sent from the [`SpeechRecognizer`](https://docs.mi
167167

168168
```javascript
169169
recognizer.Recognizing = (s, e) => {
170-
console.log(`RECOGNIZING: Text=${e.Result.Text}`);
170+
console.log(`RECOGNIZING: Text=${e.Result.text}`);
171171
};
172172

173173
recognizer.recognized = (s, e) => {
174-
if (e.Result.Reason == sdk.ResultReason.RecognizedSpeech) {
174+
if (e.Result.reason == sdk.ResultReason.RecognizedSpeech) {
175175
console.log(`RECOGNIZED: Text=${e.Result.Text}`);
176176
}
177-
else if (e.Result.Reason == sdk.ResultReason.NoMatch) {
177+
else if (e.Result.reason == sdk.ResultReason.NoMatch) {
178178
console.log("NOMATCH: Speech could not be recognized.");
179179
}
180180
};
181181

182182
recognizer.Canceled = (s, e) => {
183-
console.log(`CANCELED: Reason=${e.Reason}`);
183+
console.log(`CANCELED: Reason=${e.reason}`);
184184

185-
if (e.Reason == sdk.CancellationReason.Error) {
186-
console.log(`"CANCELED: ErrorCode=${e.ErrorCode}`);
185+
if (e.reason == sdk.CancellationReason.Error) {
186+
console.log(`"CANCELED: ErrorCode=${e.errorCode}`);
187187
console.log(`"CANCELED: ErrorDetails=${e.ErrorDetails}`);
188188
console.log("CANCELED: Did you update the subscription info?");
189189
}

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

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ Additionally, depending on the target environment use one of the following:
2222
# [import](#tab/import)
2323

2424
```javascript
25-
import * as sdk from "microsoft-cognitiveservices-speech-sdk";
25+
import {
26+
AudioConfig,
27+
SpeechConfig,
28+
SpeechSynthesizer
29+
} from "microsoft-cognitiveservices-speech-sdk";
2630
```
2731

2832
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>.
@@ -35,6 +39,18 @@ const sdk = require("microsoft-cognitiveservices-speech-sdk");
3539

3640
For more information on `require`, see <a href="https://nodejs.org/en/knowledge/getting-started/what-is-require/" target="_blank">what is require? <span class="docon docon-navigate-external x-hidden-focus"></span></a>.
3741

42+
43+
# [script](#tab/script)
44+
45+
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.
46+
47+
```html
48+
<script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script>;
49+
```
50+
51+
> [!TIP]
52+
> 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.
53+
3854
---
3955

4056
## Create a speech configuration
@@ -55,21 +71,22 @@ In this example, you create a [`SpeechConfig`](https://docs.microsoft.com/javasc
5571

5672
```javascript
5773
function synthesizeSpeech() {
58-
const speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
59-
const synthesizer = new sdk.SpeechSynthesizer(speechConfig);
74+
const speechConfig = SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
75+
const synthesizer = new SpeechSynthesizer(speechConfig);
6076
}
6177
```
6278

63-
## Synthesize speech to a file
79+
## Synthesize speech from a file
6480

6581
Next, you create a [`SpeechSynthesizer`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechsynthesizer?view=azure-node-latest) object, which executes text-to-speech conversions and outputs to speakers, files, or other output streams. The [`SpeechSynthesizer`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechsynthesizer?view=azure-node-latest) accepts as params the [`SpeechConfig`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/speechconfig?view=azure-node-latest) object created in the previous step, and an [`AudioConfig`](https://docs.microsoft.com/javascript/api/microsoft-cognitiveservices-speech-sdk/audioconfig?view=azure-node-latest) object that specifies how output results should be handled.
6682

67-
To start, create an `AudioConfig` to automatically write the output to a `.wav` file using the `fromWavFileOutput()` static function.
83+
To start, create an `AudioConfig` to automatically write the output to a `.wav` file using the `fromAudioFileOutput()` static function.
6884

6985
```javascript
7086
function synthesizeSpeech() {
71-
const speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
72-
const audioConfig = sdk.AudioConfig.fromWavFileOutput("path/to/write/file.wav");
87+
const speechConfig = SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
88+
const audioConfig = AudioConfig.fromAudioFileOutput("path/to/file.wav");
89+
const synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);
7390
}
7491
```
7592

@@ -78,10 +95,10 @@ Next, instantiate a `SpeechSynthesizer` passing your `speechConfig` object and t
7895
```javascript
7996
function synthesizeSpeech() {
8097
const speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
81-
AudioConfig audioConfig = AudioConfig.fromWavFileOutput("path/to/write/file.wav");
98+
const audioConfig = AudioConfig.fromAudioFileOutput("path/to/file.wav");
8299

83-
SpeechSynthesizer synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);
84-
synthesizer.SpeakText("A simple test to write to a file.");
100+
const synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);
101+
synthesizer.speakTextAsync("A simple test to write to a file.");
85102
}
86103
```
87104

@@ -94,10 +111,10 @@ In some cases, you may want to directly output synthesized speech directly to a
94111
```javascript
95112
function synthesizeSpeech() {
96113
const speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
97-
AudioConfig audioConfig = AudioConfig.fromDefaultSpeakerOutput();
114+
const audioConfig = AudioConfig.fromDefaultSpeakerOutput();
98115

99-
SpeechSynthesizer synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);
100-
synthesizer.SpeakText("Synthesizing directly to speaker output.");
116+
const synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);
117+
synthesizer.speakTextAsync("Synthesizing directly to speaker output.");
101118
}
102119
```
103120

@@ -126,6 +143,7 @@ function synthesizeSpeech() {
126143
result => {
127144
// Interact with the audio ArrayBuffer data
128145
const audioData = result.audioData;
146+
console.log(`Audio data byte size: ${audioData.byteLength}.`)
129147
},
130148
error => console.log(error));
131149
}
@@ -145,14 +163,14 @@ To change the audio format, you use the `setSpeechSynthesisOutputFormat()` funct
145163

146164
There are various options for different file types depending on your requirements. Note that by definition, raw formats like `Raw24Khz16BitMonoPcm` do not include audio headers. Use raw formats only when you know your downstream implementation can decode a raw bitstream, or if you plan on manually building headers based on bit-depth, sample-rate, number of channels, etc.
147165

148-
In this example, you specify a high-fidelity RIFF format `Riff24Khz16BitMonoPcm` by setting the `SpeechSynthesisOutputFormat` on the `SpeechConfig` object. Similar to the example in the previous section, get the audio `ArrayBuffer` data and interact with it.
166+
In this example, you specify a high-fidelity RIFF format `Riff24Khz16BitMonoPcm` by setting the `speechSynthesisOutputFormat` on the `SpeechConfig` object. Similar to the example in the previous section, get the audio `ArrayBuffer` data and interact with it.
149167

150168
```javascript
151169
function synthesizeSpeech() {
152-
const speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
170+
const speechConfig = SpeechConfig.fromSubscription("YourSubscriptionKey", "YourServiceRegion");
153171

154172
// Set the output format
155-
speechConfig.setSpeechSynthesisOutputFormat(sdk.SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm);
173+
speechConfig.speechSynthesisOutputFormat = SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm;
156174

157175
const synthesizer = new sdk.SpeechSynthesizer(speechConfig, null);
158176
synthesizer.speakTextAsync(
@@ -184,30 +202,13 @@ First, create a new XML file for the SSML config in your root project directory,
184202

185203
Next, you need to change the speech synthesis request to reference your XML file. The request is mostly the same, but instead of using the `speakTextAsync()` function, you use `speakSsmlAsync()`. This function expects an XML string, so first you create a function to load an XML file and return it as a string.
186204

187-
# [import](#tab/import)
188-
189205
```javascript
190-
import { readFileSync } from "fs";
191-
192206
function xmlToString(filePath) {
193207
const xml = readFileSync(filePath, "utf8");
194208
return xml;
195209
}
196210
```
197211

198-
# [require](#tab/require)
199-
200-
```javascript
201-
const fs = require("fs");
202-
203-
function xmlToString(filePath) {
204-
const xml = fs.readFileSync(filePath, "utf8");
205-
return xml;
206-
}
207-
```
208-
209-
---
210-
211212
From here, the result object is exactly the same as previous examples.
212213

213214
```javascript

0 commit comments

Comments
 (0)