diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 808f6b1db1d4..b5eff7910a6b 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -69,6 +69,7 @@ /secretmanager/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers /storage/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers /storagetransfer/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers +/texttospeech/**/* @GoogleCloudPlatform/dee-platform-ops @GoogleCloudPlatform/python-samples-reviewers /trace/**/* @ymotongpoo @GoogleCloudPlatform/python-samples-reviewers /translate/**/* @nicain @GoogleCloudPlatform/python-samples-reviewers /talent/**/* @GoogleCloudPlatform/python-samples-reviewers diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index c524caa7dca5..a7c8b85bf377 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -125,11 +125,15 @@ assign_issues_by: - 'api: datalabeling' to: - GoogleCloudPlatform/python-samples-reviewers - - ivanmkc + - ivanmkc - labels: - 'api: monitoring' - to: + to: - GoogleCloudPlatform/dee-observability +- labels: + - 'api: texttospeech' + to: + - GoogleCloudPlatform/dee-platform-ops - labels: - 'api: datacatalog' to: diff --git a/.github/header-checker-lint.yml b/.github/header-checker-lint.yml index d71c5bc28383..30c6c690f9de 100644 --- a/.github/header-checker-lint.yml +++ b/.github/header-checker-lint.yml @@ -16,6 +16,9 @@ ignoreFiles: - "**/constraints-test.txt" - "**/apt.txt" - "**/ghcnd-stations.txt" + - "texttospeech/snippets/resources/example.txt" + - "texttospeech/snippets/resources/hello.txt" + ignoreLicenseYear: true diff --git a/texttospeech/snippets/README.rst b/texttospeech/snippets/README.rst new file mode 100644 index 000000000000..ff08a7591d19 --- /dev/null +++ b/texttospeech/snippets/README.rst @@ -0,0 +1,234 @@ + +.. This file is automatically generated. Do not edit this file directly. + +Google Cloud Text-to-Speech API Python Samples +=============================================================================== + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/README.rst + + +This directory contains samples for Google Cloud Text-to-Speech API. The `Google Cloud Text To Speech API`_ enables you to generate and customize synthesized speech from text or SSML. + + + + +.. _Google Cloud Text-to-Speech API: https://cloud.google.com/text-to-speech/docs/ + + +Setup +------------------------------------------------------------------------------- + + + +Authentication +++++++++++++++ + +This sample requires you to have authentication setup. Refer to the +`Authentication Getting Started Guide`_ for instructions on setting up +credentials for applications. + +.. _Authentication Getting Started Guide: + https://cloud.google.com/docs/authentication/getting-started + + + + +Install Dependencies +++++++++++++++++++++ + +#. Clone python-docs-samples and change directory to the sample directory you want to use. + + .. code-block:: bash + + $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git + +#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. + + .. _Python Development Environment Setup Guide: + https://cloud.google.com/python/setup + +#. Create a virtualenv. Samples are compatible with Python 3.6+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + + + + + + +Samples +------------------------------------------------------------------------------- + + +Quickstart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/quickstart.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python quickstart.py + + + + +List voices ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/list_voices.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python list_voices.py + + + + +Synthesize text ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/synthesize_text.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python synthesize_text.py + + + usage: synthesize_text.py [-h] (--text TEXT | --ssml SSML) + + Google Cloud Text-To-Speech API sample application . + + Example usage: + python synthesize_text.py --text "hello" + python synthesize_text.py --ssml "Hello there." + + optional arguments: + -h, --help show this help message and exit + --text TEXT The text from which to synthesize speech. + --ssml SSML The ssml string from which to synthesize speech. + + + + + +Synthesize file ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/synthesize_file.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python synthesize_file.py + + + usage: synthesize_file.py [-h] (--text TEXT | --ssml SSML) + + Google Cloud Text-To-Speech API sample application . + + Example usage: + python synthesize_file.py --text resources/hello.txt + python synthesize_file.py --ssml resources/hello.ssml + + optional arguments: + -h, --help show this help message and exit + --text TEXT The text file from which to synthesize speech. + --ssml SSML The ssml file from which to synthesize speech. + + + + + +Audio profile ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/audio_profile.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python audio_profile.py + + + usage: audio_profile.py [-h] [--output OUTPUT] [--text TEXT] + [--effects_profile_id EFFECTS_PROFILE_ID] + + Google Cloud Text-To-Speech API sample application for audio profile. + + Example usage: + python audio_profile.py --text "hello" --effects_profile_id + "telephony-class-application" --output "output.mp3" + + optional arguments: + -h, --help show this help message and exit + --output OUTPUT The output mp3 file. + --text TEXT The text from which to synthesize speech. + --effects_profile_id EFFECTS_PROFILE_ID + The audio effects profile id to be applied. + + + + + + + + + +The client library +------------------------------------------------------------------------------- + +This sample uses the `Google Cloud Client Library for Python`_. +You can read the documentation for more details on API usage and use GitHub +to `browse the source`_ and `report issues`_. + +.. _Google Cloud Client Library for Python: + https://googlecloudplatform.github.io/google-cloud-python/ +.. _browse the source: + https://github.com/GoogleCloudPlatform/google-cloud-python +.. _report issues: + https://github.com/GoogleCloudPlatform/google-cloud-python/issues + + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ diff --git a/texttospeech/snippets/README.rst.in b/texttospeech/snippets/README.rst.in new file mode 100644 index 000000000000..8be6ab467741 --- /dev/null +++ b/texttospeech/snippets/README.rst.in @@ -0,0 +1,29 @@ +# This file is used to generate README.rst + +product: + name: Google Cloud Text-to-Speech API + short_name: Cloud TTS API + url: https://cloud.google.com/text-to-speech/docs/ + description: > + The `Google Cloud Text To Speech API`_ enables you to generate and customize synthesized speech from text or SSML. + +setup: +- auth +- install_deps + +samples: +- name: Quickstart + file: quickstart.py +- name: List voices + file: list_voices.py +- name: Synthesize text + file: synthesize_text.py + show_help: True +- name: Synthesize file + file: synthesize_file.py + show_help: True +- name: Audio profile + file: audio_profile.py + show_help: True + +cloud_client_library: true diff --git a/texttospeech/snippets/audio_profile.py b/texttospeech/snippets/audio_profile.py new file mode 100644 index 000000000000..70921cf775f2 --- /dev/null +++ b/texttospeech/snippets/audio_profile.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +"""Google Cloud Text-To-Speech API sample application for audio profile. + +Example usage: + python audio_profile.py --text "hello" --effects_profile_id + "telephony-class-application" --output "output.mp3" +""" + +import argparse + + +# [START tts_synthesize_text_audio_profile] +# [START tts_synthesize_text_audio_profile_beta] +def synthesize_text_with_audio_profile(text, output, effects_profile_id): + """Synthesizes speech from the input string of text.""" + from google.cloud import texttospeech + + client = texttospeech.TextToSpeechClient() + + 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.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.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3, + effects_profile_id=[effects_profile_id], + ) + + 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: + 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__": + 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." + ) + + args = parser.parse_args() + + 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 new file mode 100644 index 000000000000..74a2255e95ad --- /dev/null +++ b/texttospeech/snippets/audio_profile_test.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import os.path + +import audio_profile + +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) + out, err = capsys.readouterr() + + assert ('Audio content written to file "%s"' % OUTPUT) in out + assert os.path.exists(OUTPUT) + os.remove(OUTPUT) diff --git a/texttospeech/snippets/list_voices.py b/texttospeech/snippets/list_voices.py new file mode 100644 index 000000000000..ddfe099d4c1f --- /dev/null +++ b/texttospeech/snippets/list_voices.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +"""Google Cloud Text-To-Speech API sample application. + +Example usage: + python list_voices.py +""" + + +# [START tts_list_voices] +def list_voices(): + """Lists the available voices.""" + from google.cloud import texttospeech + + 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(f"Name: {voice.name}") + + # Display the supported language codes for this voice. Example: "en-US" + for language_code in voice.language_codes: + print(f"Supported language: {language_code}") + + ssml_gender = texttospeech.SsmlVoiceGender(voice.ssml_gender) + + # Display the SSML Voice Gender + print(f"SSML Voice Gender: {ssml_gender.name}") + + # Display the natural sample rate hertz for this voice. Example: 24000 + print(f"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\n") + + +# [END tts_list_voices] + + +if __name__ == "__main__": + list_voices() diff --git a/texttospeech/snippets/list_voices_test.py b/texttospeech/snippets/list_voices_test.py new file mode 100644 index 000000000000..889df226b452 --- /dev/null +++ b/texttospeech/snippets/list_voices_test.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +import list_voices + + +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 diff --git a/texttospeech/snippets/quickstart.py b/texttospeech/snippets/quickstart.py new file mode 100644 index 000000000000..1e2f92ea0ca3 --- /dev/null +++ b/texttospeech/snippets/quickstart.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +"""Google Cloud Text-To-Speech API sample application . + +Example usage: + python quickstart.py +""" + + +def run_quickstart(): + # [START tts_quickstart] + """Synthesizes speech from the input string of text or ssml. + Make sure to be working in a virtual environment. + + Note: ssml must be well-formed according to: + https://www.w3.org/TR/speech-synthesis/ + """ + from google.cloud import texttospeech + + # Instantiates a client + client = texttospeech.TextToSpeechClient() + + # Set the text input to be synthesized + 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.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL + ) + + # Select the type of audio file you want returned + 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( + input=synthesis_input, voice=voice, audio_config=audio_config + ) + + # The response's audio_content is binary. + 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__": + run_quickstart() diff --git a/texttospeech/snippets/requirements-test.txt b/texttospeech/snippets/requirements-test.txt new file mode 100644 index 000000000000..49780e035690 --- /dev/null +++ b/texttospeech/snippets/requirements-test.txt @@ -0,0 +1 @@ +pytest==7.2.0 diff --git a/texttospeech/snippets/requirements.txt b/texttospeech/snippets/requirements.txt new file mode 100644 index 000000000000..b427a1ce2bd7 --- /dev/null +++ b/texttospeech/snippets/requirements.txt @@ -0,0 +1,2 @@ +future==0.18.2 +google-cloud-texttospeech==2.12.3 diff --git a/texttospeech/snippets/resources/example.ssml b/texttospeech/snippets/resources/example.ssml new file mode 100644 index 000000000000..1e20716f0dac --- /dev/null +++ b/texttospeech/snippets/resources/example.ssml @@ -0,0 +1,4 @@ +123 Street Ln, Small Town, IL 12345 USA +1 Jenny St & Number St, Tutone City, CA 86753 +1 Piazza del Fibonacci, 12358 Pisa, Italy + \ No newline at end of file diff --git a/texttospeech/snippets/resources/example.txt b/texttospeech/snippets/resources/example.txt new file mode 100644 index 000000000000..9cd7d74db361 --- /dev/null +++ b/texttospeech/snippets/resources/example.txt @@ -0,0 +1,3 @@ +123 Street Ln, Small Town, IL 12345 USA +1 Jenny St & Number St, Tutone City, CA 86753 +1 Piazza del Fibonacci, 12358 Pisa, Italy diff --git a/texttospeech/snippets/resources/hello.ssml b/texttospeech/snippets/resources/hello.ssml new file mode 100644 index 000000000000..cd347b71fe58 --- /dev/null +++ b/texttospeech/snippets/resources/hello.ssml @@ -0,0 +1 @@ +Hello there. \ No newline at end of file diff --git a/texttospeech/snippets/resources/hello.txt b/texttospeech/snippets/resources/hello.txt new file mode 100644 index 000000000000..cd773cd131fc --- /dev/null +++ b/texttospeech/snippets/resources/hello.txt @@ -0,0 +1 @@ +Hello there! \ No newline at end of file diff --git a/texttospeech/snippets/ssml_addresses.py b/texttospeech/snippets/ssml_addresses.py new file mode 100644 index 000000000000..817929f435f7 --- /dev/null +++ b/texttospeech/snippets/ssml_addresses.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + + +# [START tts_ssml_address_imports] +import html + +from google.cloud import texttospeech + +# [END tts_ssml_address_imports] + + +# [START tts_ssml_address_audio] +def ssml_to_audio(ssml_text, outfile): + # Generates SSML text from plaintext. + # + # Given a string of SSML text and an output file name, this function + # calls the Text-to-Speech API. The API returns a synthetic audio + # version of the text, formatted according to the SSML commands. This + # function saves the synthetic audio to the designated output file. + # + # Args: + # ssml_text: string of SSML text + # outfile: string name of file under which to save audio output + # + # Returns: + # nothing + + # Instantiates a client + client = texttospeech.TextToSpeechClient() + + # Sets the text input to be synthesized + 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.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.MALE + ) + + # Selects the type of audio file to return + 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( + input=synthesis_input, voice=voice, audio_config=audio_config + ) + + # Writes the synthetic audio to the output file. + with open(outfile, "wb") as out: + out.write(response.audio_content) + print("Audio content written to file " + outfile) + # [END tts_ssml_address_audio] + + +# [START tts_ssml_address_ssml] +def text_to_ssml(inputfile): + # Generates SSML text from plaintext. + # Given an input filename, this function converts the contents of the text + # file into a string of formatted SSML text. This function formats the SSML + # string so that, when synthesized, the synthetic audio will pause for two + # seconds between each line of the text file. This function also handles + # special text characters which might interfere with SSML commands. + # + # Args: + # inputfile: string name of plaintext file + # + # Returns: + # A string of SSML text based on plaintext input + + # Parses lines of input file + with open(inputfile, "r") as f: + raw_lines = f.read() + + # Replace special characters with HTML Ampersand Character Codes + # These Codes prevent the API from confusing text with + # SSML commands + # For example, '<' --> '<' and '&' --> '&' + + escaped_lines = html.escape(raw_lines) + + # Convert plaintext to SSML + # Wait two seconds between each address + 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" + ssml_text = text_to_ssml(plaintext) + ssml_to_audio(ssml_text, "resources/example.mp3") + # [END tts_ssml_address_test] + + +if __name__ == "__main__": + main() diff --git a/texttospeech/snippets/ssml_addresses_test.py b/texttospeech/snippets/ssml_addresses_test.py new file mode 100644 index 000000000000..54b984b51e0f --- /dev/null +++ b/texttospeech/snippets/ssml_addresses_test.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +import os + +from ssml_addresses import ssml_to_audio, text_to_ssml + + +def test_text_to_ssml(capsys): + + # Read expected SSML output from resources + with open("resources/example.ssml", "r") as f: + expected_ssml = f.read() + + # Assert plaintext converted to SSML + 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: + input_ssml = f.read() + + # Assert audio file generated + ssml_to_audio(input_ssml, "test_example.mp3") + out, err = capsys.readouterr() + + # Assert MP3 file created + assert os.path.isfile("test_example.mp3") + assert "Audio content written to file test_example.mp3" in out + + # Delete MP3 test file + os.remove("test_example.mp3") diff --git a/texttospeech/snippets/synthesize_file.py b/texttospeech/snippets/synthesize_file.py new file mode 100644 index 000000000000..d9bdcd40a7f8 --- /dev/null +++ b/texttospeech/snippets/synthesize_file.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +"""Google Cloud Text-To-Speech API sample application . + +Example usage: + python synthesize_file.py --text resources/hello.txt + python synthesize_file.py --ssml resources/hello.ssml +""" + +import argparse + + +# [START tts_synthesize_text_file] +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: + text = f.read() + 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.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.FEMALE + ) + + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) + + 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: + out.write(response.audio_content) + print('Audio content written to file "output.mp3"') + + +# [END tts_synthesize_text_file] + + +# [START tts_synthesize_ssml_file] +def synthesize_ssml_file(ssml_file): + """Synthesizes speech from the input file of ssml. + + Note: ssml must be well-formed according to: + https://www.w3.org/TR/speech-synthesis/ + """ + from google.cloud import texttospeech + + client = texttospeech.TextToSpeechClient() + + with open(ssml_file, "r") as f: + ssml = f.read() + 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.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.FEMALE + ) + + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) + + 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: + out.write(response.audio_content) + print('Audio content written to file "output.mp3"') + + +# [END tts_synthesize_ssml_file] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + 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.") + + args = parser.parse_args() + + if args.text: + synthesize_text_file(args.text) + else: + synthesize_ssml_file(args.ssml) diff --git a/texttospeech/snippets/synthesize_file_test.py b/texttospeech/snippets/synthesize_file_test.py new file mode 100644 index 000000000000..d28479571c93 --- /dev/null +++ b/texttospeech/snippets/synthesize_file_test.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +import os + +import synthesize_file + +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 statinfo.st_size > 0 + + +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 statinfo.st_size > 0 diff --git a/texttospeech/snippets/synthesize_text.py b/texttospeech/snippets/synthesize_text.py new file mode 100644 index 000000000000..331106656d34 --- /dev/null +++ b/texttospeech/snippets/synthesize_text.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +"""Google Cloud Text-To-Speech API sample application . + +Example usage: + python synthesize_text.py --text "hello" + python synthesize_text.py --ssml "Hello there." +""" + +import argparse + + +# [START tts_synthesize_text] +def synthesize_text(text): + """Synthesizes speech from the input string of text.""" + from google.cloud import texttospeech + + client = texttospeech.TextToSpeechClient() + + 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.VoiceSelectionParams( + language_code="en-US", + name="en-US-Standard-C", + ssml_gender=texttospeech.SsmlVoiceGender.FEMALE, + ) + + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) + + 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: + out.write(response.audio_content) + print('Audio content written to file "output.mp3"') + + +# [END tts_synthesize_text] + + +# [START tts_synthesize_ssml] +def synthesize_ssml(ssml): + """Synthesizes speech from the input string of ssml. + + Note: ssml must be well-formed according to: + https://www.w3.org/TR/speech-synthesis/ + + Example: Hello there. + """ + from google.cloud import texttospeech + + client = texttospeech.TextToSpeechClient() + + 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.VoiceSelectionParams( + language_code="en-US", + name="en-US-Standard-C", + ssml_gender=texttospeech.SsmlVoiceGender.FEMALE, + ) + + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) + + 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: + out.write(response.audio_content) + print('Audio content written to file "output.mp3"') + + +# [END tts_synthesize_ssml] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + 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." + ) + + args = parser.parse_args() + + if args.text: + synthesize_text(args.text) + else: + synthesize_ssml(args.ssml) diff --git a/texttospeech/snippets/synthesize_text_test.py b/texttospeech/snippets/synthesize_text_test.py new file mode 100644 index 000000000000..4af85f031146 --- /dev/null +++ b/texttospeech/snippets/synthesize_text_test.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# All Rights Reserved. + +import os + +import synthesize_text + +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 statinfo.st_size > 0 + + +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 statinfo.st_size > 0