1. 總覽
開發人員可透過 Google Cloud Text-to-Speech API (Beta 版),在應用程式中加入自然的合成人類語音,並轉成可播放的音訊。Text-to-Speech API 會將文字或語音合成標記語言 (SSML) 輸入內容轉換成音訊資料,例如 MP3 或 LINEAR16 (WAV 檔案使用的編碼)。
在本程式碼研究室中,您將著重於使用 C# 搭配 Text-to-Speech API。您將瞭解如何列出可用語音,以及如何從文字合成音訊。
課程內容
- 如何使用 Cloud Shell
- 如何啟用 Text-to-Speech API
- 如何驗證 API 要求
- 如何安裝 C# 適用的 Google Cloud 用戶端程式庫
- 如何列出可用語音
- 如何從文字合成音訊
軟硬體需求
問卷調查
您會如何使用本教學課程?
您對 C# 的體驗評價如何?
您對使用 Google Cloud Platform 服務的體驗有何評價?
2. 設定和需求
自修實驗室環境設定
- 登入 Google Cloud 控制台,然後建立新專案或重複使用現有專案。如果沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶。



- 專案名稱是這個專案參與者的顯示名稱。這是 Google API 未使用的字元字串。你隨時可以更新。
- 專案 ID 在所有 Google Cloud 專案中都是不重複的,而且設定後即無法變更。Cloud 控制台會自動產生專屬字串,通常您不需要在意該字串為何。在大多數程式碼研究室中,您需要參照專案 ID (通常標示為
PROJECT_ID)。如果您不喜歡產生的 ID,可以產生另一個隨機 ID。你也可以嘗試使用自己的名稱,看看是否可用。完成這個步驟後就無法變更,且專案期間會維持不變。 - 請注意,有些 API 會使用第三個值,也就是「專案編號」。如要進一步瞭解這三種值,請參閱說明文件。
- 接著,您需要在 Cloud 控制台中啟用帳單,才能使用 Cloud 資源/API。完成這個程式碼研究室的費用不高,甚至可能完全免費。如要關閉資源,避免在本教學課程結束後繼續產生費用,請刪除您建立的資源或專案。Google Cloud 新使用者可參加價值$300 美元的免費試用計畫。
啟動 Cloud Shell
雖然可以透過筆電遠端操作 Google Cloud,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是可在雲端執行的指令列環境。
啟用 Cloud Shell
- 在 Cloud 控制台,點選「啟用 Cloud Shell」 圖示
。

如果您是首次啟動 Cloud Shell,系統會顯示中繼畫面,說明這個指令列環境。如果出現中繼畫面,請按一下「繼續」。

佈建並連至 Cloud Shell 預計只需要幾分鐘。

這部虛擬機器已載入所有必要的開發工具,並提供永久的 5 GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。本程式碼研究室幾乎所有工作都可在瀏覽器上完成。
連至 Cloud Shell 後,您應該會看到驗證已完成,專案也已設為獲派的專案 ID。
- 在 Cloud Shell 中執行下列指令,確認您已通過驗證:
gcloud auth list
指令輸出
Credentialed Accounts
ACTIVE ACCOUNT
* <my_account>@<my_domain.com>
To set the active account, run:
$ gcloud config set account `ACCOUNT`
- 在 Cloud Shell 中執行下列指令,確認 gcloud 指令知道您的專案:
gcloud config list project
指令輸出
[core] project = <PROJECT_ID>
如未設定,請輸入下列指令手動設定專案:
gcloud config set project <PROJECT_ID>
指令輸出
Updated property [core/project].
3. 啟用 Text-to-Speech API
您必須先啟用 API,才能開始使用 Text-to-Speech API。您可以在 Cloud Shell 中使用下列指令啟用 API:
gcloud services enable texttospeech.googleapis.com
4. 安裝 C# 適用的 Google Cloud Text-to-Speech API 用戶端程式庫
首先,請建立簡單的 C# 控制台應用程式,用來執行 Text-to-Speech API 範例:
dotnet new console -n TextToSpeechApiDemo
您應該會看到建立的應用程式和已解決的依附元件:
The template "Console Application" was created successfully.
Processing post-creation actions...
...
Restore succeeded.
接著前往 TextToSpeechApiDemo 資料夾:
cd TextToSpeechApiDemo/
並將 Google.Cloud.TextToSpeech.V1 NuGet 套件新增至專案:
dotnet add package Google.Cloud.TextToSpeech.V1
info : Adding PackageReference for package 'Google.Cloud.TextToSpeech.V1' into project '/home/atameldev/TextToSpeechDemo/TextToSpeechDemo.csproj'.
log : Restoring packages for /home/atameldev/TextToSpeechDemo/TextToSpeechDemo.csproj...
...
info : PackageReference for package 'Google.Cloud.TextToSpeech.V1' version '1.0.0-beta01' added to file '/home/atameldev/TextToSpeechDemo/TextToSpeechDemo.csproj'.
您現在可以使用 Text-to-Speech API 了!
5. 列出可用語音
在本節中,您會先列出所有可用的英文語音,用於音訊合成。
首先,從 Cloud Shell 右上方開啟程式碼編輯器:

