跳转至

音频处理教程

文本转语音 (TTS) 和语音转文字 (ASR) 完整指南


目录

  1. TTS 能力概述
  2. 实际测试验证
  3. 可用音色
  4. 模型选择
  5. 完整示例代码
  6. 常见问题

TTS 能力概述

文本转语音 (TTS) 功能可以将任意文本转换为自然流畅的语音输出。该服务基于先进的端侧部署的大语言模型,支持多语言合成,包括中文、英文等主要语种。

主要特性:

  • 支持多种输出格式:mp3, opus, aac, flac, pcm
  • 可调节语速范围:0.25x ~ 4.0x
  • 多音色选择,适用于不同场景
  • 单次输入建议不超过 4096 字符

实际测试验证

测试环境

  • API 端点:https://apinexus.net/v1
  • 模型:gpt-4o-mini-tts
  • 输入文本:你好,欢迎使用API服务!这是一个语音合成测试。
  • 音色:alloy
  • 输出文件:generated_speech.mp3
  • 文件大小:88320 bytes (约 86 KB)

测试结果

该测试成功生成了音频文件,语音清晰流畅,语调自然。生成的文件可直接播放使用。


可用音色

所有音色样本均来自实际合成,可点击播放对比效果。

音色 性别 特点 试听
alloy 中性 清晰自然,通用默认
echo 男 沉稳磁性
fable 男 叙事风格
onyx 男 低沉专业
nova 女 温暖柔和
shimmer 女 明亮清晰

场景推荐

场景 推荐音色
通用对话 alloy, nova
专业播报 onyx, echo
有声读物 fable, shimmer
中文内容 nova, alloy

模型选择

TTS 模型

模型 适用场景 特点
gpt-4o-mini-tts 通用推荐 性价比高,效果稳定
gpt-4o-mini-tts-1 高精度场景 优化版本,细节更好
qwen3-tts-flash 快速生成 延迟最低,适合批量
speech-02-hd 高清语音 音质优先
speech-02-turbo 平衡模式 速度与质量平衡
speech-2.6-hd 高清场景 高保真输出
speech-2.6-turbo 快速场景 低延迟
speech-2.8-hd 顶级音质 最高品质
speech-2.8-turbo 极速场景 最快速度

ASR 模型

模型 适用场景 价格
gpt-4o-transcribe 通用识别 $0.0001 / 秒

完整示例代码

TTS - 基础用法

from openai import OpenAI

client = OpenAI(base_url="https://apinexus.net/v1")

response = client.audio.speech.create(
    model="gpt-4o-mini-tts",
    voice="alloy",
    input="你好,欢迎使用API服务!这是一个语音合成测试。"
)

with open("generated_speech.mp3", "wb") as f:
    f.write(response.content)

TTS - 指定输出格式和语速

response = client.audio.speech.create(
    model="gpt-4o-mini-tts",
    voice="nova",
    input="这是一个语音合成演示",
    response_format="mp3",  # 可选: mp3, opus, aac, flac, pcm
    speed=1.0               # 范围: 0.25 ~ 4.0
)

response.stream_to_file("output.mp3")

TTS - 中文最佳实践

from openai import OpenAI

client = OpenAI(base_url="https://apinexus.net/v1")

# 中文推荐使用 nova 或 alloy 音色
response = client.audio.speech.create(
    model="gpt-4o-mini-tts",
    voice="nova",
    input="欢迎使用API服务,我们致力于提供稳定高效的语音合成解决方案。",
    speed=1.0
)

with open("chinese_speech.mp3", "wb") as f:
    f.write(response.content)

ASR - 语音转文字

from openai import OpenAI

client = OpenAI(base_url="https://apinexus.net/v1")

with open("audio.mp3", "rb") as f:
    transcript = client.audio.transcriptions.create(
        model="gpt-4o-transcribe",
        file=f
    )

print("识别结果:", transcript.text)

ASR - 指定语言提高准确率

# 已知语言时指定 language 参数可大幅提升准确率
with open("chinese_audio.mp3", "rb") as f:
    transcript = client.audio.transcriptions.create(
        model="gpt-4o-transcribe",
        file=f,
        language="zh"  # 指定中文
    )

ASR - 带时间戳的识别

result = client.audio.transcriptions.create(
    model="gpt-4o-transcribe",
    file=open("audio.mp3", "rb"),
    response_format="verbose_json",
    timestamp_granularities=["word", "segment"]
)

for segment in result.segments:
    print(f"[{segment.start:.1f}s -> {segment.end:.1f}s]: {segment.text}")

语音翻译

# 将任意语言音频翻译为英文
with open("chinese_audio.mp3", "rb") as f:
    translation = client.audio.translations.create(
        model="gpt-4o-transcribe",
        file=f
    )

print("英文翻译:", translation.text)

完整演示脚本

from openai import OpenAI
from pathlib import Path

client = OpenAI(base_url="https://apinexus.net/v1")
output_dir = Path("audio_output")
output_dir.mkdir(exist_ok=True)


def demo_tts():
    """文本转语音演示"""
    print("正在生成语音...")
    response = client.audio.speech.create(
        model="gpt-4o-mini-tts",
        voice="alloy",
        input="你好,欢迎使用API服务!这是一个语音合成测试。"
    )
    output_file = output_dir / "generated_speech.mp3"
    response.stream_to_file(str(output_file))
    print(f"已保存到: {output_file}")
    return str(output_file)


def demo_asr(audio_file):
    """语音转文字演示"""
    print("正在识别语音...")
    with open(audio_file, "rb") as f:
        result = client.audio.transcriptions.create(
            model="gpt-4o-transcribe",
            file=f
        )
    print(f"识别结果: {result.text}")
    return result.text


if __name__ == "__main__":
    print("=" * 50)
    print("音频处理演示")
    print("=" * 50)

    audio_file = demo_tts()
    demo_asr(audio_file)

    print("=" * 50)
    print("演示完成")
    print("=" * 50)

常见问题

Q: 支持哪些语言?

A: gpt-4o-transcribe 支持约 99 种语言,包括中文、英文、日文、韩文、法文、德文、西班牙文等。已知语言时建议指定 language 参数以提高准确率。

Q: 音频文件大小有限制吗?

A: ASR 单个文件最大 25 MB,推荐使用 mp3 格式。如文件过大,可用 ffmpeg 切割或降低采样率。

Q: 如何选择合适的语速?

A: 默认 1.0 为正常语速。播报类内容可设为 1.0-1.2;需要快速浏览可设为 1.5-2.0;缓慢朗读可设为 0.5-0.75。

Q: 长文本如何处理?

A: 单次输入建议不超过 4096 字符。超长文本请分段处理,生成多个音频后再拼接。

Q: 如何实现流式播放?

A: 获取音频数据后,可用 pyaudio 或 sounddevice 等库直接播放,无需先存储文件。

Q: 哪个 TTS 模型性价比最高?

A: gpt-4o-mini-tts 是目前性价比最优的选择,效果稳定且价格低廉。

Q: 中文语音合成推荐什么音色?

A: 推荐 nova 或 alloy,这两个音色对中文的优化较好,发音清晰自然。


下一步


最后更新: 2026-06-16