python - How to get phonemes from Google Cloud API Text-to-Speech
问题描述
I am following the Google Cloud API Text-to-Speech Python tutorial. I would like to know if there is a way to return the phonemes and their duration, an intermediate step in generating the interpreted speech. Is that possible? If so, can you please refer me to the documentation and hopefully some sample code that does this. I searched and could not find anything that already answered my question.
Thanks! gma
解决方案
提到从谷歌云 API Text-to-Speech 获取音素的所有步骤。在第 3 部分中,您可以找到示例代码。以下是您可以遵循的步骤:
[第1部分]
在 Google Cloud Console 的项目选择器页面上,选择或创建一个 Google Cloud 项目。
确保为您的 Cloud 项目启用了结算功能
启用 Cloud Text-to-Speech API。
创建服务帐户:在 GCP Console 中,转到创建服务帐号页面。湾。选择一个项目。C。在服务帐户名称字段中,输入名称。Cloud Console 会根据此名称填写 Service account ID 字段。d。单击完成以完成创建服务帐户。不要关闭浏览器窗口。您将在下一步中使用它。
创建服务帐户密钥:在 GCP Console 中,点击您创建的服务帐号的电子邮件地址。湾。单击键。C。单击添加密钥,然后单击创建新密钥。d。单击创建。JSON 密钥文件已下载到您的计算机。e. 单击关闭。
将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为包含您的服务帐户密钥的 JSON 文件的路径。此变量仅适用于您当前的 shell 会话,因此如果您打开一个新会话,请再次设置该变量。
示例 1. Linux 或 macOS
export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
将 KEY_PATH 替换为包含您的服务帐户密钥的 JSON 文件的路径。
例如:-
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
示例 2.窗口
对于 powershell:
$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
将 KEY_PATH 替换为包含您的服务帐户密钥的 JSON 文件的路径。
例如:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
对于命令提示符:
set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH
将 KEY_PATH 替换为包含您的服务帐户密钥的 JSON 文件的路径。
安装并初始化云 SDK。
[第2部分]
安装客户端库
pip install --upgrade google-cloud-texttospeech
[第三部分]
创建音频数据
现在您可以使用 Text-to-Speech 创建合成人类语音的音频文件。使用以下代码向 Text-to-Speech API 发送合成请求。
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
with open("output.mp3", "wb") as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
如果您遇到任何问题,请参考以下链接:
推荐阅读
- java - 为什么在 ng 构建后出现 404?
- angular - “ServiceWorkerRegistration”类型上不存在属性“periodicSync”
- java - Eclipse 调试:在断点上几分钟后跳转到代码
- cloud - 将 LB 私有 IP 与主机名 /FQDN 相关联
- apache-spark - Spark mapPartitions 问题
- javascript - db.collection 不是 React / Firestore 的函数
- vue.js - Vue2 Child Prop 不会随着来自父母的 Watch 更新
- flutter - 如何在 Flutter 中创建正弦波限幅器?
- android - Android Studio 无法在我的设备上调试
- sql-server - sql server sp_send_dbmail @query 在选择中带有变量