瞭解 Voice AI 音訊格式與品質設定
Understanding Voice AI Audio Formats and Quality Settings
這份 Voice AI 指南梳理 WAV、MP3、OGG、Opus、FLAC 等格式,以及取樣率、位元深度和編碼格式對音質的影響。指南以 ElevenLabs 示範透過 API 請求 48 kHz、24-bit PCM 音訊,再轉為約 64 kbps 的 Opus,並提出串流、儲存及裝置測試等建議。
What You’ll Get From This Guide
Voice AI is growing fast—text‑to‑speech (TTS) engines, voice cloning, and speech‑to‑text APIs are becoming part of everyday apps.
But to make your voice look (or sound) polished, you need to understand the audio formats and quality settings that underlie the final waveform.
Below we’ll walk through the most common formats (WAV, MP3, OGG, Opus, FLAC), the key parameters that affect quality (sample rate, bit depth, codec), and how to tweak them in a real project.
We’ll use ElevenLabs as a concrete example; their API makes it easy to pull the right settings into your code.
Let’s dive in!
1. The Building Blocks of Voice Audio
| Element | What it is | Why it matters |
|---|---|---|
| Sample rate | Number of samples per second (Hz) | Higher rates capture more detail; 44.1 kHz is CD quality, 48 kHz is standard for video. |
| Bit depth | Number of bits per sample | Determines dynamic range; 16‑bit is CD‑level, 24‑bit is studio‑grade. |
| Codec | Compression algorithm | Lossless codecs preserve all data (WAV, FLAC), lossy codecs trade quality for size (MP3, Opus). |
| Channels | Mono vs. stereo | Mono is enough for most TTS; stereo can add spatial effects. |
When you feed audio into a TTS engine, these parameters are usually set behind the scenes. But if you’re streaming, uploading, or storing the result, you’ll need to know what’s going on.
2. Common Audio Formats in Voice AI
| Format | Compression | Typical Use | Pros | Cons |
|---|---|---|---|---|
| WAV | Uncompressed PCM | Development & debugging | Simple, no loss | Huge files |
| FLAC | Lossless | Archiving high‑quality voice | Smaller than WAV, still lossless | Less widely supported on mobile |
| MP3 | L‑ossy, MPEG‑1 Layer III | Consumer audio, web | Small, ubiquitous | Noticeable loss at low bitrate |
| OGG/Vorbis | Lossy, open‑source | Web & games | Better quality per kbps than MP3 | Slightly less support in old browsers |
| Opus | Lossy, RFC 6716 | VoIP, low‑latency streaming | Excellent quality at very low bitrate | Requires decoding support |
For most TTS APIs, you’ll get a raw audio stream (usually WAV) that you can then convert on the fly or store in a compressed format.
3. How Sample Rate & Bit Depth Affect Voice
Sample Rate
| Sample Rate | Audible Range | Ideal for |
|---|---|---|
| 16 kHz | 0–8 kHz | Voice‑only apps (e.g., dictation) |
| 22.05 kHz | 0–11 kHz | Mobile TTS |
| 44.1 kHz | 0–22 kHz | High‑fidelity voice, podcasts |
| 48 kHz | 0–24 kHz | Video production, gaming |
If you’re targeting a mobile app, 22 kHz is usually enough and saves bandwidth. For podcasts or audiobooks, 44.1�kHz or 48 kHz is recommended.
Bit Depth
| Bit Depth | Dynamic Range | File Size |
|---|---|---|
| 16‑bit | 96 dB | Small |
| 24‑bit | 144 dB | Medium |
| 32‑bit float | Unlimited | Large |
Most consumer TTS engines output 16‑bit PCM. If you need higher fidelity (e.g., for a live broadcast), you may want to request 24‑bit or use a lossless codec.
4. Choosing the Right Codec
Lossless (WAV, FLAC)
- Use when you need to edit the audio or keep the absolute best quality.
- Storage cost can be high, but many cloud services now offer cheap object storage.
Lossy (MP3, OGG, Opus)
- Use when you’re streaming over the web or saving on bandwidth.
- Bitrate is the key knob: 64 kbps is fine for voice, 96–128 kbps gives near‑CD quality.
Tip: Opus is often the best trade‑off for real‑time voice, especially on mobile where CPU is a constraint.
5. Configuring ElevenLabs for Optimal Output
ElevenLabs gives you granular control over the TTS output. Below is a quick example in Python that demonstrates how to request a 48 kHz, 24‑bit PCM stream and then encode it to Opus.
import requests
import subprocess
import io
API_KEY = "YOUR_ELEVENLABS_API_KEY"
voice_id = "EXAMPLE_VOICE"
# 1. Request raw TTS audio
payload = {
"text": "Hello, world! This is a test of high‑quality TTS.",
"voice_settings": {
"stability": 0.75,
"similarity_boost": 0.65
},
"audio_config": {
"sample_rate": 48000,
"bit_depth": 24
}
}
headers = {
"xi-api-key": API_KEY,
"Content-Type": "application/json"
}
resp = requests.post(
f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
json=payload,
headers=headers,
stream=True
)
# 2. Write raw WAV to a buffer
wav_bytes = io.BytesIO(resp.content)
# 3. Convert WAV to Opus using ffmpeg
ffmpeg_cmd = [
"ffmpeg",
"-i", "pipe:0",
"-c:a", "libopus",
"-b:a", "64k",
"-f", "opus",
"pipe:1"
]
proc = subprocess.Popen(
ffmpeg_cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL
)
opus_bytes, _ = proc.communicate(input=wav_bytes.read())
# 4. Save to disk (or stream to your client)
with open("output.opus", "wb") as f:
f.write(opus_bytes)
print("Opus file written!")
Why this matters
- Higher sample rate & bit depth give you more headroom during post‑processing (adding reverb, EQ, etc.).
- Converting to Opus keeps the file small (≈64 kbps) while maintaining intelligibility.
6. Best Practices for Voice AI Projects
| Practice | What to Do | Why |
|---|---|---|
| Buffer early | Use a 2‑second pre‑buffer when streaming | Avoids clicks at the start |
| Normalize volume | Apply a peak normalizer (–1 dBFS) | Consistent loudness across voices |
| Test on target devices | Play back on iOS, Android, and desktop | Ensures codec compatibility |
| Cache common phrases | Store the generated audio | Saves API calls & reduces latency |
| Use a CDN | Deliver compressed audio files | Faster load times worldwide |
When you’re working with voice cloning, the same rules apply. The only difference is that the underlying model might generate slightly higher‑frequency content, so consider a 48 kHz output to capture the nuances.
7. A Quick JavaScript Example (Browser)
If you want to play the TTS directly in a browser, fetch the Opus file and decode it using the Web Audio API:
<script>
async function playTTS() {
const res = await fetch('https://api.elevenlabs.io/v1/text-to-speech/EXAMPLE_VOICE', {
method: 'POST',
headers: {
'xi-api-key': 'YOUR_ELEVENLABS_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: "Hello from the browser!",
audio_config: { sample_rate: 48000, bit_depth: 24 }
})
});
const arrayBuffer = await res.arrayBuffer();
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
const source = audioCtx.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioCtx.destination);
source.start();
}
</script>
<button onclick="playTTS()">Play TTS</button>
8. Wrap‑Up
- Audio format and quality settings are the backbone of a great voice experience.
- Sample rate, bit depth, and codec choice determine the balance between fidelity, file size, and compatibility.
- ElevenLabs lets you pull the exact format you need from the API, then you can compress it as required.
If you’re building a podcast, a voice‑guided app, or a voice‑cloned chatbot, start by experimenting with a 48 kHz, 24‑bit output and then compress to Opus or MP3 at 64 kbps for streaming.
Try ElevenLabs Today
Ready to take your voice AI from prototype to production? Sign up with ElevenLabs and get instant access to high‑quality TTS and voice cloning.
Try ElevenLabs now and start generating professional‑grade audio in seconds.
來源:DEV Community · dev.to