如何从 Google Cloud text-to-speech API 获取 SSML <mark> 时间戳

Jam*_*mes 14 markers ssml google-text-to-speech google-cloud-speech

我想通过 Google Cloud text-to-speech API使用SSML 标记来请求音频流中这些标记的时间。这些时间戳是必要的,以便为用户提供效果提示、单词/部分突出显示和反馈。

我发现这个问题是相关的,尽管这个问题是指每个单词的时间戳而不是 SSML<mark>标签。

以下 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"
 }
} 
Run Code Online (Sandbox Code Playgroud)

回复:

{
 "audioContent":"//NExAAAAANIAAAAABcFAThYGJqMWA..."
}
Run Code Online (Sandbox Code Playgroud)

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

是否有我忽略的 API 请求可以公开有关这些标记的信息,例如IBM WatsonAmazon Polly 的情况

And*_*w E 6

截至撰写本文时,时间点数据可在v1beta1Google 云文本转语音版本中找到。

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

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

from google.cloud import texttospeech as tts
Run Code Online (Sandbox Code Playgroud)

到:

from google.cloud import texttospeech_v1beta1 as tts
Run Code Online (Sandbox Code Playgroud)

又好又简单。

我需要修改发送综合请求的默认方式以包含该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"}
]
Run Code Online (Sandbox Code Playgroud)

这是示例:

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>
    """)
Run Code Online (Sandbox Code Playgroud)


小智 4

看起来这在以下版本中受支持Cloud Text-to-Speech API v1beta1https://cloud.google.com/text-to-speech/docs/reference/rest/v1beta1/text/synthesize#TimepointType

您可以使用https://texttospeech.googleapis.com/v1beta1/text:synthesize。设置。TimepointTypeSSML_MARK如果未设置此字段,则默认情况下不返回时间点。