透過雙向串流合成語音

本文將逐步說明如何使用雙向串流合成音訊。

雙向串流可讓您同時傳送文字輸入內容及接收音訊資料。也就是說,您可以在傳送完整輸入文字前開始合成語音,這樣就能縮短延遲時間,並進行即時互動。語音助理和互動式遊戲會使用雙向串流,打造更具互動性的動態應用程式。

如要進一步瞭解 Text-to-Speech 的基本概念,請參閱「Text-to-Speech 基礎」。

事前準備

您必須先完成下列動作,才能向 Text-to-Speech API 傳送要求。詳情請參閱「事前準備」頁面。

透過雙向串流合成語音

安裝用戶端程式庫

Python

安裝程式庫前,請確認您已設定適當的 Python 開發環境

pip install --upgrade google-cloud-texttospeech

傳送文字串流並接收音訊串流

API 接受 StreamingSynthesizeRequest 類型的要求串流,其中包含 StreamingSynthesisInputStreamingSynthesizeConfig

傳送提供文字輸入內容的 StreamingSynthesizeRequest 串流 StreamingSynthesisInput 之前,請先傳送一個含有 StreamingSynthesizeConfigStreamingSynthesizeRequest

串流文字轉語音功能僅支援 Chirp 3:HD 語音

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

    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-Chirp3-HD-Charon",
            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
    )

    text_iterator = [
        "Hello there. ",
        "How are you ",
        "today? It's ",
        "such nice weather outside.",
    ]

    # Request generator. Consider using Gemini or another LLM with output streaming as a generator.
    def request_generator():
        yield config_request
        for text in text_iterator:
            yield texttospeech.StreamingSynthesizeRequest(
                input=texttospeech.StreamingSynthesisInput(text=text)
            )

    streaming_responses = client.streaming_synthesize(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 console 刪除專案。

後續步驟

  • 如要進一步瞭解 Cloud Text-to-Speech,請參閱基本概念
  • 查看可用於合成語音的可用語音清單。