denoize 0.88.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
//! Dedicated finite adapter for mixture-preserving music restoration.
//!
//! This graph is deliberately narrower than the generic waveform adapter. A
//! fixed mono or stereo program window produces one candidate program with the
//! exact same geometry plus a three-state `repair_state` head ordered bypass,
//! uncertain, apply. It cannot emit stems or silently reinterpret channels.

use super::tract_runtime::SharedRunnable;
use crate::{AcceleratorRuntime, RuntimeModelPackage, RuntimeModelPackageManifestV2};
use tract_onnx::prelude::*;

const REPAIR_STATE_CLASSES: usize = 3;
const PROBABILITY_SUM_TOLERANCE: f32 = 0.001;

#[derive(Clone, Copy, Debug)]
struct MusicRestorationGraphContract {
    channels: usize,
    window_samples: usize,
    state_frames: usize,
    audio_output: usize,
    state_output: usize,
}

/// One authenticated model-window result. State probabilities are ordered
/// bypass, uncertain, apply.
pub(crate) struct MusicRestorationInference {
    pub candidate: Vec<Vec<f32>>,
    pub state_probabilities: Vec<[f32; REPAIR_STATE_CLASSES]>,
}

/// Parsed, authenticated, numerically checked program-restoration graph.
pub(crate) struct MusicRestorationModel {
    runtime: AcceleratorRuntime,
    contract: MusicRestorationGraphContract,
    runnable: SharedRunnable,
}

impl MusicRestorationModel {
    pub(crate) fn load_runtime_package(
        package: &RuntimeModelPackage,
        runtime: AcceleratorRuntime,
    ) -> Result<Self, String> {
        let manifest = package
            .manifest_v2()
            .ok_or("music restoration requires authenticated runtime model package v2")?;
        let contract = validate_runtime_package_contract(manifest)?;
        let profile = package
            .precision_profile_for(runtime)?
            .expect("v2 packages always select a precision profile");
        let mut reader = package.open_model_reader_for(runtime)?;
        let template = tract_onnx::onnx()
            .model_for_read(&mut reader)
            .map_err(|error| {
                format!(
                    "failed to load music-restoration ONNX graph from authenticated package {}: {error:#}",
                    package.package_path().display()
                )
            })?;
        reader.finish().map_err(|error| {
            format!(
                "failed to authenticate music-restoration ONNX bytes from package {}: {error}",
                package.package_path().display()
            )
        })?;
        let mut inspected = template.clone();
        if inspected.analyse(false).is_ok() {
            super::onnx::validate_v2_graph_contract(&inspected, manifest)?;
        } else {
            super::onnx::validate_v2_graph_contract(&template, manifest)?;
        }
        let vectors = package
            .numerical_vectors_for(runtime)?
            .expect("v2 precision profiles always carry numerical vectors");
        super::onnx::validate_v2_numerical_vectors(
            &template, manifest, profile, &vectors, runtime,
        )?;

        let mut model = template;
        model
            .set_input_fact(
                0,
                f32::fact(tvec!(1, contract.channels, contract.window_samples)).into(),
            )
            .map_err(model_error)?;
        model
            .set_output_fact(
                contract.audio_output,
                f32::fact(tvec!(1, contract.channels, contract.window_samples)).into(),
            )
            .map_err(model_error)?;
        model
            .set_output_fact(
                contract.state_output,
                f32::fact(tvec!(1, contract.state_frames, REPAIR_STATE_CLASSES)).into(),
            )
            .map_err(model_error)?;
        let model = model.into_typed().map_err(model_error)?;
        let runnable =
            super::tract_runtime::prepare(model, runtime, "music program-restoration model")?;
        Ok(Self {
            runtime,
            contract,
            runnable,
        })
    }

    pub(crate) fn channels(&self) -> usize {
        self.contract.channels
    }

    pub(crate) fn window_samples(&self) -> usize {
        self.contract.window_samples
    }

    pub(crate) fn state_frames(&self) -> usize {
        self.contract.state_frames
    }