前往 TextToSpeechApiDemo 資料夾中的 Program.cs 檔案,並將程式碼替換成以下程式碼:
using Google.Cloud.TextToSpeech.V1;
using System;
namespace TextToSpeechApiDemo
{
class Program
{
static void Main(string[] args)
{
var client = TextToSpeechClient.Create();
var response = client.ListVoices("en");
foreach (var voice in response.Voices)
{
Console.WriteLine($"{voice.Name} ({voice.SsmlGender}); Language codes: {string.Join(", ", voice.LanguageCodes)}");
}
}
}
}
請花一兩分鐘研究程式碼。回到 Cloud Shell,執行應用程式:
dotnet run
您應該會看到以下的輸出內容:
en-US-Wavenet-D (Male); Language codes: en-US
en-AU-Wavenet-A (Female); Language codes: en-AU
en-AU-Wavenet-B (Male); Language codes: en-AU
en-AU-Wavenet-C (Female); Language codes: en-AU
en-AU-Wavenet-D (Male); Language codes: en-AU
en-GB-Wavenet-A (Female); Language codes: en-GB
en-GB-Wavenet-B (Male); Language codes: en-GB
en-GB-Wavenet-C (Female); Language codes: en-GB
...
en-GB-Standard-A (Female); Language codes: en-GB
en-GB-Standard-B (Male); Language codes: en-GB
en-AU-Standard-D (Male); Language codes: en-AU
摘要
在這個步驟中,您已列出所有可用的英文語音,用於音訊合成。如需完整的可用語音清單,請參閱「支援的語音」頁面。
6. 從文字合成音訊
您可以使用 Text-to-Speech API 將字串轉換為音訊資料。您可以透過各種方式設定語音合成的輸出,包括選取不重複的語音或調節輸出的音調、音量、說話速度和取樣率。
如要從文字合成音訊檔案,請前往 TextToSpeechApiDemo 資料夾中的 Program.cs 檔案,並將程式碼替換成以下程式碼:
using Google.Cloud.TextToSpeech.V1;
using System;
using System.IO;
namespace TextToSpeechApiDemo
{
class Program
{
static void Main(string[] args)
{
var client = TextToSpeechClient.Create();
// The input to be synthesized, can be provided as text or SSML.
var input = new SynthesisInput
{
Text = "This is a demonstration of the Google Cloud Text-to-Speech API"
};
// Build the voice request.
var voiceSelection = new VoiceSelectionParams
{
LanguageCode = "en-US",
SsmlGender = SsmlVoiceGender.Female
};
// Specify the type of audio file.
var audioConfig = new AudioConfig
{
AudioEncoding = AudioEncoding.Mp3
};
// Perform the text-to-speech request.
var response = client.SynthesizeSpeech(input, voiceSelection, audioConfig);
// Write the response to the output file.
using (var output = File.Create("output.mp3"))
{
response.AudioContent.WriteTo(output);
}
Console.WriteLine("Audio content written to file \"output.mp3\"");
}
}
}
請花一兩分鐘研究程式碼,瞭解如何使用程式碼從文字建立音訊檔。
回到 Cloud Shell,執行應用程式:
dotnet run
您應該會看到以下的輸出內容:
Audio content written to file "output.mp3"
在程式碼編輯器中,您可以下載 MP3 檔案,並在本機電腦上播放。

摘要
在本步驟中,您已使用 Text-to-Speech API 將字串轉換為音訊 mp3 檔案。進一步瞭解如何建立語音音訊檔案。
7. 恭喜!
您已瞭解如何使用 C# 語言的 Text-to-Speech API,對音訊檔案執行不同類型的轉錄作業!
清除所用資源
如何避免系統向您的 Google Cloud Platform 帳戶收取您在本快速入門導覽課程中所用資源的相關費用:
- 前往 Cloud Platform Console。
- 選取要關閉的專案,然後按一下頂端的「刪除」,系統就會排定刪除專案的時間。
瞭解詳情
- Google Cloud Text-to-Speech API:https://cloud.google.com/text-to-speech/docs
- Google Cloud Platform 上的 C#/.NET:https://cloud.google.com/dotnet/
- Google Cloud .NET 用戶端:https://googlecloudplatform.github.io/google-cloud-dotnet/
授權
這項內容採用的授權為 Creative Commons 姓名標示 2.0 通用授權。