透過雙向串流合成語音
本文將逐步說明如何使用雙向串流合成音訊。
雙向串流可讓您同時傳送文字輸入內容及接收音訊資料。也就是說,您可以在傳送完整輸入文字前開始合成語音,這樣就能縮短延遲時間,並進行即時互動。語音助理和互動式遊戲會使用雙向串流,打造更具互動性的動態應用程式。
如要進一步瞭解 Text-to-Speech 的基本概念,請參閱「Text-to-Speech 基礎」。
事前準備
您必須先完成下列動作,才能向 Text-to-Speech API 傳送要求。詳情請參閱「事前準備」頁面。
- 在 Google Cloud 專案中啟用 Text-to-Speech。
- 確認已啟用 Text-to-Speech 的計費功能。
-
After installing the Google Cloud CLI, sign in to the gcloud CLI with your federated identity and then initialize it by running the following command:
gcloud init
透過雙向串流合成語音
安裝用戶端程式庫
Python
安裝程式庫前,請確認您已設定適當的 Python 開發環境。
pip install --upgrade google-cloud-texttospeech
傳送文字串流並接收音訊串流
API 接受 StreamingSynthesizeRequest
類型的要求串流,其中包含 StreamingSynthesisInput
或 StreamingSynthesizeConfig
。
傳送提供文字輸入內容的 StreamingSynthesizeRequest
串流 StreamingSynthesisInput
之前,請先傳送一個含有 StreamingSynthesizeConfig
的 StreamingSynthesizeRequest
。
串流文字轉語音功能僅支援 Chirp 3:HD 語音。
Python
執行範例前,請確認已設定適當的 Python 開發環境。
清除所用資源
如要避免產生不必要的 Google Cloud Platform 費用,請在不需要時使用Google Cloud console 刪除專案。
後續步驟