Skip to content

Commit f0acca6

Browse files
authored
feat!: regenerate with microgenerator (#30)
BREAKING CHANGE: This commit has breaking changes. For help migrating your code, see UPGRADING.md.
1 parent d3f5a11 commit f0acca6

11 files changed

+160
-125
lines changed

texttospeech/snippets/audio_profile.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,43 @@ def synthesize_text_with_audio_profile(text, output, effects_profile_id):
3232

3333
client = texttospeech.TextToSpeechClient()
3434

35-
input_text = texttospeech.types.SynthesisInput(text=text)
35+
input_text = texttospeech.SynthesisInput(text=text)
3636

3737
# Note: the voice can also be specified by name.
3838
# Names of voices can be retrieved with client.list_voices().
39-
voice = texttospeech.types.VoiceSelectionParams(language_code='en-US')
39+
voice = texttospeech.VoiceSelectionParams(language_code="en-US")
4040

4141
# Note: you can pass in multiple effects_profile_id. They will be applied
4242
# in the same order they are provided.
43-
audio_config = texttospeech.types.AudioConfig(
44-
audio_encoding=texttospeech.enums.AudioEncoding.MP3,
45-
effects_profile_id=[effects_profile_id])
43+
audio_config = texttospeech.AudioConfig(
44+
audio_encoding=texttospeech.AudioEncoding.MP3,
45+
effects_profile_id=[effects_profile_id],
46+
)
4647

47-
response = client.synthesize_speech(input_text, voice, audio_config)
48+
response = client.synthesize_speech(
49+
input=input_text, voice=voice, audio_config=audio_config
50+
)
4851

4952
# The response's audio_content is binary.
50-
with open(output, 'wb') as out:
53+
with open(output, "wb") as out:
5154
out.write(response.audio_content)
5255
print('Audio content written to file "%s"' % output)
5356

57+
5458
# [END tts_synthesize_text_audio_profile_beta]
5559
# [END tts_synthesize_text_audio_profile]
5660

5761

58-
if __name__ == '__main__':
62+
if __name__ == "__main__":
5963
parser = argparse.ArgumentParser(
60-
description=__doc__,
61-
formatter_class=argparse.RawDescriptionHelpFormatter)
62-
parser.add_argument('--output',
63-
help='The output mp3 file.')
64-
parser.add_argument('--text',
65-
help='The text from which to synthesize speech.')
66-
parser.add_argument('--effects_profile_id',
67-
help='The audio effects profile id to be applied.')
64+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
65+
)
66+
parser.add_argument("--output", help="The output mp3 file.")
67+
parser.add_argument("--text", help="The text from which to synthesize speech.")
68+
parser.add_argument(
69+
"--effects_profile_id", help="The audio effects profile id to be applied."
70+
)
6871

6972
args = parser.parse_args()
7073

71-
synthesize_text_with_audio_profile(args.text, args.output,
72-
args.effects_profile_id)
74+
synthesize_text_with_audio_profile(args.text, args.output, args.effects_profile_id)

texttospeech/snippets/audio_profile_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616

1717
import audio_profile
1818

19-
TEXT = 'hello'
20-
OUTPUT = 'output.mp3'
21-
EFFECTS_PROFILE_ID = 'telephony-class-application'
19+
TEXT = "hello"
20+
OUTPUT = "output.mp3"
21+
EFFECTS_PROFILE_ID = "telephony-class-application"
2222

2323

2424
def test_audio_profile(capsys):
2525
if os.path.exists(OUTPUT):
2626
os.remove(OUTPUT)
2727
assert not os.path.exists(OUTPUT)
28-
audio_profile.synthesize_text_with_audio_profile(TEXT, OUTPUT,
29-
EFFECTS_PROFILE_ID)
28+
audio_profile.synthesize_text_with_audio_profile(TEXT, OUTPUT, EFFECTS_PROFILE_ID)
3029
out, err = capsys.readouterr()
3130

3231
assert ('Audio content written to file "%s"' % OUTPUT) in out