    pub(crate) fn process(
        &self,
        channels: &[Vec<f32>],
    ) -> Result<MusicRestorationInference, String> {
        if channels.len() != self.contract.channels
            || channels
                .iter()
                .any(|channel| channel.len() != self.contract.window_samples)
        {
            return Err(format!(
                "music-restoration graph requires {} channels by {} samples",
                self.contract.channels, self.contract.window_samples
            ));
        }
        if channels
            .iter()
            .flatten()
            .any(|sample| !sample.is_finite() || !(-1.0..=1.0).contains(sample))
        {
            return Err("music-restoration input contains an invalid normalized sample".into());
        }
        let input = channels.iter().flatten().copied().collect::<Vec<_>>();
        let tensor = Tensor::from_shape(
            &tvec!(1, self.contract.channels, self.contract.window_samples),
            &input,
        )
        .map_err(model_error)?;
        let outputs = self
            .runnable
            .run(tvec!(tensor.into_tvalue()))
            .map_err(model_error)?;
        if outputs.len() != 2 {
            return Err(format!(
                "music-restoration graph returned {} outputs; expected 2",
                outputs.len()
            ));
        }
        let audio = outputs[self.contract.audio_output]
            .to_plain_array_view::<f32>()
            .map_err(model_error)?;
        let expected_audio = self
            .contract
            .channels
            .checked_mul(self.contract.window_samples)
            .ok_or_else(|| "music-restoration audio shape overflow".to_string())?;
        if audio.len() != expected_audio {
            return Err(format!(
                "music-restoration graph returned {} audio samples; expected {expected_audio}",
                audio.len()
            ));
        }
        if audio
            .iter()
            .any(|sample| !sample.is_finite() || !(-1.0..=1.0).contains(sample))
        {
            return Err("music-restoration graph returned invalid normalized audio".into());
        }
        let flat_audio = audio.iter().copied().collect::<Vec<_>>();
        let mut candidate = Vec::new();
        candidate
            .try_reserve_exact(self.contract.channels)
            .map_err(|_| "unable to reserve music-restoration candidate channels".to_string())?;
        for channel in 0..self.contract.channels {
            let start = channel * self.contract.window_samples;
            candidate.push(flat_audio[start..start + self.contract.window_samples].to_vec());
        }

        let states = outputs[self.contract.state_output]
            .to_plain_array_view::<f32>()
            .map_err(model_error)?;
        let expected_states = self
            .contract
            .state_frames
            .checked_mul(REPAIR_STATE_CLASSES)
            .ok_or_else(|| "music-restoration state shape overflow".to_string())?;
        if states.len() != expected_states {
            return Err(format!(
                "music-restoration graph returned {} state values; expected {expected_states}",
                states.len()
            ));
        }
        let states = states.iter().copied().collect::<Vec<_>>();
        let mut state_probabilities = Vec::new();
        state_probabilities
            .try_reserve_exact(self.contract.state_frames)
            .map_err(|_| "unable to reserve music-restoration state frames".to_string())?;
        for frame in 0..self.contract.state_frames {
            let index = frame * REPAIR_STATE_CLASSES;
            let probabilities = [states[index], states[index + 1], states[index + 2]];
            validate_probabilities(probabilities)?;
            state_probabilities.push(probabilities);
        }
        Ok(MusicRestorationInference {
            candidate,
            state_probabilities,
        })
    }
}

impl std::fmt::Debug for MusicRestorationModel {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("MusicRestorationModel")
            .field("runtime", &self.runtime)
            .field("contract", &self.contract)
            .finish_non_exhaustive()
    }
}

fn validate_runtime_package_contract(
    manifest: &RuntimeModelPackageManifestV2,
) -> Result<MusicRestorationGraphContract, String> {
    if manifest.runtime.mode != "finite"
        || !(8_000..=192_000).contains(&manifest.runtime.sample_rate_hz)
        || manifest.tensors.inputs.len() != 1
        || manifest.tensors.outputs.len() != 2
        || !manifest.state_pairs.is_empty()
        || manifest.frontend.channels.policy != "program-multichannel"
        || manifest.frontend.channels.geometry.is_some()
    {
        return Err(
            "music-restoration package must declare a finite, stateless program graph with one input and two outputs"
                .into(),
        );
    }
    let input = &manifest.tensors.inputs[0];
    if input.role != "audio" || input.element_type != "float32" || input.optional {
        return Err("music-restoration input must be required float32 audio".into());
    }
    let input_shape = exact_axes(
        input,
        &[("batch", Some(1)), ("channel", None), ("sample", None)],
        "audio input",
    )?;
    let channels = input_shape[1];
    let window_samples = input_shape[2];
    if !(1..=2).contains(&channels) || window_samples < 256 || window_samples > 16_777_216 {
        return Err("music-restoration input geometry is outside bounded limits".into());
    }
    validate_channel_roles(manifest, channels)?;
    if manifest.latency.frame_samples != window_samples as u64
        || manifest.latency.hop_samples == 0
        || manifest.latency.hop_samples > manifest.latency.frame_samples
    {
        return Err(
            "music-restoration package latency frame/hop must bind the fixed model window".into(),
        );
    }

    let audio_output = unique_role(&manifest.tensors.outputs, "audio")?;
    let state_output = manifest
        .tensors
        .outputs
        .iter()
        .enumerate()
        .find(|(_, tensor)| tensor.role == "diagnostic" && tensor.name == "repair_state")
        .map(|(index, _)| index)
        .ok_or("music-restoration package omits repair_state diagnostics")?;
    if manifest
        .tensors
        .outputs
        .iter()
        .filter(|tensor| tensor.role == "diagnostic")
        .count()
        != 1
    {
        return Err("music-restoration package requires exactly one diagnostic output".into());
    }
    let output_shape = exact_axes(
        &manifest.tensors.outputs[audio_output],
        &[("batch", Some(1)), ("channel", None), ("sample", None)],
        "audio output",
    )?;
    if output_shape != input_shape {
        return Err("music-restoration output must preserve exact input geometry".into());
    }
    let state_shape = exact_axes(
        &manifest.tensors.outputs[state_output],
        &[
            ("batch", Some(1)),
            ("frame", None),
            ("feature", Some(REPAIR_STATE_CLASSES)),
        ],
        "repair_state",
    )?;
    let state_frames = state_shape[1];
    if state_frames == 0
        || state_frames > window_samples
        || !window_samples.is_multiple_of(state_frames)
    {
        return Err(
            "music-restoration repair_state must evenly cover the authenticated window".into(),
        );
    }
    let state_hop = window_samples / state_frames;
    if !usize::try_from(manifest.latency.hop_samples)
        .map_err(|_| "music-restoration hop is too large".to_string())?
        .is_multiple_of(state_hop)
    {
        return Err(
            "music-restoration model hop must align to the repair-state frame clock".into(),
        );
    }
    Ok(MusicRestorationGraphContract {
        channels,
        window_samples,
        state_frames,
        audio_output,
        state_output,
    })
}

