denoize 0.87.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
//! Dedicated finite continuous-speech-separation and diarization adapter.
//!
//! Meeting separation is not accepted through the generic waveform adapter.
//! The authenticated graph must expose one fixed audio window, a bounded bank
//! of anonymous speaker tracks, per-track three-state activity probabilities,
//! and a global three-state assignment head.  The explicit contract prevents
//! a plausible-looking waveform graph from being mistaken for diarization.

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

pub(crate) const MAX_MEETING_TRACKS: usize = 8;
const PROBABILITY_CLASSES: usize = 3;
const PROBABILITY_SUM_TOLERANCE: f32 = 0.001;

#[derive(Clone, Copy, Debug)]
struct MeetingSpeakerGraphContract {
    input_channels: usize,
    tracks: usize,
    window_samples: usize,
    activity_frames: usize,
    audio_output: usize,
    track_activity_output: usize,
    meeting_state_output: usize,
}

/// One model-window result.  Track probabilities are ordered inactive,
/// uncertain, active. Meeting-state probabilities are ordered no-speech,
/// assigned, unknown.
pub(crate) struct MeetingSpeakerInference {
    pub tracks: Vec<Vec<f32>>,
    pub track_activity_probabilities: Vec<Vec<[f32; PROBABILITY_CLASSES]>>,
    pub meeting_state_probabilities: Vec<[f32; PROBABILITY_CLASSES]>,
}

/// Parsed, authenticated, numerically checked meeting-separation graph.
pub(crate) struct MeetingSpeakerModel {
    runtime: AcceleratorRuntime,
    contract: MeetingSpeakerGraphContract,
    runnable: SharedRunnable,
}

impl MeetingSpeakerModel {
    pub(crate) fn load_runtime_package(
        package: &RuntimeModelPackage,
        runtime: AcceleratorRuntime,
    ) -> Result<Self, String> {
        let manifest = package
            .manifest_v2()
            .ok_or("meeting speaker tracks require 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 meeting-speaker ONNX graph from authenticated package {}: {error:#}",
                    package.package_path().display()
                )
            })?;
        reader.finish().map_err(|error| {
            format!(
                "failed to authenticate meeting-speaker 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.input_channels, contract.window_samples)).into(),
            )
            .map_err(model_error)?;
        model
            .set_output_fact(
                contract.audio_output,
                f32::fact(tvec!(1, contract.tracks, contract.window_samples)).into(),
            )
            .map_err(model_error)?;
        model
            .set_output_fact(
                contract.track_activity_output,
                f32::fact(tvec!(
                    1,
                    contract.tracks,
                    contract.activity_frames,
                    PROBABILITY_CLASSES
                ))
                .into(),
            )
            .map_err(model_error)?;
        model
            .set_output_fact(
                contract.meeting_state_output,
                f32::fact(tvec!(1, contract.activity_frames, PROBABILITY_CLASSES)).into(),
            )
            .map_err(model_error)?;
        let model = model.into_typed().map_err(model_error)?;
        let runnable = super::tract_runtime::prepare(
            model,
            runtime,
            "meeting speaker-track separation model",
        )?;
        Ok(Self {
            runtime,
            contract,
            runnable,
        })
    }

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

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

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

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

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

        let activity = outputs[self.contract.track_activity_output]
            .to_plain_array_view::<f32>()
            .map_err(model_error)?;
        let expected_activity = self
            .contract
            .tracks
            .checked_mul(self.contract.activity_frames)
            .and_then(|value| value.checked_mul(PROBABILITY_CLASSES))
            .ok_or_else(|| "meeting-speaker activity shape overflow".to_string())?;
        if activity.len() != expected_activity {
            return Err(format!(
                "meeting-speaker graph returned {} activity values; expected {expected_activity}",
                activity.len()
            ));
        }
        let activity = activity.iter().copied().collect::<Vec<_>>();
        let mut track_activity_probabilities = Vec::new();
        track_activity_probabilities
            .try_reserve_exact(self.contract.tracks)
            .map_err(|_| "unable to reserve meeting activity probabilities".to_string())?;
        for track in 0..self.contract.tracks {
            let mut frames = Vec::new();
            frames
                .try_reserve_exact(self.contract.activity_frames)
                .map_err(|_| "unable to reserve meeting activity frames".to_string())?;
            for frame in 0..self.contract.activity_frames {
                let index = (track * self.contract.activity_frames + frame) * PROBABILITY_CLASSES;
                let probabilities = [activity[index], activity[index + 1], activity[index + 2]];
                validate_probabilities(probabilities, "track activity")?;
                frames.push(probabilities);
            }
            track_activity_probabilities.push(frames);
        }

        let meeting = outputs[self.contract.meeting_state_output]
            .to_plain_array_view::<f32>()
            .map_err(model_error)?;
        let expected_meeting = self
            .contract
            .activity_frames
            .checked_mul(PROBABILITY_CLASSES)
            .ok_or_else(|| "meeting-speaker state shape overflow".to_string())?;
        if meeting.len() != expected_meeting {
            return Err(format!(
                "meeting-speaker graph returned {} state values; expected {expected_meeting}",
                meeting.len()
            ));
        }
        let meeting = meeting.iter().copied().collect::<Vec<_>>();
        let mut meeting_state_probabilities = Vec::new();
        meeting_state_probabilities
            .try_reserve_exact(self.contract.activity_frames)
            .map_err(|_| "unable to reserve meeting state probabilities".to_string())?;
        for frame in 0..self.contract.activity_frames {
            let index = frame * PROBABILITY_CLASSES;
            let probabilities = [meeting[index], meeting[index + 1], meeting[index + 2]];
            validate_probabilities(probabilities, "meeting state")?;
            meeting_state_probabilities.push(probabilities);
        }
        Ok(MeetingSpeakerInference {
            tracks,
            track_activity_probabilities,
            meeting_state_probabilities,
        })
    }
}

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

