生成包含多个发言人的对话

本页面介绍了如何创建包含多个发言人且由 Text-to-Speech 创建的对话。

您可以使用多个发言人生成音频,以创建对话。这对于访谈、互动式故事讲述、视频游戏、在线学习平台和无障碍解决方案非常有用。

以下语音支持多发言人音频:

  • en-US-Studio-Multispeaker
    • 发言人:R
    • 发言人:S
    • 发言人:T
    • 发言人:U


示例。此示例是使用多个发言人生成的音频。

示例:如何使用多发言人标记

此示例演示了如何使用多发言人标记。

Python

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 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"')