denoize 0.49.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! 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, BackendSession, 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,
}

/// Fully selected and validated processing options.
///
/// This is the single effective configuration consumed by both execution and
/// batch recipe fingerprinting. In particular, the denoiser sample rate has
/// already been replaced with the decoded rate, compatibility sanitization
/// has already been applied, and managed backend resources have been resolved.
#[derive(Clone, Debug)]
pub struct ResolvedProcessingOptions {
    pub backend: Backend,
    pub denoiser: DenoiserConfig,
    pub backend_options: BackendOptions,
    pub loudness_lufs: Option<f64>,
    pub true_peak_dbtp: f64,
}

impl ResolvedProcessingOptions {
    /// Validate an already-resolved plan without opening or modifying audio.
    pub fn validate_config(&self) -> Result<(), String> {
        self.denoiser
            .validate_config()
            .map_err(|error| error.to_string())?;
        self.backend_options
            .validate_resolved_resources(self.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",
            )
            .map_err(|error| error.to_string())?;
        }
        validate_finite_range(
            "true_peak_dbtp",
            self.true_peak_dbtp,
            -20.0,
            0.0,
            "a finite value in -20..=0 dBTP",
        )
        .map_err(|error| error.to_string())
    }
}

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

    /// Resolve every effective option without modifying decoded audio.
    pub fn resolve(self, audio: &Audio) -> Result<ResolvedProcessingOptions, String> {
        resolve_processing_options(audio, self)
    }
}

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 catalog = crate::models::active_catalog()?;
        let model = catalog
            .find("gtcrn")
            .ok_or_else(|| "active model catalog has no unambiguous GTCRN package".to_string())?;
        options.onnx = Some(OnnxModelConfig {
            path: crate::models::verify_catalog_model(model).map_err(|error| {
                format!(
                    "GTCRN managed model is unavailable ({error}); run `denoize models install gtcrn`"
                )
            })?,
            sample_rate: model.sample_rate(),
        });
    }
    options.validate_resolved_resources(_backend)?;
    Ok(options)
}

/// Select, validate, sanitize, and resolve the exact processing configuration
/// that execution will consume.
pub fn resolve_processing_options(
    audio: &Audio,
    options: ProcessingOptions,
) -> Result<ResolvedProcessingOptions, String> {
    let backend = options
        .validate_config(audio)
        .map_err(|error| error.to_string())?;
    let mut denoiser = options.denoiser;
    denoiser.sample_rate = audio.sample_rate;
    // `Denoiser::try_new`, used by the classical backend, validates first and
    // then applies this compatibility sanitization. Resolve it here so recipe
    // hashing and execution cannot observe two different configurations.
    denoiser
        .validate_config()
        .map_err(|error| error.to_string())?;
    let denoiser = denoiser.sanitized();
    let backend_options = resolve_backend_options(backend, options.backend_options)?;
    Ok(ResolvedProcessingOptions {
        backend,
        denoiser,
        backend_options,
        loudness_lufs: options.loudness_lufs,
        true_peak_dbtp: options.true_peak_dbtp,
    })
}

/// Process decoded audio using an already-resolved effective configuration.
pub fn process_audio_resolved(
    audio: &mut Audio,
    options: &ResolvedProcessingOptions,
) -> Result<ProcessingResult, String> {
    let session = BackendSession::prepare(options.backend, options.backend_options.clone())?;
    process_audio_resolved_with_session(audio, options, &session)
}

/// Process decoded audio with an already-prepared backend session.
pub fn process_audio_resolved_with_session(
    audio: &mut Audio,
    options: &ResolvedProcessingOptions,
    session: &BackendSession,
) -> Result<ProcessingResult, String> {
    if audio.sample_rate != options.denoiser.sample_rate {
        return Err(format!(
            "resolved processing sample rate {} Hz does not match decoded audio rate {} Hz",
            options.denoiser.sample_rate, audio.sample_rate
        ));
    }
    options.validate_config()?;
    if session.backend() != options.backend || session.options() != &options.backend_options {
        return Err("prepared backend session does not match resolved processing options".into());
    }
    let (mut working, elapsed) =
        crate::process_audio_copy_with_backend_session(audio, options.denoiser.clone(), session)?;
    let loudness = options
        .loudness_lufs
        .map(|target| crate::loudness::normalize(&mut working, target, options.true_peak_dbtp))
        .transpose()?;
    *audio = working;
    Ok(ProcessingResult {
        backend: options.backend,
        elapsed,
        loudness,
    })
}

/// Process already-decoded audio with common backend and delivery behavior.
pub fn process_audio(
    audio: &mut Audio,
    options: ProcessingOptions,
) -> Result<ProcessingResult, String> {
    let resolved = resolve_processing_options(audio, options)?;
    process_audio_resolved(audio, &resolved)
}

#[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 resolution_records_the_exact_sanitized_execution_config() {
        let mut options = options();
        options.denoiser.sample_rate = 1;
        options.denoiser.smoothing = 1.0;

        let resolved = resolve_processing_options(&audio(44_100), options).unwrap();

        assert_eq!(resolved.backend, Backend::Classical);
        assert_eq!(resolved.denoiser.sample_rate, 44_100);
        assert_eq!(resolved.denoiser.smoothing, 0.95);
        resolved.validate_config().unwrap();
    }

    #[test]
    fn resolved_sample_rate_mismatch_is_transactional() {
        let mut decoded = audio(48_000);
        decoded.channels[0][0] = 2.0;
        let before = decoded.clone();
        let resolved = resolve_processing_options(&decoded, options()).unwrap();
        decoded.sample_rate = 44_100;

        let error = process_audio_resolved(&mut decoded, &resolved).unwrap_err();

        assert!(error.contains("does not match decoded audio rate"));
        assert_eq!(decoded.channels, before.channels);
        assert_eq!(decoded.sample_rate, 44_100);
    }

    #[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));
    }
}