diff --git a/samples/snippets/README.rst b/samples/snippets/README.rst index 463b006a..7b877be1 100644 --- a/samples/snippets/README.rst +++ b/samples/snippets/README.rst @@ -7,7 +7,7 @@ Google Cloud Text-to-Speech API Python Samples :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. +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. @@ -153,6 +153,39 @@ To run this sample: +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" + + 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 @@ -170,4 +203,4 @@ to `browse the source`_ and `report issues`_. https://github.com/GoogleCloudPlatform/google-cloud-python/issues -.. _Google Cloud SDK: https://cloud.google.com/sdk/ +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/samples/snippets/README.rst.in b/samples/snippets/README.rst.in index e0cee295..8be6ab46 100644 --- a/samples/snippets/README.rst.in +++ b/samples/snippets/README.rst.in @@ -22,5 +22,8 @@ samples: - 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/samples/snippets/audio_profile.py b/samples/snippets/audio_profile.py new file mode 100644 index 00000000..c616f710 --- /dev/null +++ b/samples/snippets/audio_profile.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +# Copyright 2018 Google LLC. 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 for audio profile. + +Example usage: + python audio_profile.py --text "hello" --effects_profile_id + "telephony-class-application" +""" + +import argparse + + +# [START tts_synthesize_text_with_audio_profile] +def synthesize_text_with_audio_profile(text, output, effects_profile_id): + """Synthesizes speech from the input string of text.""" + from google.cloud import texttospeech_v1beta1 as texttospeech + + client = texttospeech.TextToSpeechClient() + + 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') + + # 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]) + + response = client.synthesize_speech(input_text, voice, 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_with_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/samples/snippets/audio_profile_test.py b/samples/snippets/audio_profile_test.py new file mode 100644 index 00000000..43a30bf0 --- /dev/null +++ b/samples/snippets/audio_profile_test.py @@ -0,0 +1,34 @@ +# 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/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index 4fd188de..59730670 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1 +1 @@ -google-cloud-texttospeech==0.1.0 +google-cloud-texttospeech==0.2.0