From f0acca6dea791a4eaf9a809305047c754cf44a0f Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Mon, 1 Jun 2020 16:23:26 -0700 Subject: [PATCH] feat!: regenerate with microgenerator (#30) BREAKING CHANGE: This commit has breaking changes. For help migrating your code, see UPGRADING.md. --- texttospeech/snippets/audio_profile.py | 38 +++++------ texttospeech/snippets/audio_profile_test.py | 9 ++- texttospeech/snippets/list_voices.py | 17 ++--- texttospeech/snippets/list_voices_test.py | 6 +- texttospeech/snippets/quickstart.py | 21 +++--- texttospeech/snippets/ssml_addresses.py | 36 ++++++----- texttospeech/snippets/ssml_addresses_test.py | 10 +-- texttospeech/snippets/synthesize_file.py | 60 +++++++++-------- texttospeech/snippets/synthesize_file_test.py | 12 ++-- texttospeech/snippets/synthesize_text.py | 64 +++++++++++-------- texttospeech/snippets/synthesize_text_test.py | 12 ++-- 11 files changed, 160 insertions(+), 125 deletions(-) diff --git a/texttospeech/snippets/audio_profile.py b/texttospeech/snippets/audio_profile.py index 01f2ba884ba3..cfecdb98cdd7 100644 --- a/texttospeech/snippets/audio_profile.py +++ b/texttospeech/snippets/audio_profile.py @@ -32,41 +32,43 @@ def synthesize_text_with_audio_profile(text, output, effects_profile_id): client = texttospeech.TextToSpeechClient() - input_text = texttospeech.types.SynthesisInput(text=text) + input_text = texttospeech.SynthesisInput(text=text) # Note: the voice can also be specified by name. # Names of voices can be retrieved with client.list_voices(). - voice = texttospeech.types.VoiceSelectionParams(language_code='en-US') + voice = texttospeech.VoiceSelectionParams(language_code="en-US") # Note: you can pass in multiple effects_profile_id. They will be applied # in the same order they are provided. - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3, - effects_profile_id=[effects_profile_id]) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3, + effects_profile_id=[effects_profile_id], + ) - response = client.synthesize_speech(input_text, voice, audio_config) + response = client.synthesize_speech( + input=input_text, voice=voice, audio_config=audio_config + ) # The response's audio_content is binary. - with open(output, 'wb') as out: + with open(output, "wb") as out: out.write(response.audio_content) print('Audio content written to file "%s"' % output) + # [END tts_synthesize_text_audio_profile_beta] # [END tts_synthesize_text_audio_profile] -if __name__ == '__main__': +if __name__ == "__main__": parser = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter) - parser.add_argument('--output', - help='The output mp3 file.') - parser.add_argument('--text', - help='The text from which to synthesize speech.') - parser.add_argument('--effects_profile_id', - help='The audio effects profile id to be applied.') + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) + parser.add_argument("--output", help="The output mp3 file.") + parser.add_argument("--text", help="The text from which to synthesize speech.") + parser.add_argument( + "--effects_profile_id", help="The audio effects profile id to be applied." + ) args = parser.parse_args() - synthesize_text_with_audio_profile(args.text, args.output, - args.effects_profile_id) + synthesize_text_with_audio_profile(args.text, args.output, args.effects_profile_id) diff --git a/texttospeech/snippets/audio_profile_test.py b/texttospeech/snippets/audio_profile_test.py index 43a30bf06d16..89616cf80a84 100644 --- a/texttospeech/snippets/audio_profile_test.py +++ b/texttospeech/snippets/audio_profile_test.py @@ -16,17 +16,16 @@ import audio_profile -TEXT = 'hello' -OUTPUT = 'output.mp3' -EFFECTS_PROFILE_ID = 'telephony-class-application' +TEXT = "hello" +OUTPUT = "output.mp3" +EFFECTS_PROFILE_ID = "telephony-class-application" def test_audio_profile(capsys): if os.path.exists(OUTPUT): os.remove(OUTPUT) assert not os.path.exists(OUTPUT) - audio_profile.synthesize_text_with_audio_profile(TEXT, OUTPUT, - EFFECTS_PROFILE_ID) + audio_profile.synthesize_text_with_audio_profile(TEXT, OUTPUT, EFFECTS_PROFILE_ID) out, err = capsys.readouterr() assert ('Audio content written to file "%s"' % OUTPUT) in out diff --git a/texttospeech/snippets/list_voices.py b/texttospeech/snippets/list_voices.py index a2da5a178528..4d991a952394 100644 --- a/texttospeech/snippets/list_voices.py +++ b/texttospeech/snippets/list_voices.py @@ -25,7 +25,7 @@ def list_voices(): """Lists the available voices.""" from google.cloud import texttospeech - from google.cloud.texttospeech import enums + client = texttospeech.TextToSpeechClient() # Performs the list voices request @@ -33,22 +33,23 @@ def list_voices(): for voice in voices.voices: # Display the voice's name. Example: tpc-vocoded - print('Name: {}'.format(voice.name)) + print(f"Name: {voice.name}") # Display the supported language codes for this voice. Example: "en-US" for language_code in voice.language_codes: - print('Supported language: {}'.format(language_code)) + print(f"Supported language: {language_code}") - ssml_gender = enums.SsmlVoiceGender(voice.ssml_gender) + ssml_gender = texttospeech.SsmlVoiceGender(voice.ssml_gender) # Display the SSML Voice Gender - print('SSML Voice Gender: {}'.format(ssml_gender.name)) + print(f"SSML Voice Gender: {ssml_gender.name}") # Display the natural sample rate hertz for this voice. Example: 24000 - print('Natural Sample Rate Hertz: {}\n'.format( - voice.natural_sample_rate_hertz)) + print(f"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\n") + + # [END tts_list_voices] -if __name__ == '__main__': +if __name__ == "__main__": list_voices() diff --git a/texttospeech/snippets/list_voices_test.py b/texttospeech/snippets/list_voices_test.py index fd3255694922..9260e86d5e13 100644 --- a/texttospeech/snippets/list_voices_test.py +++ b/texttospeech/snippets/list_voices_test.py @@ -18,6 +18,6 @@ def test_list_voices(capsys): list_voices.list_voices() out, err = capsys.readouterr() - assert 'en-US' in out - assert 'SSML Voice Gender: MALE' in out - assert 'SSML Voice Gender: FEMALE' in out + assert "en-US" in out + assert "SSML Voice Gender: MALE" in out + assert "SSML Voice Gender: FEMALE" in out diff --git a/texttospeech/snippets/quickstart.py b/texttospeech/snippets/quickstart.py index f462139d794b..f9e3da5ccee8 100644 --- a/texttospeech/snippets/quickstart.py +++ b/texttospeech/snippets/quickstart.py @@ -34,29 +34,32 @@ def run_quickstart(): client = texttospeech.TextToSpeechClient() # Set the text input to be synthesized - synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!") + synthesis_input = texttospeech.SynthesisInput(text="Hello, World!") # Build the voice request, select the language code ("en-US") and the ssml # voice gender ("neutral") - voice = texttospeech.types.VoiceSelectionParams( - language_code='en-US', - ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL) + voice = texttospeech.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL + ) # Select the type of audio file you want returned - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) # Perform the text-to-speech request on the text input with the selected # voice parameters and audio file type - response = client.synthesize_speech(synthesis_input, voice, audio_config) + response = client.synthesize_speech( + input=synthesis_input, voice=voice, audio_config=audio_config + ) # The response's audio_content is binary. - with open('output.mp3', 'wb') as out: + with open("output.mp3", "wb") as out: # Write the response to the output file. out.write(response.audio_content) print('Audio content written to file "output.mp3"') # [END tts_quickstart] -if __name__ == '__main__': +if __name__ == "__main__": run_quickstart() diff --git a/texttospeech/snippets/ssml_addresses.py b/texttospeech/snippets/ssml_addresses.py index dd9276058a44..512293576655 100644 --- a/texttospeech/snippets/ssml_addresses.py +++ b/texttospeech/snippets/ssml_addresses.py @@ -41,26 +41,29 @@ def ssml_to_audio(ssml_text, outfile): client = texttospeech.TextToSpeechClient() # Sets the text input to be synthesized - synthesis_input = texttospeech.types.SynthesisInput(ssml=ssml_text) + synthesis_input = texttospeech.SynthesisInput(ssml=ssml_text) # Builds the voice request, selects the language code ("en-US") and # the SSML voice gender ("MALE") - voice = texttospeech.types.VoiceSelectionParams( - language_code='en-US', - ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE) + voice = texttospeech.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.MALE + ) # Selects the type of audio file to return - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) # Performs the text-to-speech request on the text input with the selected # voice parameters and audio file type - response = client.synthesize_speech(synthesis_input, voice, audio_config) + response = client.synthesize_speech( + input=synthesis_input, voice=voice, audio_config=audio_config + ) # Writes the synthetic audio to the output file. - with open(outfile, 'wb') as out: + with open(outfile, "wb") as out: out.write(response.audio_content) - print('Audio content written to file ' + outfile) + print("Audio content written to file " + outfile) # [END tts_ssml_address_audio] @@ -80,7 +83,7 @@ def text_to_ssml(inputfile): # A string of SSML text based on plaintext input # Parses lines of input file - with open(inputfile, 'r') as f: + with open(inputfile, "r") as f: raw_lines = f.read() # Replace special characters with HTML Ampersand Character Codes @@ -92,22 +95,25 @@ def text_to_ssml(inputfile): # Convert plaintext to SSML # Wait two seconds between each address - ssml = '{}'.format( - escaped_lines.replace('\n', '\n')) + ssml = "{}".format( + escaped_lines.replace("\n", '\n') + ) # Return the concatenated string of ssml script return ssml + + # [END tts_ssml_address_ssml] # [START tts_ssml_address_test] def main(): # test example address file - plaintext = 'resources/example.txt' + plaintext = "resources/example.txt" ssml_text = text_to_ssml(plaintext) - ssml_to_audio(ssml_text, 'resources/example.mp3') + ssml_to_audio(ssml_text, "resources/example.mp3") # [END tts_ssml_address_test] -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/texttospeech/snippets/ssml_addresses_test.py b/texttospeech/snippets/ssml_addresses_test.py index 5f0016085b15..caf25c0929bb 100644 --- a/texttospeech/snippets/ssml_addresses_test.py +++ b/texttospeech/snippets/ssml_addresses_test.py @@ -20,11 +20,11 @@ def test_text_to_ssml(capsys): # Read expected SSML output from resources - with open('resources/example.ssml', 'r') as f: + with open("resources/example.ssml", "r") as f: expected_ssml = f.read() # Assert plaintext converted to SSML - input_text = 'resources/example.txt' + input_text = "resources/example.txt" tested_ssml = text_to_ssml(input_text) assert expected_ssml == tested_ssml @@ -32,15 +32,15 @@ def test_text_to_ssml(capsys): def test_ssml_to_audio(capsys): # Read SSML input from resources - with open('resources/example.ssml', 'r') as f: + with open("resources/example.ssml", "r") as f: input_ssml = f.read() # Assert audio file generated - ssml_to_audio(input_ssml, 'test_example.mp3') + ssml_to_audio(input_ssml, "test_example.mp3") out, err = capsys.readouterr() # Assert MP3 file created - assert os.path.isfile('test_example.mp3') + assert os.path.isfile("test_example.mp3") assert "Audio content written to file test_example.mp3" in out # Delete MP3 test file diff --git a/texttospeech/snippets/synthesize_file.py b/texttospeech/snippets/synthesize_file.py index f62d6330d7d5..977c049515b2 100644 --- a/texttospeech/snippets/synthesize_file.py +++ b/texttospeech/snippets/synthesize_file.py @@ -28,27 +28,33 @@ def synthesize_text_file(text_file): """Synthesizes speech from the input file of text.""" from google.cloud import texttospeech + client = texttospeech.TextToSpeechClient() - with open(text_file, 'r') as f: + with open(text_file, "r") as f: text = f.read() - input_text = texttospeech.types.SynthesisInput(text=text) + input_text = texttospeech.SynthesisInput(text=text) # Note: the voice can also be specified by name. # Names of voices can be retrieved with client.list_voices(). - voice = texttospeech.types.VoiceSelectionParams( - language_code='en-US', - ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE) + voice = texttospeech.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.FEMALE + ) - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) - response = client.synthesize_speech(input_text, voice, audio_config) + response = client.synthesize_speech( + request={"input": input_text, "voice": voice, "audio_config": audio_config} + ) # The response's audio_content is binary. - with open('output.mp3', 'wb') as out: + with open("output.mp3", "wb") as out: out.write(response.audio_content) print('Audio content written to file "output.mp3"') + + # [END tts_synthesize_text_file] @@ -60,39 +66,43 @@ def synthesize_ssml_file(ssml_file): https://www.w3.org/TR/speech-synthesis/ """ from google.cloud import texttospeech + client = texttospeech.TextToSpeechClient() - with open(ssml_file, 'r') as f: + with open(ssml_file, "r") as f: ssml = f.read() - input_text = texttospeech.types.SynthesisInput(ssml=ssml) + input_text = texttospeech.SynthesisInput(ssml=ssml) # Note: the voice can also be specified by name. # Names of voices can be retrieved with client.list_voices(). - voice = texttospeech.types.VoiceSelectionParams( - language_code='en-US', - ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE) + voice = texttospeech.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.FEMALE + ) - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) - response = client.synthesize_speech(input_text, voice, audio_config) + response = client.synthesize_speech( + input=input_text, voice=voice, audio_config=audio_config + ) # The response's audio_content is binary. - with open('output.mp3', 'wb') as out: + with open("output.mp3", "wb") as out: out.write(response.audio_content) print('Audio content written to file "output.mp3"') + + # [END tts_synthesize_ssml_file] -if __name__ == '__main__': +if __name__ == "__main__": parser = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter) + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) group = parser.add_mutually_exclusive_group(required=True) - group.add_argument('--text', - help='The text file from which to synthesize speech.') - group.add_argument('--ssml', - help='The ssml file from which to synthesize speech.') + group.add_argument("--text", help="The text file from which to synthesize speech.") + group.add_argument("--ssml", help="The ssml file from which to synthesize speech.") args = parser.parse_args() diff --git a/texttospeech/snippets/synthesize_file_test.py b/texttospeech/snippets/synthesize_file_test.py index 2652009f98b6..be6b6e052403 100644 --- a/texttospeech/snippets/synthesize_file_test.py +++ b/texttospeech/snippets/synthesize_file_test.py @@ -15,16 +15,16 @@ import synthesize_file -TEXT_FILE = 'resources/hello.txt' -SSML_FILE = 'resources/hello.ssml' +TEXT_FILE = "resources/hello.txt" +SSML_FILE = "resources/hello.ssml" def test_synthesize_text_file(capsys): synthesize_file.synthesize_text_file(text_file=TEXT_FILE) out, err = capsys.readouterr() - assert 'Audio content written to file' in out - statinfo = os.stat('output.mp3') + assert "Audio content written to file" in out + statinfo = os.stat("output.mp3") assert statinfo.st_size > 0 @@ -32,6 +32,6 @@ def test_synthesize_ssml_file(capsys): synthesize_file.synthesize_ssml_file(ssml_file=SSML_FILE) out, err = capsys.readouterr() - assert 'Audio content written to file' in out - statinfo = os.stat('output.mp3') + assert "Audio content written to file" in out + statinfo = os.stat("output.mp3") assert statinfo.st_size > 0 diff --git a/texttospeech/snippets/synthesize_text.py b/texttospeech/snippets/synthesize_text.py index 768c0da03178..c22bff40e2f4 100644 --- a/texttospeech/snippets/synthesize_text.py +++ b/texttospeech/snippets/synthesize_text.py @@ -28,26 +28,33 @@ def synthesize_text(text): """Synthesizes speech from the input string of text.""" from google.cloud import texttospeech + client = texttospeech.TextToSpeechClient() - input_text = texttospeech.types.SynthesisInput(text=text) + input_text = texttospeech.SynthesisInput(text=text) # Note: the voice can also be specified by name. # Names of voices can be retrieved with client.list_voices(). - voice = texttospeech.types.VoiceSelectionParams( - language_code='en-US', - name='en-US-Standard-C', - ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE) + voice = texttospeech.VoiceSelectionParams( + language_code="en-US", + name="en-US-Standard-C", + ssml_gender=texttospeech.SsmlVoiceGender.FEMALE, + ) - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) - response = client.synthesize_speech(input_text, voice, audio_config) + response = client.synthesize_speech( + request={"input": input_text, "voice": voice, "audio_config": audio_config} + ) # The response's audio_content is binary. - with open('output.mp3', 'wb') as out: + with open("output.mp3", "wb") as out: out.write(response.audio_content) print('Audio content written to file "output.mp3"') + + # [END tts_synthesize_text] @@ -61,38 +68,45 @@ def synthesize_ssml(ssml): Example: Hello there. """ from google.cloud import texttospeech + client = texttospeech.TextToSpeechClient() - input_text = texttospeech.types.SynthesisInput(ssml=ssml) + input_text = texttospeech.SynthesisInput(ssml=ssml) # Note: the voice can also be specified by name. # Names of voices can be retrieved with client.list_voices(). - voice = texttospeech.types.VoiceSelectionParams( - language_code='en-US', - name='en-US-Standard-C', - ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE) + voice = texttospeech.VoiceSelectionParams( + language_code="en-US", + name="en-US-Standard-C", + ssml_gender=texttospeech.SsmlVoiceGender.FEMALE, + ) - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) - response = client.synthesize_speech(input_text, voice, audio_config) + response = client.synthesize_speech( + input=input_text, voice=voice, audio_config=audio_config + ) # The response's audio_content is binary. - with open('output.mp3', 'wb') as out: + with open("output.mp3", "wb") as out: out.write(response.audio_content) print('Audio content written to file "output.mp3"') + + # [END tts_synthesize_ssml] -if __name__ == '__main__': +if __name__ == "__main__": parser = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter) + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) group = parser.add_mutually_exclusive_group(required=True) - group.add_argument('--text', - help='The text from which to synthesize speech.') - group.add_argument('--ssml', - help='The ssml string from which to synthesize speech.') + group.add_argument("--text", help="The text from which to synthesize speech.") + group.add_argument( + "--ssml", help="The ssml string from which to synthesize speech." + ) args = parser.parse_args() diff --git a/texttospeech/snippets/synthesize_text_test.py b/texttospeech/snippets/synthesize_text_test.py index 948d58da26df..68c52e38a648 100644 --- a/texttospeech/snippets/synthesize_text_test.py +++ b/texttospeech/snippets/synthesize_text_test.py @@ -15,16 +15,16 @@ import synthesize_text -TEXT = 'Hello there.' -SSML = 'Hello there.' +TEXT = "Hello there." +SSML = "Hello there." def test_synthesize_text(capsys): synthesize_text.synthesize_text(text=TEXT) out, err = capsys.readouterr() - assert 'Audio content written to file' in out - statinfo = os.stat('output.mp3') + assert "Audio content written to file" in out + statinfo = os.stat("output.mp3") assert statinfo.st_size > 0 @@ -32,6 +32,6 @@ def test_synthesize_ssml(capsys): synthesize_text.synthesize_ssml(ssml=SSML) out, err = capsys.readouterr() - assert 'Audio content written to file' in out - statinfo = os.stat('output.mp3') + assert "Audio content written to file" in out + statinfo = os.stat("output.mp3") assert statinfo.st_size > 0