fn validate_runtime_package_contract(
    manifest: &RuntimeModelPackageManifestV2,
) -> Result<MeetingSpeakerGraphContract, 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() != 3
        || !manifest.state_pairs.is_empty()
        || !matches!(
            manifest.frontend.channels.policy.as_str(),
            "independent-mono" | "microphone-array"
        )
        || (manifest.frontend.channels.policy == "microphone-array"
            && manifest.frontend.channels.geometry.is_none())
    {
        return Err(
            "meeting-speaker package must declare a finite, stateless, mono-or-fixed-array graph with one input and three outputs"
                .into(),
        );
    }
    let input = &manifest.tensors.inputs[0];
    if input.role != "audio" || input.element_type != "float32" || input.optional {
        return Err("meeting-speaker input must be required float32 audio".into());
    }
    let input_shape = exact_axes(
        input,
        &[("batch", Some(1)), ("channel", None), ("sample", None)],
        "audio input",
    )?;
    let input_channels = input_shape[1];
    let window_samples = input_shape[2];
    if input_channels == 0
        || input_channels > 64
        || window_samples < 256
        || window_samples > 16_777_216
    {
        return Err("meeting-speaker input geometry is outside bounded limits".into());
    }
    if manifest.frontend.channels.policy == "independent-mono" && input_channels != 1 {
        return Err("meeting-speaker independent-mono packages require one input channel".into());
    }
    if manifest.latency.frame_samples != window_samples as u64
        || manifest.latency.hop_samples == 0
        || manifest.latency.hop_samples > manifest.latency.frame_samples
    {
        return Err(
            "meeting-speaker package latency frame/hop must bind the fixed model window".into(),
        );
    }

    let audio_output = unique_role(&manifest.tensors.outputs, "audio")?;
    let diagnostics = manifest
        .tensors
        .outputs
        .iter()
        .enumerate()
        .filter(|(_, tensor)| tensor.role == "diagnostic")
        .collect::<Vec<_>>();
    if diagnostics.len() != 2 {
        return Err("meeting-speaker package requires two diagnostic outputs".into());
    }
    let track_activity_output = diagnostics
        .iter()
        .find(|(_, tensor)| tensor.name == "track_activity")
        .map(|(index, _)| *index)
        .ok_or("meeting-speaker package omits track_activity diagnostics")?;
    let meeting_state_output = diagnostics
        .iter()
        .find(|(_, tensor)| tensor.name == "meeting_state")
        .map(|(index, _)| *index)
        .ok_or("meeting-speaker package omits meeting_state diagnostics")?;

    let output = &manifest.tensors.outputs[audio_output];
    let output_shape = exact_axes(
        output,
        &[("batch", Some(1)), ("channel", None), ("sample", None)],
        "audio output",
    )?;
    let tracks = output_shape[1];
    if !(1..=MAX_MEETING_TRACKS).contains(&tracks) || output_shape[2] != window_samples {
        return Err(format!(
            "meeting-speaker output requires 1..={MAX_MEETING_TRACKS} tracks and exact input duration"
        ));
    }
    let activity = &manifest.tensors.outputs[track_activity_output];
    let activity_shape = exact_axes(
        activity,
        &[
            ("batch", Some(1)),
            ("channel", None),
            ("frame", None),
            ("feature", Some(PROBABILITY_CLASSES)),
        ],
        "track_activity",
    )?;
    if activity_shape[1] != tracks
        || activity_shape[2] == 0
        || activity_shape[2] > window_samples
        || window_samples % activity_shape[2] != 0
    {
        return Err(
            "meeting-speaker track_activity must cover each track with evenly spaced frames".into(),
        );
    }
    let activity_hop = window_samples / activity_shape[2];
    if manifest.latency.hop_samples % activity_hop as u64 != 0 {
        return Err(
            "meeting-speaker model hop must align to the authenticated activity frame clock".into(),
        );
    }
    let state = &manifest.tensors.outputs[meeting_state_output];
    let state_shape = exact_axes(
        state,
        &[
            ("batch", Some(1)),
            ("frame", None),
            ("feature", Some(PROBABILITY_CLASSES)),
        ],
        "meeting_state",
    )?;
    if state_shape[1] != activity_shape[2] {
        return Err("meeting-speaker global state must use the track-activity frame clock".into());
    }
    Ok(MeetingSpeakerGraphContract {
        input_channels,
        tracks,
        window_samples,
        activity_frames: activity_shape[2],
        audio_output,
        track_activity_output,
        meeting_state_output,
    })
}

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!(
            "meeting-speaker 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!(
            "meeting-speaker {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 meeting-speaker {label} shape"))?;
    for (axis, (kind, exact)) in tensor.axes.iter().zip(expected) {
        if axis.kind != *kind {
            return Err(format!(
                "meeting-speaker {label} axes do not match the closed contract"
            ));
        }
        let fixed = axis
            .fixed
            .ok_or_else(|| format!("meeting-speaker {label} requires fixed tensor dimensions"))?;
        let fixed = usize::try_from(fixed)
            .map_err(|_| format!("meeting-speaker {label} dimension is too large"))?;
        if exact.is_some_and(|value| value != fixed) {
            return Err(format!(
                "meeting-speaker {label} dimension does not match the closed contract"
            ));
        }
        shape.push(fixed);
    }
    Ok(shape)
}

fn validate_probabilities(values: [f32; PROBABILITY_CLASSES], label: &str) -> 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(format!(
            "meeting-speaker graph returned invalid {label} probabilities"
        ));
    }
    Ok(())
}

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