|
| 1 | +use crate::{get_default_provider, utils::cstring_from_str}; |
| 2 | +use eyre::{bail, Result}; |
| 3 | +use std::mem; |
| 4 | + |
| 5 | +#[derive(Debug)] |
| 6 | +pub struct DolphinRecognizer { |
| 7 | + recognizer: *const sherpa_rs_sys::SherpaOnnxOfflineRecognizer, |
| 8 | +} |
| 9 | + |
| 10 | +pub type DolphinRecognizerResult = super::OfflineRecognizerResult; |
| 11 | + |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub struct DolphinConfig { |
| 14 | + pub model: String, |
| 15 | + pub tokens: String, |
| 16 | + pub decoding_method: String, |
| 17 | + |
| 18 | + pub provider: Option<String>, |
| 19 | + pub num_threads: Option<i32>, |
| 20 | + pub debug: bool, |
| 21 | +} |
| 22 | + |
| 23 | +impl Default for DolphinConfig { |
| 24 | + fn default() -> Self { |
| 25 | + Self { |
| 26 | + model: String::new(), |
| 27 | + tokens: String::new(), |
| 28 | + decoding_method: String::from("greedy_search"), |
| 29 | + debug: false, |
| 30 | + provider: None, |
| 31 | + num_threads: Some(1), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl DolphinRecognizer { |
| 37 | + pub fn new(config: DolphinConfig) -> Result<Self> { |
| 38 | + let debug = config.debug.into(); |
| 39 | + let provider = config.provider.unwrap_or(get_default_provider()); |
| 40 | + |
| 41 | + let provider_ptr = cstring_from_str(&provider); |
| 42 | + let num_threads = config.num_threads.unwrap_or(2); |
| 43 | + let model_ptr = cstring_from_str(&config.model); |
| 44 | + let tokens_ptr = cstring_from_str(&config.tokens); |
| 45 | + let decoding_method_ptr = cstring_from_str(&config.decoding_method); |
| 46 | + |
| 47 | + let model_config = unsafe { |
| 48 | + sherpa_rs_sys::SherpaOnnxOfflineModelConfig { |
| 49 | + debug, |
| 50 | + num_threads, |
| 51 | + provider: provider_ptr.as_ptr(), |
| 52 | + dolphin: sherpa_rs_sys::SherpaOnnxOfflineDolphinModelConfig { |
| 53 | + model: model_ptr.as_ptr(), |
| 54 | + }, |
| 55 | + tokens: tokens_ptr.as_ptr(), |
| 56 | + |
| 57 | + // Zeros |
| 58 | + nemo_ctc: mem::zeroed::<_>(), |
| 59 | + paraformer: mem::zeroed::<_>(), |
| 60 | + tdnn: mem::zeroed::<_>(), |
| 61 | + telespeech_ctc: mem::zeroed::<_>(), |
| 62 | + fire_red_asr: mem::zeroed::<_>(), |
| 63 | + transducer: mem::zeroed::<_>(), |
| 64 | + whisper: mem::zeroed::<_>(), |
| 65 | + sense_voice: mem::zeroed::<_>(), |
| 66 | + moonshine: mem::zeroed::<_>(), |
| 67 | + bpe_vocab: mem::zeroed::<_>(), |
| 68 | + model_type: mem::zeroed::<_>(), |
| 69 | + modeling_unit: mem::zeroed::<_>(), |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + let config = unsafe { |
| 74 | + sherpa_rs_sys::SherpaOnnxOfflineRecognizerConfig { |
| 75 | + decoding_method: decoding_method_ptr.as_ptr(), |
| 76 | + model_config, |
| 77 | + feat_config: sherpa_rs_sys::SherpaOnnxFeatureConfig { |
| 78 | + sample_rate: 16000, |
| 79 | + feature_dim: 80, |
| 80 | + }, |
| 81 | + hotwords_file: mem::zeroed::<_>(), |
| 82 | + hotwords_score: mem::zeroed::<_>(), |
| 83 | + lm_config: mem::zeroed::<_>(), |
| 84 | + max_active_paths: mem::zeroed::<_>(), |
| 85 | + rule_fars: mem::zeroed::<_>(), |
| 86 | + rule_fsts: mem::zeroed::<_>(), |
| 87 | + blank_penalty: mem::zeroed::<_>(), |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + let recognizer = unsafe { sherpa_rs_sys::SherpaOnnxCreateOfflineRecognizer(&config) }; |
| 92 | + |
| 93 | + if recognizer.is_null() { |
| 94 | + bail!("Failed to create recognizer"); |
| 95 | + } |
| 96 | + |
| 97 | + Ok(Self { recognizer }) |
| 98 | + } |
| 99 | + |
| 100 | + pub fn transcribe(&mut self, sample_rate: u32, samples: &[f32]) -> DolphinRecognizerResult { |
| 101 | + unsafe { |
| 102 | + let stream = sherpa_rs_sys::SherpaOnnxCreateOfflineStream(self.recognizer); |
| 103 | + sherpa_rs_sys::SherpaOnnxAcceptWaveformOffline( |
| 104 | + stream, |
| 105 | + sample_rate as i32, |
| 106 | + samples.as_ptr(), |
| 107 | + samples.len().try_into().unwrap(), |
| 108 | + ); |
| 109 | + sherpa_rs_sys::SherpaOnnxDecodeOfflineStream(self.recognizer, stream); |
| 110 | + let result_ptr = sherpa_rs_sys::SherpaOnnxGetOfflineStreamResult(stream); |
| 111 | + let raw_result = result_ptr.read(); |
| 112 | + let result = DolphinRecognizerResult::new(&raw_result); |
| 113 | + // Free |
| 114 | + sherpa_rs_sys::SherpaOnnxDestroyOfflineRecognizerResult(result_ptr); |
| 115 | + sherpa_rs_sys::SherpaOnnxDestroyOfflineStream(stream); |
| 116 | + result |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +unsafe impl Send for DolphinRecognizer {} |
| 122 | +unsafe impl Sync for DolphinRecognizer {} |
| 123 | + |
| 124 | +impl Drop for DolphinRecognizer { |
| 125 | + fn drop(&mut self) { |
| 126 | + unsafe { |
| 127 | + sherpa_rs_sys::SherpaOnnxDestroyOfflineRecognizer(self.recognizer); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments