You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cognitive-services/Speech-Service/includes/how-to/text-to-speech-basics/text-to-speech-basics-csharp.md
+87-81Lines changed: 87 additions & 81 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,13 +26,12 @@ To run the examples in this article, include the following `using` statements at
26
26
27
27
```csharp
28
28
usingSystem;
29
-
usingMicrosoft.CognitiveServices.Speech;
30
-
usingMicrosoft.CognitiveServices.Speech.Audio;
31
-
usingSystem.Threading.Tasks;
32
-
usingSystem.Net;
33
29
usingSystem.IO;
34
30
usingSystem.Text;
31
+
usingSystem.Threading.Tasks;
35
32
usingSystem.Xml.Linq;
33
+
usingMicrosoft.CognitiveServices.Speech;
34
+
usingMicrosoft.CognitiveServices.Speech.Audio;
36
35
```
37
36
38
37
## Create a speech configuration
@@ -54,14 +53,14 @@ In this example, you create a [`SpeechConfig`](https://docs.microsoft.com/dotnet
Next, you create a [`SpeechSynthesizer`](https://docs.microsoft.com/dotnet/api/microsoft.cognitiveservices.speech.speechsynthesizer?view=azure-dotnet) object, which executes text-to-speech conversions and outputs to speakers, files, or other output streams. The [`SpeechSynthesizer`](https://docs.microsoft.com/dotnet/api/microsoft.cognitiveservices.speech.speechsynthesizer?view=azure-dotnet) accepts as params the [`SpeechConfig`](https://docs.microsoft.com/dotnet/api/microsoft.cognitiveservices.speech.speechconfig?view=azure-dotnet) object created in the previous step, and an [`AudioConfig`](https://docs.microsoft.com/dotnet/api/microsoft.cognitiveservices.speech.audio.audioconfig?view=azure-dotnet) object that specifies how output results should be handled.
72
71
73
-
To start, create an `AudioConfig` to automatically write the output to a `.wav` file, using the `FromWavFileOutput()` function, and wrap it in a `using`block.
72
+
To start, create an `AudioConfig` to automatically write the output to a `.wav` file, using the `FromWavFileOutput()` function, and instantiate it with a `using`statement. A `using` statement in this context automatically disposes of unmanaged resources and causes the object to go out of scope after disposal.
Next, inside the `using` block you just created, create a nested `using`block and initialize the `SpeechSynthesizer`. Pass your `config` object and the `fileOutput` object as params. Then, executing speech synthesis and writing to a file is as simple as running `SpeakTextAsync()` with a string of text.
82
+
Next, instantiate a `SpeechSynthesizer` with another `using`statement. Pass your `config` object and the `audioConfig` object as params. Then, executing speech synthesis and writing to a file is as simple as running `SpeakTextAsync()` with a string of text.
awaitsynthesizer.SpeakTextAsync("A simple test to write to a file.");
98
91
}
99
92
```
100
93
101
94
Run the program, and a synthesized `.wav` file is written to the location you specified. This is a good example of the most basic usage, but next you look at customizing output and handling the output response as an in-memory stream for working with custom scenarios.
102
95
96
+
### Synthesize to speaker output
97
+
98
+
In some cases, you may want to directly output synthesized speech directly to a speaker. To do this, simply omit the `AudioConfig` param when creating the `SpeechSynthesizer` in the example above. This outputs to the current active output device.
awaitsynthesizer.SpeakTextAsync("A simple test to write to a file.");
106
+
}
107
+
```
108
+
103
109
## Get result as an in-memory stream
104
110
105
111
For many scenarios in speech application development, you likely need the resulting audio data as an in-memory stream rather than directly writing to a file. This will allow you to build custom behavior including:
@@ -108,25 +114,26 @@ For many scenarios in speech application development, you likely need the result
108
114
* Integrate the result with other API's or services.
109
115
* Modify the audio data, write custom `.wav` headers, etc.
110
116
111
-
It's simple to make this change from the previous example. First, remove the `AudioConfig` block, as you will manage the output behavior manually from this point onward for increased control. Then pass `null` for the `AudioConfig` in the `SpeechSynthesizer` constructor.
117
+
It's simple to make this change from the previous example. First, remove the `AudioConfig` block, as you will manage the output behavior manually from this point onward for increased control. Then pass `null` for the `AudioConfig` in the `SpeechSynthesizer` constructor.
118
+
119
+
> ![NOTE]
120
+
> Passing `null` for the `AudioConfig`, rather than omitting it like in the speaker output example
121
+
> above, will not play the audio by default on the current active output device.
112
122
113
123
This time, you save the result to a [`SpeechSynthesisResult`](https://docs.microsoft.com/dotnet/api/microsoft.cognitiveservices.speech.speechsynthesisresult?view=azure-dotnet) variable. The `AudioData` property contains a `byte []` of the output data. Simply grab the `byte []` and write it to a new `MemoryStream`. From here you can implement any custom behavior using the resulting output, but in this example you write to a file manually.
@@ -145,49 +152,51 @@ There are various options for different file types depending on your requirement
145
152
First, create a function `WriteWavHeader()` to write the necessary audio metadata to the front of your `MemoryStream`. Since `Raw24Khz16BitMonoPcm` is a raw audio format, you need to write standardized audio file headers so that other software knows information like the number of channels, sample rate, and bit depth when your file is played.
Next, set the `SpeechSynthesisOutputFormat` on the `SpeechConfig` object. Similar to the example in the previous section, you write the `byte []` from the result to a `MemoryStream`, but first you must write the custom `.wav` headers for the chosen file type. Use the function you created above, passing the memory stream by reference. For the other params, the number of **channels** is 1 (mono), the **bit-depth** is 16, the **sample-rate** is 24,000 (24Khz), and the **total samples** is the length of the raw `byte []` from the `SpeechSynthesisResult`.
@@ -198,7 +207,7 @@ Running your program again will write a custom-formatted `.wav` file to the spec
198
207
Speech Synthesis Markup Language (SSML) allows you to fine-tune the pitch, pronunciation, speaking rate, volume, and more of the text-to-speech output by submitting your requests from an XML schema. This section shows a few practical usage examples, but for a more detailed guide, see the [SSML how-to article](../../../speech-synthesis-markup.md).
199
208
200
209
To start using SSML for customization, you make a simple change that switches the voice.
201
-
First, create a new XML file for the SSML config in your root project directory, in this example `ssml.xml`. The root element is always `<speak>`, and wrapping the text in a `<voice>` element allows you to change the voice using the `name` param. This example changes the voice to a male English (UK) voice. See the [full list](https://docs.microsoft.com/azure/cognitive-services/speech-service/language-support#standard-voices) of supported standard voices for additional options.
210
+
First, create a new XML file for the SSML config in your root project directory, in this example `ssml.xml`. The root element is always `<speak>`, and wrapping the text in a `<voice>` element allows you to change the voice using the `name` param. This example changes the voice to a male English (UK) voice. Note that this voice is a **standard** voice, which has different pricing and availability than **neural** voices. See the [full list](https://docs.microsoft.com/azure/cognitive-services/speech-service/language-support#standard-voices) of supported **standard** voices.
0 commit comments