生成多位說話者的對話

本頁說明如何使用 Text-to-Speech 建立多位說話者的對話。

你可以生成多位說話者的語音,打造對話。這項功能適用於訪談、互動式故事、電玩遊戲、電子學習平台和無障礙解決方案。

如果音訊中有多名說話者,系統支援以下聲音:

  • en-US-Studio-Multispeaker
    • 音箱:R
    • 音箱:S
    • 音箱:T
    • 音箱:U


範例。這個樣本是使用多個揚聲器生成的音訊。

如何使用多位講者標記的範例

以下範例說明如何使用多位講者標記。

Python

如要瞭解如何安裝及使用 Text-to-Speech 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Text-to-Speech Python API 參考說明文件

如要向 Text-to-Speech 服務驗證身分,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

"""Synthesizes speech for multiple speakers.
Make sure to be working in a virtual environment.
"""
from google.cloud import texttospeech_v1beta1 as texttospeech

# Instantiates a client
client = texttospeech.TextToSpeechClient()

multi_speaker_markup = texttospeech.MultiSpeakerMarkup(
    turns=[
        texttospeech.MultiSpeakerMarkup.Turn(
            text="I've heard that the Google Cloud multi-speaker audio generation sounds amazing!",
            speaker="R",
        ),
        texttospeech.MultiSpeakerMarkup.Turn(
            text="Oh? What's so good about it?", speaker="S"
        ),
        texttospeech.MultiSpeakerMarkup.Turn(text="Well..", speaker="R"),
        texttospeech.MultiSpeakerMarkup.Turn(text="Well what?", speaker="S"),
        texttospeech.MultiSpeakerMarkup.Turn(
            text="Well, you should find it out by yourself!", speaker="R"
        ),
        texttospeech.MultiSpeakerMarkup.Turn(
            text="Alright alright, let's try it out!", speaker="S"
        ),
    ]
)

# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(
    multi_speaker_markup=multi_speaker_markup
)

# Build the voice request, select the language code ('en-US') and the voice
voice = texttospeech.VoiceSelectionParams(
    language_code="en-US", name="en-US-Studio-MultiSpeaker"
)

# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.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(
    input=synthesis_input, voice=voice, audio_config=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"')