-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Refactor Rust API for speech denoiser #3371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,6 @@ mod online_asr; | |
| mod online_punctuation; | ||
| mod online_speech_denoiser; | ||
| mod speaker_embedding; | ||
| mod speech_denoiser; | ||
| mod tts; | ||
| mod utils; | ||
| mod vad; | ||
|
|
@@ -22,7 +21,6 @@ pub use online_asr::*; | |
| pub use online_punctuation::*; | ||
| pub use online_speech_denoiser::*; | ||
| pub use speaker_embedding::*; | ||
| pub use speech_denoiser::*; | ||
| pub use tts::*; | ||
| pub use utils::*; | ||
| pub use vad::*; | ||
|
Comment on lines
21
to
26
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,105 @@ | ||
| use crate::speech_denoiser::{DenoisedAudio, OfflineSpeechDenoiserModelConfig}; | ||
| use crate::utils::to_c_ptr; | ||
| use sherpa_onnx_sys as sys; | ||
| use std::ffi::CString; | ||
| use std::ptr; | ||
| use std::slice; | ||
|
|
||
| #[derive(Clone, Debug, Default)] | ||
| pub struct OfflineSpeechDenoiserGtcrnModelConfig { | ||
| pub model: Option<String>, | ||
| } | ||
|
|
||
| impl OfflineSpeechDenoiserGtcrnModelConfig { | ||
| pub(crate) fn to_sys( | ||
| &self, | ||
| cstrings: &mut Vec<CString>, | ||
| ) -> sys::OfflineSpeechDenoiserGtcrnModelConfig { | ||
| sys::OfflineSpeechDenoiserGtcrnModelConfig { | ||
| model: to_c_ptr(&self.model, cstrings), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Default)] | ||
| pub struct OfflineSpeechDenoiserDpdfNetModelConfig { | ||
| pub model: Option<String>, | ||
| } | ||
|
|
||
| impl OfflineSpeechDenoiserDpdfNetModelConfig { | ||
| pub(crate) fn to_sys( | ||
| &self, | ||
| cstrings: &mut Vec<CString>, | ||
| ) -> sys::OfflineSpeechDenoiserDpdfNetModelConfig { | ||
| sys::OfflineSpeechDenoiserDpdfNetModelConfig { | ||
| model: to_c_ptr(&self.model, cstrings), | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+8
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The structs macro_rules! define_denoiser_model_config {
($name:ident, $sys_name:ty) => {
#[derive(Clone, Debug, Default)]
pub struct $name {
pub model: Option<String>,
}
impl $name {
pub(crate) fn to_sys(
&self,
cstrings: &mut Vec<CString>,
) -> $sys_name {
<$sys_name> {
model: to_c_ptr(&self.model, cstrings),
}
}
}
};
}
define_denoiser_model_config!(
OfflineSpeechDenoiserGtcrnModelConfig,
sys::OfflineSpeechDenoiserGtcrnModelConfig
);
define_denoiser_model_config!(
OfflineSpeechDenoiserDpdfNetModelConfig,
sys::OfflineSpeechDenoiserDpdfNetModelConfig
); |
||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct OfflineSpeechDenoiserModelConfig { | ||
| pub gtcrn: OfflineSpeechDenoiserGtcrnModelConfig, | ||
| pub dpdfnet: OfflineSpeechDenoiserDpdfNetModelConfig, | ||
| pub num_threads: i32, | ||
| pub debug: bool, | ||
| pub provider: Option<String>, | ||
| } | ||
|
|
||
| impl Default for OfflineSpeechDenoiserModelConfig { | ||
| fn default() -> Self { | ||
| Self { | ||
| gtcrn: Default::default(), | ||
| dpdfnet: Default::default(), | ||
| num_threads: 1, | ||
| debug: false, | ||
| provider: Some("cpu".to_string()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl OfflineSpeechDenoiserModelConfig { | ||
| pub(crate) fn to_sys( | ||
| &self, | ||
| cstrings: &mut Vec<CString>, | ||
| ) -> sys::OfflineSpeechDenoiserModelConfig { | ||
| sys::OfflineSpeechDenoiserModelConfig { | ||
| gtcrn: self.gtcrn.to_sys(cstrings), | ||
| num_threads: self.num_threads, | ||
| debug: self.debug as i32, | ||
| provider: to_c_ptr(&self.provider, cstrings), | ||
| dpdfnet: self.dpdfnet.to_sys(cstrings), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Default)] | ||
| pub struct DenoisedAudio { | ||
| pub samples: Vec<f32>, | ||
| pub sample_rate: i32, | ||
| } | ||
|
|
||
| impl DenoisedAudio { | ||
| pub(crate) fn from_ptr(ptr: *const sys::DenoisedAudio) -> Self { | ||
| if ptr.is_null() { | ||
| return Self::default(); | ||
| } | ||
|
|
||
| unsafe { | ||
| let n = (*ptr).n.max(0) as usize; | ||
| let samples = if (*ptr).samples.is_null() || n == 0 { | ||
| vec![] | ||
| } else { | ||
| slice::from_raw_parts((*ptr).samples, n).to_vec() | ||
| }; | ||
| let sample_rate = (*ptr).sample_rate; | ||
| sys::SherpaOnnxDestroyDenoisedAudio(ptr); | ||
| Self { | ||
| samples, | ||
| sample_rate, | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+82
to
+101
|
||
| } | ||
|
|
||
| #[derive(Clone, Debug, Default)] | ||
| pub struct OfflineSpeechDenoiserConfig { | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
offline_speech_denoiser.rsexists andonline_speech_denoiser.rsnow imports fromcrate::offline_speech_denoiser::..., butlib.rsdoes not declaremod offline_speech_denoiser;in the module list shown here. This will fail to compile with an unresolved import unless the module is declared elsewhere. Addmod offline_speech_denoiser;(and consider whether it needs to bepub/re-exported depending on intended public API).