volumeGainDb not working for Google text-to-speech

Viewed 125

My reference is this page from Google website: Method: text.synthesize

Here is some simple python code:

import os
from google.cloud import texttospeech

############################################################
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] =\
 'FAKE'
client = texttospeech.TextToSpeechClient()
############################################################

ssml = '<speak>Mary had a little lamb, it\'s fleece was white as snow</speak>'

synthesis_input = texttospeech.SynthesisInput(ssml=ssml)

audio_config = texttospeech.AudioConfig(
    audio_encoding = texttospeech.AudioEncoding.MP3,
    speaking_rate = 1.0,
    pitch = -2.0,
#WITHOUT THIS EVERTHING WORKS FINE
    volumeGainDb = 2.0
)

voice = texttospeech.VoiceSelectionParams(
    language_code = 'en-GB',
    ssml_gender = texttospeech.SsmlVoiceGender.MALE,
    name = 'en-GB-Wavenet-B'
)

response = client.synthesize_speech(
    input = synthesis_input,
    voice = voice,
    audio_config = audio_config
)

with open('./audioFiles/audio1.mp3', 'wb') as output1:
        output1.write(response.audio_content)

And here is what appears on the console:

(envo) D:\py_new\ttsgcp>python tts_stack.py Traceback (most recent call last): File "D:\py_new\envo\lib\site-packages\proto\message.py", line 507, in init pb_type = self._meta.fields[key].pb_type KeyError: 'volumeGainDb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:\py_new\ttsgcp\tts_stack.py", line 15, in audio_config = texttospeech.AudioConfig( File "D:\py_new\envo\lib\site-packages\proto\message.py", line 512, in init raise ValueError( ValueError: Unknown field for AudioConfig: volumeGainDb

(envo) D:\py_new\ttsgcp>

Everything works great if I leave out the volumeGainDb field. So that seems to be where the problem lies

1 Answers

OK problem solved. Despite appearances on the reference page, the correct field in

texttospeech.AudioConfig is:

volume_gain_db

It is NOT

volumeGainDb

as the page suggests. (That MAY work for Nodejs. will check)

So the correct code is:

audio_config = texttospeech.AudioConfig(
    audio_encoding = texttospeech.AudioEncoding.MP3,
    speaking_rate = 1.0,
    pitch = -2.0,
    volume_gain_db = 8.0
)

I discovered this when I did:

print(help(texttospeech.AudioConfig()))
Related