2828def synthesize_text_file (text_file ):
2929 """Synthesizes speech from the input file of text."""
3030 from google .cloud import texttospeech
31+
3132 client = texttospeech .TextToSpeechClient ()
3233
33- with open (text_file , 'r' ) as f :
34+ with open (text_file , "r" ) as f :
3435 text = f .read ()
35- input_text = texttospeech .types . SynthesisInput (text = text )
36+ input_text = texttospeech .SynthesisInput (text = text )
3637
3738 # Note: the voice can also be specified by name.
3839 # Names of voices can be retrieved with client.list_voices().
39- voice = texttospeech .types . VoiceSelectionParams (
40- language_code = ' en-US' ,
41- ssml_gender = texttospeech . enums . SsmlVoiceGender . FEMALE )
40+ voice = texttospeech .VoiceSelectionParams (
41+ language_code = " en-US" , ssml_gender = texttospeech . SsmlVoiceGender . FEMALE
42+ )
4243
43- audio_config = texttospeech .types .AudioConfig (
44- audio_encoding = texttospeech .enums .AudioEncoding .MP3 )
44+ audio_config = texttospeech .AudioConfig (
45+ audio_encoding = texttospeech .AudioEncoding .MP3
46+ )
4547
46- response = client .synthesize_speech (input_text , voice , audio_config )
48+ response = client .synthesize_speech (
49+ request = {"input" : input_text , "voice" : voice , "audio_config" : audio_config }
50+ )
4751
4852 # The response's audio_content is binary.
49- with open (' output.mp3' , 'wb' ) as out :
53+ with open (" output.mp3" , "wb" ) as out :
5054 out .write (response .audio_content )
5155 print ('Audio content written to file "output.mp3"' )
56+
57+
5258# [END tts_synthesize_text_file]
5359
5460
@@ -60,39 +66,43 @@ def synthesize_ssml_file(ssml_file):
6066 https://www.w3.org/TR/speech-synthesis/
6167 """
6268 from google .cloud import texttospeech
69+
6370 client = texttospeech .TextToSpeechClient ()
6471
65- with open (ssml_file , 'r' ) as f :
72+ with open (ssml_file , "r" ) as f :
6673 ssml = f .read ()
67- input_text = texttospeech .types . SynthesisInput (ssml = ssml )
74+ input_text = texttospeech .SynthesisInput (ssml = ssml )
6875
6976 # Note: the voice can also be specified by name.
7077 # Names of voices can be retrieved with client.list_voices().
71- voice = texttospeech .types . VoiceSelectionParams (
72- language_code = ' en-US' ,
73- ssml_gender = texttospeech . enums . SsmlVoiceGender . FEMALE )
78+ voice = texttospeech .VoiceSelectionParams (
79+ language_code = " en-US" , ssml_gender = texttospeech . SsmlVoiceGender . FEMALE
80+ )
7481
75- audio_config = texttospeech .types .AudioConfig (
76- audio_encoding = texttospeech .enums .AudioEncoding .MP3 )
82+ audio_config = texttospeech .AudioConfig (
83+ audio_encoding = texttospeech .AudioEncoding .MP3
84+ )
7785
78- response = client .synthesize_speech (input_text , voice , audio_config )
86+ response = client .synthesize_speech (
87+ input = input_text , voice = voice , audio_config = audio_config
88+ )
7989
8090 # The response's audio_content is binary.
81- with open (' output.mp3' , 'wb' ) as out :
91+ with open (" output.mp3" , "wb" ) as out :
8292 out .write (response .audio_content )
8393 print ('Audio content written to file "output.mp3"' )
94+
95+
8496# [END tts_synthesize_ssml_file]
8597
8698
87- if __name__ == ' __main__' :
99+ if __name__ == " __main__" :
88100 parser = argparse .ArgumentParser (
89- description = __doc__ ,
90- formatter_class = argparse . RawDescriptionHelpFormatter )
101+ description = __doc__ , formatter_class = argparse . RawDescriptionHelpFormatter
102+ )
91103 group = parser .add_mutually_exclusive_group (required = True )
92- group .add_argument ('--text' ,
93- help = 'The text file from which to synthesize speech.' )
94- group .add_argument ('--ssml' ,
95- help = 'The ssml file from which to synthesize speech.' )
104+ group .add_argument ("--text" , help = "The text file from which to synthesize speech." )
105+ group .add_argument ("--ssml" , help = "The ssml file from which to synthesize speech." )
96106
97107 args = parser .parse_args ()
98108
0 commit comments