1. 概览
Google Cloud Text-to-Speech API(Beta 版)允许开发者在其应用中将自然发音的合成人类语音添加为可播放音频。Text-to-Speech API 可将文本或语音合成标记语言 (SSML) 输入转换为音频数据,例如 MP3 或 LINEAR16(WAV 文件中使用的编码)。
在此 Codelab 中,您将重点通过 C# 使用 Text-to-Speech API。您将学习如何列出可用的语音,以及如何将文字合成音频。
学习内容
- 如何使用 Cloud Shell
- 如何启用 Text-to-Speech API
- 如何对 API 请求进行身份验证
- 如何安装适用于 C# 的 Google Cloud 客户端库
- 如何列出可用的语音
- 如何从文本合成音频
所需条件
调查问卷
您将如何使用本教程?
您如何评价自己使用 C# 的体验?
您如何评价自己在使用 Google Cloud Platform 服务方面的经验水平?
<ph type="x-smartling-placeholder">2. 设置和要求
自定进度的环境设置
- 登录 Google Cloud 控制台,然后创建一个新项目或重复使用现有项目。如果您还没有 Gmail 或 Google Workspace 账号,则必须创建一个。
- 项目名称是此项目参与者的显示名称。它是 Google API 尚未使用的字符串。您可以随时对其进行更新。
- 项目 ID 在所有 Google Cloud 项目中是唯一的,并且是不可变的(一经设置便无法更改)。Cloud 控制台会自动生成一个唯一字符串;通常情况下,您无需关注该字符串。在大多数 Codelab 中,您都需要引用项目 ID(通常用
PROJECT_ID
标识)。如果您不喜欢生成的 ID,可以再随机生成一个 ID。或者,您也可以尝试自己的项目 ID,看看是否可用。完成此步骤后便无法更改该 ID,并且此 ID 在项目期间会一直保留。 - 此外,还有第三个值,即部分 API 使用的项目编号,供您参考。如需详细了解所有这三个值,请参阅文档。
- 接下来,您需要在 Cloud 控制台中启用结算功能,以便使用 Cloud 资源/API。运行此 Codelab 应该不会产生太多的费用(如果有的话)。若要关闭资源以避免产生超出本教程范围的结算费用,您可以删除自己创建的资源或删除项目。Google Cloud 新用户符合参与 300 美元免费试用计划的条件。
启动 Cloud Shell
虽然可以通过笔记本电脑对 Google Cloud 进行远程操作,但在此 Codelab 中,您将使用 Google Cloud Shell,这是一个在云端运行的命令行环境。
激活 Cloud Shell
- 在 Cloud Console 中,点击激活 Cloud Shell
。
如果这是您第一次启动 Cloud Shell,系统会显示一个中间屏幕,说明它是什么。如果您看到中间屏幕,请点击继续。
预配和连接到 Cloud Shell 只需花几分钟时间。
这个虚拟机装有所需的所有开发工具。它提供了一个持久的 5 GB 主目录,并在 Google Cloud 中运行,大大增强了网络性能和身份验证功能。您在此 Codelab 中的大部分(即使不是全部)工作都可以通过浏览器完成。
在连接到 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
您必须先启用 Text-to-Speech API,然后才能开始使用该 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 Attribution 2.0 通用许可授权。