Skip to content

Commit

Permalink
feat!: regenerate with microgenerator (#30)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This commit has breaking changes.

For help migrating your code, see UPGRADING.md.
  • Loading branch information
busunkim96 authored Jun 1, 2020
1 parent 4957380 commit db1a24c
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 125 deletions.
38 changes: 20 additions & 18 deletions texttospeech/snippets/audio_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 4 additions & 5 deletions texttospeech/snippets/audio_profile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions texttospeech/snippets/list_voices.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,31 @@
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
voices = client.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()
6 changes: 3 additions & 3 deletions texttospeech/snippets/list_voices_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 12 additions & 9 deletions texttospeech/snippets/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
36 changes: 21 additions & 15 deletions texttospeech/snippets/ssml_addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand All @@ -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
Expand All @@ -92,22 +95,25 @@ def text_to_ssml(inputfile):

# Convert plaintext to SSML
# Wait two seconds between each address
ssml = '<speak>{}</speak>'.format(
escaped_lines.replace('\n', '\n<break time="2s"/>'))
ssml = "<speak>{}</speak>".format(
escaped_lines.replace("\n", '\n<break time="2s"/>')
)

# 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()
10 changes: 5 additions & 5 deletions texttospeech/snippets/ssml_addresses_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@
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


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
Expand Down
60 changes: 35 additions & 25 deletions texttospeech/snippets/synthesize_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand All @@ -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()

Expand Down
Loading

0 comments on commit db1a24c

Please sign in to comment.