製作長篇音訊

本文將逐步說明如何合成長篇音訊。長音訊合成功能會以非同步方式合成最多 100 萬個位元組的輸入內容。如要進一步瞭解 Text-to-Speech 的基本概念,請參閱「Text-to-Speech 基礎」。

事前準備

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

使用指令列從文字合成長篇音訊

如要將長篇文字轉換為音訊,請向 https://texttospeech.googleapis.com/v1beta1/projects/{$project_number}/locations/global:synthesizeLongAudio 端點發出 HTTP POST 要求。在 POST 指令的主體中,指定下列欄位。

voice:要合成的語音類型。

input.text:要合成的文字。

audioConfig:要建立的音訊類型。

output_gcs_uri:格式為「gs://bucket_name/file_name.wav」的 Google Cloud 輸出路徑。

parent:父項,格式為「projects/{YOUR_PROJECT_NUMBER}/locations/{YOUR_PROJECT_LOCATION}」。

輸入內容最多可包含 1 MB 的字元,確切限制可能因輸入內容而異。

  1. 在用於執行合成作業的專案底下,建立 Google Cloud 儲存空間 bucket。請確認用於執行合成作業的服務帳戶具備輸出 Google Cloud bucket 的讀取和寫入權限。

  2. 在指令列執行 REST 要求,使用 Text-to-Speech 從文字合成音訊。這個指令會使用 gcloud auth application-default print-access-token 指令,擷取要求的授權權杖。

    HTTP 方法和網址:

    POST https://texttospeech.googleapis.com/v1beta1/projects/12345/locations/global:synthesizeLongAudio

    JSON 要求主體:

    {
      "parent": "projects/12345/locations/global",
      "audio_config":{
          "audio_encoding":"LINEAR16"
      },
      "input":{
          "text":"hello"
      },
      "voice":{
          "language_code":"en-us",
          "name":"en-us-Standard-A"
      },
      "output_gcs_uri": "gs://bucket_name/file_name.wav"
    }
    

    如要傳送要求,請展開以下其中一個選項:

    您應該會收到如下的 JSON 回應:

    {
      "name": "23456",
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.texttospeech.v1beta1.SynthesizeLongAudioMetadata",
        "progressPercentage": 0,
        "startTime": "2022-12-20T00:46:56.296191037Z",
        "lastUpdateTime": "2022-12-20T00:46:56.296191037Z"
      },
      "done": false
    }
    

  3. REST 指令的 JSON 輸出內容包含 name 欄位中的長時間作業名稱。在指令列執行 REST 要求,查詢長時間執行的作業狀態。

    請確認執行 GET 作業的服務帳戶與用於合成的服務帳戶來自同一個專案。

    HTTP 方法和網址:

    GET https://texttospeech.googleapis.com/v1beta1/projects/12345/locations/global/operations/23456

    如要傳送要求,請展開以下其中一個選項:

    您應該會收到如下的 JSON 回應:

    {
      "name": "projects/12345/locations/global/operations/23456",
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.texttospeech.v1beta1.SynthesizeLongAudioMetadata",
        "progressPercentage": 100
      },
      "done": true
    }
    

  4. 如要查詢特定專案下執行的所有作業清單,請執行 REST 要求。

    請確認執行 LIST 作業的服務帳戶與用於合成的服務帳戶來自同一個專案。

    HTTP 方法和網址:

    GET https://texttospeech.googleapis.com/v1beta1/projects/12345/locations/global/operations

    如要傳送要求,請展開以下其中一個選項:

    您應該會收到如下的 JSON 回應:

    {
      "operations": [
        {
          "name": "12345",
          "done": false
        },
        {
          "name": "23456",
          "done": false
        }
      ],
      "nextPageToken": ""
    }
    

  5. 長時間執行的作業順利完成後,請在 output_gcs_uri 欄位中,於指定 bucket URI 尋找輸出音訊檔案。如果作業未順利完成,請使用 GET REST 指令查詢錯誤、修正錯誤,然後再次發出 RPC。

使用用戶端程式庫從文字合成長篇音訊

如要合成長音訊,請按照下列說明操作。

安裝用戶端程式庫

Python

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

pip install --upgrade google-cloud-texttospeech

建立音訊資料

您可以使用 Text-to-Speech 建立合成人類語音的長音訊檔。使用下列程式碼在 Google Cloud 值區中建立長音訊檔案。

Python

執行範例前,請確認已設定適當的 Python 開發環境

# Copyright 2023 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.

from google.cloud import texttospeech


def synthesize_long_audio(project_id: str, output_gcs_uri: str) -> None:
    """
    Synthesizes long input, writing the resulting audio to `output_gcs_uri`.

    Args:
        project_id: ID or number of the Google Cloud project you want to use.
        output_gcs_uri: Specifies a Cloud Storage URI for the synthesis results.
            Must be specified in the format:
            ``gs://bucket_name/object_name``, and the bucket must
            already exist.
    """

    client = texttospeech.TextToSpeechLongAudioSynthesizeClient()

    input = texttospeech.SynthesisInput(
        text="Test input. Replace this with any text you want to synthesize, up to 1 million bytes long!"
    )

    audio_config = texttospeech.AudioConfig(
        audio_encoding=texttospeech.AudioEncoding.LINEAR16
    )

    voice = texttospeech.VoiceSelectionParams(
        language_code="en-US", name="en-US-Standard-A"
    )

    parent = f"projects/{project_id}/locations/us-central1"

    request = texttospeech.SynthesizeLongAudioRequest(
        parent=parent,
        input=input,
        audio_config=audio_config,
        voice=voice,
        output_gcs_uri=output_gcs_uri,
    )

    operation = client.synthesize_long_audio(request=request)
    # Set a deadline for your LRO to finish. 300 seconds is reasonable, but can be adjusted depending on the length of the input.
    # If the operation times out, that likely means there was an error. In that case, inspect the error, and try again.
    result = operation.result(timeout=300)
    print(
        "\nFinished processing, check your GCS bucket to find your audio file! Printing what should be an empty result: ",
        result,
    )

清除所用資源

請透過Google Cloud console 刪除不需要的專案,以免產生不必要的 Google Cloud 費用。

後續步驟

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