SSML y control de la prosodia
Speech Synthesis Markup Language: pausas, énfasis, velocidad y tono.
SSML y control de la prosodia es una lección gratuita de AI Prompt Engineering en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de AI Prompt Engineering, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de AI Prompt Engineering incluye 4 lecciones en total.
¿Qué es SSML?
SSML (Speech Synthesis Markup Language) es un lenguaje basado en XML que ofrece un control detallado sobre cómo los motores de texto a voz leen el texto. Es compatible con Google Cloud TTS, Amazon Polly, Microsoft Azure TTS y muchos otros.
Mientras que el texto sin formato solo proporciona las palabras, SSML permite controlar las pausas, el énfasis, la velocidad, el tono, la pronunciación y mucho más.
Estructura básica de SSML
Todos los documentos SSML están contenidos en una etiqueta <speak>. En su interior, se combina texto sin formato con elementos de marcado SSML. Los motores TTS procesan el SSML y producen el audio correspondiente.
# SSML document structure
SSML_BASIC = """
<speak>
Welcome to the course.
<break time="500ms"/>
Today we will cover three topics.
First, we will discuss SSML basics.
<break time="300ms"/>
Second, we will explore prosody control.
<break time="300ms"/>
And third, we will look at advanced features.
</speak>
"""
# Send to Google Cloud TTS
from google.cloud import texttospeech
client_tts = texttospeech.TextToSpeechClient()
input_text = texttospeech.SynthesisInput(ssml=SSML_BASIC)
voice = texttospeech.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
print('SSML document ready to synthesize')El elemento break
<break> inserta una pausa en el discurso. Utilícelo para crear un ritmo natural, separar elementos de una lista o añadir un efecto dramático. El atributo time acepta milisegundos (ms) o segundos (s).
# break element examples
BREAK_EXAMPLES = """
<speak>
Are you ready?
<break time="1s"/>
Let us begin.
The three rules are:
number one,
<break time="300ms"/>
always test your code.
<break time="200ms"/>
Number two,
<break time="300ms"/>
write clear documentation.
<break time="200ms"/>
And number three,
<break time="300ms"/>
review before you ship.
<break strength="x-strong"/>
That is all for today.
</speak>
"""
# break strength values: none, x-weak, weak, medium, strong, x-strong
# These map to approximate pause durations defined by the engine
print('break time: explicit duration (e.g. 500ms)')
print('break strength: semantic pause level (weak/medium/strong)')El elemento emphasis
<emphasis> añade énfasis a las palabras: hace que el motor las pronuncie con mayor volumen, más despacio o con un tono más agudo. Utilícelo con moderación, ya que un énfasis excesivo suena poco natural.
EMPHASIS_EXAMPLES = """
<speak>
This update is
<emphasis level="strong">critically important</emphasis>.
Please read it carefully.
The deadline is
<emphasis level="moderate">this Friday</emphasis>,
not next week.
We
<emphasis level="reduced">recommend</emphasis>
enabling this feature, but it is optional.
</speak>
"""
# emphasis level values:
# strong — much more stress (louder, slower, higher pitch)
# moderate — some additional stress (default if level omitted)
# reduced — less stress (quieter, faster)
print('Use strong for critical information')
print('Use reduced for parenthetical or secondary information')El elemento prosody: rate
<prosody rate> controla la velocidad del discurso. Redúzcala para la información importante y auméntela para los detalles secundarios o las exenciones de responsabilidad.
PROSODY_RATE_EXAMPLES = """
<speak>
<prosody rate="slow">
This is the most important thing to remember.
Take a moment to let it sink in.
</prosody>
<prosody rate="medium">
Now, for some context about how we got here.
</prosody>
<prosody rate="fast">
And now a quick summary of less critical details that you
can refer back to in the documentation.
</prosody>
</speak>
"""
# rate values:
# x-slow, slow, medium (default), fast, x-fast
# Or percentage: rate="75%" (75% of normal speed)
# Or absolute: rate="200 words per minute"
print('Slow: for emphasis, complex information, or pauses for thought')
print('Fast: for disclaimers, secondary info, rapid listing')El elemento prosody: pitch
<prosody pitch> ajusta la frecuencia fundamental de la voz. Utilícelo para señalar cambios de tono, como preguntas, entusiasmo o contenido solemne.
PROSODY_PITCH_EXAMPLES = """
<speak>
<prosody pitch="high" rate="medium">
Exciting news! We just launched a brand new feature!
</prosody>
<prosody pitch="low" rate="slow">
Unfortunately, this service will be discontinued.
We apologize for any inconvenience.
</prosody>
<prosody pitch="+20%">
Did you know that our users save three hours per week on average?
</prosody>
<prosody pitch="-15%">
Please review the terms and conditions carefully.
</prosody>
</speak>
"""
# pitch values:
# x-low, low, medium, high, x-high
# Or relative: +20%, -15%
# Or semitones: +2st, -4st
print('High pitch: excitement, questions, announcements')
print('Low pitch: serious, cautionary, or somber content')El elemento say-as
<say-as> indica al motor TTS cómo debe interpretar el texto: como una fecha, un número de teléfono, una divisa, caracteres o cualquier otro formato. Es la solución fiable para los números y los valores especiales.
SAY_AS_EXAMPLES = """
<speak>
Your appointment is on
<say-as interpret-as="date" format="mdy">01/15/2025</say-as>.
Call us at
<say-as interpret-as="telephone">1-800-555-1234</say-as>.
Your confirmation code is
<say-as interpret-as="characters">XK7T9</say-as>.
The total is
<say-as interpret-as="currency" language="en-US">$47.50</say-as>.
This is version
<say-as interpret-as="characters">2.3.1</say-as>
of the software.
</speak>
"""
# interpret-as values:
# characters — spell out each character
# cardinal — number as cardinal ("forty-seven")
# ordinal — "forty-seventh"
# fraction — "three halves"
# date — format string controls order
# telephone — phone number formatting
# currency — monetary value with currency name
print('say-as is the most reliable way to control number pronunciation')El elemento phoneme
<phoneme> proporciona una pronunciación fonética explícita para las palabras que el motor TTS pronuncia mal sistemáticamente, como nombres de marcas, términos técnicos o palabras extranjeras.
PHONEME_EXAMPLES = """
<speak>
Welcome to
<phoneme alphabet="ipa" ph="ent.ro.pi">Entropiq</phoneme>,
the leading analytics platform.
Our CEO,
<phoneme alphabet="ipa" ph="joo.serf">Josef</phoneme>,
will present the results.
This API uses
<phoneme alphabet="ipa" ph="kwer.i">GraphQL</phoneme>
for data fetching.
</speak>
"""
# IPA (International Phonetic Alphabet) is the most precise
# x-sampa is an alternative ASCII-friendly phonetic alphabet
# Use an IPA converter tool to find the right phonemes:
# - https://tophonetics.com
# - Dictionary.com pronunciation guides use IPA
print('Use phoneme for brand names, technical terms, proper nouns')
print('Test with multiple phoneme values until it sounds right')SSML con Amazon Polly
Amazon Polly admite el SSML estándar y también extensiones específicas de Polly. El uso de la API es ligeramente distinto del de Google Cloud TTS, pero el marcado SSML es el mismo.
import boto3
polly = boto3.client('polly', region_name='us-east-1')
SSML_CONTENT = """
<speak>
<prosody rate="slow" pitch="low">
Welcome to our quarterly earnings call.
</prosody>
<break time="1s"/>
We are pleased to report
<emphasis level="strong">record revenue</emphasis>
of
<say-as interpret-as="currency" language="en-US">$4200000</say-as>
this quarter.
</speak>
"""
response = polly.synthesize_speech(
Text=SSML_CONTENT,
TextType='ssml', # Tell Polly this is SSML
OutputFormat='mp3',
VoiceId='Joanna', # US English female voice
)
if 'AudioStream' in response:
with open('output.mp3', 'wb') as f:
f.write(response['AudioStream'].read())
print('Audio saved to output.mp3')Generación de SSML con LLM
Puede utilizar un LLM para convertir texto sin formato en guiones de voz anotados con SSML. Así podrá escribir el contenido con normalidad y procesarlo posteriormente para optimizar su entrega mediante TTS.
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
SSML_GENERATION_PROMPT = (
'Convert the following text into an SSML document for Google Cloud TTS.\n\n'
'Rules:\n'
'- Wrap the entire output in <speak> tags\n'
'- Add <break time="500ms"/> between major points\n'
'- Add <emphasis level="strong"> around key terms or critical information\n'
'- Use <prosody rate="slow"> for important warnings or summaries\n'
'- Use <say-as interpret-as="date"> for all dates\n'
'- Use <say-as interpret-as="telephone"> for phone numbers\n'
'- Return only the SSML, no explanation\n\n'
'Text: {text}'
)
def text_to_ssml(text):
prompt = SSML_GENERATION_PROMPT.format(text=text)
r = client.messages.create(
model='claude-opus-4-5',
max_tokens=2000,
messages=[{'role': 'user', 'content': prompt}]
)
return r.content[0].textValidación y pruebas de SSML
Un SSML mal formado provoca errores en la API de TTS o hace que el motor lea en voz alta las etiquetas XML sin procesar. Valide siempre el SSML antes de enviarlo a producción.
import xml.etree.ElementTree as ET
def validate_ssml(ssml_string):
"""
Basic SSML validation: checks XML is well-formed
and has a <speak> root element.
"""
try:
root = ET.fromstring(ssml_string)
if root.tag != 'speak':
return False, 'Root element must be <speak>'
# Check for common misuse patterns
warnings = []
for elem in root.iter():
if elem.tag == 'break' and 'time' not in elem.attrib and 'strength' not in elem.attrib:
warnings.append('<break> has no time or strength attribute')
return True, warnings if warnings else 'Valid'
except ET.ParseError as e:
return False, f'XML parse error: {e}'
# Test
valid_ssml = '<speak>Hello <break time="500ms"/> world.</speak>'
bad_ssml = '<speak>Hello <break> world.</speak>' # break not self-closed
print(validate_ssml(valid_ssml))
print(validate_ssml(bad_ssml))Compruebe sus conocimientos: say-as de SSML
¿Cuál es la finalidad principal del elemento <say-as> de SSML?
Resumen: SSML y control de la prosodia
SSML ofrece un control detallado de la salida de TTS. Elementos clave: <break> (pausas por tiempo o intensidad), <emphasis> (niveles de énfasis: strong/moderate/reduced), <prosody rate> (velocidad del discurso), <prosody pitch> (frecuencia vocal), <say-as> (interpretación semántica de números, fechas y números de teléfono) y <phoneme> (pronunciación explícita en IPA). Tanto Google Cloud TTS como Amazon Polly admiten SSML con el mismo conjunto básico de etiquetas. Utilice LLM para generar SSML automáticamente a partir de texto sin formato y valide siempre que el XML esté bien formado antes de enviarlo a producción.
Aprende AI Prompt Engineering con un tutor de IA — gratis
Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.
- Cursos
- 53
- Lecciones
- 199
Preguntas frecuentes
¿La lección «SSML y control de la prosodia» es gratis?
Sí — el texto completo de «SSML y control de la prosodia» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de AI Prompt Engineering, actualiza a CoddyKit PRO. El curso de AI Prompt Engineering incluye 4 lecciones en total.
¿Qué aprenderé en «SSML y control de la prosodia»?
Speech Synthesis Markup Language: pausas, énfasis, velocidad y tono. Practicas AI Prompt Engineering con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar AI Prompt Engineering?
No se requiere experiencia previa. AI Prompt Engineering en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «SSML y control de la prosodia»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de AI Prompt Engineering?
Sí. Cada lección de AI Prompt Engineering incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Patrones de prompts TTS para un habla natural
- SSML y control de la prosodia
- Diseño de personalidades para IA de voz
- Agentes de voz y texto multimodales