You need to enable JavaScript to run this app. 如何在Google Text-to-Speech代码中将pitch调整为-1.20?
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Text-to-Speech代码中将pitch调整为-1.20?

核心配置规则

pitch参数是音频配置类AudioConfig的内置入参,不属于语音身份选择的参数集合,不需要改动原有代码的其他逻辑,仅需在初始化音频配置对象时追加对应参数值即可。

有效取值校验

  • 你设置的pitch=-1.20完全落在服务端规定的[-20.0, 20.0]有效取值区间内,不会触发参数校验报错
  • 配置时直接传入浮点类型数值即可,不需要额外做单位换算、格式转换

代码修改位置

原有代码仅需调整audio_config的初始化逻辑,在原有参数后追加pitch字段:

audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.AudioEncoding.LINEAR16,
    pitch=-1.20
)

调整后的完整可运行代码

from google.cloud import texttospeech

def text_to_wav(voice_name, text):
    language_code = "-".join(voice_name.split("-")[:2])
    text_input = texttospeech.SynthesisInput(text=text)
    voice_params = texttospeech.VoiceSelectionParams(
        language_code=language_code, name=voice_name)

    audio_config = texttospeech.AudioConfig(
        audio_encoding=texttospeech.AudioEncoding.LINEAR16,
        pitch=-1.20
    )

    client = texttospeech.TextToSpeechClient()
    response = client.synthesize_speech(
        input=text_input, voice=voice_params, audio_config=audio_config)

    filename = f"{language_code}.wav"
    with open(filename, "wb") as out:
        out.write(response.audio_content)
        print(f'Audio content written to "{filename}"')

说明:后续如果需要调整语音语速、输出音量等音频效果类参数,均可在同一个AudioConfig初始化节点追加对应入参,不需要修改其他模块的代码逻辑。

内容的提问来源于stack exchange,提问作者daquezada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.31 22:57:08