From 2dc2a395a56a935d136b2ed2f70b8c50ef4b49cd Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 21 Mar 2018 15:14:20 -0700 Subject: [PATCH 1/9] Add text-to-speech beta samples --- texttospeech/cloud-client/README.rst | 139 ++++++++++++++++++ texttospeech/cloud-client/README.rst.in | 24 +++ texttospeech/cloud-client/list_voices.py | 55 +++++++ texttospeech/cloud-client/list_voices_test.py | 23 +++ texttospeech/cloud-client/quickstart.py | 61 ++++++++ texttospeech/cloud-client/requirements.txt | 1 + .../cloud-client/resources/hello.ssml | 7 + texttospeech/cloud-client/resources/hello.txt | 1 + texttospeech/cloud-client/synthesize_file.py | 102 +++++++++++++ .../cloud-client/synthesize_file_test.py | 31 ++++ texttospeech/cloud-client/synthesize_text.py | 106 +++++++++++++ .../cloud-client/synthesize_text_test.py | 38 +++++ 12 files changed, 588 insertions(+) create mode 100644 texttospeech/cloud-client/README.rst create mode 100644 texttospeech/cloud-client/README.rst.in create mode 100644 texttospeech/cloud-client/list_voices.py create mode 100644 texttospeech/cloud-client/list_voices_test.py create mode 100644 texttospeech/cloud-client/quickstart.py create mode 100644 texttospeech/cloud-client/requirements.txt create mode 100644 texttospeech/cloud-client/resources/hello.ssml create mode 100644 texttospeech/cloud-client/resources/hello.txt create mode 100644 texttospeech/cloud-client/synthesize_file.py create mode 100644 texttospeech/cloud-client/synthesize_file_test.py create mode 100644 texttospeech/cloud-client/synthesize_text.py create mode 100644 texttospeech/cloud-client/synthesize_text_test.py diff --git a/texttospeech/cloud-client/README.rst b/texttospeech/cloud-client/README.rst new file mode 100644 index 000000000000..700a788ae951 --- /dev/null +++ b/texttospeech/cloud-client/README.rst @@ -0,0 +1,139 @@ +.. 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 +++++++++++++++++++++ + +#. 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 2.7 and 3.4+. + + .. 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 + + +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 + + + + +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/ \ No newline at end of file diff --git a/texttospeech/cloud-client/README.rst.in b/texttospeech/cloud-client/README.rst.in new file mode 100644 index 000000000000..7a450b1683a3 --- /dev/null +++ b/texttospeech/cloud-client/README.rst.in @@ -0,0 +1,24 @@ +# 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 +- name: Synthesize file + file: synthesize_file.py + +cloud_client_library: true diff --git a/texttospeech/cloud-client/list_voices.py b/texttospeech/cloud-client/list_voices.py new file mode 100644 index 000000000000..e9175c409d0a --- /dev/null +++ b/texttospeech/cloud-client/list_voices.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# 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. + +"""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('Name: {}'.format(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)) + + # Names of SSML voice genders from google.cloud.texttospeech.enums + ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE', 'FEMALE', + 'NEUTRAL'] + + # Display the supported SSML - gender for this voice. Example: FEMALE + print('SSML gender: {}'.format(ssml_voice_genders[voice.ssml_gender])) + + # Display the natural sample rate hertz for this voice. Example: 24000 + print('Natural Sample Rate Hertz: {}\n'.format( + voice.natural_sample_rate_hertz)) +# [END tts_list_voices] + + +if __name__ == '__main__': + list_voices() diff --git a/texttospeech/cloud-client/list_voices_test.py b/texttospeech/cloud-client/list_voices_test.py new file mode 100644 index 000000000000..ccacdbd7d4a6 --- /dev/null +++ b/texttospeech/cloud-client/list_voices_test.py @@ -0,0 +1,23 @@ +# Copyright 2018, Google, Inc. +# 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 list_voices + + +def test_list_voices(capsys): + list_voices.list_voices() + out, err = capsys.readouterr() + + assert 'en-US' in out + assert 'SSML gender: MALE' in out + assert 'SSML gender: FEMALE' in out diff --git a/texttospeech/cloud-client/quickstart.py b/texttospeech/cloud-client/quickstart.py new file mode 100644 index 000000000000..e734a4b69955 --- /dev/null +++ b/texttospeech/cloud-client/quickstart.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# 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. + +"""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. + + 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.types.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='NEUTRAL') + + # Select the type of audio file you want returned + audio_config = texttospeech.types.AudioConfig( + audio_encoding=texttospeech.enums.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) + + # 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() \ No newline at end of file diff --git a/texttospeech/cloud-client/requirements.txt b/texttospeech/cloud-client/requirements.txt new file mode 100644 index 000000000000..4fd188de7402 --- /dev/null +++ b/texttospeech/cloud-client/requirements.txt @@ -0,0 +1 @@ +google-cloud-texttospeech==0.1.0 diff --git a/texttospeech/cloud-client/resources/hello.ssml b/texttospeech/cloud-client/resources/hello.ssml new file mode 100644 index 000000000000..a610237dd4bb --- /dev/null +++ b/texttospeech/cloud-client/resources/hello.ssml @@ -0,0 +1,7 @@ + + + Hello there. + \ No newline at end of file diff --git a/texttospeech/cloud-client/resources/hello.txt b/texttospeech/cloud-client/resources/hello.txt new file mode 100644 index 000000000000..cd773cd131fc --- /dev/null +++ b/texttospeech/cloud-client/resources/hello.txt @@ -0,0 +1 @@ +Hello there! \ No newline at end of file diff --git a/texttospeech/cloud-client/synthesize_file.py b/texttospeech/cloud-client/synthesize_file.py new file mode 100644 index 000000000000..7c63232c8b84 --- /dev/null +++ b/texttospeech/cloud-client/synthesize_file.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# 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. + +"""Google Cloud Text-To-Speech API sample application . + +Example usage: + python synthesize_file.py --text_file resources/hello.txt + python synthesize_file.py --ssml_file 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.types.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) + + audio_config = texttospeech.types.AudioConfig( + audio_encoding=texttospeech.enums.AudioEncoding.MP3) + + response = client.synthesize_speech(input_text, voice, 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.types.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) + + audio_config = texttospeech.types.AudioConfig( + audio_encoding=texttospeech.enums.AudioEncoding.MP3) + + response = client.synthesize_speech(input_text, voice, 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/cloud-client/synthesize_file_test.py b/texttospeech/cloud-client/synthesize_file_test.py new file mode 100644 index 000000000000..1bc03ce9e4af --- /dev/null +++ b/texttospeech/cloud-client/synthesize_file_test.py @@ -0,0 +1,31 @@ +# Copyright 2018, Google, Inc. +# 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 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 + + +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 diff --git a/texttospeech/cloud-client/synthesize_text.py b/texttospeech/cloud-client/synthesize_text.py new file mode 100644 index 000000000000..b45800cfee05 --- /dev/null +++ b/texttospeech/cloud-client/synthesize_text.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# 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. + +"""Google Cloud Text-To-Speech API sample application . + +Example usage: + python synthesize_text.py --text "hello" --output hello.mp3 + python synthesize_text.py --ssml " + + Hello there. + + """ + from google.cloud import texttospeech + client = texttospeech.TextToSpeechClient() + + input_text = texttospeech.types.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) + + audio_config = texttospeech.types.AudioConfig( + audio_encoding=texttospeech.enums.AudioEncoding.MP3) + + response = client.synthesize_speech(input_text, voice, 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/cloud-client/synthesize_text_test.py b/texttospeech/cloud-client/synthesize_text_test.py new file mode 100644 index 000000000000..4e45b7dfe464 --- /dev/null +++ b/texttospeech/cloud-client/synthesize_text_test.py @@ -0,0 +1,38 @@ +# Copyright 2018, Google, Inc. +# 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 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 + + +def test_synthesize_ssml(capsys): + synthesize_text.synthesize_ssml(ssml=SSML) + out, err = capsys.readouterr() + + assert 'Audio content written to file' in out From e1a5a0d37de2f828cd955015e8fb866bf3807cd4 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 21 Mar 2018 15:56:15 -0700 Subject: [PATCH 2/9] Style --- texttospeech/cloud-client/list_voices.py | 4 ++-- texttospeech/cloud-client/quickstart.py | 2 +- texttospeech/cloud-client/synthesize_file.py | 2 +- texttospeech/cloud-client/synthesize_text.py | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/texttospeech/cloud-client/list_voices.py b/texttospeech/cloud-client/list_voices.py index e9175c409d0a..2f3b9910e046 100644 --- a/texttospeech/cloud-client/list_voices.py +++ b/texttospeech/cloud-client/list_voices.py @@ -39,8 +39,8 @@ def list_voices(): print('Supported language: {}'.format(language_code)) # Names of SSML voice genders from google.cloud.texttospeech.enums - ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE', 'FEMALE', - 'NEUTRAL'] + ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE', + 'FEMALE', 'NEUTRAL'] # Display the supported SSML - gender for this voice. Example: FEMALE print('SSML gender: {}'.format(ssml_voice_genders[voice.ssml_gender])) diff --git a/texttospeech/cloud-client/quickstart.py b/texttospeech/cloud-client/quickstart.py index e734a4b69955..b5384faa4e57 100644 --- a/texttospeech/cloud-client/quickstart.py +++ b/texttospeech/cloud-client/quickstart.py @@ -58,4 +58,4 @@ def run_quickstart(): if __name__ == '__main__': - run_quickstart() \ No newline at end of file + run_quickstart() diff --git a/texttospeech/cloud-client/synthesize_file.py b/texttospeech/cloud-client/synthesize_file.py index 7c63232c8b84..f5ea00f65a5b 100644 --- a/texttospeech/cloud-client/synthesize_file.py +++ b/texttospeech/cloud-client/synthesize_file.py @@ -23,6 +23,7 @@ import argparse + # [START tts_synthesize_text_file] def synthesize_text_file(text_file): """Synthesizes speech from the input file of text.""" @@ -99,4 +100,3 @@ def synthesize_ssml_file(ssml_file): synthesize_text_file(args.text) else: synthesize_ssml_file(args.ssml) - diff --git a/texttospeech/cloud-client/synthesize_text.py b/texttospeech/cloud-client/synthesize_text.py index b45800cfee05..a45775dd4013 100644 --- a/texttospeech/cloud-client/synthesize_text.py +++ b/texttospeech/cloud-client/synthesize_text.py @@ -23,6 +23,7 @@ import argparse + # [START tts_synthesize_text] def synthesize_text(text): """Synthesizes speech from the input string of text.""" From bfc4357838394ce9582e6772a250ba86cbc813b7 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 21 Mar 2018 16:25:03 -0700 Subject: [PATCH 3/9] Add Text-To-Speech api to prepare testing script --- scripts/prepare-testing-project.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/prepare-testing-project.sh b/scripts/prepare-testing-project.sh index 9a6243dc0a98..482011e0087e 100755 --- a/scripts/prepare-testing-project.sh +++ b/scripts/prepare-testing-project.sh @@ -50,4 +50,4 @@ echo "Creating speech resources." gsutil cp speech/api-client/resources/audio.raw gs://$GCLOUD_PROJECT/speech/ echo "To finish setup, follow this link to enable APIs." -echo "https://console.cloud.google.com/flows/enableapi?project=${GCLOUD_PROJECT}&apiid=bigtable.googleapis.com,bigtableadmin.googleapis.com,bigquery,bigquerydatatransfer.googleapis.com,cloudmonitoring,compute_component,datastore,datastore.googleapis.com,dataproc,dns,plus,pubsub,logging,storage_api,vision.googleapis.com" +echo "https://console.cloud.google.com/flows/enableapi?project=${GCLOUD_PROJECT}&apiid=bigtable.googleapis.com,bigtableadmin.googleapis.com,bigquery,bigquerydatatransfer.googleapis.com,cloudmonitoring,compute_component,datastore,datastore.googleapis.com,dataproc,dns,plus,pubsub,logging,storage_api,texttospeech.googleapis.com,vision.googleapis.com" From 622289d0d2e755841362a8417349e72b6045f558 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 22 Mar 2018 13:10:36 -0700 Subject: [PATCH 4/9] Update README --- texttospeech/cloud-client/README.rst | 28 +++++++++++++++++++++++++ texttospeech/cloud-client/README.rst.in | 2 ++ 2 files changed, 30 insertions(+) diff --git a/texttospeech/cloud-client/README.rst b/texttospeech/cloud-client/README.rst index 700a788ae951..e9c975d800c7 100644 --- a/texttospeech/cloud-client/README.rst +++ b/texttospeech/cloud-client/README.rst @@ -102,6 +102,20 @@ To run this sample: $ 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" --output hello.mp3 + python synthesize_text.py --ssml " Date: Fri, 23 Mar 2018 09:46:41 -0700 Subject: [PATCH 5/9] Update README and files with simplified ssml command --- texttospeech/cloud-client/README.rst | 8 ++++---- texttospeech/cloud-client/synthesize_file.py | 4 ++-- texttospeech/cloud-client/synthesize_text.py | 13 +++---------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/texttospeech/cloud-client/README.rst b/texttospeech/cloud-client/README.rst index e9c975d800c7..a917784c2c97 100644 --- a/texttospeech/cloud-client/README.rst +++ b/texttospeech/cloud-client/README.rst @@ -107,8 +107,8 @@ To run this sample: Google Cloud Text-To-Speech API sample application . Example usage: - python synthesize_text.py --text "hello" --output hello.mp3 - python synthesize_text.py --ssml "Hello there." optional arguments: -h, --help show this help message and exit @@ -137,8 +137,8 @@ To run this sample: Google Cloud Text-To-Speech API sample application . Example usage: - python synthesize_file.py --text_file resources/hello.txt - python synthesize_file.py --ssml_file resources/hello.ssml + 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 diff --git a/texttospeech/cloud-client/synthesize_file.py b/texttospeech/cloud-client/synthesize_file.py index f5ea00f65a5b..f62d6330d7d5 100644 --- a/texttospeech/cloud-client/synthesize_file.py +++ b/texttospeech/cloud-client/synthesize_file.py @@ -17,8 +17,8 @@ """Google Cloud Text-To-Speech API sample application . Example usage: - python synthesize_file.py --text_file resources/hello.txt - python synthesize_file.py --ssml_file resources/hello.ssml + python synthesize_file.py --text resources/hello.txt + python synthesize_file.py --ssml resources/hello.ssml """ import argparse diff --git a/texttospeech/cloud-client/synthesize_text.py b/texttospeech/cloud-client/synthesize_text.py index a45775dd4013..d5886bd16f15 100644 --- a/texttospeech/cloud-client/synthesize_text.py +++ b/texttospeech/cloud-client/synthesize_text.py @@ -17,8 +17,8 @@ """Google Cloud Text-To-Speech API sample application . Example usage: - python synthesize_text.py --text "hello" --output hello.mp3 - python synthesize_text.py --ssml "Hello there." """ import argparse @@ -57,14 +57,7 @@ def synthesize_ssml(ssml): Note: ssml must be well-formed according to: https://www.w3.org/TR/speech-synthesis/ - Example: - - - Hello there. - + Example: Hello there. """ from google.cloud import texttospeech client = texttospeech.TextToSpeechClient() From 7a3a4ac0a18191dc014fb546d4e573042243cb8d Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 23 Mar 2018 14:02:06 -0700 Subject: [PATCH 6/9] Clarification --- texttospeech/cloud-client/list_voices.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/texttospeech/cloud-client/list_voices.py b/texttospeech/cloud-client/list_voices.py index 2f3b9910e046..3f43499c6742 100644 --- a/texttospeech/cloud-client/list_voices.py +++ b/texttospeech/cloud-client/list_voices.py @@ -38,12 +38,13 @@ def list_voices(): for language_code in voice.language_codes: print('Supported language: {}'.format(language_code)) - # Names of SSML voice genders from google.cloud.texttospeech.enums + # SSML Voice Gender values from google.cloud.texttospeech.enums ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE', 'FEMALE', 'NEUTRAL'] - # Display the supported SSML - gender for this voice. Example: FEMALE - print('SSML gender: {}'.format(ssml_voice_genders[voice.ssml_gender])) + # Display the SSML Voice Gender + print('SSML Voice Gender: {}'.format( + ssml_voice_genders[voice.ssml_gender])) # Display the natural sample rate hertz for this voice. Example: 24000 print('Natural Sample Rate Hertz: {}\n'.format( From c09349ffc346c1411467c47eb21d1cb7b7c8708b Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 23 Mar 2018 15:58:35 -0700 Subject: [PATCH 7/9] Simplify ssml --- texttospeech/cloud-client/list_voices_test.py | 4 ++-- texttospeech/cloud-client/resources/hello.ssml | 8 +------- texttospeech/cloud-client/synthesize_text_test.py | 9 +-------- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/texttospeech/cloud-client/list_voices_test.py b/texttospeech/cloud-client/list_voices_test.py index ccacdbd7d4a6..fd3255694922 100644 --- a/texttospeech/cloud-client/list_voices_test.py +++ b/texttospeech/cloud-client/list_voices_test.py @@ -19,5 +19,5 @@ def test_list_voices(capsys): out, err = capsys.readouterr() assert 'en-US' in out - assert 'SSML gender: MALE' in out - assert 'SSML gender: FEMALE' in out + assert 'SSML Voice Gender: MALE' in out + assert 'SSML Voice Gender: FEMALE' in out diff --git a/texttospeech/cloud-client/resources/hello.ssml b/texttospeech/cloud-client/resources/hello.ssml index a610237dd4bb..cd347b71fe58 100644 --- a/texttospeech/cloud-client/resources/hello.ssml +++ b/texttospeech/cloud-client/resources/hello.ssml @@ -1,7 +1 @@ - - - Hello there. - \ No newline at end of file +Hello there. \ No newline at end of file diff --git a/texttospeech/cloud-client/synthesize_text_test.py b/texttospeech/cloud-client/synthesize_text_test.py index 4e45b7dfe464..0f4275947475 100644 --- a/texttospeech/cloud-client/synthesize_text_test.py +++ b/texttospeech/cloud-client/synthesize_text_test.py @@ -14,14 +14,7 @@ import synthesize_text TEXT = 'Hello there.' -SSML = """ - - Hello there. - -""" +SSML = 'Hello there.' def test_synthesize_text(capsys): From 386228fa7f62ea08f137862bb270a3b433ca56f5 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Mon, 26 Mar 2018 14:11:41 -0700 Subject: [PATCH 8/9] Use enum and check output file size --- texttospeech/cloud-client/quickstart.py | 5 +++-- texttospeech/cloud-client/synthesize_file_test.py | 5 +++++ texttospeech/cloud-client/synthesize_text_test.py | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/texttospeech/cloud-client/quickstart.py b/texttospeech/cloud-client/quickstart.py index b5384faa4e57..f462139d794b 100644 --- a/texttospeech/cloud-client/quickstart.py +++ b/texttospeech/cloud-client/quickstart.py @@ -38,8 +38,9 @@ def run_quickstart(): # 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='NEUTRAL') + voice = texttospeech.types.VoiceSelectionParams( + language_code='en-US', + ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL) # Select the type of audio file you want returned audio_config = texttospeech.types.AudioConfig( diff --git a/texttospeech/cloud-client/synthesize_file_test.py b/texttospeech/cloud-client/synthesize_file_test.py index 1bc03ce9e4af..a436126ff450 100644 --- a/texttospeech/cloud-client/synthesize_file_test.py +++ b/texttospeech/cloud-client/synthesize_file_test.py @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import synthesize_file TEXT_FILE = 'resources/hello.txt' @@ -22,6 +23,8 @@ def test_synthesize_text_file(capsys): 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): @@ -29,3 +32,5 @@ def test_synthesize_ssml_file(capsys): 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/cloud-client/synthesize_text_test.py b/texttospeech/cloud-client/synthesize_text_test.py index 0f4275947475..7dc79f4c69a9 100644 --- a/texttospeech/cloud-client/synthesize_text_test.py +++ b/texttospeech/cloud-client/synthesize_text_test.py @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import synthesize_text TEXT = 'Hello there.' @@ -22,6 +23,8 @@ def test_synthesize_text(capsys): 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): @@ -29,3 +32,5 @@ def test_synthesize_ssml(capsys): out, err = capsys.readouterr() assert 'Audio content written to file' in out + statinfo = os.stat('output.mp3') + assert statinfo.st_size > 0 From ca09053021aa9949f8ff7c78b55b11436885c38c Mon Sep 17 00:00:00 2001 From: nnegrey Date: Mon, 26 Mar 2018 14:40:15 -0700 Subject: [PATCH 9/9] Add new line --- texttospeech/cloud-client/synthesize_file_test.py | 1 + texttospeech/cloud-client/synthesize_text_test.py | 1 + 2 files changed, 2 insertions(+) diff --git a/texttospeech/cloud-client/synthesize_file_test.py b/texttospeech/cloud-client/synthesize_file_test.py index a436126ff450..2652009f98b6 100644 --- a/texttospeech/cloud-client/synthesize_file_test.py +++ b/texttospeech/cloud-client/synthesize_file_test.py @@ -12,6 +12,7 @@ # limitations under the License. import os + import synthesize_file TEXT_FILE = 'resources/hello.txt' diff --git a/texttospeech/cloud-client/synthesize_text_test.py b/texttospeech/cloud-client/synthesize_text_test.py index 7dc79f4c69a9..948d58da26df 100644 --- a/texttospeech/cloud-client/synthesize_text_test.py +++ b/texttospeech/cloud-client/synthesize_text_test.py @@ -12,6 +12,7 @@ # limitations under the License. import os + import synthesize_text TEXT = 'Hello there.'