From c1a37751ddd6563d7d745e1dd455b540a6bb82a0 Mon Sep 17 00:00:00 2001 From: jiahua Date: Wed, 24 Apr 2024 08:52:57 +0800 Subject: [PATCH 01/12] fix(example): WhisperError::InputOutputLengthMismatch in example --- examples/full_usage/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/full_usage/src/main.rs b/examples/full_usage/src/main.rs index a6d0557..03b5daf 100644 --- a/examples/full_usage/src/main.rs +++ b/examples/full_usage/src/main.rs @@ -43,7 +43,7 @@ fn main() { } let original_samples = parse_wav_file(audio_path); - let mut samples = Vec::with_capacity(original_samples.len()); + let mut samples = vec![0.0f32; original_samples.len()]; whisper_rs::convert_integer_to_float_audio(&original_samples, &mut samples) .expect("failed to convert samples"); From 74e83185bf04952313cdfabefe28dca4d97d981c Mon Sep 17 00:00:00 2001 From: jiahua Date: Thu, 25 Apr 2024 15:30:56 +0800 Subject: [PATCH 02/12] refactor(state): remove lifetime binding from whisper context --- src/whisper_state.rs | 56 ++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/whisper_state.rs b/src/whisper_state.rs index d9b02c3..99f2ff1 100644 --- a/src/whisper_state.rs +++ b/src/whisper_state.rs @@ -1,19 +1,20 @@ -use crate::{FullParams, WhisperContext, WhisperError, WhisperToken, WhisperTokenData}; use std::ffi::{c_int, CStr}; -use std::marker::PhantomData; +use std::sync::Arc; + +use crate::{FullParams, WhisperContext, WhisperError, WhisperToken, WhisperTokenData}; /// Rustified pointer to a Whisper state. #[derive(Debug)] -pub struct WhisperState<'a> { - ctx: *mut whisper_rs_sys::whisper_context, +pub struct WhisperState { + ctx: Arc, ptr: *mut whisper_rs_sys::whisper_state, - _phantom: PhantomData<&'a WhisperContext>, } -unsafe impl<'a> Send for WhisperState<'a> {} -unsafe impl<'a> Sync for WhisperState<'a> {} +unsafe impl Send for WhisperState {} -impl<'a> Drop for WhisperState<'a> { +unsafe impl Sync for WhisperState {} + +impl Drop for WhisperState { fn drop(&mut self) { unsafe { whisper_rs_sys::whisper_free_state(self.ptr); @@ -21,15 +22,14 @@ impl<'a> Drop for WhisperState<'a> { } } -impl<'a> WhisperState<'a> { +impl WhisperState { pub(crate) fn new( - ctx: *mut whisper_rs_sys::whisper_context, + ctx: Arc, ptr: *mut whisper_rs_sys::whisper_state, ) -> Self { Self { ctx, ptr, - _phantom: PhantomData, } } @@ -45,13 +45,13 @@ impl<'a> WhisperState<'a> { /// /// # C++ equivalent /// `int whisper_pcm_to_mel(struct whisper_context * ctx, const float * samples, int n_samples, int n_threads)` - pub fn pcm_to_mel(&mut self, pcm: &[f32], threads: usize) -> Result<(), WhisperError> { + pub fn pcm_to_mel(&self, pcm: &[f32], threads: usize) -> Result<(), WhisperError> { if threads < 1 { return Err(WhisperError::InvalidThreadCount); } let ret = unsafe { whisper_rs_sys::whisper_pcm_to_mel_with_state( - self.ctx, + self.ctx.ctx, self.ptr, pcm.as_ptr(), pcm.len() as c_int, @@ -81,7 +81,7 @@ impl<'a> WhisperState<'a> { /// # C++ equivalent /// `int whisper_pcm_to_mel(struct whisper_context * ctx, const float * samples, int n_samples, int n_threads)` pub fn pcm_to_mel_phase_vocoder( - &mut self, + &self, pcm: &[f32], threads: usize, ) -> Result<(), WhisperError> { @@ -90,7 +90,7 @@ impl<'a> WhisperState<'a> { } let ret = unsafe { whisper_rs_sys::whisper_pcm_to_mel_phase_vocoder_with_state( - self.ctx, + self.ctx.ctx, self.ptr, pcm.as_ptr(), pcm.len() as c_int, @@ -122,12 +122,12 @@ impl<'a> WhisperState<'a> { /// /// # C++ equivalent /// `int whisper_set_mel(struct whisper_context * ctx, const float * data, int n_len, int n_mel)` - pub fn set_mel(&mut self, data: &[f32]) -> Result<(), WhisperError> { + pub fn set_mel(&self, data: &[f32]) -> Result<(), WhisperError> { let hop_size = 160; let n_len = (data.len() / hop_size) * 2; let ret = unsafe { whisper_rs_sys::whisper_set_mel_with_state( - self.ctx, + self.ctx.ctx, self.ptr, data.as_ptr(), n_len as c_int, @@ -155,13 +155,13 @@ impl<'a> WhisperState<'a> { /// /// # C++ equivalent /// `int whisper_encode(struct whisper_context * ctx, int offset, int n_threads)` - pub fn encode(&mut self, offset: usize, threads: usize) -> Result<(), WhisperError> { + pub fn encode(&self, offset: usize, threads: usize) -> Result<(), WhisperError> { if threads < 1 { return Err(WhisperError::InvalidThreadCount); } let ret = unsafe { whisper_rs_sys::whisper_encode_with_state( - self.ctx, + self.ctx.ctx, self.ptr, offset as c_int, threads as c_int, @@ -192,7 +192,7 @@ impl<'a> WhisperState<'a> { /// # C++ equivalent /// `int whisper_decode(struct whisper_context * ctx, const whisper_token * tokens, int n_tokens, int n_past, int n_threads)` pub fn decode( - &mut self, + &self, tokens: &[WhisperToken], n_past: usize, threads: usize, @@ -202,7 +202,7 @@ impl<'a> WhisperState<'a> { } let ret = unsafe { whisper_rs_sys::whisper_decode_with_state( - self.ctx, + self.ctx.ctx, self.ptr, tokens.as_ptr(), tokens.len() as c_int, @@ -240,7 +240,7 @@ impl<'a> WhisperState<'a> { let mut lang_probs: Vec = vec![0.0; crate::standalone::get_lang_max_id() as usize + 1]; let ret = unsafe { whisper_rs_sys::whisper_lang_auto_detect_with_state( - self.ctx, + self.ctx.ctx, self.ptr, offset_ms as c_int, threads as c_int, @@ -309,7 +309,7 @@ impl<'a> WhisperState<'a> { /// `int whisper_n_vocab (struct whisper_context * ctx)` #[inline] pub fn n_vocab(&self) -> c_int { - unsafe { whisper_rs_sys::whisper_n_vocab(self.ctx) } + unsafe { whisper_rs_sys::whisper_n_vocab(self.ctx.ctx) } } /// Run the entire model: PCM -> log mel spectrogram -> encoder -> decoder -> text @@ -327,7 +327,7 @@ impl<'a> WhisperState<'a> { /// /// # C++ equivalent /// `int whisper_full(struct whisper_context * ctx, struct whisper_full_params params, const float * samples, int n_samples)` - pub fn full(&mut self, params: FullParams, data: &[f32]) -> Result { + pub fn full(&self, params: FullParams, data: &[f32]) -> Result { if data.is_empty() { // can randomly trigger segmentation faults if we don't check this return Err(WhisperError::NoSamples); @@ -335,7 +335,7 @@ impl<'a> WhisperState<'a> { let ret = unsafe { whisper_rs_sys::whisper_full_with_state( - self.ctx, + self.ctx.ctx, self.ptr, params.fp, data.as_ptr(), @@ -495,7 +495,7 @@ impl<'a> WhisperState<'a> { ) -> Result { let ret = unsafe { whisper_rs_sys::whisper_full_get_token_text_from_state( - self.ctx, self.ptr, segment, token, + self.ctx.ctx, self.ptr, segment, token, ) }; if ret.is_null() { @@ -527,7 +527,7 @@ impl<'a> WhisperState<'a> { ) -> Result { let ret = unsafe { whisper_rs_sys::whisper_full_get_token_text_from_state( - self.ctx, self.ptr, segment, token, + self.ctx.ctx, self.ptr, segment, token, ) }; if ret.is_null() { @@ -610,7 +610,7 @@ impl<'a> WhisperState<'a> { /// /// # C++ equivalent /// `bool whisper_full_get_segment_speaker_turn_next_from_state(struct whisper_state * state, int i_segment)` - pub fn full_get_segment_speaker_turn_next(&mut self, i_segment: c_int) -> bool { + pub fn full_get_segment_speaker_turn_next(&self, i_segment: c_int) -> bool { unsafe { whisper_rs_sys::whisper_full_get_segment_speaker_turn_next_from_state( self.ptr, i_segment, From 9d978add9dabeff58f41c0037045fa79def2dc14 Mon Sep 17 00:00:00 2001 From: jiahua Date: Thu, 25 Apr 2024 15:33:06 +0800 Subject: [PATCH 03/12] refactor(state): create_state from whisper context wrapper --- src/lib.rs | 2 ++ src/whisper_ctx.rs | 20 +------------------- src/whisper_ctx_wrapper.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 src/whisper_ctx_wrapper.rs diff --git a/src/lib.rs b/src/lib.rs index a6da664..d9b75cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ mod whisper_state; mod whisper_sys_log; #[cfg(feature = "whisper-cpp-tracing")] mod whisper_sys_tracing; +mod whisper_ctx_wrapper; #[cfg(any(feature = "whisper-cpp-log", feature = "whisper-cpp-tracing"))] static LOG_TRAMPOLINE_INSTALL: Once = Once::new(); @@ -22,6 +23,7 @@ pub use standalone::*; use std::sync::Once; pub use utilities::*; pub use whisper_ctx::WhisperContext; +pub use whisper_ctx_wrapper::WhisperContextWrapper; pub use whisper_ctx::WhisperContextParameters; pub use whisper_grammar::{WhisperGrammarElement, WhisperGrammarElementType}; pub use whisper_params::{FullParams, SamplingStrategy}; diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index 8d948cd..6f71af3 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -10,7 +10,7 @@ use std::ffi::{c_int, CStr, CString}; /// then run a full transcription with [WhisperState::full]. #[derive(Debug)] pub struct WhisperContext { - ctx: *mut whisper_rs_sys::whisper_context, + pub(crate) ctx: *mut whisper_rs_sys::whisper_context, } impl WhisperContext { @@ -114,24 +114,6 @@ impl WhisperContext { } } - // we don't implement `whisper_init()` here since i have zero clue what `whisper_model_loader` does - - /// Create a new state object, ready for use. - /// - /// # Returns - /// Ok(WhisperState) on success, Err(WhisperError) on failure. - /// - /// # C++ equivalent - /// `struct whisper_state * whisper_init_state(struct whisper_context * ctx);` - pub fn create_state(&self) -> Result { - let state = unsafe { whisper_rs_sys::whisper_init_state(self.ctx) }; - if state.is_null() { - Err(WhisperError::InitError) - } else { - // SAFETY: this is known to be a valid pointer to a `whisper_state` struct - Ok(WhisperState::new(self.ctx, state)) - } - } /// Convert the provided text into tokens. /// diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs new file mode 100644 index 0000000..74fa4d0 --- /dev/null +++ b/src/whisper_ctx_wrapper.rs @@ -0,0 +1,37 @@ +use std::sync::Arc; + +use crate::{WhisperContext, WhisperContextParameters, WhisperError, WhisperState}; + +pub struct WhisperContextWrapper { + ctx: Arc, +} + +impl WhisperContextWrapper { + /// wrapper of WhisperContext::new_with_params. + pub fn new_with_params( + path: &str, + parameters: WhisperContextParameters, + ) -> Result { + let ctx = WhisperContext::new_with_params(path, parameters)?; + Ok(Self { ctx: Arc::new(ctx) }) + } + + // we don't implement `whisper_init()` here since i have zero clue what `whisper_model_loader` does + + /// Create a new state object, ready for use. + /// + /// # Returns + /// Ok(WhisperState) on success, Err(WhisperError) on failure. + /// + /// # C++ equivalent + /// `struct whisper_state * whisper_init_state(struct whisper_context * ctx);` + pub fn create_state(&self) -> Result { + let state = unsafe { whisper_rs_sys::whisper_init_state(self.ctx.ctx) }; + if state.is_null() { + Err(WhisperError::InitError) + } else { + // SAFETY: this is known to be a valid pointer to a `whisper_state` struct + Ok(WhisperState::new(self.ctx.clone(), state)) + } + } +} \ No newline at end of file From 5eef5afd5e299d103f68c6b867eaf08f293c2120 Mon Sep 17 00:00:00 2001 From: jiahua Date: Thu, 25 Apr 2024 15:55:17 +0800 Subject: [PATCH 04/12] wrap more functions --- src/whisper_ctx.rs | 2 +- src/whisper_ctx_wrapper.rs | 444 ++++++++++++++++++++++++++++++++++++- 2 files changed, 442 insertions(+), 4 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index 6f71af3..dbb7448 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -511,7 +511,7 @@ impl WhisperContext { /// /// # C++ equivalent /// `bool whisper_full_get_segment_speaker_turn_next(struct whisper_context * ctx, int i_segment)` - pub fn full_get_segment_speaker_turn_next(&mut self, i_segment: c_int) -> bool { + pub fn full_get_segment_speaker_turn_next(&self, i_segment: c_int) -> bool { unsafe { whisper_rs_sys::whisper_full_get_segment_speaker_turn_next(self.ctx, i_segment) } } } diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index 74fa4d0..57e1b77 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -1,21 +1,459 @@ +use std::ffi::{c_int, CStr}; use std::sync::Arc; -use crate::{WhisperContext, WhisperContextParameters, WhisperError, WhisperState}; +use crate::{WhisperContext, WhisperContextParameters, WhisperError, WhisperState, WhisperToken}; pub struct WhisperContextWrapper { ctx: Arc, } impl WhisperContextWrapper { - /// wrapper of WhisperContext::new_with_params. + fn wrap(ctx: WhisperContext) -> Self { + Self { + ctx: Arc::new(ctx), + } + } + + /// Create a new WhisperContext from a file, with parameters. + /// + /// # Arguments + /// * path: The path to the model file. + /// * parameters: A parameter struct containing the parameters to use. + /// + /// # Returns + /// Ok(Self) on success, Err(WhisperError) on failure. + /// + /// # C++ equivalent + /// `struct whisper_context * whisper_init_from_file_with_params_no_state(const char * path_model, struct whisper_context_params params);` pub fn new_with_params( path: &str, parameters: WhisperContextParameters, ) -> Result { let ctx = WhisperContext::new_with_params(path, parameters)?; - Ok(Self { ctx: Arc::new(ctx) }) + Ok(Self::wrap(ctx)) } + /// Create a new WhisperContext from a buffer. + /// + /// # Arguments + /// * buffer: The buffer containing the model. + /// + /// # Returns + /// Ok(Self) on success, Err(WhisperError) on failure. + /// + /// # C++ equivalent + /// `struct whisper_context * whisper_init_from_buffer_with_params_no_state(void * buffer, size_t buffer_size, struct whisper_context_params params);` + pub fn new_from_buffer_with_params( + buffer: &[u8], + parameters: WhisperContextParameters, + ) -> Result { + let ctx = WhisperContext::new_from_buffer_with_params(buffer, parameters)?; + Ok(Self::wrap(ctx)) + } + + /// Create a new WhisperContext from a file. + /// + /// # Arguments + /// * path: The path to the model file. + /// + /// # Returns + /// Ok(Self) on success, Err(WhisperError) on failure. + /// + /// # C++ equivalent + /// `struct whisper_context * whisper_init_from_file_no_state(const char * path_model)` + #[deprecated = "Use `new_with_params` instead"] + pub fn new(path: &str) -> Result { + let ctx = WhisperContext::new(path)?; + Ok(Self::wrap(ctx)) + } + + /// Create a new WhisperContext from a buffer. + /// + /// # Arguments + /// * buffer: The buffer containing the model. + /// + /// # Returns + /// Ok(Self) on success, Err(WhisperError) on failure. + /// + /// # C++ equivalent + /// `struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size)` + #[deprecated = "Use `new_from_buffer_with_params` instead"] + pub fn new_from_buffer(buffer: &[u8]) -> Result { + let ctx = WhisperContext::new_from_buffer(buffer)?; + Ok(Self::wrap(ctx)) + } + + + /// Convert the provided text into tokens. + /// + /// # Arguments + /// * text: The text to convert. + /// + /// # Returns + /// `Ok(Vec)` on success, `Err(WhisperError)` on failure. + /// + /// # C++ equivalent + /// `int whisper_tokenize(struct whisper_context * ctx, const char * text, whisper_token * tokens, int n_max_tokens);` + pub fn tokenize( + &self, + text: &str, + max_tokens: usize, + ) -> Result, WhisperError> { + self.ctx.tokenize(text, max_tokens) + } + + /// Get n_vocab. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_n_vocab (struct whisper_context * ctx)` + #[inline] + pub fn n_vocab(&self) -> c_int { + self.ctx.n_vocab() + } + + /// Get n_text_ctx. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_n_text_ctx (struct whisper_context * ctx);` + #[inline] + pub fn n_text_ctx(&self) -> c_int { + self.ctx.n_text_ctx() + } + + /// Get n_audio_ctx. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_n_audio_ctx (struct whisper_context * ctx);` + #[inline] + pub fn n_audio_ctx(&self) -> c_int { + self.ctx.n_audio_ctx() + } + + /// Does this model support multiple languages? + /// + /// # C++ equivalent + /// `int whisper_is_multilingual(struct whisper_context * ctx)` + #[inline] + pub fn is_multilingual(&self) -> bool { + self.ctx.is_multilingual() + } + + /// Get model_n_vocab. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_vocab (struct whisper_context * ctx);` + #[inline] + pub fn model_n_vocab(&self) -> c_int { + self.ctx.model_n_vocab() + } + + /// Get model_n_audio_ctx. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_audio_ctx (struct whisper_context * ctx)` + #[inline] + pub fn model_n_audio_ctx(&self) -> c_int { + self.ctx.model_n_audio_ctx() + } + + /// Get model_n_audio_state. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_audio_state(struct whisper_context * ctx);` + #[inline] + pub fn model_n_audio_state(&self) -> c_int { + self.ctx.model_n_audio_state() + } + + /// Get model_n_audio_head. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_audio_head (struct whisper_context * ctx);` + #[inline] + pub fn model_n_audio_head(&self) -> c_int { + self.ctx.model_n_audio_head() + } + + /// Get model_n_audio_layer. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_audio_layer(struct whisper_context * ctx);` + #[inline] + pub fn model_n_audio_layer(&self) -> c_int { + self.ctx.model_n_audio_layer() + } + + /// Get model_n_text_ctx. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_text_ctx (struct whisper_context * ctx)` + #[inline] + pub fn model_n_text_ctx(&self) -> c_int { + self.ctx.model_n_text_ctx() + } + + /// Get model_n_text_state. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_text_state (struct whisper_context * ctx);` + #[inline] + pub fn model_n_text_state(&self) -> c_int { + self.ctx.model_n_text_state() + } + + /// Get model_n_text_head. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_text_head (struct whisper_context * ctx);` + #[inline] + pub fn model_n_text_head(&self) -> c_int { + self.ctx.model_n_text_head() + } + + /// Get model_n_text_layer. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_text_layer (struct whisper_context * ctx);` + #[inline] + pub fn model_n_text_layer(&self) -> c_int { + self.ctx.model_n_text_layer() + } + + /// Get model_n_mels. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_n_mels (struct whisper_context * ctx);` + #[inline] + pub fn model_n_mels(&self) -> c_int { + self.ctx.model_n_mels() + } + + /// Get model_ftype. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_ftype (struct whisper_context * ctx);` + #[inline] + pub fn model_ftype(&self) -> c_int { + self.ctx.model_ftype() + } + + /// Get model_type. + /// + /// # Returns + /// c_int + /// + /// # C++ equivalent + /// `int whisper_model_type (struct whisper_context * ctx);` + #[inline] + pub fn model_type(&self) -> c_int { + self.ctx.model_type() + } + + // token functions + /// Convert a token ID to a string. + /// + /// # Arguments + /// * token_id: ID of the token. + /// + /// # Returns + /// Ok(&str) on success, Err(WhisperError) on failure. + /// + /// # C++ equivalent + /// `const char * whisper_token_to_str(struct whisper_context * ctx, whisper_token token)` + pub fn token_to_str(&self, token_id: WhisperToken) -> Result<&str, WhisperError> { + self.ctx.token_to_str(token_id) + } + + /// Convert a token ID to a &CStr. + /// + /// # Arguments + /// * token_id: ID of the token. + /// + /// # Returns + /// Ok(String) on success, Err(WhisperError) on failure. + /// + /// # C++ equivalent + /// `const char * whisper_token_to_str(struct whisper_context * ctx, whisper_token token)` + pub fn token_to_cstr(&self, token_id: WhisperToken) -> Result<&CStr, WhisperError> { + self.ctx.token_to_cstr(token_id) + } + + /// Undocumented but exposed function in the C++ API. + /// `const char * whisper_model_type_readable(struct whisper_context * ctx);` + /// + /// # Returns + /// Ok(String) on success, Err(WhisperError) on failure. + pub fn model_type_readable(&self) -> Result { + self.ctx.model_type_readable() + } + + /// Get the ID of the eot token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_eot (struct whisper_context * ctx)` + #[inline] + pub fn token_eot(&self) -> WhisperToken { + self.ctx.token_eot() + } + + /// Get the ID of the sot token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_sot (struct whisper_context * ctx)` + #[inline] + pub fn token_sot(&self) -> WhisperToken { + self.ctx.token_sot() + } + + /// Get the ID of the solm token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_solm(struct whisper_context * ctx)` + #[inline] + pub fn token_solm(&self) -> WhisperToken { + self.ctx.token_solm() + } + + /// Get the ID of the prev token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_prev(struct whisper_context * ctx)` + #[inline] + pub fn token_prev(&self) -> WhisperToken { + self.ctx.token_prev() + } + + /// Get the ID of the nosp token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_nosp(struct whisper_context * ctx)` + #[inline] + pub fn token_nosp(&self) -> WhisperToken { + self.ctx.token_nosp() + } + + /// Get the ID of the not token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_not (struct whisper_context * ctx)` + #[inline] + pub fn token_not(&self) -> WhisperToken { + self.ctx.token_not() + } + + /// Get the ID of the beg token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_beg (struct whisper_context * ctx)` + #[inline] + pub fn token_beg(&self) -> WhisperToken { + self.ctx.token_beg() + } + + /// Get the ID of a specified language token + /// + /// # Arguments + /// * lang_id: ID of the language + /// + /// # C++ equivalent + /// `whisper_token whisper_token_lang(struct whisper_context * ctx, int lang_id)` + #[inline] + pub fn token_lang(&self, lang_id: c_int) -> WhisperToken { + self.ctx.token_lang(lang_id) + } + + /// Print performance statistics to stderr. + /// + /// # C++ equivalent + /// `void whisper_print_timings(struct whisper_context * ctx)` + #[inline] + pub fn print_timings(&self) { + self.ctx.print_timings() + } + + /// Reset performance statistics. + /// + /// # C++ equivalent + /// `void whisper_reset_timings(struct whisper_context * ctx)` + #[inline] + pub fn reset_timings(&self) { + self.ctx.reset_timings() + } + + // task tokens + /// Get the ID of the translate task token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_translate ()` + pub fn token_translate(&self) -> WhisperToken { + self.ctx.token_translate() + } + + /// Get the ID of the transcribe task token. + /// + /// # C++ equivalent + /// `whisper_token whisper_token_transcribe()` + pub fn token_transcribe(&self) -> WhisperToken { + self.ctx.token_transcribe() + } + + /// Get whether the next segment is predicted as a speaker turn + /// + /// # Arguments + /// * i_segment: Segment index. + /// + /// # Returns + /// bool + /// + /// # C++ equivalent + /// `bool whisper_full_get_segment_speaker_turn_next(struct whisper_context * ctx, int i_segment)` + pub fn full_get_segment_speaker_turn_next(&self, i_segment: c_int) -> bool { + self.ctx.full_get_segment_speaker_turn_next(i_segment) + } + + // we don't implement `whisper_init()` here since i have zero clue what `whisper_model_loader` does /// Create a new state object, ready for use. From 6e04f76e416babbbd7444b4e0e55b4fb3cd1b23a Mon Sep 17 00:00:00 2001 From: jiahua Date: Thu, 25 Apr 2024 16:00:36 +0800 Subject: [PATCH 05/12] rename WhisperInnerContext --- src/lib.rs | 2 +- src/whisper_ctx.rs | 16 ++++++++-------- src/whisper_ctx_wrapper.rs | 14 +++++++------- src/whisper_state.rs | 6 +++--- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d9b75cd..eca465b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ pub use standalone::*; #[cfg(any(feature = "whisper-cpp-log", feature = "whisper-cpp-tracing"))] use std::sync::Once; pub use utilities::*; -pub use whisper_ctx::WhisperContext; +use whisper_ctx::WhisperInnerContext; pub use whisper_ctx_wrapper::WhisperContextWrapper; pub use whisper_ctx::WhisperContextParameters; pub use whisper_grammar::{WhisperGrammarElement, WhisperGrammarElementType}; diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index dbb7448..d7415ab 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -5,15 +5,15 @@ use std::ffi::{c_int, CStr, CString}; /// Safe Rust wrapper around a Whisper context. /// -/// You likely want to create this with [WhisperContext::new_with_params], -/// create a state with [WhisperContext::create_state], +/// You likely want to create this with [WhisperInnerContext::new_with_params], +/// create a state with [WhisperInnerContext::create_state], /// then run a full transcription with [WhisperState::full]. #[derive(Debug)] -pub struct WhisperContext { +pub struct WhisperInnerContext { pub(crate) ctx: *mut whisper_rs_sys::whisper_context, } -impl WhisperContext { +impl WhisperInnerContext { /// Create a new WhisperContext from a file, with parameters. /// /// # Arguments @@ -516,7 +516,7 @@ impl WhisperContext { } } -impl Drop for WhisperContext { +impl Drop for WhisperInnerContext { #[inline] fn drop(&mut self) { unsafe { whisper_rs_sys::whisper_free(self.ctx) }; @@ -525,8 +525,8 @@ impl Drop for WhisperContext { // following implementations are safe // see https://github.com/ggerganov/whisper.cpp/issues/32#issuecomment-1272790388 -unsafe impl Send for WhisperContext {} -unsafe impl Sync for WhisperContext {} +unsafe impl Send for WhisperInnerContext {} +unsafe impl Sync for WhisperInnerContext {} pub struct WhisperContextParameters { /// Use GPU if available. @@ -570,7 +570,7 @@ mod test_with_tiny_model { #[test] fn test_tokenize_round_trip() { - let ctx = WhisperContext::new(MODEL_PATH).expect("Download the ggml-tiny.en model using 'sys/whisper.cpp/models/download-ggml-model.sh tiny.en'"); + let ctx = WhisperInnerContext::new(MODEL_PATH).expect("Download the ggml-tiny.en model using 'sys/whisper.cpp/models/download-ggml-model.sh tiny.en'"); let text_in = " And so my fellow Americans, ask not what your country can do for you, ask what you can do for your country."; let tokens = ctx.tokenize(text_in, 1024).unwrap(); let text_out = tokens diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index 57e1b77..51bcdac 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -1,14 +1,14 @@ use std::ffi::{c_int, CStr}; use std::sync::Arc; -use crate::{WhisperContext, WhisperContextParameters, WhisperError, WhisperState, WhisperToken}; +use crate::{WhisperInnerContext, WhisperContextParameters, WhisperError, WhisperState, WhisperToken}; pub struct WhisperContextWrapper { - ctx: Arc, + ctx: Arc, } impl WhisperContextWrapper { - fn wrap(ctx: WhisperContext) -> Self { + fn wrap(ctx: WhisperInnerContext) -> Self { Self { ctx: Arc::new(ctx), } @@ -29,7 +29,7 @@ impl WhisperContextWrapper { path: &str, parameters: WhisperContextParameters, ) -> Result { - let ctx = WhisperContext::new_with_params(path, parameters)?; + let ctx = WhisperInnerContext::new_with_params(path, parameters)?; Ok(Self::wrap(ctx)) } @@ -47,7 +47,7 @@ impl WhisperContextWrapper { buffer: &[u8], parameters: WhisperContextParameters, ) -> Result { - let ctx = WhisperContext::new_from_buffer_with_params(buffer, parameters)?; + let ctx = WhisperInnerContext::new_from_buffer_with_params(buffer, parameters)?; Ok(Self::wrap(ctx)) } @@ -63,7 +63,7 @@ impl WhisperContextWrapper { /// `struct whisper_context * whisper_init_from_file_no_state(const char * path_model)` #[deprecated = "Use `new_with_params` instead"] pub fn new(path: &str) -> Result { - let ctx = WhisperContext::new(path)?; + let ctx = WhisperInnerContext::new(path)?; Ok(Self::wrap(ctx)) } @@ -79,7 +79,7 @@ impl WhisperContextWrapper { /// `struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size)` #[deprecated = "Use `new_from_buffer_with_params` instead"] pub fn new_from_buffer(buffer: &[u8]) -> Result { - let ctx = WhisperContext::new_from_buffer(buffer)?; + let ctx = WhisperInnerContext::new_from_buffer(buffer)?; Ok(Self::wrap(ctx)) } diff --git a/src/whisper_state.rs b/src/whisper_state.rs index 99f2ff1..025c109 100644 --- a/src/whisper_state.rs +++ b/src/whisper_state.rs @@ -1,12 +1,12 @@ use std::ffi::{c_int, CStr}; use std::sync::Arc; -use crate::{FullParams, WhisperContext, WhisperError, WhisperToken, WhisperTokenData}; +use crate::{FullParams, WhisperInnerContext, WhisperError, WhisperToken, WhisperTokenData}; /// Rustified pointer to a Whisper state. #[derive(Debug)] pub struct WhisperState { - ctx: Arc, + ctx: Arc, ptr: *mut whisper_rs_sys::whisper_state, } @@ -24,7 +24,7 @@ impl Drop for WhisperState { impl WhisperState { pub(crate) fn new( - ctx: Arc, + ctx: Arc, ptr: *mut whisper_rs_sys::whisper_state, ) -> Self { Self { From 8b587b97a90cc22e28730505c42d0d70ebaf002a Mon Sep 17 00:00:00 2001 From: jiahua Date: Thu, 25 Apr 2024 16:01:20 +0800 Subject: [PATCH 06/12] rename WhisperContext --- src/lib.rs | 2 +- src/whisper_ctx_wrapper.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index eca465b..88926ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ pub use standalone::*; use std::sync::Once; pub use utilities::*; use whisper_ctx::WhisperInnerContext; -pub use whisper_ctx_wrapper::WhisperContextWrapper; +pub use whisper_ctx_wrapper::WhisperContext; pub use whisper_ctx::WhisperContextParameters; pub use whisper_grammar::{WhisperGrammarElement, WhisperGrammarElementType}; pub use whisper_params::{FullParams, SamplingStrategy}; diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index 51bcdac..b20379f 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -3,11 +3,11 @@ use std::sync::Arc; use crate::{WhisperInnerContext, WhisperContextParameters, WhisperError, WhisperState, WhisperToken}; -pub struct WhisperContextWrapper { +pub struct WhisperContext { ctx: Arc, } -impl WhisperContextWrapper { +impl WhisperContext { fn wrap(ctx: WhisperInnerContext) -> Self { Self { ctx: Arc::new(ctx), From f30d0ea0a1ae0f12d8e14edfd6a06d42003ec38f Mon Sep 17 00:00:00 2001 From: jiahua Date: Fri, 26 Apr 2024 16:23:21 +0800 Subject: [PATCH 07/12] fix: rustfmt --- examples/full_usage/src/main.rs | 2 +- src/lib.rs | 4 ++-- src/whisper_ctx.rs | 1 - src/whisper_ctx_wrapper.rs | 12 +++++------- src/whisper_state.rs | 17 ++++++++++------- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/full_usage/src/main.rs b/examples/full_usage/src/main.rs index 03b5daf..c3352fd 100644 --- a/examples/full_usage/src/main.rs +++ b/examples/full_usage/src/main.rs @@ -43,7 +43,7 @@ fn main() { } let original_samples = parse_wav_file(audio_path); - let mut samples = vec![0.0f32; original_samples.len()]; + let mut samples = vec![0.0f32; original_samples.len()]; whisper_rs::convert_integer_to_float_audio(&original_samples, &mut samples) .expect("failed to convert samples"); diff --git a/src/lib.rs b/src/lib.rs index 88926ab..f5dc43f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ mod error; mod standalone; mod utilities; mod whisper_ctx; +mod whisper_ctx_wrapper; mod whisper_grammar; mod whisper_params; mod whisper_state; @@ -12,7 +13,6 @@ mod whisper_state; mod whisper_sys_log; #[cfg(feature = "whisper-cpp-tracing")] mod whisper_sys_tracing; -mod whisper_ctx_wrapper; #[cfg(any(feature = "whisper-cpp-log", feature = "whisper-cpp-tracing"))] static LOG_TRAMPOLINE_INSTALL: Once = Once::new(); @@ -22,9 +22,9 @@ pub use standalone::*; #[cfg(any(feature = "whisper-cpp-log", feature = "whisper-cpp-tracing"))] use std::sync::Once; pub use utilities::*; +pub use whisper_ctx::WhisperContextParameters; use whisper_ctx::WhisperInnerContext; pub use whisper_ctx_wrapper::WhisperContext; -pub use whisper_ctx::WhisperContextParameters; pub use whisper_grammar::{WhisperGrammarElement, WhisperGrammarElementType}; pub use whisper_params::{FullParams, SamplingStrategy}; #[cfg(feature = "raw-api")] diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index d7415ab..2cc1df1 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -114,7 +114,6 @@ impl WhisperInnerContext { } } - /// Convert the provided text into tokens. /// /// # Arguments diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index b20379f..3462126 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -1,7 +1,9 @@ use std::ffi::{c_int, CStr}; use std::sync::Arc; -use crate::{WhisperInnerContext, WhisperContextParameters, WhisperError, WhisperState, WhisperToken}; +use crate::{ + WhisperContextParameters, WhisperError, WhisperInnerContext, WhisperState, WhisperToken, +}; pub struct WhisperContext { ctx: Arc, @@ -9,9 +11,7 @@ pub struct WhisperContext { impl WhisperContext { fn wrap(ctx: WhisperInnerContext) -> Self { - Self { - ctx: Arc::new(ctx), - } + Self { ctx: Arc::new(ctx) } } /// Create a new WhisperContext from a file, with parameters. @@ -83,7 +83,6 @@ impl WhisperContext { Ok(Self::wrap(ctx)) } - /// Convert the provided text into tokens. /// /// # Arguments @@ -453,7 +452,6 @@ impl WhisperContext { self.ctx.full_get_segment_speaker_turn_next(i_segment) } - // we don't implement `whisper_init()` here since i have zero clue what `whisper_model_loader` does /// Create a new state object, ready for use. @@ -472,4 +470,4 @@ impl WhisperContext { Ok(WhisperState::new(self.ctx.clone(), state)) } } -} \ No newline at end of file +} diff --git a/src/whisper_state.rs b/src/whisper_state.rs index 025c109..9fbd0bd 100644 --- a/src/whisper_state.rs +++ b/src/whisper_state.rs @@ -1,7 +1,7 @@ use std::ffi::{c_int, CStr}; use std::sync::Arc; -use crate::{FullParams, WhisperInnerContext, WhisperError, WhisperToken, WhisperTokenData}; +use crate::{FullParams, WhisperError, WhisperInnerContext, WhisperToken, WhisperTokenData}; /// Rustified pointer to a Whisper state. #[derive(Debug)] @@ -27,10 +27,7 @@ impl WhisperState { ctx: Arc, ptr: *mut whisper_rs_sys::whisper_state, ) -> Self { - Self { - ctx, - ptr, - } + Self { ctx, ptr } } /// Convert raw PCM audio (floating point 32 bit) to log mel spectrogram. @@ -495,7 +492,10 @@ impl WhisperState { ) -> Result { let ret = unsafe { whisper_rs_sys::whisper_full_get_token_text_from_state( - self.ctx.ctx, self.ptr, segment, token, + self.ctx.ctx, + self.ptr, + segment, + token, ) }; if ret.is_null() { @@ -527,7 +527,10 @@ impl WhisperState { ) -> Result { let ret = unsafe { whisper_rs_sys::whisper_full_get_token_text_from_state( - self.ctx.ctx, self.ptr, segment, token, + self.ctx.ctx, + self.ptr, + segment, + token, ) }; if ret.is_null() { From e4f8910b1bfe528d5b64df71675137a157a22558 Mon Sep 17 00:00:00 2001 From: jiahua Date: Tue, 30 Apr 2024 09:09:52 +0800 Subject: [PATCH 08/12] fix: keep &mut self --- src/whisper_ctx.rs | 2 +- src/whisper_ctx_wrapper.rs | 2 +- src/whisper_state.rs | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index 2cc1df1..910a2c4 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -510,7 +510,7 @@ impl WhisperInnerContext { /// /// # C++ equivalent /// `bool whisper_full_get_segment_speaker_turn_next(struct whisper_context * ctx, int i_segment)` - pub fn full_get_segment_speaker_turn_next(&self, i_segment: c_int) -> bool { + pub fn full_get_segment_speaker_turn_next(&mut self, i_segment: c_int) -> bool { unsafe { whisper_rs_sys::whisper_full_get_segment_speaker_turn_next(self.ctx, i_segment) } } } diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index 3462126..4e5fa8c 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -448,7 +448,7 @@ impl WhisperContext { /// /// # C++ equivalent /// `bool whisper_full_get_segment_speaker_turn_next(struct whisper_context * ctx, int i_segment)` - pub fn full_get_segment_speaker_turn_next(&self, i_segment: c_int) -> bool { + pub fn full_get_segment_speaker_turn_next(&mut self, i_segment: c_int) -> bool { self.ctx.full_get_segment_speaker_turn_next(i_segment) } diff --git a/src/whisper_state.rs b/src/whisper_state.rs index 9fbd0bd..6805aa5 100644 --- a/src/whisper_state.rs +++ b/src/whisper_state.rs @@ -42,7 +42,7 @@ impl WhisperState { /// /// # C++ equivalent /// `int whisper_pcm_to_mel(struct whisper_context * ctx, const float * samples, int n_samples, int n_threads)` - pub fn pcm_to_mel(&self, pcm: &[f32], threads: usize) -> Result<(), WhisperError> { + pub fn pcm_to_mel(&mut self, pcm: &[f32], threads: usize) -> Result<(), WhisperError> { if threads < 1 { return Err(WhisperError::InvalidThreadCount); } @@ -78,7 +78,7 @@ impl WhisperState { /// # C++ equivalent /// `int whisper_pcm_to_mel(struct whisper_context * ctx, const float * samples, int n_samples, int n_threads)` pub fn pcm_to_mel_phase_vocoder( - &self, + &mut self, pcm: &[f32], threads: usize, ) -> Result<(), WhisperError> { @@ -119,7 +119,7 @@ impl WhisperState { /// /// # C++ equivalent /// `int whisper_set_mel(struct whisper_context * ctx, const float * data, int n_len, int n_mel)` - pub fn set_mel(&self, data: &[f32]) -> Result<(), WhisperError> { + pub fn set_mel(&mut self, data: &[f32]) -> Result<(), WhisperError> { let hop_size = 160; let n_len = (data.len() / hop_size) * 2; let ret = unsafe { @@ -152,7 +152,7 @@ impl WhisperState { /// /// # C++ equivalent /// `int whisper_encode(struct whisper_context * ctx, int offset, int n_threads)` - pub fn encode(&self, offset: usize, threads: usize) -> Result<(), WhisperError> { + pub fn encode(&mut self, offset: usize, threads: usize) -> Result<(), WhisperError> { if threads < 1 { return Err(WhisperError::InvalidThreadCount); } @@ -189,7 +189,7 @@ impl WhisperState { /// # C++ equivalent /// `int whisper_decode(struct whisper_context * ctx, const whisper_token * tokens, int n_tokens, int n_past, int n_threads)` pub fn decode( - &self, + &mut self, tokens: &[WhisperToken], n_past: usize, threads: usize, @@ -324,7 +324,7 @@ impl WhisperState { /// /// # C++ equivalent /// `int whisper_full(struct whisper_context * ctx, struct whisper_full_params params, const float * samples, int n_samples)` - pub fn full(&self, params: FullParams, data: &[f32]) -> Result { + pub fn full(&mut self, params: FullParams, data: &[f32]) -> Result { if data.is_empty() { // can randomly trigger segmentation faults if we don't check this return Err(WhisperError::NoSamples); @@ -613,7 +613,7 @@ impl WhisperState { /// /// # C++ equivalent /// `bool whisper_full_get_segment_speaker_turn_next_from_state(struct whisper_state * state, int i_segment)` - pub fn full_get_segment_speaker_turn_next(&self, i_segment: c_int) -> bool { + pub fn full_get_segment_speaker_turn_next(&mut self, i_segment: c_int) -> bool { unsafe { whisper_rs_sys::whisper_full_get_segment_speaker_turn_next_from_state( self.ptr, i_segment, From 23d16f285898756b385eafb75e25d8fbfe6196ed Mon Sep 17 00:00:00 2001 From: jiahua Date: Tue, 30 Apr 2024 09:34:38 +0800 Subject: [PATCH 09/12] fix: let context stateless, remove whisper_full_get_segment_speaker_turn_next from context, better to use the same fn in state --- src/whisper_ctx.rs | 14 -------------- src/whisper_ctx_wrapper.rs | 14 -------------- 2 files changed, 28 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index 910a2c4..1520f7a 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -499,20 +499,6 @@ impl WhisperInnerContext { pub fn token_transcribe(&self) -> WhisperToken { unsafe { whisper_rs_sys::whisper_token_transcribe(self.ctx) } } - - /// Get whether the next segment is predicted as a speaker turn - /// - /// # Arguments - /// * i_segment: Segment index. - /// - /// # Returns - /// bool - /// - /// # C++ equivalent - /// `bool whisper_full_get_segment_speaker_turn_next(struct whisper_context * ctx, int i_segment)` - pub fn full_get_segment_speaker_turn_next(&mut self, i_segment: c_int) -> bool { - unsafe { whisper_rs_sys::whisper_full_get_segment_speaker_turn_next(self.ctx, i_segment) } - } } impl Drop for WhisperInnerContext { diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index 4e5fa8c..04ecfcb 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -438,20 +438,6 @@ impl WhisperContext { self.ctx.token_transcribe() } - /// Get whether the next segment is predicted as a speaker turn - /// - /// # Arguments - /// * i_segment: Segment index. - /// - /// # Returns - /// bool - /// - /// # C++ equivalent - /// `bool whisper_full_get_segment_speaker_turn_next(struct whisper_context * ctx, int i_segment)` - pub fn full_get_segment_speaker_turn_next(&mut self, i_segment: c_int) -> bool { - self.ctx.full_get_segment_speaker_turn_next(i_segment) - } - // we don't implement `whisper_init()` here since i have zero clue what `whisper_model_loader` does /// Create a new state object, ready for use. From e316c08f147c6cbf7680282437c41632a01c174d Mon Sep 17 00:00:00 2001 From: jiahua Date: Tue, 30 Apr 2024 13:44:19 +0800 Subject: [PATCH 10/12] refactor: remove deprecated fn --- src/whisper_ctx.rs | 22 ---------------------- src/whisper_ctx_wrapper.rs | 16 ---------------- 2 files changed, 38 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index 1520f7a..d1c4a50 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -1,5 +1,4 @@ use crate::error::WhisperError; -use crate::whisper_state::WhisperState; use crate::WhisperToken; use std::ffi::{c_int, CStr, CString}; @@ -71,27 +70,6 @@ impl WhisperInnerContext { } } - /// Create a new WhisperContext from a file. - /// - /// # Arguments - /// * path: The path to the model file. - /// - /// # Returns - /// Ok(Self) on success, Err(WhisperError) on failure. - /// - /// # C++ equivalent - /// `struct whisper_context * whisper_init_from_file_no_state(const char * path_model)` - #[deprecated = "Use `new_with_params` instead"] - pub fn new(path: &str) -> Result { - let path_cstr = CString::new(path)?; - let ctx = unsafe { whisper_rs_sys::whisper_init_from_file_no_state(path_cstr.as_ptr()) }; - if ctx.is_null() { - Err(WhisperError::InitError) - } else { - Ok(Self { ctx }) - } - } - /// Create a new WhisperContext from a buffer. /// /// # Arguments diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index 04ecfcb..cff0237 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -51,22 +51,6 @@ impl WhisperContext { Ok(Self::wrap(ctx)) } - /// Create a new WhisperContext from a file. - /// - /// # Arguments - /// * path: The path to the model file. - /// - /// # Returns - /// Ok(Self) on success, Err(WhisperError) on failure. - /// - /// # C++ equivalent - /// `struct whisper_context * whisper_init_from_file_no_state(const char * path_model)` - #[deprecated = "Use `new_with_params` instead"] - pub fn new(path: &str) -> Result { - let ctx = WhisperInnerContext::new(path)?; - Ok(Self::wrap(ctx)) - } - /// Create a new WhisperContext from a buffer. /// /// # Arguments From 85edf85e5f7125ba0c054c3ca69af3fdae8efdb4 Mon Sep 17 00:00:00 2001 From: jiahua Date: Tue, 30 Apr 2024 13:47:05 +0800 Subject: [PATCH 11/12] refactor: remove deprecated fn --- src/whisper_ctx.rs | 21 --------------------- src/whisper_ctx_wrapper.rs | 15 --------------- 2 files changed, 36 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index d1c4a50..d43f5e4 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -70,27 +70,6 @@ impl WhisperInnerContext { } } - /// Create a new WhisperContext from a buffer. - /// - /// # Arguments - /// * buffer: The buffer containing the model. - /// - /// # Returns - /// Ok(Self) on success, Err(WhisperError) on failure. - /// - /// # C++ equivalent - /// `struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size)` - #[deprecated = "Use `new_from_buffer_with_params` instead"] - pub fn new_from_buffer(buffer: &[u8]) -> Result { - let ctx = unsafe { - whisper_rs_sys::whisper_init_from_buffer_no_state(buffer.as_ptr() as _, buffer.len()) - }; - if ctx.is_null() { - Err(WhisperError::InitError) - } else { - Ok(Self { ctx }) - } - } /// Convert the provided text into tokens. /// diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index cff0237..0e56658 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -51,21 +51,6 @@ impl WhisperContext { Ok(Self::wrap(ctx)) } - /// Create a new WhisperContext from a buffer. - /// - /// # Arguments - /// * buffer: The buffer containing the model. - /// - /// # Returns - /// Ok(Self) on success, Err(WhisperError) on failure. - /// - /// # C++ equivalent - /// `struct whisper_context * whisper_init_from_buffer_no_state(void * buffer, size_t buffer_size)` - #[deprecated = "Use `new_from_buffer_with_params` instead"] - pub fn new_from_buffer(buffer: &[u8]) -> Result { - let ctx = WhisperInnerContext::new_from_buffer(buffer)?; - Ok(Self::wrap(ctx)) - } /// Convert the provided text into tokens. /// From 7281db50255f85fee25d2a58d7c95c7beb8990bb Mon Sep 17 00:00:00 2001 From: jiahua Date: Tue, 30 Apr 2024 13:48:55 +0800 Subject: [PATCH 12/12] fix: rustfmt --- src/whisper_ctx.rs | 1 - src/whisper_ctx_wrapper.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index d43f5e4..30bd75a 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -70,7 +70,6 @@ impl WhisperInnerContext { } } - /// Convert the provided text into tokens. /// /// # Arguments diff --git a/src/whisper_ctx_wrapper.rs b/src/whisper_ctx_wrapper.rs index 0e56658..ff3caff 100644 --- a/src/whisper_ctx_wrapper.rs +++ b/src/whisper_ctx_wrapper.rs @@ -51,7 +51,6 @@ impl WhisperContext { Ok(Self::wrap(ctx)) } - /// Convert the provided text into tokens. /// /// # Arguments