texttospeech/snippets/list_voices.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,31 @@
2525
def list_voices():
2626
"""Lists the available voices."""
2727
from google.cloud import texttospeech
28-
from google.cloud.texttospeech import enums
28+
2929
client = texttospeech.TextToSpeechClient()
3030

3131
# Performs the list voices request
3232
voices = client.list_voices()
3333

3434
for voice in voices.voices:
3535
# Display the voice's name. Example: tpc-vocoded
36-
print('Name: {}'.format(voice.name))
36+
print(f"Name: {voice.name}")
3737

3838
# Display the supported language codes for this voice. Example: "en-US"
3939
for language_code in voice.language_codes:
40-
print('Supported language: {}'.format(language_code))
40+
print(f"Supported language: {language_code}")
4141

42-
ssml_gender = enums.SsmlVoiceGender(voice.ssml_gender)
42+
ssml_gender = texttospeech.SsmlVoiceGender(voice.ssml_gender)
4343

4444
# Display the SSML Voice Gender
45-
print('SSML Voice Gender: {}'.format(ssml_gender.name))
45+
print(f"SSML Voice Gender: {ssml_gender.name}")
4646

4747
# Display the natural sample rate hertz for this voice. Example: 24000
48-
print('Natural Sample Rate Hertz: {}\n'.format(
49-
voice.natural_sample_rate_hertz))
48+
print(f"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\n")
49+
50+
5051
# [END tts_list_voices]
5152

5253

53-
if __name__ == '__main__':
54+
if __name__ == "__main__":
5455
list_voices()

texttospeech/snippets/list_voices_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ def test_list_voices(capsys):
1818
list_voices.list_voices()
1919
out, err = capsys.readouterr()
2020

21-
assert 'en-US' in out
22-
assert 'SSML Voice Gender: MALE' in out
23-
assert 'SSML Voice Gender: FEMALE' in out
21+
assert "en-US" in out
22+
assert "SSML Voice Gender: MALE" in out
23+
assert "SSML Voice Gender: FEMALE" in out

texttospeech/snippets/quickstart.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,32 @@ def run_quickstart():
3434
client = texttospeech.TextToSpeechClient()
3535

3636
# Set the text input to be synthesized
37-
synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!")
37+
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")
3838

3939
# Build the voice request, select the language code ("en-US") and the ssml
4040
# voice gender ("neutral")
41-
voice = texttospeech.types.VoiceSelectionParams(
42-
language_code='en-US',
43-
ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)
41+
voice = texttospeech.VoiceSelectionParams(
42+
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
43+
)
4444

