Skip to content

語音合成與識別原理

💡 學習指南:本章節將帶你深入了解 AI 音訊底層原理。我們不僅會探討「生澀」的聲學專業術語(如 STFT、流匹配、音色嵌入),還會透過通俗的比喻和直觀的互動演示,讓你徹底明白 AI 是如何「聽懂人話」並「開口說話」的。即使你是零基礎讀者,也能輕鬆掌握!

🎙️ First AI Audio Experience: Let Machines Speak
From speech synthesis to voice cloning, explore how AI gives machines a voice.
🎵

Choose a scenario to experience AI audio

💡TTS: text to speech, letting AI read any text aloud
🎯ASR: speech recognition, converting speech into text
🎭Voice cloning: copy a voice from only a few seconds of audio

0. 引言:物理聲波的「數位化翻譯」

人類的語音和世界上的各種聲音,本質上是空氣振動產生的連續物理聲波。但電腦的腦子裡只有 01,它聽不見聲音。因此,讓 AI 處理聲音的第一步,就是跨越「物理世界」與「數位世界」的鴻溝。

這個過程叫做聲數轉換 (A/D 轉換),其核心輸出就是 脈衝編碼調變 (PCM) 波形,也就是我們常見的音訊資料。它由兩個核心指標決定:

  1. 取樣率 (Sample Rate):一秒鐘內給聲波拍多少次「照片」。比如 16kHz 就是一秒鐘記錄 16,000 個振幅數字。
  2. 位元深度 (Bit Depth):每次拍照的「標尺」有多精細。16-bit 意味著振幅有 65,536 個層級的區分度。

但這帶來了一個問題:一秒鐘 16,000 個數字,一句話幾十萬個數字,資訊量大且冗雜。如果直接把這長長的一維波形丟給神經網路去處理,這就好比讓一個人透過湊近看毛衣上的一根根毛線結構,去判斷這件毛衣的圖案好不好看——這顯然是極其困難的計算挑戰。


1. 特徵工程:給 AI 戴上「人類的耳朵」

既然直接看「一維波形 (Time-Domain)」行不通,科學家們便想到了一個降維打擊的辦法:把一維的聲音,變成二維的頻率圖譜 (Frequency-Domain)。

1.1 從一條線到一張圖:短時傅立葉轉換 (STFT)

想像一下,聽一首交響樂時,我們很少去在意某個瞬間空氣振動的位移總量,我們更在意的是這段時間裡有哪些樂器(不同頻率)、聲音有多大(能量)

透過短時傅立葉轉換 (STFT) 這個數學魔法,我們可以把平鋪直敘的聲波,拆解成一張包含「時間、頻率、能量(顏色深淺)」的二維矩陣圖片,這被稱為 頻譜圖 (Spectrogram)。至此,處理聲音的問題,被巧妙地轉化為了 AI 更擅長處理的「看圖」問題。

1.2 迎合聽覺習慣:梅爾刻度 (Mel Scale)

物理學上的頻率分佈是線性的(0-100Hz 的跨度和 10000-10100Hz 一樣長)。但人類的耳朵是非常「雙標」的:我們對低沉的聲音(低頻)變化極其敏感,卻對尖銳的高保真聲音(高頻)的細微差別遲鈍不已。

為了讓 AI 能像人類一樣,「把有限的注意力放在更重要的地方」,研究者引入了非線性的 梅爾濾波器組 (Mel Filterbanks)。它在低頻區域劃分極細,高頻區域則粗略包裹。 經過對數轉換後,我們得到了當代音訊 AI 的靈魂基石——梅爾頻譜 (Mel-Spectrogram)

👇 動手點點看:在下方觀察一維的機器波形如何被轉化為符合人類感知的二維色彩圖譜。

📊 Mel Spectrogram: How AI “Sees” Sound
Sound is a wave, but AI sees a spectrogram. See how waveforms become images that AI can understand.
1024
80
🔊 Waveform (time domain)Raw audio amplitude over time
STFT transform
📈 Linear spectrumLow high-frequency resolution
VS
🎯 Mel spectrogramMatches human hearing
🎧 Why use the Mel scale?
Human hearing
100Hz→200Hz and 10000Hz→10100Hz can feel similarly different
Linear scale
Equal frequency intervals do not match human perception
💡

