forked from Azure-Samples/cognitive-services-speech-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_synthesis_samples.cs
551 lines (482 loc) · 27.6 KB
/
speech_synthesis_samples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
namespace MicrosoftSpeechSDKSamples
{
public class SpeechSynthesisSamples
{
// Speech synthesis to the default speaker.
public static async Task SynthesisToSpeakerAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
// The default language is "en-us".
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates a speech synthesizer using the default speaker as audio output.
using (var synthesizer = new SpeechSynthesizer(config))
{
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to speaker.
Console.WriteLine("Type some text that you want to speak...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized to speaker for text [{text}]");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
// This is to give some time for the speaker to finish playing back the audio
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
// Speech synthesis in the specified spoken language.
public static async Task SynthesisWithLanguageAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Sets the synthesis language.
// The full list of supported language can be found here:
// https://docs.microsoft.com/azure/cognitive-services/speech-service/language-support
var language = "de-DE";
config.SpeechSynthesisLanguage = language;
// Creates a speech synthesizer for the specified language, using the default speaker as audio output.
using (var synthesizer = new SpeechSynthesizer(config))
{
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to speaker.
Console.WriteLine("Type some text that you want to speak...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized to speaker for text [{text}] with language [{language}]");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
// This is to give some time for the speaker to finish playing back the audio
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
// Speech synthesis in the specified voice.
public static async Task SynthesisWithVoiceAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Sets the voice name.
// e.g. "Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)"
// The full list of supported voices can be found here:
// https://docs.microsoft.com/azure/cognitive-services/speech-service/language-support
var voice = "Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)";
config.SpeechSynthesisVoiceName = voice;
// Creates a speech synthesizer for the specified voice, using the default speaker as audio output.
using (var synthesizer = new SpeechSynthesizer(config))
{
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to speaker.
Console.WriteLine("Type some text that you want to speak...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized to speaker for text [{text}] with voice [{voice}]");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
// This is to give some time for the speaker to finish playing back the audio
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
// Speech synthesis to wave file.
public static async Task SynthesisToWaveFileAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
// The default language is "en-us".
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates a speech synthesizer using file as audio output.
// Replace with your own audio file name.
var fileName = "outputaudio.wav";
using (var fileOutput = AudioConfig.FromWavFileOutput(fileName))
using (var synthesizer = new SpeechSynthesizer(config, fileOutput))
{
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to wave file.
Console.WriteLine("Type some text that you want to synthesize...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized for text [{text}], and the audio was saved to [{fileName}]");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
}
}
// Speech synthesis to MP3 file.
public static async Task SynthesisToMp3FileAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
// The default language is "en-us".
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Sets the synthesis output format.
// The full list of supported format can be found here:
// https://docs.microsoft.com/azure/cognitive-services/speech-service/rest-text-to-speech#audio-outputs
config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3);
// Creates a speech synthesizer using file as audio output.
// Replace with your own audio file name.
var fileName = "outputaudio.mp3";
using (var fileOutput = AudioConfig.FromWavFileOutput(fileName))
using (var synthesizer = new SpeechSynthesizer(config, fileOutput))
{
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to mp3 file.
Console.WriteLine("Type some text that you want to synthesize...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized for text [{text}], and the audio was saved to [{fileName}]");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
}
}
// Speech synthesis to pull audio output stream.
public static async Task SynthesisToPullAudioOutputStreamAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates an audio out stream.
using (var stream = AudioOutputStream.CreatePullStream())
{
// Creates a speech synthesizer using audio stream output.
using (var streamConfig = AudioConfig.FromStreamOutput(stream))
using (var synthesizer = new SpeechSynthesizer(config, streamConfig))
{
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to pull audio output stream.
Console.WriteLine("Type some text that you want to synthesize...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized for text [{text}], and the audio was written to output stream.");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
}
// Reads(pulls) data from the stream
byte[] buffer = new byte[32000];
uint filledSize = 0;
uint totalSize = 0;
while ((filledSize = stream.Read(buffer)) > 0)
{
Console.WriteLine($"{filledSize} bytes received.");
totalSize += filledSize;
}
Console.WriteLine($"Totally {totalSize} bytes received.");
}
}
// Speech synthesis to push audio output stream.
public static async Task SynthesisToPushAudioOutputStreamAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates an instance of a customer class inherited from PushAudioOutputStreamCallback
var callback = new PushAudioOutputStreamSampleCallback();
// Creates an audio out stream from the callback.
using (var stream = AudioOutputStream.CreatePushStream(callback))
{
// Creates a speech synthesizer using audio stream output.
using (var streamConfig = AudioConfig.FromStreamOutput(stream))
using (var synthesizer = new SpeechSynthesizer(config, streamConfig))
{
for (int i = 0; i < 2; ++i)
{
// Receive a text from console input and synthesize it to push audio output stream.
Console.WriteLine("Type some text that you want to synthesize...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized for text [{text}], and the audio was written to output stream.");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
}
Console.WriteLine($"Totally {callback.GetAudioData().Length} bytes received.");
}
}
// Gets synthesized audio data from result.
public static async Task SynthesisToResultAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates a speech synthesizer with a null output stream.
// This means the audio output data will not be written to any stream.
// You can just get the audio from the result.
using (var synthesizer = new SpeechSynthesizer(config, null))
{
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to result.
Console.WriteLine("Type some text that you want to synthesize...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized for text [{text}].");
var audioData = result.AudioData;
Console.WriteLine($"{audioData.Length} bytes of audio data received for text [{text}]");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
}
}
// Speech synthesis to audio data stream.
public static async Task SynthesisToAudioDataStreamAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates a speech synthesizer with a null output stream.
// This means the audio output data will not be written to any stream.
// You can just get the audio from the result.
using (var synthesizer = new SpeechSynthesizer(config, null))
{
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to result.
Console.WriteLine("Type some text that you want to synthesize...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized for text [{text}].");
using (var audioDataStream = AudioDataStream.FromResult(result))
{
// You can save all the data in the audio data stream to a file
string fileName = "outputaudio" + (i + 1) + ".wav";
await audioDataStream.SaveToWaveFileAsync(fileName);
Console.WriteLine($"Audio data for text [{text}] was saved to [{fileName}]");
// You can also read data from audio data stream and process it in memory
// Reset the stream position to the beginnging since saving to file puts the postion to end
audioDataStream.SetPosition(0);
byte[] buffer = new byte[16000];
uint totalSize = 0;
uint filledSize = 0;
while ((filledSize = audioDataStream.ReadData(buffer)) > 0)
{
Console.WriteLine($"{filledSize} bytes received.");
totalSize += filledSize;
}
Console.WriteLine($"{totalSize} bytes of audio data received for text [{text}]");
}
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
}
}
// Speech synthesis events.
public static async Task SynthesisEventsAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates a speech synthesizer with a null output stream.
// This means the audio output data will not be written to any stream.
// You can just get the audio from the result.
using (var synthesizer = new SpeechSynthesizer(config, null))
{
// Subscribes to events
synthesizer.SynthesisStarted += (s, e) =>
{
Console.WriteLine("Synthesis started.");
};
synthesizer.Synthesizing += (s, e) =>
{
Console.WriteLine($"Synthesizing event received with audio chunk of {e.Result.AudioData.Length} bytes.");
};
synthesizer.SynthesisCompleted += (s, e) =>
{
Console.WriteLine("Synthesis completed.");
};
for (int i = 0; i < 2; ++i)
{
// Receives a text from console input and synthesize it to result.
Console.WriteLine("Type some text that you want to synthesize...");
Console.Write("> ");
string text = Console.ReadLine();
using (var result = await synthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized for text [{text}].");
var audioData = result.AudioData;
Console.WriteLine($"{audioData.Length} bytes of audio data received for text [{text}]");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
}
}
}
}
}