4545
# Select the type of audio file you want returned
46-
audio_config = texttospeech.types.AudioConfig(
47-
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
46+
audio_config = texttospeech.AudioConfig(
47+
audio_encoding=texttospeech.AudioEncoding.MP3
48+
)
4849

4950
# Perform the text-to-speech request on the text input with the selected
5051
# voice parameters and audio file type
51-
response = client.synthesize_speech(synthesis_input, voice, audio_config)
52+
response = client.synthesize_speech(
53+
input=synthesis_input, voice=voice, audio_config=audio_config
54+
)
5255

5356
# The response's audio_content is binary.
54-
with open('output.mp3', 'wb') as out:
57+
with open("output.mp3", "wb") as out:
5558
# Write the response to the output file.
5659
out.write(response.audio_content)
5760
print('Audio content written to file "output.mp3"')
5861
# [END tts_quickstart]
5962

6063

61-
if __name__ == '__main__':
64+
if __name__ == "__main__":
6265
run_quickstart()

texttospeech/snippets/ssml_addresses.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,29 @@ def ssml_to_audio(ssml_text, outfile):
4141
client = texttospeech.TextToSpeechClient()
4242

4343
# Sets the text input to be synthesized
44-
synthesis_input = texttospeech.types.SynthesisInput(ssml=ssml_text)
44+
synthesis_input = texttospeech.SynthesisInput(ssml=ssml_text)
4545

4646
# Builds the voice request, selects the language code ("en-US") and
4747
# the SSML voice gender ("MALE")
48-
voice = texttospeech.types.VoiceSelectionParams(
49-
language_code='en-US',
50-
ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE)
48+
voice = texttospeech.VoiceSelectionParams(
49+
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.MALE
50+
)
5151

5252
# Selects the type of audio file to return
53-
audio_config = texttospeech.types.AudioConfig(
54-
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
53+
audio_config = texttospeech.AudioConfig(
54+
audio_encoding=texttospeech.AudioEncoding.MP3
55+
)
5556

5657
# Performs the text-to-speech request on the text input with the selected
5758
# voice parameters and audio file type
58-
response = client.synthesize_speech(synthesis_input, voice, audio_config)
59+
response = client.synthesize_speech(
60+
input=synthesis_input, voice=voice, audio_config=audio_config
61+
)
5962

6063
# Writes the synthetic audio to the output file.
61-
with open(outfile, 'wb') as out:
64+
with open(outfile, "wb") as out:
6265
out.write(response.audio_content)
63-
print('Audio content written to file ' + outfile)
66+
print("Audio content written to file " + outfile)
6467
# [END tts_ssml_address_audio]
6568

6669

@@ -80,7 +83,7 @@ def text_to_ssml(inputfile):
8083
# A string of SSML text based on plaintext input
8184

8285
# Parses lines of input file
83-
with open(inputfile, 'r') as f:
86+
with open(inputfile, "r") as f:
8487
raw_lines = f.read()
8588

8689
# Replace special characters with HTML Ampersand Character Codes
@@ -92,22 +95,25 @@ def text_to_ssml(inputfile):
9295

9396
# Convert plaintext to SSML
9497
# Wait two seconds between each address
95-
ssml = '<speak>{}</speak>'.format(
96-
escaped_lines.replace('\n', '\n<break time="2s"/>'))
98+
ssml = "<speak>{}</speak>".format(
99+
escaped_lines.replace("\n", '\n<break time="2s"/>')
100+
)
97101

98102
# Return the concatenated string of ssml script
99103
return ssml
104+
105+
100106
# [END tts_ssml_address_ssml]
101107

102108

103109
# [START tts_ssml_address_test]
104110
def main():
105111
# test example address file
106-
plaintext = 'resources/example.txt'
112+
plaintext = "resources/example.txt"
107113
ssml_text = text_to_ssml(plaintext)
108-
ssml_to_audio(ssml_text, 'resources/example.mp3')
114+
ssml_to_audio(ssml_text, "resources/example.mp3")
109115
# [END tts_ssml_address_test]
110116

111117

112-
if __name__ == '__main__':
118+
if __name__ == "__main__":
113119
main()

texttospeech/snippets/ssml_addresses_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@
2020
def test_text_to_ssml(capsys):
2121

2222
# Read expected SSML output from resources
23-
with open('resources/example.ssml', 'r') as f:
23+
with open("resources/example.ssml", "r") as f:
2424
expected_ssml = f.read()
2525

2626
# Assert plaintext converted to SSML
27-
input_text = 'resources/example.txt'
27+
input_text = "resources/example.txt"
2828
tested_ssml = text_to_ssml(input_text)
2929
assert expected_ssml == tested_ssml
3030

3131

3232
def test_ssml_to_audio(capsys):
3333

3434
# Read SSML input from resources
35-
with open('resources/example.ssml', 'r') as f:
35+
with open("resources/example.ssml", "r") as f:
3636
input_ssml = f.read()
3737

3838
# Assert audio file generated
39-
ssml_to_audio(input_ssml, 'test_example.mp3')
39+
ssml_to_audio(input_ssml, "test_example.mp3")
4040
out, err = capsys.readouterr()
4141

4242
# Assert MP3 file created
43-
assert os.path.isfile('test_example.mp3')
43+
assert os.path.isfile("test_example.mp3")
4444
assert "Audio content written to file test_example.mp3" in out
4545

4646
# Delete MP3 test file

texttospeech/snippets/synthesize_file.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,33 @@
2828
def 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

Comments
 (0)