Mel spectrogram principle: The Mel scale models the nonlinear way humans perceive frequency. We are more sensitive to low-frequency changes and less sensitive to high-frequency changes. Mel spectrograms map frequency to this scale so AI focuses on perceptually important regions.


2. 讓大模型學會「外語」:兩種主流生成範式

當提取完特徵後,我們該如何教 AI 生成聲音?目前學術界和工業界有兩大並行的「魔法陣」。

2.1 範式一:把聲音當文字 (Audio Tokenization)

伴隨 ChatGPT 的火爆,科學家們思考:如果把聲音也變成一個接一個的「漢字(Token)」,大語言模型(LLM)是不是就能直接唱歌說話了?

  • 壓縮與量化:依靠強大的 神經編解碼器 (Neural Codec,如 EnCodec) 和 VQ-VAE 架構,一段幾兆大小的音訊會被極限壓縮,最終變成一本字典裡的一個個離散代號(比如序列:[82, 105, 33...])。
  • 生成接龍:AI 模型只需像做文字接龍一樣,預測下一個聲音 Token 是什麼。這極大地統一了多模態學習的底層架構!
🎵 Audio Tokenization: Neural Codecs
🔽 Encoder
Raw waveform
24kHz, 16-bit
Conv 1
Conv 2
Conv 3
Conv 4
CNN downsampling
320x dimension reduction
VQ quantization
Discrete token
Compressed: ~1.5 kbps
🔼 Decoder
4212872553391
Discrete token
Codebook index
ConvT 4
ConvT 3
ConvT 2
ConvT 1
Transposed convolution
Upsampling
Reconstructed waveform
24kHz
📊 Bitrate comparison
1.5 kbps
EnCodec-24k
Sample rate:24 kHz
Frame rate:75 Hz
Codebook size:1024
4
3.0 kbps
EnCodec-48k
Sample rate:48 kHz
Frame rate:75 Hz
Codebook size:1024
5
6.0 kbps
SoundStream
Sample rate:16 kHz
Frame rate:50 Hz
Codebook size:1024
4.5
0.98 kbps
SNAC
Sample rate:24 kHz
Frame rate:43 Hz
Codebook size:4096
4
🔢 Token sequence visualization
0.1s0.2s0.30000000000000004s0.4s0.5s0.6000000000000001s0.7000000000000001s0.8s0.9s1s1.1s1.2000000000000002s1.3s1.4000000000000001s1.5s1.6s1.7000000000000002s1.8s1.9000000000000001s2s
Low-frequency components Mid-frequency components High-frequency components
🎯 Why audio tokenization?
🚀
Efficient transfer
Compress audio to ~1.5 kbps, about 256x smaller than raw audio, making it suitable for network transfer.
🧠
Language-model friendly
Discrete tokens can be processed directly by LLMs, enabling unified text-to-audio modeling.
🎵
Music generation
Models such as MusicGen and AudioLDM use audio tokens to generate music and sound effects.
🗣️
Speech synthesis
TTS models such as VALL-E and SoundStorm can generate audio tokens directly.

💡Neural audio codecs: Models such as EnCodec (Meta), SoundStream (Google), and SNAC use VQ-VAE style architectures to compress audio into discrete tokens. These tokens can be handled by language models for high-quality audio generation and compression.

2.2 範式二:把聲音當畫作 (Spectrogram Generation)

這是目前大量成熟語音軟體的基石方案,可控性極佳。

  • 譜圖生成:AI 模型並不輸出最終的音訊波形,而是直接學習「文字」到「二維梅爾頻譜圖」的映射,像畫家一樣畫出一張聲學特徵圖。
  • 還原波形 (Vocoder):由於頻譜圖丟失了相位等細節資訊無法直接播放,我們需要一個聲碼器 (Vocoder,如 HiFi-GAN) 充當翻譯官,將這張圖完好無損地等效還原回能推動喇叭振動的一維波形。

3. 雙端互逆:ASR 與 TTS 的協同翻譯

讓機器擁有「耳朵」和「嘴巴」,其實是在做兩場南轅北轍的翻譯:

  • 自動語音識別 (ASR):將聲音翻譯為文字。這是一道多對一的收斂選擇題。模型(如 Whisper)必須在充滿嘈雜環境噪音、口音變化、同音字干擾(「期中」與「期終」)的海量音訊中,提煉鎖定出唯一正確的語義文字。
  • 文本轉語音 (TTS):將文字翻譯為聲音。這是一道一對多的發散創作題。同樣一句乾癟的「你好」,它可以帶著一萬種不同的語速、情緒、停頓和嗓音。模型必須有能力腦補出這些缺失的參數。
