|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2014 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +"""Google Cloud Text-To-Speech API sample application . |
| 18 | +
|
| 19 | +Example usage: |
| 20 | + python synthesize_speech_multiple_speakers.py |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +def synthesize_speech_multiple_speakers(): |
| 25 | + # [START tts_synthesize_speech_multiple_speakers] |
| 26 | + """Synthesizes speech for multiple speakers. |
| 27 | + Make sure to be working in a virtual environment. |
| 28 | + """ |
| 29 | + from google.cloud import texttospeech_v1beta1 as texttospeech |
| 30 | + |
| 31 | + # Instantiates a client |
| 32 | + client = texttospeech.TextToSpeechClient() |
| 33 | + |
| 34 | + multi_speaker_markup = texttospeech.MultiSpeakerMarkup( |
| 35 | + turns=[ |
| 36 | + texttospeech.MultiSpeakerMarkup.Turn( |
| 37 | + text="I've heard that the Google Cloud multi-speaker audio generation sounds amazing!", |
| 38 | + speaker="R", |
| 39 | + ), |
| 40 | + texttospeech.MultiSpeakerMarkup.Turn( |
| 41 | + text="Oh? What's so good about it?", speaker="S" |
| 42 | + ), |
| 43 | + texttospeech.MultiSpeakerMarkup.Turn(text="Well..", speaker="R"), |
| 44 | + texttospeech.MultiSpeakerMarkup.Turn(text="Well what?", speaker="S"), |
| 45 | + texttospeech.MultiSpeakerMarkup.Turn( |
| 46 | + text="Well, you should find it out by yourself!", speaker="R" |
| 47 | + ), |
| 48 | + texttospeech.MultiSpeakerMarkup.Turn( |
| 49 | + text="Alright alright, let's try it out!", speaker="S" |
| 50 | + ), |
| 51 | + ] |
| 52 | + ) |
| 53 | + |
| 54 | + # Set the text input to be synthesized |
| 55 | + synthesis_input = texttospeech.SynthesisInput( |
| 56 | + multi_speaker_markup=multi_speaker_markup |
| 57 | + ) |
| 58 | + |
| 59 | + # Build the voice request, select the language code ('en-US') and the voice |
| 60 | + voice = texttospeech.VoiceSelectionParams( |
| 61 | + language_code="en-US", name="en-US-Studio-MultiSpeaker" |
| 62 | + ) |
| 63 | + |
| 64 | + # Select the type of audio file you want returned |
| 65 | + audio_config = texttospeech.AudioConfig( |
| 66 | + audio_encoding=texttospeech.AudioEncoding.MP3 |
| 67 | + ) |
| 68 | + |
| 69 | + # Perform the text-to-speech request on the text input with the selected |
| 70 | + # voice parameters and audio file type |
| 71 | + response = client.synthesize_speech( |
| 72 | + input=synthesis_input, voice=voice, audio_config=audio_config |
| 73 | + ) |
| 74 | + |
| 75 | + # The response's audio_content is binary. |
| 76 | + with open("output.mp3", "wb") as out: |
| 77 | + # Write the response to the output file. |
| 78 | + out.write(response.audio_content) |
| 79 | + print('Audio content written to file "output.mp3"') |
| 80 | + # [END tts_synthesize_speech_multiple_speakers] |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + synthesize_speech_multiple_speakers() |
0 commit comments