Skip to main content

Crate rs_voice_toolkit_stt

Crate rs_voice_toolkit_stt 

Source
Expand description

§STT (Speech-to-Text) Module - 语音转文本模块

这个模块提供了基于 OpenAI Whisper 模型的高质量语音识别功能, 支持文件转录、实时流式处理、语音活动检测等多种功能。

§主要功能

§核心特性

  • 高精度识别: 基于 Whisper 模型,支持多种语言
  • 文件转录: 支持多种音频格式的批量转录
  • 实时流式处理: 支持音频流的实时转录
  • 语音活动检测: 智能检测语音片段,提高处理效率
  • 多模型支持: 支持 tiny、base、small、medium、large 等不同规模的模型
  • 性能监控: 提供详细的性能指标和基准测试

§支持的音频格式

  • WAV: 原生支持,无需转换
  • MP3: 自动转换为兼容格式
  • FLAC: 自动转换为兼容格式
  • M4A: 自动转换为兼容格式
  • OGG: 自动转换为兼容格式

§快速开始

§基本文件转录

use rs_voice_toolkit_stt::{transcribe_file, WhisperConfig, SttError};
 
#[tokio::main]
async fn main() -> Result<(), SttError> {
    let model_path = "models/ggml-base.bin";
    let audio_path = "audio/hello.wav";
     
    // 基本转录
    let result = transcribe_file(model_path, audio_path).await?;
    println!("转录结果: {}", result.text);
    println!("处理时间: {:?}", result.processing_time);
     
    Ok(())
}

§自定义配置转录

use rs_voice_toolkit_stt::{transcribe_file_with_config, WhisperConfig, SttError};
 
#[tokio::main]
async fn main() -> Result<(), SttError> {
    let model_path = "models/ggml-base.bin";
    let audio_path = "audio/hello.wav";
     
    // 自定义配置
    let config = WhisperConfig::new(model_path)
        .with_language("zh")          // 指定中文
        .with_temperature(0.2)         // 降低温度
        .with_vad(true)               // 启用语音活动检测
        .with_translate(false);        // 禁用翻译
     
    let result = transcribe_file_with_config(model_path, audio_path, Some(config)).await?;
    println!("转录结果: {}", result.text);
     
    Ok(())
}

§流式转录

use rs_voice_toolkit_stt::{StreamingTranscriber, StreamingConfig, SttError};
 
#[tokio::main]
async fn main() -> Result<(), SttError> {
    let model_path = "models/ggml-base.bin";
     
    // 创建流式转录器
    let mut transcriber = StreamingTranscriber::new(model_path).await?;
     
    // 配置参数
    transcriber.set_language("auto")?;
    transcriber.set_task("transcribe")?;
    transcriber.enable_vad(true)?;
     
    // 模拟音频流处理
    let audio_chunks: Vec<Vec<f32>> = vec/*[音频数据块]*/;
     
    for chunk in audio_chunks {
        let segments = transcriber.process_audio(&chunk).await?;
        for segment in segments {
            println!("[{}s-{}s] {}", segment.start_time, segment.end_time, segment.text);
        }
    }
     
    // 获取最终结果
    let final_result = transcriber.finalize().await?;
    println!("最终转录: {}", final_result.text);
     
    Ok(())
}

§模型选择指南

模型大小速度准确度适用场景
tiny~39MB极快一般快速测试、实时应用
base~74MB良好日常应用、平衡性能
small~244MB中等很好高要求应用
medium~769MB较慢优秀专业应用
large~1550MB最佳最高精度要求

§性能优化

§模型加载优化

  • 首次加载模型后保持实例,避免重复加载
  • 对于长期运行的应用,预加载常用模型
  • 使用模型缓存减少启动时间

§音频处理优化

  • 启用 VAD (语音活动检测) 跳过静音部分
  • 预转换音频为 Whisper 兼容格式
  • 批量处理多个文件减少初始化开销

§系统资源优化

  • 启用 GPU 加速 (CUDA/Vulkan/Metal)
  • 调整线程数以优化 CPU 使用率
  • 监控内存使用,避免大文件处理时的内存溢出

§错误处理

模块提供了详细的错误类型,帮助快速定位问题:

use rs_voice_toolkit_stt::{SttError, transcribe_file};
 
match transcribe_file("model.bin", "audio.wav").await {
    Ok(result) => println!("转录成功: {}", result.text),
    Err(SttError::ModelLoadError(e)) => println!("模型加载失败: {}", e),
    Err(SttError::AudioProcessingError(e)) => println!("音频处理失败: {}", e),
    Err(SttError::WhisperError(e)) => println!("Whisper 处理失败: {}", e),
    Err(SttError::IoError(e)) => println!("IO 错误: {}", e),
    Err(e) => println!("其他错误: {}", e),
}

§系统要求

  • 内存:

    • tiny 模型: ~200MB
    • base 模型: ~400MB
    • small 模型: ~800MB
    • medium 模型: ~1.5GB
    • large 模型: ~3GB
  • CPU: 支持多线程处理,推荐 4 核以上

  • GPU: 可选,支持 CUDA/Vulkan/Metal 加速

  • 磁盘: 模型文件存储空间

§注意事项

  • 首次使用需要下载 Whisper 模型文件
  • 建议在使用前验证音频文件格式
  • 长音频文件建议使用流式处理
  • 实时应用建议使用 tiny 或 base 模型

Re-exports§

pub use error::SttError;
pub use error::SttResult;
pub use audio::AudioData;
pub use whisper::transcribe_file;
pub use whisper::transcribe_file_with_config;
pub use whisper::transcribe_file_with_language;
pub use whisper::transcribe_file_with_transcriber;
pub use whisper::TranscriptionResult;
pub use whisper::TranscriptionSegment;
pub use whisper::WhisperConfig;
pub use whisper::WhisperTranscriber;
pub use vad::SimpleVad;

Modules§

audio
音频处理工具模块
error
STT模块的错误处理
vad
语音活动检测 (VAD) 模块
whisper
Whisper 语音转文本实现

Structs§

AudioConfig
音频参数配置

Enums§

AudioFormat
音频格式枚举