将设备配置文件用于生成的音频

本页面介绍了如何为使用 Text-to-Speech 创建的音频选择设备配置文件。

您可以优化 Text-to-Speech 生成的合成语音,以便在各类硬件上播放。例如,如果您的应用主要在较小的“穿戴式”设备上运行,您可以通过专门针对小型扬声器进行优化的 Text-to-Speech 创建合成语音。

您还可以将多个设备配置文件应用到同一合成语音。Text-to-Speech API 会按照向 text:synthesize 端点发出的请求中提供的顺序,依次将设备配置文件应用到此音频。一个配置文件只能指定一次,因为多次应用相同的配置文件可能会产生不良结果。

使用音频配置文件是可选操作。如果您选择使用一个(或多个)音频配置文件,则 Text-to-Speech 会将配置文件应用于合成后的语音结果。如果您选择不使用音频配置文件,则会收到不涉及任何合成后修改的语音结果。

如果您想听听不同配置文件生成的音频之间的差异,请比较下面的两个剪辑。


示例 1:使用 handset-class-device 配置文件生成的音频


示例 2:使用 telephony-class-application 配置文件生成的音频

注意:通过调整音频效果的范围,每个音频配置文件都已针对特定设备进行了优化。但是,用于调整配置文件的设备的品牌和型号可能与用户的播放设备不完全匹配。您可能需要尝试不同的配置文件,以便为您的应用找到最佳的声音输出。

可用的音频配置文件

下表提供可供 Text-to-Speech API 使用的设备配置文件的 ID 和示例。

音频配置文件 ID 优化目标
wearable-class-device 智能手表和其他可穿戴设备,例如 Apple Watch、Wear OS 手表
handset-class-device 智能手机,如 Google Pixel、Samsung Galaxy、Apple iPhone
headphone-class-device 用于音频播放的耳塞或耳机,如 Sennheiser 耳机
small-bluetooth-speaker-class-device 小型家庭扬声器,例如 Google Home Mini
medium-bluetooth-speaker-class-device 智能家居扬声器,例如 Google Home
large-home-entertainment-class-device 家庭娱乐系统或智能电视,例如 Google Home Max、LG TV
large-automotive-class-device 车载扬声器
telephony-class-application 交互式语音响应 (IVR) 系统

指定要使用的音频配置文件

如果要指定将要使用的音频配置文件,请为语音合成请求设置 effectsProfileId 字段。

协议

要生成音频文件,请发出 POST 请求并提供相应的请求正文。以下示例展示了一个使用 curl 发出的 POST 请求。该示例使用 Google Cloud CLI 来检索请求的访问令牌。如需了解如何安装 gcloud CLI,请参阅向 Text-to-Speech 进行身份验证

以下示例显示如何向 text:synthesize 端点发送请求。

curl \   -H "Authorization: Bearer "$(gcloud auth print-access-token) \   -H "Content-Type: application/json; charset=utf-8" \   --data "{     'input':{       'text':'This is a sentence that helps test how audio profiles can change the way Cloud Text-to-Speech sounds.'     },     'voice':{       'languageCode':'en-us',     },     'audioConfig':{       'audioEncoding':'LINEAR16',       'effectsProfileId': ['telephony-class-application']     }   }" "https://texttospeech.googleapis.com/v1beta1/text:synthesize" > audio-profile.txt

如果请求成功,Text-to-Speech API 将返回包含在 JSON 输出中的 base64 编码数据格式的合成音频。audio-profiles.txt 文件中的 JSON 输出如下所示:

{   "audioContent": "//NExAASCCIIAAhEAGAAEMW4kAYPnwwIKw/BBTpwTvB+IAxIfghUfW.." }

要将 Cloud Text-to-Speech API 的合成结果解码为 MP3 音频文件,请从 audio-profiles.txt 文件所在的目录运行以下命令。

 sed 's|audioContent| |' < audio-profile.txt > tmp-output.txt && \ tr -d '\n ":{}' < tmp-output.txt > tmp-output-2.txt && \ base64 tmp-output-2.txt --decode > audio-profile.wav && \ rm tmp-output*.txt 

Go

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 Text-to-Speech 客户端库。 如需了解详情,请参阅 Text-to-Speech Go API 参考文档

如需向 Text-to-Speech 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证

 import ( 	"fmt" 	"io" 	"os"  	"context"  	texttospeech "cloud.google.com/go/texttospeech/apiv1" 	"cloud.google.com/go/texttospeech/apiv1/texttospeechpb" )  // audioProfile generates audio from text using a custom synthesizer like a telephone call. func audioProfile(w io.Writer, text string, outputFile string) error { 	// text := "hello" 	// outputFile := "out.mp3"  	ctx := context.Background()  	client, err := texttospeech.NewClient(ctx) 	if err != nil { 		return fmt.Errorf("NewClient: %w", err) 	} 	defer client.Close()  	req := &texttospeechpb.SynthesizeSpeechRequest{ 		Input: &texttospeechpb.SynthesisInput{ 			InputSource: &texttospeechpb.SynthesisInput_Text{Text: text}, 		}, 		Voice: &texttospeechpb.VoiceSelectionParams{LanguageCode: "en-US"}, 		AudioConfig: &texttospeechpb.AudioConfig{ 			AudioEncoding:    texttospeechpb.AudioEncoding_MP3, 			EffectsProfileId: []string{"telephony-class-application"}, 		}, 	}  	resp, err := client.SynthesizeSpeech(ctx, req) 	if err != nil { 		return fmt.Errorf("SynthesizeSpeech: %w", err) 	}  	if err = os.WriteFile(outputFile, resp.AudioContent, 0644); err != nil { 		return err 	}  	fmt.Fprintf(w, "Audio content written to file: %v\n", outputFile)  	return nil } 

