Skip to content

Refactor Rust API for speech denoiser - #3371

Merged
csukuangfj merged 1 commit into
k2-fsa:masterfrom
csukuangfj:refactor-rust
Mar 20, 2026
Merged

csukuangfj merged 1 commit into
k2-fsa:masterfrom
csukuangfj:refactor-rust

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Mar 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Refactor
    • Reorganized speech denoiser module structure and API organization. Model configuration types and audio handling structures for speech denoising have been relocated within the crate. Users importing speech denoiser types may need to update import paths. All speech denoising functionality remains available with the updated module organization.

Copilot AI review requested due to automatic review settings March 20, 2026 03:48
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Mar 20, 2026
@coderabbitai

coderabbitai Bot commented Mar 20, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9d83b1a8-11c1-4686-a66b-aabbbd8fd3c4

📥 Commits

Reviewing files that changed from the base of the PR and between cdf8e60 and d12d322.

📒 Files selected for processing (4)
  • sherpa-onnx/rust/sherpa-onnx/src/lib.rs
  • sherpa-onnx/rust/sherpa-onnx/src/offline_speech_denoiser.rs
  • sherpa-onnx/rust/sherpa-onnx/src/online_speech_denoiser.rs
  • sherpa-onnx/rust/sherpa-onnx/src/speech_denoiser.rs

📝 Walkthrough

Walkthrough

The speech_denoiser module is being relocated and consolidated. Its type definitions and implementations are moved to offline_speech_denoiser.rs, the module is removed from lib.rs exports, and online_speech_denoiser.rs is updated to import from the new location.

Changes

Cohort / File(s) Summary
Module Cleanup
sherpa-onnx/rust/sherpa-onnx/src/lib.rs
Removed mod speech_denoiser; declaration and pub use speech_denoiser::*; re-export, eliminating crate-level access to speech denoiser types.
Types Consolidation
sherpa-onnx/rust/sherpa-onnx/src/offline_speech_denoiser.rs
Added local definitions of DenoisedAudio, OfflineSpeechDenoiserGtcrnModelConfig, OfflineSpeechDenoiserDpdfNetModelConfig, and OfflineSpeechDenoiserModelConfig. Implemented to_sys() conversion methods and DenoisedAudio::from_ptr() for safe pointer handling and cleanup.
Import Path Update
sherpa-onnx/rust/sherpa-onnx/src/online_speech_denoiser.rs
Updated imports of DenoisedAudio and OfflineSpeechDenoiserModelConfig from crate::speech_denoiser to crate::offline_speech_denoiser.
Module Deletion
sherpa-onnx/rust/sherpa-onnx/src/speech_denoiser.rs
Entire module removed, including configuration structs and conversion implementations previously used as a central re-export location.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • k2-fsa/sherpa-onnx#3328: Modifies the same speech-denoiser module structure, type definitions, and import paths across multiple files.

Suggested labels

size:L

Poem

🐰 The denoiser hops to a better home,
From scattered files to consolidated dome,
Types now nestled in offline's embrace,
Cleaner imports, a more orderly space! ✨

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the Rust API for the speech denoiser by consolidating related structures and configurations into the offline_speech_denoiser module. This change streamlines the module organization, making the API more coherent and easier to maintain by eliminating a standalone speech_denoiser module and updating its references.

Highlights

  • Module Consolidation: The core structures and configurations for speech denoiser models, including DenoisedAudio and OfflineSpeechDenoiserModelConfig, were moved from a generic speech_denoiser module to the more specific offline_speech_denoiser module.
  • API Streamlining: The standalone speech_denoiser module was removed, simplifying the overall API structure and reducing redundancy.
  • Dependency Updates: Internal module imports and public re-exports were updated across the crate to reflect the new location of the speech denoiser components.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@csukuangfj
csukuangfj merged commit 50a24cd into k2-fsa:master Mar 20, 2026
1 of 2 checks passed
@csukuangfj
csukuangfj deleted the refactor-rust branch March 20, 2026 03:49

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Rust API for the speech denoiser by consolidating shared components. The changes move definitions from speech_denoiser.rs (which is now removed) into offline_speech_denoiser.rs. This is a good structural improvement that clarifies the dependency of the online denoiser on these shared components. I have one suggestion to reduce some code duplication in the newly moved code, which should improve long-term maintainability.

Comment on lines +8 to +37
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),
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The structs OfflineSpeechDenoiserGtcrnModelConfig and OfflineSpeechDenoiserDpdfNetModelConfig and their to_sys implementations are identical. You can reduce code duplication by using a macro to generate these structs and their implementations. This will make the code more concise and easier to maintain.

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
);

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the Rust speech denoiser API by moving shared types/configs into the offline denoiser module and updating imports accordingly.

Changes:

  • Removed speech_denoiser.rs and migrated its configs/types into offline_speech_denoiser.rs
  • Updated online_speech_denoiser.rs to import denoiser types from offline_speech_denoiser
  • Removed speech_denoiser module and its public re-exports from lib.rs

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
sherpa-onnx/rust/sherpa-onnx/src/speech_denoiser.rs Deleted; its structs/conversion logic were relocated.
sherpa-onnx/rust/sherpa-onnx/src/offline_speech_denoiser.rs Added the moved model config structs and DenoisedAudio implementation.
sherpa-onnx/rust/sherpa-onnx/src/online_speech_denoiser.rs Updated imports to reference the new module location.
sherpa-onnx/rust/sherpa-onnx/src/lib.rs Removed module declaration and re-export for speech_denoiser.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 7 to 12
mod online_punctuation;
mod online_speech_denoiser;
mod speaker_embedding;
mod speech_denoiser;
mod tts;
mod utils;
mod vad;

Copilot AI Mar 20, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offline_speech_denoiser.rs exists and online_speech_denoiser.rs now imports from crate::offline_speech_denoiser::..., but lib.rs does not declare mod offline_speech_denoiser; in the module list shown here. This will fail to compile with an unresolved import unless the module is declared elsewhere. Add mod offline_speech_denoiser; (and consider whether it needs to be pub/re-exported depending on intended public API).

Copilot uses AI. Check for mistakes.
Comment on lines 21 to 26
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::*;

Copilot AI Mar 20, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR removes pub use speech_denoiser::*; (and the entire speech_denoiser module), which is a breaking change for downstream crates importing sherpa_onnx::speech_denoiser::* or relying on those re-exports. If the intent is a non-breaking refactor, consider keeping a compatibility shim (e.g., reintroduce speech_denoiser as a module that re-exports the moved items, possibly marked #[deprecated]), or re-export the moved items from the crate root to preserve prior paths.

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +101
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,
}
}
}

Copilot AI Mar 20, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_ptr always calls SherpaOnnxDestroyDenoisedAudio(ptr), so it takes ownership of the incoming pointer. That ownership contract isn’t obvious from the name/signature (especially since it accepts *const). To reduce the risk of accidental double-free/use-after-free by future callers, consider renaming to something that encodes ownership (e.g., from_owned_ptr), adding a brief doc comment stating it consumes/frees the pointer, and/or switching the parameter type to *mut sys::DenoisedAudio if the C API expects a mutable pointer for destroy.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants