AI 文本转语音 2026:OpenAI TTS、ElevenLabs 与语音克隆
使用自然语音 TTS 和自定义语音克隆构建语音 AI 应用
AI 文本转语音 2026:OpenAI TTS、ElevenLabs 与语音克隆
使用自然语音 TTS 和自定义语音克隆构建语音 AI 应用
完整的 TTS API 对比与教程。OpenAI TTS 用于生产环境,ElevenLabs 用于语音克隆,流式 TTS 用于聊天机器人,以及构建完整的语音 AI 助手。
文本转语音 AI 2026:OpenAI TTS、ElevenLabs 与语音克隆
现代 AI TTS 能够生成与人类语音难以区分的自然语音。
API 对比 2026
OpenAI TTS API
python
from openai import OpenAI
from pathlib import Pathclient = OpenAI()
生成语音
speech = client.audio.speech.create(
model='tts-1-hd', # tts-1 (快速) 或 tts-1-hd (更高质量)
voice='alloy', # alloy, echo, fable, onyx, nova, shimmer
input='欢迎来到 AI Skill Navigator。本教程涵盖文本转语音 API。'
)speech.stream_to_file('output.mp3')
实时流式传输到扬声器
import pyaudiop = pyaudio.PyAudio()
stream = p.open(format=8, channels=1, rate=24000, output=True)
with client.audio.speech.with_streaming_response.create(
model='tts-1',
voice='nova',
input='这段文本在生成的同时实时流式传输到扬声器。'
) as response:
for chunk in response.iter_bytes(1024):
stream.write(chunk)
stream.close()
p.terminate()
ElevenLabs API (最佳质量)
python
from elevenlabs import ElevenLabs, saveclient = ElevenLabs(api_key='your-api-key')
文本转语音
audio = client.text_to_speech.convert(
voice_id='21m00Tcm4TlvDq8ikWAM', # 语音 ID (Rachel)
text='你好!这是 ElevenLabs 生成的 AI 语音。',
model_id='eleven_multilingual_v2',
voice_settings={
'stability': 0.5,
'similarity_boost': 0.8,
'style': 0.3,
'use_speaker_boost': True
}
)
save(audio, 'elevenlabs_output.mp3')克隆语音
voice = client.voices.clone(
name='我的自定义语音',
description='专业旁白语音',
files=['sample1.mp3', 'sample2.mp3', 'sample3.mp3'], # 1-30 个样本
)
print(f'新语音 ID: {voice.voice_id}')使用克隆的语音
audio = client.text_to_speech.convert(
voice_id=voice.voice_id,
text='这是使用我克隆的语音生成的!'
)
用于聊天机器人的流式 TTS
python
import asyncioasync def speak_stream(text: str):
"""在生成时流式传输 TTS 音频,实现低延迟的聊天机器人响应。"""
async with client.audio.speech.with_streaming_response.create(
model='tts-1',
voice='alloy',
input=text
) as response:
async for chunk in response.aiter_bytes(1024):
yield chunk # 将块传输到音频播放器
在 FastAPI 端点中:
from fastapi import FastAPI
from fastapi.responses import StreamingResponseapp = FastAPI()
@app.post('/speak')
async def speak_endpoint(text: str):
async def generate():
async for chunk in speak_stream(text):
yield chunk
return StreamingResponse(generate(), media_type='audio/mpeg')
构建语音 AI 助手
python
class VoiceAssistant:
def __init__(self):
self.client = OpenAI()
self.history = []
def listen(self) -> str:
# 使用 pyaudio 录制音频
# ...录制代码...
with open('/tmp/recording.wav', 'rb') as f:
return self.client.audio.transcriptions.create(model='whisper-1', file=f).text
def think(self, user_input: str) -> str:
self.history.append({'role': 'user', 'content': user_input})
r = self.client.chat.completions.create(
model='gpt-4o',
messages=[{'role': 'system', 'content': '你是一个有用的语音助手。'}] + self.history
)
response = r.choices[0].message.content
self.history.append({'role': 'assistant', 'content': response})
return response
def speak(self, text: str):
speech = self.client.audio.speech.create(model='tts-1', voice='nova', input=text)
speech.stream_to_file('/tmp/response.mp3')
import subprocess
subprocess.run(['afplay', '/tmp/response.mp3']) # macOS
def run(self):
while True:
print('正在聆听...')
text = self.listen()
print(f'你: {text}')
response = self.think(text)
print(f'助手: {response}')
self.speak(response)
结论
2026 年的 AI TTS 能够为应用提供自然语音。OpenAI TTS 适用于生产 API,ElevenLabs 适用于语音克隆和最高质量,Kokoro 本地运行可实现免费推理。
相关工具
相关教程
集成语音合成 API 实现自定义语音
检测并分割音频流中的语音
使用Whisper转录音频文件、会议和实时语音
掌握 GPT-4o 的多模态特性,包括图像分析、音频转录以及用于交互式应用的全新实时流式 API
使用 ElevenLabs API 进行语音克隆、文本转语音和构建自动化音频内容管道的完整指南
使用 OpenAI Whisper 和 GPT-4o 构建自动会议转录、说话人分离和智能会议摘要