| Crates.io | rs-voice-toolkit-stt |
| lib.rs | rs-voice-toolkit-stt |
| version | 0.16.0 |
| created_at | 2025-08-22 03:29:35.152896+00 |
| updated_at | 2025-09-02 06:39:09.946613+00 |
| description | A Rust library for speech-to-text processing using Whisper |
| homepage | https://github.com/soddygo/rs-voice-toolkit |
| repository | https://github.com/soddygo/rs-voice-toolkit |
| max_upload_size | |
| id | 1805853 |
| size | 182,044 |
Version: 0.15.0 - 统一的语音处理工具包
一个功能强大的 Rust 语音处理工具库,提供统一的 API 接口,支持语音转文本 (STT)、文本转语音 (TTS) 和音频处理功能。基于 OpenAI Whisper 模型提供高质量的语音识别,支持多种音频格式转换和实时语音处理。
在 Cargo.toml 中添加依赖:
[dependencies]
voice-toolkit = { version = "0.15.0", features = ["stt", "tts", "audio"] }
brew install ffmpegsudo apt-get install ffmpeguse voice_toolkit::transcribe_file_unified;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let model_path = "models/ggml-base.bin";
let audio_path = "audio/hello.wav";
let result = transcribe_file_unified(model_path, audio_path).await?;
println!("转录结果: {}", result.text);
println!("处理时间: {:?}", result.processing_time);
Ok(())
}
use voice_toolkit::audio;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let input_path = "audio/input.mp3";
let output_path = "audio/output.wav";
// 将 MP3 转换为 Whisper 兼容的 WAV 格式
audio::convert_to_whisper_format(input_path, output_path).await?;
println!("转换完成: {}", output_path);
Ok(())
}
use voice_toolkit::tts;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let text = "你好,世界!欢迎使用语音工具库。";
let output_path = "output/hello.wav";
// 使用 Index-TTS 生成语音
tts::synthesize_text(text, output_path, None).await?;
println!("语音合成完成: {}", output_path);
Ok(())
}
工具包支持通过 Cargo 特性标志来选择功能:
[dependencies]
# 基本功能(STT + 音频处理)
voice-toolkit = { version = "0.15.0", features = ["stt", "audio"] }
# 完整功能(STT + TTS + 音频处理)
voice-toolkit = { version = "0.15.0", features = ["stt", "tts", "audio"] }
# 带流式处理
voice-toolkit = { version = "0.15.0", features = ["stt", "audio", "streaming"] }
# 带GPU加速
voice-toolkit = { version = "0.15.0", features = ["stt", "audio", "cuda"] }
stt: 语音转文本功能(默认启用)tts: 文本转语音功能audio: 音频处理工具(默认启用)streaming: 实时流式转录(需要 stt)cuda: CUDA GPU 加速(需要 stt)vulkan: Vulkan GPU 加速(需要 stt)metal: Metal GPU 加速(需要 stt)默认特性集包括 stt 和 audio,提供全面的语音处理能力。
macOS:
brew install ffmpeg
Ubuntu/Debian:
sudo apt-get update
sudo apt-get install ffmpeg
Windows: 使用 vcpkg 安装 FFmpeg,参考 ez-ffmpeg 仓库
从 Hugging Face 下载 Whisper 模型文件:
# 下载 tiny 模型(快速,适合测试)
wget https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.bin
# 下载 base 模型(平衡性能和准确度)
wget https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.bin
# 下载 large 模型(最高准确度)
wget https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large.bin
# 克隆仓库
git clone https://github.com/soddygo/rs-voice-toolkit.git
cd rs-voice-toolkit
# 构建所有组件
cargo build --release
# 运行测试
cargo test
# 生成文档
cargo doc --no-deps
# 运行文件转录示例
cargo run -p stt --example transcribe_file -- models/ggml-base.bin samples/hello.wav
# 运行带 VAD 的转录
cargo run -p stt --example transcribe_with_vad -- models/ggml-base.bin samples/hello.wav
# 启用 streaming 特性并运行流式转录
cargo run -p stt --features streaming --example streaming_transcribe -- models/ggml-base.bin samples/hello.wav
# 运行性能基准测试
cargo run -p stt --example bench_transcribe -- models/ggml-base.bin samples/hello.wav 3
# 运行文本转语音示例
cargo run -p tts --example synthesize -- "你好,世界" out.wav
# 运行综合示例
cargo run --example usage_examples
# 运行流式转录示例(需要 streaming 特性)
cargo run --example streaming_examples --features streaming
rs-voice-toolkit/
├── voice-toolkit/ # 统一接口库
│ ├── src/
│ │ ├── lib.rs # 主要导出和文档
│ │ └── error.rs # 统一错误处理
│ └── examples/
│ ├── usage_examples.rs # 综合使用示例
│ └── streaming_examples.rs # 流式转录示例
├── stt/ # 语音转文本 (rs-voice-toolkit-stt)
│ ├── src/
│ │ ├── lib.rs # STT 主要接口
│ │ ├── whisper.rs # Whisper 模型封装
│ │ ├── streaming.rs # 流式转录
│ │ ├── vad.rs # 语音活动检测
│ │ └── error.rs # STT 错误处理
│ └── examples/
│ ├── transcribe_file.rs # 文件转录示例
│ ├── streaming_transcribe.rs # 流式转录示例
│ ├── transcribe_with_vad.rs # 带 VAD 的转录
│ └── bench_transcribe.rs # 性能基准测试
├── audio/ # 音频处理 (rs-voice-toolkit-audio)
│ ├── src/
│ │ ├── lib.rs # 音频处理接口
│ │ ├── converter.rs # 格式转换
│ │ ├── resampler.rs # 音频重采样
│ │ └── utils.rs # 工具函数
└── tts/ # 文本转语音 (rs-voice-toolkit-tts)
├── src/
│ ├── lib.rs # TTS 主要接口
│ ├── engine.rs # TTS 引擎封装
│ └── error.rs # TTS 错误处理
└── examples/
└── synthesize.rs # 语音合成示例
Voice Toolkit 使用统一的错误处理机制,所有函数都返回 Result<T, Error> 类型:
use voice_toolkit::{Error, Result};
fn process_audio() -> Result<()> {
// 处理逻辑
Ok(())
}
fn main() {
match process_audio() {
Ok(_) => println!("处理成功"),
Err(Error::Audio(e)) => println!("音频处理错误: {}", e),
Err(Error::Stt(e)) => println!("语音识别错误: {}", e),
Err(Error::Tts(e)) => println!("语音合成错误: {}", e),
Err(Error::Io(e)) => println!("IO错误: {}", e),
Err(Error::Other(e)) => println!("其他错误: {}", e),
}
}
cuda: NVIDIA GPU 加速vulkan: 跨平台 GPU 加速metal: Apple Silicon GPU 加速# 运行所有测试
cargo test
# 运行特定模块的测试
cargo test -p stt
cargo test -p audio
cargo test -p tts
# 运行集成测试
cargo test --test integration_tests
项目包含测试用的音频文件和模型:
# 下载测试数据
./fixtures/get-fixtures.sh
# 运行测试示例
cargo run -p stt --example transcribe_file -- fixtures/models/ggml-tiny.bin fixtures/audio/jfk.wav
本项目采用 MIT 或 Apache 2.0 双许可证。详情请参阅:
欢迎贡献代码!请遵循以下步骤:
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)如果你在使用过程中遇到问题,请:
在配备 Apple M1 Pro 的 MacBook Pro 上的性能数据:
| 模型 | 音频长度 | 处理时间 | RTF | 内存使用 |
|---|---|---|---|---|
| tiny | 10s | 0.8s | 0.08 | 200MB |
| base | 10s | 2.1s | 0.21 | 400MB |
| small | 10s | 6.2s | 0.62 | 800MB |
| medium | 10s | 18.5s | 1.85 | 1.5GB |
| large | 10s | 42.3s | 4.23 | 2.8GB |
RTF (Real-Time Factor) = 处理时间 / 音频时长,值小于 1 表示能实时处理