denoize 0.38.0

Pure-Rust audio denoiser with classical DSP and optional RNNoise
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Shared application service used by the CLI and graphical frontends.

use crate::loudness::LoudnessReport;
#[cfg(feature = "gtcrn")]
use crate::OnnxModelConfig;
use crate::{Audio, Backend, BackendOptions, ConfigError, DenoiserConfig};
use std::time::Duration;

/// User-facing backend choice shared by every application frontend.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BackendChoice {
    Auto,
    Explicit(Backend),
}

/// Options for processing decoded audio.
#[derive(Clone, Debug)]
pub struct ProcessingOptions {
    pub backend: BackendChoice,
    pub quality: Option<String>,
    pub denoiser: DenoiserConfig,
    pub backend_options: BackendOptions,
    pub loudness_lufs: Option<f64>,
    pub true_peak_dbtp: f64,
}

impl ProcessingOptions {
    /// Validate effective processing options without opening a model or
    /// modifying decoded audio. The selected backend is returned so callers
    /// can resolve managed model configuration only after validation succeeds.
    pub fn validate_config(&self, audio: &Audio) -> Result<Backend, ConfigError> {
        if let Some(quality) = self.quality.as_deref() {
            let quality = quality.to_ascii_lowercase();
            if !matches!(quality.as_str(), "high" | "ultra" | "max" | "highest") {
                return Err(ConfigError::invalid(
                    "quality",
                    "one of high, ultra, max, or highest",
                ));
            }
        }

        let duration = audio.frames() as f64 / audio.sample_rate.max(1) as f64;
        let backend = select_backend(self.backend, duration, self.quality.as_deref());
        let mut denoiser = self.denoiser.clone();
        denoiser.sample_rate = audio.sample_rate;
        denoiser.validate_config()?;
        self.backend_options.validate_config(backend)?;

        if let Some(target) = self.loudness_lufs {
            validate_finite_range(
                "loudness_lufs",
                target,
                -70.0,
                0.0,
                "a finite value in -70..=0 LUFS",
            )?;
        }
        validate_finite_range(
            "true_peak_dbtp",
            self.true_peak_dbtp,
            -20.0,
            0.0,
            "a finite value in -20..=0 dBTP",
        )?;
        Ok(backend)
    }
}

fn validate_finite_range(
    field: &'static str,
    value: f64,
    min: f64,
    max: f64,
    expected: &'static str,
) -> Result<(), ConfigError> {
    if !value.is_finite() || value < min || value > max {
        return Err(ConfigError::invalid(field, expected));
    }
    Ok(())
}

/// Information produced by a completed processing operation.
#[derive(Clone, Copy, Debug)]
pub struct ProcessingResult {
    pub backend: Backend,
    pub elapsed: Duration,
    pub loudness: Option<LoudnessReport>,
}

/// Stable display/configuration name for a compiled backend.
pub fn backend_name(backend: Backend) -> &'static str {
    match backend {
        Backend::Classical => "classical",
        #[cfg(feature = "rnnoise")]
        Backend::Rnnoise => "rnnoise",
        #[cfg(feature = "deepfilter")]
        Backend::DeepFilter => "deepfilter",
        #[cfg(feature = "onnx")]
        Backend::Onnx => "onnx",
        #[cfg(feature = "mpsenet")]
        Backend::MpSenet => "mpsenet",
        #[cfg(feature = "bsrnn")]
        Backend::Bsrnn => "bsrnn",
        #[cfg(feature = "mossformer2")]
        Backend::Mossformer2 => "mossformer2",
        #[cfg(feature = "sgmse")]
        Backend::Sgmse => "sgmse",
        #[cfg(feature = "gtcrn")]
        Backend::Gtcrn => "gtcrn",
    }
}

/// Whether a backend needs a user-selected ONNX file rather than embedded or
/// managed weights.
pub fn requires_external_model(backend: Backend) -> bool {
    match backend {
        #[cfg(feature = "onnx")]
        Backend::Onnx => true,
        #[cfg(feature = "mpsenet")]
        Backend::MpSenet => true,
        #[cfg(feature = "bsrnn")]
        Backend::Bsrnn => true,
        #[cfg(feature = "mossformer2")]
        Backend::Mossformer2 => true,
        #[cfg(feature = "sgmse")]
        Backend::Sgmse => true,
        _ => false,
    }
}