🔄 ASR ↔ TTS: Two-Way Speech Conversion
Explore the inverse processes of speech recognition and speech synthesis.
🎙️
ASR Speech Recognition
Audio → Text
or
🔊
TTS Speech Synthesis
Text → Audio
📊 ASR vs TTS
🎙️
ASR
Input:Audio waveform
Output:Text sequence
Challenge:Noise, accents, homophones
🔊
TTS
Input:Text sequence
Output:Audio waveform
Challenge:Prosody, emotion, naturalness
🔀 Architecture comparison
ASR Pipeline
Audio
Features
Encoder
Decoder
Text
TTS Pipeline
Text
Encoder
Decoder
Vocoder
Audio
💡

Inverse relationship: ASR and TTS are two core directions in speech technology and inverse processes of each other. ASR converts continuous audio signals into discrete text, while TTS converts discrete text into continuous audio signals. Both rely on acoustic models and language models.


4. 從「擠牙膏」到「直通車」:TTS 核心架構換代

在了解了基礎流程後,我們看看 TTS 引擎是如何追求極致速度和連貫性的。

  • 序列式笨方法 (自迴歸 AR):老一代模型必須遵循時間先後,生成完上一毫秒,才能以此為基準預測下一毫秒。這種方法雖然穩妥,但極易卡殼且速度緩慢
  • 神級預判 (非自迴歸 NAR):後續的模型引入了時長預測器 (Duration Predictor),不再排隊生成,而是一次性為每個聲素「算命」出它該有的時長,接著兵分多路瞬間並行輸出整句音訊
  • 常微分快車道 (流匹配 Flow Matching):這是當下的終極前沿方案(如 F5-TTS)。它運用連續正規化流和常微分方程 (ODE) 等複雜數學原理,摒棄了傳統的生硬搭建。模型學習的是一條從「純白雜訊」到「完美頻譜」的最優直達運動軌跡(機率流)。不僅計算效率呈指數級上升,其聲音的平滑與自然度也達到了巔峰。
🔄 TTS Architecture Evolution: From Slow to Fast
Explore how text becomes speech and compare different architectures.
1
📝
Text processing
Tokenize & phonemes
2
🔢
Text embedding
Feature extraction
3
🌊
Flow matching
Optimal transport
4
🔊
Vocoder
Spectrum to waveform
📝
Text processing
Convert input text into a phoneme sequence
Input:Raw text
Output:Phoneme sequence
Tech:G2P
📊 Architecture comparison
Feature
Autoregressive
Non-autoregressive
Flow matching
Generation speed
Slow
Fast
Very fast
Audio quality
High
Medium-high
High
Stability
Medium
High
High
Controllability
Medium
High
High
🏆 Representative models
Tacotron 2
AR
Classic AR model with excellent audio quality
FastSpeech 2
NAR
Parallel generation with high speed
F5-TTS
Flow
Recent SOTA, generated in 10 steps
CosyVoice
Flow
Alibaba open-source model with multilingual support
💡

TTS evolution trend: TTS has moved from early autoregressive models such as Tacotron, to non-autoregressive models such as FastSpeech, and now to flow matching models such as F5-TTS. The direction is faster, more stable, and higher-quality synthesis.


5. 零樣本聲音複製 (Zero-Shot Voice Cloning)

僅僅在幾年前,要想用 AI 模仿某人的聲音,還得讓他在極其安靜的錄音棚錄上幾萬句話並花費數天訓練模型。而今天,僅需 3 秒鐘的語音條,AI 就能以假亂真。

這背後依賴一項核心技術:說話人特徵編碼器 (Speaker Encoder) 和度量學習。

  • 這不僅是一個監聽器,更是一個「基因提取儀」。它的任務是剝離掉音訊裡的背景雜訊和具體說了什麼話(Text),強行且唯一地抓取出關於你的生理恆定特徵:聲帶有多寬?共鳴音腔有多大?咬字有什麼習慣?
  • 這些特徵最終會被壓扁成一個幾百維的說話人嵌入向量 (Speaker Embeddings, 如 x-vector)。這串如同條碼般的數字完全表徵了你的聲音身分。隨後的 TTS 模型只要「帶上這串向量」進行條件生成,吐出的任何語言都會帶上你的嗓音特色。
