在撰写本文时,时间点数据可在v1beta1
Google 云文本转语音版本中获得。
除了默认访问权限之外,我无需登录任何额外的开发人员计划即可访问测试版。
在 Python 中导入(例如)来自:
from google.cloud import texttospeech as tts
至:
from google.cloud import texttospeech_v1beta1 as tts
很好很简单。
我需要修改发送综合请求的默认方式以包含该enable_time_pointing
标志。
我发现这里混合了机器可读的 API 描述和阅读我已经下载的 Python 库代码。
值得庆幸的是,普遍可用版本中的源代码也包含该v1beta
版本 - 感谢 Google!
我在下面放了一个可运行的示例。运行它需要与通用文本转语音示例相同的身份验证和设置,您可以通过遵循官方文档获得。
这就是它对我的作用(为了便于阅读,略微格式化):
$ python tools/try-marks.py
Marks content written to file: .../demo.json
Audio content written to file: .../demo.mp3
$ cat demo.json
[
{"sec": 0.4300000071525574, "name": "here"},
{"sec": 0.9234582781791687, "name": "there"}
]
这是示例:
import json
from pathlib import Path
from google.cloud import texttospeech_v1beta1 as tts
def go_ssml(basename: Path, ssml):
client = tts.TextToSpeechClient()
voice = tts.VoiceSelectionParams(
language_code="en-AU",
name="en-AU-Wavenet-B",
ssml_gender=tts.SsmlVoiceGender.MALE,
)
response = client.synthesize_speech(
request=tts.SynthesizeSpeechRequest(
input=tts.SynthesisInput(ssml=ssml),
voice=voice,
audio_config=tts.AudioConfig(audio_encoding=tts.AudioEncoding.MP3),
enable_time_pointing=[
tts.SynthesizeSpeechRequest.TimepointType.SSML_MARK]
)
)
# cheesy conversion of array of Timepoint proto.Message objects into plain-old data
marks = [dict(sec=t.time_seconds, name=t.mark_name)
for t in response.timepoints]
name = basename.with_suffix('.json')
with name.open('w') as out:
json.dump(marks, out)
print(f'Marks content written to file: {name}')
name = basename.with_suffix('.mp3')
with name.open('wb') as out:
out.write(response.audio_content)
print(f'Audio content written to file: {name}')
go_ssml(Path.cwd() / 'demo', """
<speak>
Go from <mark name="here"/> here, to <mark name="there"/> there!
</speak>
""")