Java

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 Text-to-Speech 客户端库。 如需了解详情,请参阅 Text-to-Speech Java API 参考文档

如需向 Text-to-Speech 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证

/**  * Demonstrates using the Text to Speech client with audio profiles to synthesize text or ssml  *  * @param text the raw text to be synthesized. (e.g., "Hello there!")  * @param effectsProfile audio profile to be used for synthesis. (e.g.,  *     "telephony-class-application")  * @throws Exception on TextToSpeechClient Errors.  */ public static void synthesizeTextWithAudioProfile(String text, String effectsProfile)     throws Exception {   // Instantiates a client   try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {     // Set the text input to be synthesized     SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();      // Build the voice request     VoiceSelectionParams voice =         VoiceSelectionParams.newBuilder()             .setLanguageCode("en-US") // languageCode = "en_us"             .setSsmlGender(SsmlVoiceGender.FEMALE) // ssmlVoiceGender = SsmlVoiceGender.FEMALE             .build();      // Select the type of audio file you want returned and the audio profile     AudioConfig audioConfig =         AudioConfig.newBuilder()             .setAudioEncoding(AudioEncoding.MP3) // MP3 audio.             .addEffectsProfileId(effectsProfile) // audio profile             .build();      // Perform the text-to-speech request     SynthesizeSpeechResponse response =         textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);      // Get the audio contents from the response     ByteString audioContents = response.getAudioContent();      // Write the response to the output file.     try (OutputStream out = new FileOutputStream("output.mp3")) {       out.write(audioContents.toByteArray());       System.out.println("Audio content written to file \"output.mp3\"");     }   } }

Node.js

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 Text-to-Speech 客户端库。 如需了解详情,请参阅 Text-to-Speech Node.js API 参考文档

如需向 Text-to-Speech 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证

 /**  * TODO(developer): Uncomment these variables before running the sample.  */ // const text = 'Text you want to vocalize'; // const outputFile = 'YOUR_OUTPUT_FILE_LOCAtION; // const languageCode = 'LANGUAGE_CODE_FOR_OUTPUT'; // const ssmlGender = 'SSML_GENDER_OF_SPEAKER';  // Imports the Google Cloud client library const speech = require('@google-cloud/text-to-speech'); const fs = require('fs'); const util = require('util');  // Creates a client const client = new speech.TextToSpeechClient();  async function synthesizeWithEffectsProfile() {   // Add one or more effects profiles to array.   // Refer to documentation for more details:   // https://cloud.google.com/text-to-speech/docs/audio-profiles   const effectsProfileId = ['telephony-class-application'];    const request = {     input: {text: text},     voice: {languageCode: languageCode, ssmlGender: ssmlGender},     audioConfig: {audioEncoding: 'MP3', effectsProfileId: effectsProfileId},   };    const [response] = await client.synthesizeSpeech(request);   const writeFile = util.promisify(fs.writeFile);   await writeFile(outputFile, response.audioContent, 'binary');   console.log(`Audio content written to file: ${outputFile}`); }

Python

如需了解如何安装和使用 Text-to-Speech 客户端库,请参阅 Text-to-Speech 客户端库。 如需了解详情,请参阅 Text-to-Speech Python API 参考文档

如需向 Text-to-Speech 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证

def synthesize_text_with_audio_profile():     """Synthesizes speech from the input string of text."""     from google.cloud import texttospeech      text = "hello"     output = "output.mp3"     effects_profile_id = "telephony-class-application"     client = texttospeech.TextToSpeechClient()      input_text = texttospeech.SynthesisInput(text=text)      # Note: the voice can also be specified by name.     # Names of voices can be retrieved with client.list_voices().     voice = texttospeech.VoiceSelectionParams(language_code="en-US")      # Note: you can pass in multiple effects_profile_id. They will be applied     # in the same order they are provided.     audio_config = texttospeech.AudioConfig(         audio_encoding=texttospeech.AudioEncoding.MP3,         effects_profile_id=[effects_profile_id],     )      response = client.synthesize_speech(         input=input_text, voice=voice, audio_config=audio_config     )      # The response's audio_content is binary.     with open(output, "wb") as out:         out.write(response.audio_content)         print('Audio content written to file "%s"' % output)  

其他语言

C#:请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 的 TText-to-Speech 参考文档

PHP:请按照客户端库页面上的 PHP 设置说明 操作,然后访问 PHP 的 Text-to-Speech 参考文档

Ruby:请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 的 Text-to-Speech 参考文档