python-docs-samples icon indicating copy to clipboard operation
python-docs-samples copied to clipboard

SSML google:style tag not working

Open rsandx opened this issue 2 months ago • 0 comments

I created a helper function to test the SSML google:style tag (Python).

def synthesize_ssml(style_name): from google.cloud import texttospeech

ssml_text = f'<speak><google:style name="{style_name}">Hello I'm so happy today!</google:style></speak>'
client = texttospeech.TextToSpeechClient()

input_text = texttospeech.SynthesisInput(ssml=ssml_text)

# Note: The following voices can speak in multiple styles: en-US-Neural2-F, en-US-Neural2-J
# Ref: https://docs.cloud.google.com/text-to-speech/docs/ssml
voice = texttospeech.VoiceSelectionParams(
    language_code="en-US",
    name="en-US-Neural2-F",   # or "en-US-Neural2-J"
)

audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.AudioEncoding.MP3
)

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

# The response's audio_content is binary.
out_file = f"output_{style_name}.mp3"
with open(out_file, "wb") as out:
    out.write(response.audio_content)
    print('Audio content written to file {out_file}')

The resulting audios of synthesize_ssml('apologetic'), synthesize_ssml('calm'), synthesize_ssml('empathetic'), synthesize_ssml('firm'), synthesize_ssml('lively') all sound the same. I also tested Azure TTS voices supporting styles and the same text using different styles do sound different.

rsandx avatar Nov 19 '25 04:11 rsandx