Text to Speech enableTimePointing not working in Python

Viewed 211

Using texttospeech_v1beta1 to get ssml_mark but getting

"TypeError: synthesize_speech() got an unexpected keyword argument 'enableTimePointing'" error.

    from google.cloud import texttospeech_v1beta1

    client = texttospeech_v1beta1.TextToSpeechClient()synthesis_input = texttospeech_v1beta1.SynthesisInput(ssml=text)
    voice = texttospeech_v1beta1.VoiceSelectionParams(language_code='tr-TR',name='tr-TR-Wavenet-E')
    audio_config = texttospeech_v1beta1.AudioConfig(
    audio_encoding=texttospeech_v1beta1.AudioEncoding.MP3)
    response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config, enableTimePointing = 'SSML_MARK')
    print(response.timepoints)
2 Answers

You need to create a SynthesizeSpeechRequest Object instead of passing the arguments individually into client.synthesize_speech

So,

request = texttospeech_v1beta1.SynthesizeSpeechRequest(
    input=synthesis_input,
    voice=voice,
    audio_config=audio_config,
    enable_time_pointing=[texttospeech_v1beta1.SynthesizeSpeechRequest.TimepointType.SSML_MARK]

response = client.synthesize_speech(request)

Full example

from google.cloud.texttospeech_v1beta1 import VoiceSelectionParams, AudioConfig, AudioEncoding, SynthesizeSpeechRequest, SynthesisInput, TextToSpeechClient

client = TextToSpeechClient()

synthesis_input = SynthesisInput(ssml="<speak><mark name=\"1st\"/>Hello <mark name=\"2nd\"/>world</speak>")
voice = VoiceSelectionParams(language_code='en-US', name='en-US-Wavenet-D', ssml_gender='MALE')
audio_config = AudioConfig(audio_encoding=AudioEncoding.MP3)
request = SynthesizeSpeechRequest(input=synthesis_input, voice=voice, audio_config=audio_config, enable_time_pointing=[SynthesizeSpeechRequest.TimepointType.SSML_MARK])
response = client.synthesize_speech(request=request)
timepoints = list(response.timepoints)
# do something with timepoints
Related