diff --git a/texttospeech/ssml_addresses/README.rst b/texttospeech/ssml_addresses/README.rst new file mode 100644 index 00000000000..b009f236156 --- /dev/null +++ b/texttospeech/ssml_addresses/README.rst @@ -0,0 +1,97 @@ +.. This file is automatically generated. Do not edit this file directly. + +Google Cloud Text-to-Speech API 'Speaking Addresses with SSML' Tutorial 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 'Speaking Addresses with SSML' Tutorial. Google Cloud Text-To-Speech API 'Speaking Addresses with SSML Tutorial'_ enables you to use Speech Synthesis Markup Language (SSML) to speak a text file of addresses. You can embed SSML commands in a string of text to personalize synthetic audio from Cloud Text-to-Speech API. + + + + +.. _Google Cloud Text-to-Speech API 'Speaking Addresses with SSML' Tutorial: https://cloud.google.com/text-to-speech/docs/ssml-tutorial + +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 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 +------------------------------------------------------------------------------- + +Speaking addresses with SSML Tutorial ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. 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=/tts.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python tts.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/ssml_addresses/README.rst.in b/texttospeech/ssml_addresses/README.rst.in new file mode 100644 index 00000000000..e8e55fec3f5 --- /dev/null +++ b/texttospeech/ssml_addresses/README.rst.in @@ -0,0 +1,19 @@ + +# This file is used to generate README.rst + +product: + name: Google Cloud Text-to-Speech API 'Speaking Addresses with SSML' Tutorial + short_name: Cloud TTS API SSML Addresses Tutorial + url: https://cloud.google.com/text-to-speech/docs/ssml-tutorial + description: > + Google Cloud Text-To-Speech API 'Speaking Addresses with SSML Tutorial'_ enables you to use Speech Synthesis Markup Language (SSML) to speak a text file of addresses. You can embed SSML commands in a string of text to personalize synthetic audio from Cloud Text-to-Speech API. + +setup: +- auth +- install_deps + +samples: +- name: Speaking addresses with SSML Tutorial + file: tts.py + +cloud_client_library: true \ No newline at end of file diff --git a/texttospeech/ssml_addresses/requirements.txt b/texttospeech/ssml_addresses/requirements.txt new file mode 100644 index 00000000000..9b2ccbe9d2a --- /dev/null +++ b/texttospeech/ssml_addresses/requirements.txt @@ -0,0 +1 @@ +google-cloud-texttospeech==0.4.0 \ No newline at end of file diff --git a/texttospeech/ssml_addresses/resources/example.ssml b/texttospeech/ssml_addresses/resources/example.ssml new file mode 100644 index 00000000000..1e20716f0da --- /dev/null +++ b/texttospeech/ssml_addresses/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/ssml_addresses/resources/example.txt b/texttospeech/ssml_addresses/resources/example.txt new file mode 100644 index 00000000000..9cd7d74db36 --- /dev/null +++ b/texttospeech/ssml_addresses/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/ssml_addresses/resources/expected_example.mp3 b/texttospeech/ssml_addresses/resources/expected_example.mp3 new file mode 100644 index 00000000000..407b85f7f5d Binary files /dev/null and b/texttospeech/ssml_addresses/resources/expected_example.mp3 differ diff --git a/texttospeech/ssml_addresses/tts.py b/texttospeech/ssml_addresses/tts.py new file mode 100644 index 00000000000..0482b8f0b7e --- /dev/null +++ b/texttospeech/ssml_addresses/tts.py @@ -0,0 +1,116 @@ +# Copyright 2019 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. + + +# [START tts_ssml_address_imports] +from google.cloud import texttospeech + +# For Python 3, instead use: +# import html +import cgi +# [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.types.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) + + # Selects the type of audio file to return + audio_config = texttospeech.types.AudioConfig( + audio_encoding=texttospeech.enums.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) + + # 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 '&' --> '&' + + # For Python 3, instead use: + # escaped_lines = html.escape(raw_lines) + escaped_lines = cgi.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/ssml_addresses/tts_test.py b/texttospeech/ssml_addresses/tts_test.py new file mode 100644 index 00000000000..992200d144f --- /dev/null +++ b/texttospeech/ssml_addresses/tts_test.py @@ -0,0 +1,54 @@ +# Copyright 2019 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. + +from tts import text_to_ssml +from tts import ssml_to_audio + +import filecmp +import os + + +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') + assert os.path.isfile('test_example.mp3') + + # Assert audio file generated correctly + assert filecmp.cmp('test_example.mp3', + 'resources/expected_example.mp3', + shallow=True) + out, err = capsys.readouterr() + + # Delete test file + os.remove("test_example.mp3") + + # Assert success message printed + assert "Audio content written to file test_example.mp3" in out