/// Select a backend consistently for CLI and graphical processing.
pub fn select_backend(
    choice: BackendChoice,
    _duration_seconds: f64,
    _quality: Option<&str>,
) -> Backend {
    if let BackendChoice::Explicit(backend) = choice {
        return backend;
    }
    #[cfg(feature = "deepfilter")]
    {
        let high_quality = _quality.is_some_and(|quality| {
            matches!(
                quality.to_ascii_lowercase().as_str(),
                "high" | "ultra" | "max" | "highest"
            )
        });
        if high_quality || _duration_seconds <= 10.0 * 60.0 {
            return Backend::DeepFilter;
        }
    }
    #[cfg(feature = "rnnoise")]
    {
        return Backend::Rnnoise;
    }
    #[allow(unreachable_code)]
    Backend::Classical
}

/// Select the low-latency backend preferred for realtime sessions.
pub fn select_live_backend() -> Backend {
    #[cfg(feature = "rnnoise")]
    {
        return Backend::Rnnoise;
    }
    #[allow(unreachable_code)]
    Backend::Classical
}

/// Fill backend options that can be resolved from the managed model library.
pub fn resolve_backend_options(
    _backend: Backend,
    #[allow(unused_mut)] mut options: BackendOptions,
) -> Result<BackendOptions, String> {
    options
        .validate_config(_backend)
        .map_err(|error| error.to_string())?;
    #[cfg(feature = "gtcrn")]
    if _backend == Backend::Gtcrn && options.onnx.is_none() {
        let model = crate::models::find("gtcrn").expect("built-in GTCRN manifest entry");
        options.onnx = Some(OnnxModelConfig {
            path: crate::models::verify(model).map_err(|_| {
                "GTCRN model is not installed; run `denoize models install gtcrn`".to_string()
            })?,
            sample_rate: model.sample_rate,
        });
    }
    options.validate_resolved_resources(_backend)?;
    Ok(options)
}

