-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
631 lines (540 loc) · 32.2 KB
/
main.py
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
try:
import azure.cognitiveservices.speech as speechsdk
except ImportError:
print("""
Importing the Speech SDK for Python failed.
Refer to
https://docs.microsoft.com/azure/cognitive-services/speech-service/quickstart-text-to-speech-python for
installation instructions.
""")
import sys
sys.exit(1)
# Set up the subscription info for the Speech Service:
#SpeechConfig(subscription: Optional[str] = 9046921b-55cf-4809-a709-0e729cf4c3c8, region: Optional[str] = eastus, endpoint: Optional[str] = https://eastus.api.cognitive.microsoft.com/sts/v1.0/issuetoken, host: Optional[str] = None, auth_token: Optional[str] = e1a022e65fba43b58b865d11802bac0d, speech_recognition_language: Optional[str] = None)
# Replace with your own subscription key and service region (e.g., "westus").
speech_key, service_region = "e1a022e65fba43b58b865d11802bac0d", "eastus"
# Note: if only language is set, the default voice of that language is chosen.
#
# speech_config.speech_synthesis_language = "en-US" # For example, "de-DE"
# The voice setting will overwrite the language setting.
# The voice setting will not overwrite the voice element in input SSML.
# speech_config.speech_synthesis_voice_name ="en-US-JennyNeural"
#!/usr/bin/env python
# coding: utf-8
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
"""
Speech synthesis samples for the Microsoft Cognitive Services Speech SDK
"""
def speech_synthesis_to_speaker():
"""performs speech synthesis to the default speaker"""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates a speech synthesizer using the default speaker as audio output.
# The default spoken language is "en-us".
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
# Receives a text from console input and synthesizes it to speaker.
while True:
print("Enter some text that you want to speak, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized to speaker for text [{}]".format(text))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_with_language():
"""performs speech synthesis to the default speaker with specified spoken language"""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Sets the synthesis language.
# The full list of supported languages can be found here:
# https://docs.microsoft.com/azure/cognitive-services/speech-service/language-support#text-to-speech
language = "en-US";
speech_config.speech_synthesis_language = language
# Creates a speech synthesizer for the specified language,
# using the default speaker as audio output.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
# Receives a text from console input and synthesizes it to speaker.
while True:
print("Enter some text that you want to speak, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized to speaker for text [{}] with language [{}]".format(text, language))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_with_voice():
"""performs speech synthesis to the default speaker with specified voice"""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Sets the synthesis voice name.
# e.g. "Microsoft Server Speech Text to Speech Voice (en-US, JennyNeural)".
# The full list of supported voices can be found here:
# https://aka.ms/csspeech/voicenames
# And, you can try get_voices_async method to get all available voices (see speech_synthesis_get_available_voices() sample below).
voice = "Microsoft Server Speech Text to Speech Voice (en-US, JennyNeural)"
speech_config.speech_synthesis_voice_name = voice
# Creates a speech synthesizer for the specified voice,
# using the default speaker as audio output.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
# Receives a text from console input and synthesizes it to speaker.
while True:
print("Enter some text that you want to speak, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized to speaker for text [{}] with voice [{}]".format(text, voice))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_using_custom_voice():
"""performs speech synthesis to the default speaker using custom voice.
see https://aka.ms/customvoice"""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Replace with the endpoint id of your Custom Voice model.
speech_config.endpoint_id = "YourEndpointId"
# Replace with the voice name of your Custom Voice model.
speech_config.speech_synthesis_voice_name = "YourVoiceName"
# Creates a speech synthesizer for Custom Voice,
# using the default speaker as audio output.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
# Receives a text from console input and synthesizes it to speaker.
while True:
print("Enter some text that you want to speak, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized to speaker for text [{}]".format(text))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_to_wave_file():
"""performs speech synthesis to a wave file"""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Sets the synthesis voice name.
# e.g. "Microsoft Server Speech Text to Speech Voice (en-US, JennyNeural)".
# The full list of supported voices can be found here:
# https://aka.ms/csspeech/voicenames
# And, you can try get_voices_async method to get all available voices (see speech_synthesis_get_available_voices() sample below).
voice = "Microsoft Server Speech Text to Speech Voice (en-US, JennyNeural)"
speech_config.speech_synthesis_voice_name = voice
# Creates a speech synthesizer for the specified voice,
# using the default speaker as audio output.
# Creates a speech synthesizer using file as audio output.
# Replace with your own audio file name.
file_name = "outputaudio.wav"
file_config = speechsdk.audio.AudioOutputConfig(filename=file_name)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=file_config)
# Receives a text from console input and synthesizes it to wave file.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}], and the audio was saved to [{}]".format(text, file_name))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_to_mp3_file():
"""performs speech synthesis to an mp3 file"""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# 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
speech_config.set_speech_synthesis_output_format(speechsdk.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3)
# Sets the synthesis voice name.
# e.g. "Microsoft Server Speech Text to Speech Voice (en-US, JennyNeural)".
# The full list of supported voices can be found here:
# https://aka.ms/csspeech/voicenames
# And, you can try get_voices_async method to get all available voices (see speech_synthesis_get_available_voices() sample below).
voice = "Microsoft Server Speech Text to Speech Voice (en-US, JennyNeural)"
speech_config.speech_synthesis_voice_name = voice
# Creates a speech synthesizer for the specified voice,
# using the default speaker as audio output.
# Creates a speech synthesizer using file as audio output.
# Replace with your own audio file name.
file_name = "outputaudio.mp3"
file_config = speechsdk.audio.AudioOutputConfig(filename=file_name)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=file_config)
# Subscribes to events
speech_synthesizer.synthesis_started.connect(lambda evt: print("Synthesis started: {}".format(evt)))
speech_synthesizer.synthesizing.connect(lambda evt: print("Synthesis ongoing, audio chunk received: {}".format(evt)))
speech_synthesizer.synthesis_completed.connect(lambda evt: print("Synthesis completed: {}".format(evt)))
# Receives a text from console input and synthesizes it to mp3 file.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}], and the audio was saved to [{}]".format(text, file_name))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_to_pull_audio_output_stream():
"""performs speech synthesis and pull audio output from a stream"""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates an audio output stream
pull_stream = speechsdk.audio.PullAudioOutputStream()
# Creates a speech synthesizer using pull stream as audio output.
stream_config = speechsdk.audio.AudioOutputConfig(stream=pull_stream)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=stream_config)
# Receives a text from console input and synthesizes it to stream output.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}], and the audio was written to output stream.".format(text))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
# Destroys result which is necessary for destroying speech synthesizer
del result
# Destroys the synthesizer in order to close the output stream.
del speech_synthesizer
# Reads(pulls) data from the stream
audio_buffer = bytes(32000)
total_size = 0
filled_size = pull_stream.read(audio_buffer)
while filled_size > 0:
print("{} bytes received.".format(filled_size))
total_size += filled_size
filled_size = pull_stream.read(audio_buffer)
print("Totally {} bytes received.".format(total_size))
def speech_synthesis_to_push_audio_output_stream():
"""performs speech synthesis and push audio output to a stream"""
class PushAudioOutputStreamSampleCallback(speechsdk.audio.PushAudioOutputStreamCallback):
"""
Example class that implements the PushAudioOutputStreamCallback, which is used to show
how to push output audio to a stream
"""
def __init__(self):
super().__init__()
self._audio_data = bytes(0)
self._closed = False
def write(self, audio_buffer: memoryview) -> int:
"""
The callback function which is invoked when the synthesizer has a output audio chunk
to write out
"""
self._audio_data += audio_buffer
print("{} bytes received.".format(audio_buffer.nbytes))
return audio_buffer.nbytes
def close(self):
"""
The callback function which is invoked when the synthesizer is about to close the
stream.
"""
self._closed = True
print("Push audio output stream closed.")
def get_audio_data(self) -> bytes:
return self._audio_data
def get_audio_size(self) -> int:
return len(self._audio_data)
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates customized instance of PushAudioOutputStreamCallback
stream_callback = PushAudioOutputStreamSampleCallback()
# Creates audio output stream from the callback
push_stream = speechsdk.audio.PushAudioOutputStream(stream_callback)
# Creates a speech synthesizer using push stream as audio output.
stream_config = speechsdk.audio.AudioOutputConfig(stream=push_stream)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=stream_config)
# Receives a text from console input and synthesizes it to stream output.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}], and the audio was written to output stream.".format(text))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
# Destroys result which is necessary for destroying speech synthesizer
del result
# Destroys the synthesizer in order to close the output stream.
del speech_synthesizer
print("Totally {} bytes received.".format(stream_callback.get_audio_size()))
def speech_synthesis_to_result():
"""performs speech synthesis and gets synthesized audio data from result."""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates a speech synthesizer with a null output stream.
# This means the audio output data will not be written to any output channel.
# You can just get the audio from the result.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
# Receives a text from console input and synthesizes it to result.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}]".format(text))
audio_data = result.audio_data
print("{} bytes of audio data received.".format(len(audio_data)))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_to_audio_data_stream():
"""performs speech synthesis and gets the audio data from single request based stream."""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates a speech synthesizer with a null output stream.
# This means the audio output data will not be written to any output channel.
# You can just get the audio from the result.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
# Receives a text from console input and synthesizes it to result.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}]".format(text))
audio_data_stream = speechsdk.AudioDataStream(result)
# You can save all the data in the audio data stream to a file
file_name = "outputaudio.wav"
audio_data_stream.save_to_wav_file(file_name)
print("Audio data for text [{}] was saved to [{}]".format(text, file_name))
# You can also read data from audio data stream and process it in memory
# Reset the stream position to the beginning since saving to file puts the postion to end.
audio_data_stream.position = 0
# Reads data from the stream
audio_buffer = bytes(16000)
total_size = 0
filled_size = audio_data_stream.read_data(audio_buffer)
while filled_size > 0:
print("{} bytes received.".format(filled_size))
total_size += filled_size
filled_size = audio_data_stream.read_data(audio_buffer)
print("Totally {} bytes received for text [{}].".format(total_size, text))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_events():
"""performs speech synthesis and shows the speech synthesis events."""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates a speech synthesizer with a null output stream.
# This means the audio output data will not be written to any output channel.
# You can just get the audio from the result.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
# Subscribes to events
speech_synthesizer.synthesis_started.connect(lambda evt: print("Synthesis started: {}".format(evt)))
speech_synthesizer.synthesizing.connect(lambda evt: print("Synthesis ongoing, audio chunk received: {}".format(evt)))
speech_synthesizer.synthesis_completed.connect(lambda evt: print("Synthesis completed: {}".format(evt)))
# Receives a text from console input and synthesizes it to result.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}]".format(text))
audio_data = result.audio_data
print("{} bytes of audio data received.".format(len(audio_data)))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_word_boundary_event():
"""performs speech synthesis and shows the word boundary event."""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates a speech synthesizer with a null output stream.
# This means the audio output data will not be written to any output channel.
# You can just get the audio from the result.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
# Subscribes to word boundary event
# The unit of evt.audio_offset is tick (1 tick = 100 nanoseconds), divide it by 10,000 to convert to milliseconds.
speech_synthesizer.synthesis_word_boundary.connect(lambda evt: print(
"Word boundary event received: {}, audio offset in ms: {}ms".format(evt, evt.audio_offset / 10000)))
# Receives a text from console input and synthesizes it to result.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}]".format(text))
audio_data = result.audio_data
print("{} bytes of audio data received.".format(len(audio_data)))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_viseme_event():
"""performs speech synthesis and shows the viseme event."""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates a speech synthesizer with a null output stream.
# This means the audio output data will not be written to any output channel.
# You can just get the audio from the result.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
# Subscribes to viseme received event
# The unit of evt.audio_offset is tick (1 tick = 100 nanoseconds), divide it by 10,000 to convert to milliseconds.
speech_synthesizer.viseme_received.connect(lambda evt: print(
"Viseme event received: audio offset: {}ms, viseme id: {}.".format(evt.audio_offset / 10000, evt.viseme_id)))
# Receives a text from console input and synthesizes it to result.
while True:
print("Enter some text that you want to synthesize, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}]".format(text))
audio_data = result.audio_data
print("{} bytes of audio data received.".format(len(audio_data)))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_bookmark_event():
"""performs speech synthesis and shows the bookmark event."""
# Creates an instance of a speech config with specified subscription key and service region.
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates a speech synthesizer with a null output stream.
# This means the audio output data will not be written to any output channel.
# You can just get the audio from the result.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
# Subscribes to viseme received event
# The unit of evt.audio_offset is tick (1 tick = 100 nanoseconds), divide it by 10,000 to convert to milliseconds.
speech_synthesizer.bookmark_reached.connect(lambda evt: print(
"Bookmark reached: {}, audio offset: {}ms, bookmark text: {}.".format(evt, evt.audio_offset / 10000, evt.text)))
print("Press Enter to start synthesizing.")
input()
# Bookmark tag is needed in the SSML, e.g.
ssml = "<speak version='1.0' xml:lang='en-US' xmlns='http://www.w3.org/2001/10/synthesis' xmlns:mstts='http://www.w3.org/2001/mstts'><voice name='Microsoft Server Speech Text to Speech Voice (en-US, AriaNeural)'><bookmark mark='bookmark_one'/> one. <bookmark mark='bookmark_two'/> two. three. four.</voice></speak>";
result = speech_synthesizer.speak_ssml_async(ssml).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized.")
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_with_auto_language_detection_to_speaker():
"""performs speech synthesis to the default speaker with auto language detection
Note: this is a preview feature, which might be updated in future versions."""
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# create the auto detection language configuration without specific languages
auto_detect_source_language_config = \
speechsdk.languageconfig.AutoDetectSourceLanguageConfig()
# Creates a speech synthesizer using the default speaker as audio output.
speech_synthesizer = speechsdk.SpeechSynthesizer(
speech_config=speech_config, auto_detect_source_language_config=auto_detect_source_language_config)
while True:
# Receives a text from console input and synthesizes it to speaker.
# For example, you can input "Bonjour le monde. Hello world.", then you will hear "Bonjour le monde."
# spoken in a French voice and "Hello world." in an English voice.
print("Enter some multi lingual text that you want to speak, Ctrl-Z to exit")
try:
text = input()
except EOFError:
break
result = speech_synthesizer.speak_text_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized to speaker for text [{}]".format(text))
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
def speech_synthesis_get_available_voices():
"""gets the available voices list."""
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
# Creates a speech synthesizer.
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
print("Enter a locale in BCP-47 format (e.g. en-US) that you want to get the voices of, or enter empty to get voices in all locales.")
try:
text = input()
except EOFError:
pass
result = speech_synthesizer.get_voices_async(text).get()
# Check result
if result.reason == speechsdk.ResultReason.VoicesListRetrieved:
print('Voices successfully retrieved, they are:')
for voice in result.voices:
print(voice.name)
elif result.reason == speechsdk.ResultReason.Canceled:
print("Speech synthesis canceled; error details: {}".format(result.error_details))
speech_synthesis_to_mp3_file()