python - 如何在 Google Text to Speech API 中调整发音音高
问题描述
我使用了 Google Text2Speech API,效果很好,但我想调整音高。我使用了 gTTS。
tts = gTTS("ご返信ありがとうございます。", lang = 'ja')
我该怎么走?提前致谢!
解决方案
查看官方文档,text2speech API 有一个AudioConfig
功能,您可以在其中传递音调。音高可以在范围内改变[-20.0, 20.0]
。这是一个工作示例。
from google.cloud import texttospeech
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
pitch=-1.20,
audio_encoding=texttospeech.AudioEncoding.MP3
)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# The response's audio_content is binary.
with open("output.mp3", "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
推荐阅读
- bash - 排除查找中的目录数组/列表
- javascript - 仅允许文本框中仅包含特定字母的数字
- python - 为什么熊猫数据框消耗的 RAM 比原始文本文件的大小要多得多?
- spark-streaming - Spark Streaming 中是否允许使用 Sleep 语句
- python - 如何根据列值在现有数据框中添加新行?
- playframework - Swagger-play2 - 隐藏内部参数
- oracle - 使用 If 循环触发显示编译错误
- reactjs - 如何在反应功能组件中声明默认道具
- javascript - 将 Authorization Header Basic 与获取和禁止浏览器密码提示一起使用
- java - 如何修复“表已使用元数据存在”camunda 流程引擎异常?