/// Process already-decoded audio with common backend and delivery behavior.
pub fn process_audio(
    audio: &mut Audio,
    options: ProcessingOptions,
) -> Result<ProcessingResult, String> {
    let backend = options
        .validate_config(audio)
        .map_err(|error| error.to_string())?;
    let backend_options = resolve_backend_options(backend, options.backend_options)?;
    backend_options.validate_resolved_resources(backend)?;
    let (mut working, elapsed) = crate::process_audio_copy_with_backend_config(
        audio,
        options.denoiser,
        backend,
        &backend_options,
    )?;
    let loudness = options
        .loudness_lufs
        .map(|target| crate::loudness::normalize(&mut working, target, options.true_peak_dbtp))
        .transpose()?;
    *audio = working;
    Ok(ProcessingResult {
        backend,
        elapsed,
        loudness,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn audio(sample_rate: u32) -> Audio {
        Audio {
            sample_rate,
            channels: vec![vec![0.25; 256]],
            bits_per_sample: 32,
            sample_format: hound::SampleFormat::Float,
            channel_mask: None,
        }
    }

    fn options() -> ProcessingOptions {
        ProcessingOptions {
            backend: BackendChoice::Explicit(Backend::Classical),
            quality: None,
            denoiser: DenoiserConfig::default(48_000),
            backend_options: BackendOptions::default(),
            loudness_lufs: None,
            true_peak_dbtp: -1.0,
        }
    }

    #[test]
    fn explicit_backend_is_preserved() {
        assert_eq!(
            select_backend(
                BackendChoice::Explicit(Backend::Classical),
                10.0,
                Some("ultra")
            ),
            Backend::Classical
        );
    }

    #[test]
    fn automatic_backend_is_compiled() {
        let selected = select_backend(BackendChoice::Auto, 10.0, None);
        assert!(Backend::available_names().contains(&backend_name(selected)));
    }

    #[test]
    fn live_backend_is_compiled() {
        assert!(Backend::available_names().contains(&backend_name(select_live_backend())));
    }

    #[test]
    fn classical_does_not_require_external_weights() {
        assert!(!requires_external_model(Backend::Classical));
    }

    #[test]
    fn processing_validation_uses_the_decoded_sample_rate() {
        let mut options = options();
        options.denoiser.sample_rate = 0;
        assert_eq!(
            options.validate_config(&audio(48_000)).unwrap(),
            Backend::Classical
        );
        assert!(matches!(
            options.validate_config(&audio(0)),
            Err(ConfigError::InvalidValue {
                field: "sample_rate",
                ..
            })
        ));
        assert!(options
            .validate_config(&audio(crate::config::MAX_SAMPLE_RATE))
            .is_ok());
        assert!(options
            .validate_config(&audio(crate::config::MAX_SAMPLE_RATE + 1))
            .is_err());
    }

    #[test]
    fn quality_is_a_closed_case_insensitive_contract() {
        for quality in ["high", "ultra", "max", "highest", "HIGH"] {
            let mut options = options();
            options.quality = Some(quality.into());
            assert!(options.validate_config(&audio(48_000)).is_ok());
        }
        let mut invalid = options();
        invalid.quality = Some("fastest".into());
        assert!(matches!(
            invalid.validate_config(&audio(48_000)),
            Err(ConfigError::InvalidValue {
                field: "quality",
                ..
            })
        ));
    }

    #[test]
    fn loudness_and_true_peak_ranges_are_checked() {
        for target in [-70.0, 0.0] {
            let mut options = options();
            options.loudness_lufs = Some(target);
            assert!(options.validate_config(&audio(48_000)).is_ok());
        }
        for peak in [-20.0, 0.0] {
            let mut options = options();
            options.true_peak_dbtp = peak;
            assert!(options.validate_config(&audio(48_000)).is_ok());
        }
        for target in [f64::NAN, f64::INFINITY, -70.01, 0.01] {
            let mut options = options();
            options.loudness_lufs = Some(target);
            assert!(matches!(
                options.validate_config(&audio(48_000)),
                Err(ConfigError::InvalidValue {
                    field: "loudness_lufs",
                    ..
                })
            ));
        }
        for peak in [f64::NAN, f64::INFINITY, -20.01, 0.01] {
            let mut options = options();
            options.true_peak_dbtp = peak;
            assert!(matches!(
                options.validate_config(&audio(48_000)),
                Err(ConfigError::InvalidValue {
                    field: "true_peak_dbtp",
                    ..
                })
            ));
        }
    }

    #[test]
    fn invalid_options_do_not_mutate_decoded_audio() {
        let mut decoded = audio(48_000);
        decoded.channels[0][0] = 2.0;
        let before = decoded.channels.clone();
        let mut options = options();
        options.denoiser.strength = f64::NAN;

        let error = process_audio(&mut decoded, options).unwrap_err();

        assert!(error.contains("`strength`"));
        assert_eq!(decoded.channels, before);
    }

    #[cfg(feature = "onnx")]
    #[test]
    fn missing_model_resource_does_not_mutate_decoded_audio() {
        let mut decoded = audio(48_000);
        decoded.channels[0][0] = 2.0;
        let before = decoded.channels.clone();
        let mut options = options();
        options.backend = BackendChoice::Explicit(Backend::Onnx);
        options.backend_options.onnx = Some(crate::OnnxModelConfig {
            path: "model-that-does-not-exist.onnx".into(),
            sample_rate: 48_000,
        });

        let error = process_audio(&mut decoded, options).unwrap_err();

        assert!(
            error.contains("model does not exist"),
            "unexpected error: {error}"
        );
        assert_eq!(decoded.channels, before);
    }

    #[test]
    fn loudness_failure_does_not_commit_processed_audio() {
        let mut decoded = audio(48_000);
        decoded.channels[0].fill(0.0);
        decoded.channels[0][0] = 0.5;
        let before = decoded.clone();
        let mut options = options();
        options.loudness_lufs = Some(-23.0);

        let error = process_audio(&mut decoded, options).unwrap_err();

        assert!(
            error.contains("undefined") || error.contains("too short"),
            "unexpected error: {error}"
        );
        assert_eq!(decoded.sample_rate, before.sample_rate);
        assert_eq!(decoded.channels, before.channels);
        assert_eq!(decoded.bits_per_sample, before.bits_per_sample);
        assert_eq!(decoded.sample_format, before.sample_format);
        assert_eq!(decoded.channel_mask, before.channel_mask);
    }

    #[test]
    fn invalid_quality_precedes_missing_model_configuration() {
        #[cfg(feature = "onnx")]
        {
            let mut options = options();
            options.backend = BackendChoice::Explicit(Backend::Onnx);
            options.quality = Some("unknown".into());
            let error = options.validate_config(&audio(48_000)).unwrap_err();
            assert!(matches!(
                error,
                ConfigError::InvalidValue {
                    field: "quality",
                    ..
                }
            ));
        }
    }

    #[cfg(feature = "onnx")]
    #[test]
    fn generic_onnx_requires_external_weights() {
        assert!(requires_external_model(Backend::Onnx));
    }
}