🎭 Voice Cloning: Let AI Imitate Anyone
With only a few seconds of reference audio, AI can learn a speaker voice.
1 Provide reference audio
👨
Male voice A
Low and magnetic
👩
Female voice B
Gentle and sweet
🧒
Child voice
Lively and cute
👴
Elder voice
Weathered and steady
or
2 AI learns voice features
📂
Load audio
🔢
Encode features
🎨
Extract timbre
💎
Build embedding
3 Enter text to generate speech
💡 Voice cloning tips
⏱️
Reference duration

3-10 seconds is enough; quality matters more than length.

🔇
Environment

Use a quiet environment and avoid background noise.

🗣️
Content choice

Audio with varied pitch and speaking speed works better.

🔬

Technical principle: Voice cloning extracts timbre, intonation, and speaking style from reference audio to build a speaker embedding. During generation, the TTS model combines text content with this speaker embedding to synthesize speech similar to the reference voice.


6. 賦予靈魂:情感節奏與細粒度風格控制

一句「真的嗎」,既可以是驚喜,也可以是憤怒質疑。商業級的高階 AI 不僅要「讀對字」,更要「帶有感情」。

學術界提出了 全域風格 Token (GST) 以及特徵瓶頸機制。大模型可以從海量的人類演繹錄音中聚類提取出對應的「傷心」、「激動」、「慵懶」等抽象的軟向量。 在工程落地時,我們還引入了基頻 (F0,掌控音調升降)、能量 (Energy,掌控音量爆破音) 等直觀的適配器調節參數,賦予了創作者像捏遊戲人物臉型一樣,精細捏合「語音情緒」的能力。

🎭 Emotion and Style Control
Choose emotion style
😐
Neutral
Steady and natural
😊
Happy
Light and cheerful
😢
Sad
Low and slow
😠
Angry
Forceful and intense
🤩
Excited
Warm and energetic
😌
Calm
Relaxed and soothing
Emotion Embedding Space
Neutral Happy Sad Angry Excited Calm
🎚️ Fine-grained controls
Speed1x
SlowNormalFast
Pitch0
LowNormalHigh
Energy dynamics100%
SoftModerateIntense
Pause control150ms
CompactNaturalRelaxed
🎙️ Preview synthesis

💡Emotion control: Modern TTS systems can synthesize natural speech and precisely control emotion, speed, pitch, and other style features. This lets AI voiceover adapt to different scenarios, from calm customer-service dialogs to energetic speeches.


7. 結語

從基礎的數位訊號轉換(PCM),到降維提純(Mel-Spectrogram),直至時下大火的基於「流匹配演算法(Flow Matching)」和「神經編解碼(Neural Codec)」的多模態大基座,音訊 AI 正在上演一場從機械模擬向原生理解的躍升。

未來的人工智慧代理(AI Agent),將徹底打通人類視、聽、說的高維鏈路,像擁有真人直覺一般應對每一次交流!


8. 核心術語速查表 (Glossary)

術語英文全稱釋義
PCMPulse-Code Modulation脈衝編碼調變,最原始、最龐大的一維音訊波形記錄方式。
STFTShort-Time Fourier Transform短時傅立葉轉換,將聲音從隨時間變化的單一振幅,變為兼具頻率與能量的數學分析方法。
梅爾頻譜Mel-Spectrogram大模型處理聲音的基礎特徵:一種經過對數與人類非線性聽覺偏好調整後的高價值二維音訊圖譜。
神經編解碼器Neural Codec依靠極其硬核的變分自編碼殘差技術,將超大尺寸連續聲波高度壓縮轉化成離散標號(Token)的 AI 元件。
Vocoder聲碼器「逆向翻譯官」:負責將二維的梅爾頻譜圖重新物理渲染回能驅動音響發聲的一維音訊波形。
Speaking Embeddings說話人特徵向量將特定人員的專屬嗓音音色固定下來的極高維度且不可變的數學 ID(如 x-vector)。
Flow Matching流匹配將常態分佈轉化為經驗資料分佈的一種無需昂貴微分隨機計算,而是沿常微分方程建立一條常態直線平滑生成路徑的前沿 AI 推斷過程。