使用双向数据流合成语音

本文档将引导您完成使用双向数据流合成音频的过程。

双向流式传输可让您同时发送文本输入和接收音频数据。这意味着,您可以在发送完整输入文本之前开始合成语音,从而缩短延迟时间并实现实时互动。语音助理和互动式游戏使用双向流式传输来打造更具动态性和响应速度更快的应用。

如需详细了解 Text-to-Speech 中的基本概念,请阅读 Text-to-Speech 基础知识

准备工作

您必须先完成以下操作,然后才能向 Text-to-Speech API 发送请求。如需了解详情,请参阅准备工作页面。

使用双向数据流合成语音

安装客户端库

Python

在安装库之前,请确保已经为 Python 开发准备好环境

pip install --upgrade google-cloud-texttospeech

发送文本流并接收音频流

该 API 接受类型为 StreamingSynthesizeRequest 的请求流,其中包含 StreamingSynthesisInputStreamingSynthesizeConfig

在使用 StreamingSynthesisInput(提供文本输入)发送流 StreamingSynthesizeRequest 之前,请使用 StreamingSynthesizeConfig 发送恰好一个 StreamingSynthesizeRequest

流式 Text-to-Speech 仅与 Journey 语音兼容。

Python

在运行该示例之前,请确保已经为 Python 开发准备好环境

#!/usr/bin/env python
# Copyright 2024 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.
#

"""Google Cloud Text-To-Speech API streaming sample application .

Example usage:
    python streaming_tts_quickstart.py
"""


def run_streaming_tts_quickstart():
    """Synthesizes speech from a stream of input text.
    """
    from google.cloud import texttospeech
    import itertools

    client = texttospeech.TextToSpeechClient()

    # See https://cloud.google.com/text-to-speech/docs/voices for all voices.
    streaming_config = texttospeech.StreamingSynthesizeConfig(voice=texttospeech.VoiceSelectionParams(name="en-US-Journey-D", language_code="en-US"))

    # Set the config for your stream. The first request must contain your config, and then each subsequent request must contain text.
    config_request = texttospeech.StreamingSynthesizeRequest(streaming_config=streaming_config)

    # Request generator. Consider using Gemini or another LLM with output streaming as a generator.
    def request_generator():
        yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="Hello there. "))
        yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="How are you "))
        yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="today? It's "))
        yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="such nice weather outside."))

    streaming_responses = client.streaming_synthesize(itertools.chain([config_request], request_generator()))
    for response in streaming_responses:
        print(f"Audio content size in bytes is: {len(response.audio_content)}")


if __name__ == "__main__":
    run_streaming_tts_quickstart()

清理

为避免产生不必要的 Google Cloud Platform 费用,请使用 Google Cloud 控制台删除您不需要的项目。

后续步骤