CosyVoice3 TTS
Local text-to-speech using Alibaba's CosyVoice3 on macOS Apple Silicon.
Overview
CosyVoice3 is an advanced TTS system based on large language models, supporting:
- - 9 languages: Chinese, English, Japanese, Korean, German, Spanish, French, Italian, Russian
- 18+ Chinese dialects: Cantonese, Sichuan, Dongbei, Shanghai, etc.
- Zero-shot voice cloning: Clone any voice from 3-10 seconds of audio
- Cross-lingual synthesis: Speak Chinese with English voice or vice versa
- Fine-grained control: Emotions, speed, volume via text tags
Prerequisites
- - macOS with Apple Silicon (M1/M2/M3)
- Python 3.10
- Conda installed
- ~5GB disk space for models
Installation
Run the installation script:
CODEBLOCK0
This will:
- 1. Create conda environment INLINECODE0
- Install PyTorch (CPU version for Apple Silicon)
- Install CosyVoice dependencies
- Download Fun-CosyVoice3-0.5B model (~2GB)
Usage
Quick Start - Basic TTS
重要:CosyVoice3 需要在参考文本中添加 <|endofprompt|> 标记!
CODEBLOCK1
Using the TTS Script
Generate speech from text:
CODEBLOCK2
Available Assets
Reference audio files in cosyvoice3-repo/asset/:
- -
zero_shot_prompt.wav - Default Chinese female voice - INLINECODE4 - English prompt for cross-lingual
Advanced Features
Voice Cloning
Clone a voice from 3-10 seconds of reference audio:
CODEBLOCK3
Fine-Grained Control
Control prosody with special tags:
CODEBLOCK4
Dialect Support
Use instruct mode for dialects:
CODEBLOCK5
Troubleshooting
Model not found
If you get "model not found" errors, download models manually:
CODEBLOCK6
Memory issues
For long text, split into sentences:
CODEBLOCK7
Audio format
Reference audio requirements:
- - Format: WAV, MP3
- Sample rate: 16kHz+ (automatically resampled)
- Duration: 3-10 seconds optimal
- Content: Clear speech, minimal background noise
Resources
Scripts
- -
install.sh - Installation script for macOS - INLINECODE6 - Main TTS script with CLI interface
- INLINECODE7 - Download pretrained models
References
Model Files
Located in cosyvoice3-repo/pretrained_models/:
- -
Fun-CosyVoice3-0.5B/ - Main model (recommended) - INLINECODE10 - Previous version
- INLINECODE11 - Lighter model
- INLINECODE12 - SFT version
- INLINECODE13 - Instruct version
Notes
- - First inference takes ~30 seconds (model warmup)
- Subsequent inferences are faster
- Apple Silicon uses CPU mode (no CUDA)
- RTF (real-time factor) ~0.3-0.5 on M-series chips
- Model files are cached locally after first download
CosyVoice3 TTS
在 macOS Apple Silicon 上使用阿里巴巴 CosyVoice3 的本地文本转语音系统。
概述
CosyVoice3 是一个基于大语言模型的先进 TTS 系统,支持:
- - 9 种语言:中文、英语、日语、韩语、德语、西班牙语、法语、意大利语、俄语
- 18 种以上中文方言:粤语、四川话、东北话、上海话等
- 零样本语音克隆:从 3-10 秒音频中克隆任意声音
- 跨语言合成:用英语声音说中文,反之亦然
- 细粒度控制:通过文本标签控制情感、语速、音量
前提条件
- - 配备 Apple Silicon (M1/M2/M3) 的 macOS
- Python 3.10
- 已安装 Conda
- 约 5GB 磁盘空间用于模型
安装
运行安装脚本:
bash
cd /Users/lhz/.openclaw/workspace/skills/cosyvoice3/scripts
bash install.sh
此操作将:
- 1. 创建 conda 环境 cosyvoice
- 安装 PyTorch(Apple Silicon 的 CPU 版本)
- 安装 CosyVoice 依赖项
- 下载 Fun-CosyVoice3-0.5B 模型(约 2GB)
使用方法
快速开始 - 基础 TTS
重要:CosyVoice3 需要在参考文本中添加 <|endofprompt|> 标记!
bash
cd /Users/lhz/.openclaw/workspace/cosyvoice3-repo
export PATH=$HOME/miniconda3/bin:$PATH
conda activate cosyvoice
python -c
import sys
sys.path.append(third_party/Matcha-TTS)
from cosyvoice.cli.cosyvoice import AutoModel
import torchaudio
cosyvoice = AutoModel(modeldir=pretrainedmodels/Fun-CosyVoice3-0.5B)
for i, j in enumerate(cosyvoice.inferencezeroshot(
你好,这是CosyVoice3语音合成测试。,
希望你以后能够做的比我还好呦。<|endofprompt|>, # 注意这个标记!
asset/zeroshotprompt.wav
)):
torchaudio.save(output.wav, j[ttsspeech], cosyvoice.samplerate)
print(已生成: output.wav)
使用 TTS 脚本
从文本生成语音:
bash
cd /Users/lhz/.openclaw/workspace/skills/cosyvoice3/scripts
conda activate cosyvoice
使用默认语音的基础 TTS
python tts.py 你好,这是一个测试。
使用自定义参考音频进行语音克隆
python tts.py 你好,这是克隆的声音。 --reference /path/to/reference.wav
跨语言(英语文本,中文语音)
python tts.py Hello, this is cross-lingual synthesis. --reference asset/zero
shotprompt.wav --lang en
带语速控制
python tts.py 这是一段快速的语音。 --speed 1.5
保存到指定路径
python tts.py 你好。 --output ~/Desktop/greeting.wav
可用资源
cosyvoice3-repo/asset/ 中的参考音频文件:
- - zeroshotprompt.wav - 默认中文女声
- crosslingualprompt.wav - 用于跨语言的英语提示
高级功能
语音克隆
从 3-10 秒的参考音频中克隆声音:
python
from cosyvoice.cli.cosyvoice import AutoModel
import torchaudio
cosyvoice = AutoModel(modeldir=pretrainedmodels/Fun-CosyVoice3-0.5B)
克隆声音并生成
for i, j in enumerate(cosyvoice.inference
zeroshot(
这是克隆后的声音在说话。,
参考文本转录,
/path/to/reference.wav
)):
torchaudio.save(cloned.wav, j[tts
speech], cosyvoice.samplerate)
细粒度控制
使用特殊标签控制韵律:
python
添加笑声
他突然[laughter]笑了起来[laughter]。
添加呼吸声
他说完这句话[breath],深吸一口气。
强烈强调
这是
非常重要的。
组合使用
在面对挑战时,他展现了非凡的
勇气与
智慧[breath]。
方言支持
使用指令模式处理方言:
python
cosyvoice = AutoModel(modeldir=pretrainedmodels/CosyVoice-300M-Instruct)
for i, j in enumerate(cosyvoice.inference_instruct(
你好,这是测试语音。,
中文男,
用四川话说这句话<|endofprompt|>
)):
torchaudio.save(sichuan.wav, j[ttsspeech], cosyvoice.samplerate)
故障排除
模型未找到
如果遇到模型未找到错误,请手动下载模型:
bash
cd /Users/lhz/.openclaw/workspace/cosyvoice3-repo
export PATH=$HOME/miniconda3/bin:$PATH
conda activate cosyvoice
python -c
from modelscope import snapshot_download
snapshotdownload(FunAudioLLM/Fun-CosyVoice3-0.5B-2512, localdir=pretrained_models/Fun-CosyVoice3-0.5B)
内存问题
对于长文本,请拆分为句子:
python
text = 很长的文本...
sentences = text.split(。)
for sent in sentences:
if sent.strip():
# 处理每个句子
音频格式
参考音频要求:
- - 格式:WAV、MP3
- 采样率:16kHz 以上(自动重采样)
- 时长:最佳 3-10 秒
- 内容:清晰的语音,最小化背景噪音
资源
脚本
- - install.sh - macOS 安装脚本
- tts.py - 带 CLI 界面的主 TTS 脚本
- download_models.py - 下载预训练模型
参考
模型文件
位于 cosyvoice3-repo/pretrained_models/:
- - Fun-CosyVoice3-0.5B/ - 主模型(推荐)
- CosyVoice2-0.5B/ - 先前版本
- CosyVoice-300M/ - 轻量模型
- CosyVoice-300M-SFT/ - SFT 版本
- CosyVoice-300M-Instruct/ - 指令版本
注意事项
- - 首次推理约需 30 秒(模型预热)
- 后续推理速度更快
- Apple Silicon 使用 CPU 模式(无 CUDA)
- M 系列芯片上的 RTF(实时因子)约 0.3-0.5
- 首次下载后模型文件会本地缓存