Google Cloud Text-to-Speech Node.js 客户端技术文档

Google Cloud Text-to-Speech Node.js 客户端技术文档

1. 安装指南

1.1 准备工作

在开始安装之前,请确保您已完成以下步骤:

  1. 选择或创建一个云平台项目:您需要有一个Google Cloud项目。如果没有,请先创建一个。
  2. 启用计费:确保您的项目已启用计费功能。
  3. 启用Google Cloud Text-to-Speech API:在Google Cloud控制台中启用Text-to-Speech API。
  4. 设置身份验证:使用服务帐户设置身份验证,以便从本地工作站访问API。

1.2 安装客户端库

使用以下命令安装Google Cloud Text-to-Speech Node.js客户端库:

npm install @google-cloud/text-to-speech

2. 项目使用说明

2.1 快速开始

以下是一个简单的示例,展示如何使用Google Cloud Text-to-Speech API将文本转换为语音:

// 导入Google Cloud客户端库
const textToSpeech = require('@google-cloud/text-to-speech');

// 导入其他必需的库
const fs = require('fs');
const util = require('util');

// 创建客户端
const client = new textToSpeech.TextToSpeechClient();

async function quickStart() {
  // 要合成的文本
  const text = 'hello, world!';

  // 构建请求
  const request = {
    input: {text: text},
    // 选择语言和SSML语音性别(可选)
    voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
    // 选择音频编码类型
    audioConfig: {audioEncoding: 'MP3'},
  };

  // 执行文本到语音的请求
  const [response] = await client.synthesizeSpeech(request);
  // 将二进制音频内容写入本地文件
  const writeFile = util.promisify(fs.writeFile);
  await writeFile('output.mp3', response.audioContent, 'binary');
  console.log('音频内容已写入文件: output.mp3');
}

quickStart();

2.2 示例代码

项目中提供了多个示例代码,您可以在samples/目录下找到这些示例。每个示例的README.md文件中都有运行该示例的说明。

3. 项目API使用文档

3.1 API概述

Google Cloud Text-to-Speech API允许您将文本转换为自然语音。API支持多种语言和语音类型,并提供多种音频编码格式。

3.2 主要API方法

  • synthesizeSpeech:将文本转换为语音并返回音频内容。
  • listVoices:列出可用的语音列表。

3.3 请求参数

  • input:包含要合成的文本。
  • voice:指定语音的语言代码和性别。
  • audioConfig:指定音频编码格式。

3.4 响应参数

  • audioContent:包含合成的音频内容的二进制数据。

4. 项目安装方式

4.1 使用npm安装

使用以下命令安装Google Cloud Text-to-Speech Node.js客户端库:

npm install @google-cloud/text-to-speech

4.2 手动安装

如果您需要手动安装,可以从GitHub仓库下载源代码并手动构建。

git clone https://github.com/googleapis/nodejs-text-to-speech.git
cd nodejs-text-to-speech
npm install

通过以上步骤,您可以成功安装并使用Google Cloud Text-to-Speech Node.js客户端库。

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值