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

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

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

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

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


示例 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 Platform Cloud SDK 为项目设置的服务帐号的访问令牌。如需了解有关安装 Cloud SDK、使用服务帐号设置项目以及获取访问令牌的说明,请参阅快速入门

以下示例显示如何向 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

C#

/// <summary>
/// Creates an audio file from the text input, applying the specifed
/// device profile to the output.
/// </summary>
/// <param name="text">Text to synthesize into audio</param>
/// <param name="outputFile">Name of audio output file</param>
/// <param name="effectProfileId">Audio effect profile to apply</param>
/// <remarks>
/// Output file saved in project folder.
/// </remarks>
public static int SynthesizeTextWithAudioProfile(string text,
                                                 string outputFile,
                                                 string effectProfileId)
{
    var client = TextToSpeechClient.Create();
    var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
    {
        Input = new SynthesisInput
        {
            Text = text
        },
        // Note: voices can also be specified by name
        // Names of voices can be retrieved with client.ListVoices().
        Voice = new VoiceSelectionParams
        {
            LanguageCode = "en-US",
            SsmlGender = SsmlVoiceGender.Female
        },
        AudioConfig = new AudioConfig
        {
            AudioEncoding = AudioEncoding.Mp3,
            // Note: you can pass in multiple audio effects profiles.
            // They are applied in the same order as provided.
            EffectsProfileId = { effectProfileId }
        }
    });

    // The response's AudioContent is binary.
    using (Stream output = File.Create(outputFile))
    {
        response.AudioContent.WriteTo(output);
    }

    return 0;
}

Go


import (
	"fmt"
	"io"
	"io/ioutil"

	"context"

	texttospeech "cloud.google.com/go/texttospeech/apiv1"
	texttospeechpb "google.golang.org/genproto/googleapis/cloud/texttospeech/v1"
)

// 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: %v", err)
	}

	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: %v", err)
	}

	if err = ioutil.WriteFile(outputFile, resp.AudioContent, 0644); err != nil {
		return err
	}

	fmt.Fprintf(w, "Audio content written to file: %v\n", outputFile)

	return nil
}

Java

/**
 * 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


/**
 * 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

def synthesize_text_with_audio_profile(text, output, effects_profile_id):
    """Synthesizes speech from the input string of text."""
    from google.cloud import texttospeech

    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)