跳到正文
DEV Community· VoiceDeveloper·· 3 小時前AI 評分26

認識 SSML:改善 Voice AI 語音輸出

Understanding SSML for Better Voice AI Output

AI 導讀

SSML 透過標籤控制文字轉語音引擎的停頓、語速、音高及重點,改善 Voice AI 的語音輸出。ElevenLabs 的 API 支援 SSML,文中示範如何用 Python 和 JavaScript 傳送內容並將生成的音訊儲存為 MP3。SSML 亦可配合 ElevenLabs 的聲音複製功能使用。

正文

What is SSML and Why It Matters for Voice AI

When you’re building a voice‑first experience—whether it’s a chatbot, a navigation assistant, or a custom voice clone—you quickly realize that raw text just isn’t enough. The human voice has a ton of subtle cues: pauses, emphasis, intonation, and even the way it deals with long sentences. Speech‑Synthesis Markup Language (SSML) is the industry‑standard way to give those cues to your text‑to‑speech engine.

SSML is essentially HTML for speech. It lets you wrap your text in tags that control how the engine speaks it. Think of <prosody>, <break>, <emphasis>, <voice>, and <audio> as your toolbox for shaping the output. By mastering SSML, you can make your synthetic voice sound natural, reduce misunderstandings, and give your users a more engaging experience.

Below, we’ll walk through the core SSML tags, show practical examples in Python and JavaScript, and explain how you can leverage ElevenLabs’ API to bring your SSML to life.


Core SSML Tags You Should Know

Tag What It Does Example
<speak> Root element that tells the engine the content is SSML. <speak>Hello world</speak>
<prosody> Adjust pitch, rate, volume. <prosody rate="slow" pitch="+2st">Slow and higher.</prosody>
<break> Insert pauses. <break time="500ms"/>
<emphasis> Highlight a word or phrase. <emphasis level="strong">Important!</emphasis>
<voice> Switch to a different voice profile. <voice name="en-US-Wavenet-D">Hi</voice>
<audio> Play a short clip inline. <audio src="https://example.com/clip.mp3"/>

Quick‑start: A Simple SSML String

<speak>
  <prosody rate="medium" pitch="+0.5st">
    Hello, <emphasis level="moderate">developer</emphasis>!
  </prosody>
</speak>

In this snippet we:

  1. Set a moderate speaking rate.
  2. Slightly raise the pitch.
  3. Emphasize the word “developer.”

Integrating SSML with ElevenLabs

ElevenLabs offers a robust TTS API that accepts SSML out of the box. The API is simple to call and works in any language that can make HTTP requests. Below are two quick examples: one in Python and one in JavaScript.

Python Example (using requests)

import requests

url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
headers = {
    "Accept": "audio/mpeg",
    "Content-Type": "application/json",
    "xi-api-key": "YOUR_ELEVENLABS_API_KEY",
}
payload = {
    "text": """
    <speak>
      Welcome to your <emphasis level="strong">voice AI</emphasis> demo.
      <break time="300ms"/>
      Enjoy the experience!
    </speak>
    """,
    "model_id": "eleven_monolingual_v1",
    "voice_settings": {
        "stability": 0.5,
        "similarity_boost": 0.75
    }
}

response = requests.post(url, json=payload, headers=headers)

if response.status_code == 200:
    with open("output.mp3", "wb") as f:
        f.write(response.content)
    print("✅ Audio saved to output.mp3")
else:
    print("❌ Error:", response.text)

Tip – Replace {voice_id} with the actual ID of the voice you want to use. You can find available voices in the ElevenLabs dashboard or via the API.

JavaScript Example (Node.js)

const fetch = require('node-fetch');
const fs = require('fs');

const url = 'https://api.elevenlabs.io/v1/text-to-speech/{voice_id}';
const apiKey = 'YOUR_ELEVENLABS_API_KEY';

const payload = {
  text: `
    <speak>
      Hello from <emphasis level="moderate">ElevenLabs</emphasis>!
      <break time="200ms"/>How can we help you today?
    </speak>
  `,
  model_id: 'eleven_monolingual_v1',
  voice_settings: { stability: 0.6, similarity_boost: 0.8 }
};

fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'audio/mpeg',
    'xi-api-key': apiKey
  },
  body: JSON.stringify(payload)
})
  .then(res => res.buffer())
  .then(buffer => {
    fs.writeFileSync('output.mp3', buffer);
    console.log('✅ Audio saved to output.mp3');
  })
  .catch(err => console.error('❌ Error:', err));

Both snippets send SSML directly to ElevenLabs and stream the resulting MP3 to disk. You can also stream to a browser or a real‑time audio player, depending on your use case.


Common Pitfalls and How to Avoid Them

Issue What Happens Fix
Missing <speak> tag API rejects the request or treats the payload as plain text. Always wrap your SSML in <speak>…</speak>.
Unsupported SSML tags Some tags may not be supported by the chosen voice model. Check the ElevenLabs documentation or use the API’s “supported tags” endpoint.
Excessive <break> tags Creates unnatural long pauses. Use time="200ms" or break with a strength attribute (e.g., break strength="medium").
Incorrect pitch units Pitch values outside the valid range (-20st to +20st) cause errors. Keep pitch adjustments modest and test them in the console.
Large payloads Exceeding request size limits can lead to timeouts. Split long monologues into chunks, each wrapped in <speak>.

Advanced SSML: Voice Cloning & Custom Voices

ElevenLabs shines when you’re not just using generic voices. Their voice cloning feature lets you upload a small sample (a few minutes of clean speech) and generate a unique voice that sounds just like the original speaker.

<speak>
  <voice name="my_cloned_voice">
    This is a demo of your very own custom voice.
  </voice>
</speak>

How it works:

  1. Upload a Sample – Use the “Create Voice” endpoint with your audio clip.
  2. Generate an ID – The API returns a voice ID you’ll use in subsequent TTS calls.
  3. Use SSML – Wrap your text in <voice name="..."> with the new ID.

By combining SSML with a cloned voice, you can craft brand‑specific personalities or even recreate historical figures for educational apps.


Why ElevenLabs is a Great Choice

ElevenLabs offers:

  • High‑fidelity, natural‑sounding voices that outperform many free TTS engines.
  • Full SSML support out of the box, so you don’t need to build a wrapper.
  • Fast, low‑latency APIs that are great for real‑time applications.
  • Voice cloning that lets you create unique voices with just a few minutes of audio.

If you’re building a voice‑first product and want a reliable, production‑ready solution, ElevenLabs is a solid partner. You can start exploring with their free tier and upgrade as your usage grows.


Wrap‑Up: Take Your Voice AI to the Next Level

Mastering SSML gives you fine‑grained control over the speech output, and pairing it with ElevenLabs’ powerful API means you can deliver polished, natural audio in minutes. Whether you’re adding a conversational layer to a chatbot, generating dynamic narration for e‑learning, or cloning a brand voice, the combination of SSML and ElevenLabs is a recipe for success.

Ready to give it a try? Head over to https://try.elevenlabs.io/kr07zfuqn1bp and start building voice experiences that truly resonate with your audience. Happy coding!

來源:DEV Community · dev.to