fn validate_channel_roles(
    manifest: &RuntimeModelPackageManifestV2,
    channels: usize,
) -> Result<(), String> {
    let roles = &manifest.frontend.channels.roles;
    let valid = match channels {
        1 => roles.len() == 1 && roles[0].channel_index == 0 && roles[0].role == "program-center",
        2 => {
            roles.len() == 2
                && roles[0].channel_index == 0
                && roles[0].role == "program-left"
                && roles[1].channel_index == 1
                && roles[1].role == "program-right"
        }
        _ => false,
    };
    if !valid {
        return Err(
            "music-restoration package requires exact mono-center or ordered stereo L/R roles"
                .into(),
        );
    }
    Ok(())
}

fn unique_role(
    tensors: &[crate::RuntimeModelTensorContractV2],
    role: &str,
) -> Result<usize, String> {
    let matches = tensors
        .iter()
        .enumerate()
        .filter(|(_, tensor)| tensor.role == role)
        .map(|(index, _)| index)
        .collect::<Vec<_>>();
    if matches.len() != 1 {
        return Err(format!(
            "music-restoration package requires exactly one {role} output"
        ));
    }
    Ok(matches[0])
}

fn exact_axes(
    tensor: &crate::RuntimeModelTensorContractV2,
    expected: &[(&str, Option<usize>)],
    label: &str,
) -> Result<Vec<usize>, String> {
    if tensor.element_type != "float32"
        || tensor.optional
        || tensor.state_id.is_some()
        || tensor.axes.len() != expected.len()
    {
        return Err(format!(
            "music-restoration {label} has the wrong element type or rank"
        ));
    }
    let mut shape = Vec::new();
    shape
        .try_reserve_exact(expected.len())
        .map_err(|_| format!("unable to reserve music-restoration {label} shape"))?;
    for (axis, (kind, exact)) in tensor.axes.iter().zip(expected) {
        if axis.kind != *kind {
            return Err(format!(
                "music-restoration {label} axes do not match the closed contract"
            ));
        }
        let fixed = axis
            .fixed
            .ok_or_else(|| format!("music-restoration {label} requires fixed dimensions"))?;
        let fixed = usize::try_from(fixed)
            .map_err(|_| format!("music-restoration {label} dimension is too large"))?;
        if exact.is_some_and(|value| value != fixed) {
            return Err(format!(
                "music-restoration {label} dimension does not match the closed contract"
            ));
        }
        shape.push(fixed);
    }
    Ok(shape)
}

fn validate_probabilities(values: [f32; REPAIR_STATE_CLASSES]) -> Result<(), String> {
    if values
        .iter()
        .any(|value| !value.is_finite() || !(0.0..=1.0).contains(value))
        || (values.iter().sum::<f32>() - 1.0).abs() > PROBABILITY_SUM_TOLERANCE
    {
        return Err("music-restoration graph returned invalid state probabilities".into());
    }
    Ok(())
}

fn model_error(error: impl std::fmt::Display) -> String {
    format!("music-restoration ONNX inference failed: {error}")
}