如何从 Google Cloud 文本转语音 API 获取 SSML 时间戳

2023-12-27

我想用SSML标记 https://www.w3.org/TR/2009/REC-speech-synthesis-20090303/#edef_mark通过 Google Cloud 文本转语音 API 来请求音频流中这些标记的计时。为了向用户提供效果、单词/部分突出显示和反馈的提示,这些时间戳是必要的。

I found 这个问题 https://stackoverflow.com/questions/55320826/google-cloud-text-to-speech-word-timestamps这是相关的,尽管问题指的是每个单词的时间戳而不是 SSML<mark> tag.

以下 API 请求返回 OK,但显示缺少请求的标记数据。这是使用Cloud Text-to-Speech API v1.

{
 "voice": {
  "languageCode": "en-US"
 },
 "input": {
  "ssml": "<speak>First, <mark name=\"a\"/> second, <mark name=\"b\"/> third.</speak>"
 },
 "audioConfig": {
  "audioEncoding": "mp3"
 }
} 

回复:

{
 "audioContent":"//NExAAAAANIAAAAABcFAThYGJqMWA..."
}

它仅提供合成音频,没有任何上下文信息。

是否有一个我忽略的 API 请求可以公开有关这些标记的信息,例如IBM沃森 https://cloud.ibm.com/docs/services/text-to-speech?topic=text-to-speech-timing and 亚马逊波莉 https://docs.aws.amazon.com/polly/latest/dg/using-speechmarks.html?


在撰写本文时,时间点数据可在v1beta1谷歌云文本转语音功能发布。

除了默认访问之外,我不需要登录任何额外的开发人员计划即可访问测试版。

Python 中的导入(例如)来自:

from google.cloud import texttospeech as tts

to:

from google.cloud import texttospeech_v1beta1 as tts

又好又简单。

我需要修改发送综合请求的默认方式以包括enable_time_pointing flag.

我发现通过混合探索机器可读的API描述在这里 https://texttospeech.googleapis.com/%24discovery/rest?version=v1beta1并阅读我已经下载的Python库代码。

值得庆幸的是,通用版本中的源代码还包括v1beta版本 - 谢谢谷歌!

我在下面放置了一个可运行的示例。运行此程序需要与一般文本转语音示例相同的身份验证和设置,您可以通过遵循官方文档来获取该示例。

这就是它对我的作用(为了可读性而进行了轻微的格式化):

$ 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>
    """)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Google Cloud 文本转语音 API 获取 SSML 时间戳 的相关文章

随机推荐