|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 Google LLC. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Google Cloud Text-To-Speech API sample application for audio profile. |
| 18 | +
|
| 19 | +Example usage: |
| 20 | + python audio_profile.py --text "hello" --effects_profile_id |
| 21 | + "telephony-class-application" |
| 22 | +""" |
| 23 | + |
| 24 | +import argparse |
| 25 | + |
| 26 | + |
| 27 | +# [START tts_synthesize_text_with_audio_profile] |
| 28 | +def synthesize_text_with_audio_profile(text, output, effects_profile_id): |
| 29 | + """Synthesizes speech from the input string of text.""" |
| 30 | + from google.cloud import texttospeech_v1beta1 as texttospeech |
| 31 | + |
| 32 | + client = texttospeech.TextToSpeechClient() |
| 33 | + |
| 34 | + input_text = texttospeech.types.SynthesisInput(text=text) |
| 35 | + |
| 36 | + # Note: the voice can also be specified by name. |
| 37 | + # Names of voices can be retrieved with client.list_voices(). |
| 38 | + voice = texttospeech.types.VoiceSelectionParams(language_code='en-US') |
| 39 | + |
| 40 | + # Note: you can pass in multiple effects_profile_id. They will be applied |
| 41 | + # in the same order they are provided. |
| 42 | + audio_config = texttospeech.types.AudioConfig( |
| 43 | + audio_encoding=texttospeech.enums.AudioEncoding.MP3, |
| 44 | + effects_profile_id=[effects_profile_id]) |
| 45 | + |
| 46 | + response = client.synthesize_speech(input_text, voice, audio_config) |
| 47 | + |
| 48 | + # The response's audio_content is binary. |
| 49 | + with open(output, 'wb') as out: |
| 50 | + out.write(response.audio_content) |
| 51 | + print('Audio content written to file "%s"' % output) |
| 52 | + |
| 53 | +# [END tts_synthesize_text_with_audio_profile] |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + parser = argparse.ArgumentParser( |
| 58 | + description=__doc__, |
| 59 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 60 | + parser.add_argument('--output', |
| 61 | + help='The output mp3 file.') |
| 62 | + parser.add_argument('--text', |
| 63 | + help='The text from which to synthesize speech.') |
| 64 | + parser.add_argument('--effects_profile_id', |
| 65 | + help='The audio effects profile id to be applied.') |
| 66 | + |
| 67 | + args = parser.parse_args() |
| 68 | + |
| 69 | + synthesize_text_with_audio_profile(args.text, args.output, |
| 70 | + args.effects_profile_id) |
0 commit comments