
whisper.rn API完全参考从基础调用到自定义音频流处理【免费下载链接】whisper.rnReact Native binding of whisper.cpp.项目地址: https://gitcode.com/gh_mirrors/wh/whisper.rnwhisper.rn是一个强大的React Native绑定库基于whisper.cpp构建为移动应用提供高效的语音识别能力。本指南将帮助你全面掌握whisper.rn的API使用从基础的语音转文本功能到高级的实时音频流处理让你轻松构建专业的语音识别应用。核心API概览 whisper.rn提供了三个主要上下文类覆盖不同的语音处理需求WhisperContext核心语音识别WhisperContext是进行语音转文本的核心类支持文件转录和实时流处理。通过initWhisper函数初始化export async function initWhisper({ modelPath, language, device, ...options }: ContextOptions): PromiseWhisperContext主要功能包括transcribeFile: 转录音频文件startStreaming: 开始实时音频流转录stopStreaming: 停止实时转录ParakeetContext轻量级语音处理ParakeetContext提供更轻量级的语音处理能力适合资源受限的设备export async function initParakeet({ modelPath, language, ...options }: ParakeetContextOptions): PromiseParakeetContextWhisperVadContext语音活动检测WhisperVadContext专注于语音活动检测(VAD)用于识别音频中的语音片段export async function initWhisperVad({ modelPath, ...options }: VadContextOptions): PromiseWhisperVadContext基础使用指南 安装与初始化首先克隆仓库并安装依赖git clone https://gitcode.com/gh_mirrors/wh/whisper.rn cd whisper.rn yarn install初始化Whisper上下文需要模型文件你可以通过官方渠道获取预训练模型。文件转录示例使用WhisperContext转录本地音频文件const context await initWhisper({ modelPath: /path/to/model, language: en, }); const result await context.transcribeFile({ path: /path/to/audio.wav, wordTimestamps: true, }); console.log(result.text);实时转录高级功能 ⚡RealtimeTranscriber实时音频流处理RealtimeTranscriber类提供完整的实时转录解决方案位于src/realtime-transcription/RealtimeTranscriber.ts。它结合了音频流处理、VAD检测和语音识别export class RealtimeTranscriber { constructor( private dependencies: RealtimeTranscriberDependencies, private options: RealtimeOptions, private callbacks: RealtimeTranscriberCallbacks, ) {} start(): void; stop(): Promisevoid; // 更多方法... }音频流适配器whisper.rn提供多种音频流适配器满足不同场景需求AudioPcmStreamAdapter: 适配PCM音频流SimulateFileAudioStreamAdapter: 模拟文件音频流用于测试JestAudioStreamAdapter: Jest测试专用适配器这些适配器实现了AudioStreamInterface接口确保统一的音频处理方式。配置选项详解 ⚙️上下文配置ContextOptions接口定义了Whisper上下文的主要配置参数export type ContextOptions { modelPath: string; language?: string; device?: auto | cpu | gpu; threads?: number; // 更多配置... };关键参数说明modelPath: 模型文件路径必需language: 目标语言代码如en、zhdevice: 计算设备选择threads: 线程数影响性能和资源占用实时转录配置RealtimeOptions接口控制实时转录行为export interface RealtimeOptions { sampleRate: number; frameDuration: number; vad: { enabled: boolean; threshold?: number; pre?: number; post?: number; }; // 更多配置... }实用工具类 ️WavFileReader与WavFileWriter处理WAV音频文件的工具类位于src/utils/WavFileReader.ts和src/utils/WavFileWriter.tsexport class WavFileReader { constructor(private fs: WavFileReaderFs, private path: string) {} async readHeader(): PromiseWavFileHeader; async readData(): PromiseUint8Array; } export class WavFileWriter { constructor( private fs: WavFileWriterFs, private path: string, private config: WavFileConfig, ) {} async write(data: Uint8Array): Promisevoid; async close(): Promisevoid; }环形缓冲区RingBuffer和RingBufferVad类提供高效的音频数据缓冲机制特别适合实时处理场景。错误处理与资源管理 资源释放使用完上下文后应及时释放资源// 释放单个上下文 await context.release(); // 释放所有上下文 await releaseAllWhisper(); await releaseAllParakeet(); await releaseAllWhisperVad();日志管理通过以下方法控制原生日志输出// 切换日志启用状态 await toggleNativeLog(enabled); // 添加日志监听器 addNativeLogListener((log) { console.log(Native log:, log); });实际应用示例 构建实时语音助手结合RealtimeTranscriber和适当的音频流适配器可以构建实时语音助手const transcriber new RealtimeTranscriber( { whisperContext: await initWhisper({ modelPath }), audioStream: new AudioPcmStreamAdapter(), // 其他依赖... }, { sampleRate: 16000, frameDuration: 20, vad: { enabled: true }, }, { onTranscribe: (event) { console.log(Transcript:, event.text); // 处理转录结果... }, // 其他回调... }, ); transcriber.start(); // 使用完毕后停止 // transcriber.stop();性能优化建议 模型选择根据应用需求选择合适大小的模型平衡速度和准确性线程配置合理设置threads参数避免过多线程导致性能下降设备选择在支持的设备上使用GPU加速(device: gpu)VAD配置优化VAD参数减少无效转录提高响应速度总结whisper.rn提供了从基础语音识别到高级实时音频处理的完整API通过本指南你已经了解了主要类、方法和配置选项。无论是构建简单的语音转文本应用还是开发复杂的实时语音交互系统whisper.rn都能满足你的需求。要深入了解更多细节请参考项目文档docs/目录下的详细说明以及src/目录中的源代码实现。祝你在语音识别应用开发中取得成功【免费下载链接】whisper.rnReact Native binding of whisper.cpp.项目地址: https://gitcode.com/gh_mirrors/wh/whisper.rn创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考