polyvoice 0.18.0

Speaker diarization for Rust — who spoke when. ONNX path optional: default features are empty (ort-free BYO-embedder core); enable onnx for Silero VAD, WeSpeaker embeddings, and Pyannote segmentation.
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
use super::*;
use crate::types::{DiarizationResult, SpeakerId, SpeakerTurn, TimeRange};

fn sample_result() -> DiarizationResult {
    let turns = vec![SpeakerTurn {
        speaker: SpeakerId(0),
        time: TimeRange {
            start: 0.5,
            end: 2.0,
        },
        text: Some("hello".to_owned()),
        stable: true,
    }];
    DiarizationResult::new(vec![], turns, 1)
}

#[test]
fn render_result_rejects_unknown_format() {
    let result = sample_result();
    assert_eq!(
        render_result(&result, 42),
        Err(PolyvoiceStatus::InvalidArg as c_int)
    );
    assert_eq!(
        render_result(&result, -1),
        Err(PolyvoiceStatus::InvalidArg as c_int)
    );
}

#[test]
fn render_result_covers_every_format() {
    let result = sample_result();
    for (format, marker) in [
        (PolyvoiceFormat::Json as c_int, "num_speakers"),
        (PolyvoiceFormat::Rttm as c_int, "SPEAKER audio 1"),
        (PolyvoiceFormat::Srt as c_int, "00:00:00,500"),
        (PolyvoiceFormat::Vtt as c_int, "WEBVTT"),
        (PolyvoiceFormat::Txt as c_int, "SPEAKER_00: hello"),
    ] {
        let rendered = render_result(&result, format).unwrap();
        assert!(
            rendered.contains(marker),
            "format {format} missing marker {marker:?}: {rendered}"
        );
    }
}

#[test]
fn cache_dir_rejects_parent_dir_traversal() {
    for path in ["../evil", "models/../../evil", ".."] {
        assert_eq!(
            validate_cache_dir(path),
            Err(PolyvoiceStatus::InvalidArg as c_int),
            "traversal path must be rejected: {path}"
        );
    }
}

#[test]
fn cache_dir_accepts_absolute_and_relative_paths() {
    for path in ["/opt/polyvoice/models", "models/cache", "."] {
        assert!(
            validate_cache_dir(path).is_ok(),
            "legitimate cache dir must be accepted: {path}"
        );
    }
}

// ---------------------------------------------------------------------
// Entry-point tests: create/destroy lifecycle, run/run_format argument
// validation, and error-code mapping. Pipelines are built from the
// pipeline_v2 mock components so no ONNX models or network are needed.
// ---------------------------------------------------------------------

use crate::pipeline_v2::mocks::{MockClusterer, MockEmbedder, MockSegmenter, raw_segment};
use crate::resegmentation::OverlapResegmenter;
use std::ptr;

const INVALID_ARG: c_int = PolyvoiceStatus::InvalidArg as c_int;
const AUDIO_TOO_LONG: c_int = PolyvoiceStatus::AudioTooLong as c_int;
const INFERENCE: c_int = PolyvoiceStatus::Inference as c_int;
const REGISTRY: c_int = PolyvoiceStatus::Registry as c_int;

/// A pipeline handle backed by constant-output mock components: no model
/// files, no registry, no network.
fn mock_handle(segments: Vec<crate::segmentation::RawSegment>) -> *mut PolyvoicePipeline {
    let cfg = PipelineConfig {
        profile: Profile::Custom,
        resegment_overlap: false,
        min_speech_secs: 0.0,
        max_gap_secs: 0.0,
        ..PipelineConfig::default()
    };
    let pipeline = Pipeline::from_components(
        cfg,
        Box::new(MockSegmenter { segments }),
        Box::new(MockEmbedder::default()),
        Box::new(MockClusterer::default()),
        Box::new(OverlapResegmenter::default()),
    );
    Box::into_raw(Box::new(PolyvoicePipeline { inner: pipeline }))
}

/// Embedder that always fails, to drive the pipeline-error mapping.
struct FailingEmbedder;

impl crate::embedder::Embedder for FailingEmbedder {
    fn dim(&self) -> usize {
        192
    }

    fn embed(&self, _audio: &[f32]) -> Result<Vec<f32>, crate::embedder::EmbedderError> {
        Err(crate::embedder::EmbedderError::AudioTooShort {
            actual_secs: 0.0,
            min_secs: 0.01,
        })
    }
}

fn failing_handle() -> *mut PolyvoicePipeline {
    let cfg = PipelineConfig {
        profile: Profile::Custom,
        resegment_overlap: false,
        min_speech_secs: 0.0,
        max_gap_secs: 0.0,
        ..PipelineConfig::default()
    };
    let pipeline = Pipeline::from_components(
        cfg,
        Box::new(MockSegmenter {
            segments: vec![raw_segment(0.0, 1.0, 0, false)],
        }),
        Box::new(FailingEmbedder),
        Box::new(MockClusterer::default()),
        Box::new(OverlapResegmenter::default()),
    );
    Box::into_raw(Box::new(PolyvoicePipeline { inner: pipeline }))
}

#[test]
fn create_null_out_handle_returns_invalid_arg() {
    // SAFETY: null out_handle is the condition under test.
    let rc = unsafe { polyvoice_pipeline_create(0, ptr::null(), ptr::null_mut()) };
    assert_eq!(rc, INVALID_ARG);
}

#[test]
fn create_invalid_profile_returns_invalid_arg() {
    for profile in [-1, 2, 42] {
        let mut handle: *mut PolyvoicePipeline = ptr::null_mut();
        // SAFETY: handle is non-null; invalid profile is the condition under test.
        let rc = unsafe { polyvoice_pipeline_create(profile, ptr::null(), &mut handle) };
        assert_eq!(rc, INVALID_ARG, "profile {profile} must be rejected");
        assert!(handle.is_null(), "handle must stay null on error");
    }
}

#[test]
fn create_rejects_non_utf8_cache_dir() {
    let dir = CString::new([0xFFu8, 0xFE]).unwrap();
    let mut handle: *mut PolyvoicePipeline = ptr::null_mut();
    // SAFETY: handle is non-null; dir is a valid nul-terminated (non-UTF-8) string.
    let rc = unsafe { polyvoice_pipeline_create(1, dir.as_ptr(), &mut handle) };
    assert_eq!(rc, INVALID_ARG);
    assert!(handle.is_null());
}

#[test]
fn create_rejects_parent_dir_traversal() {
    let dir = CString::new("../evil").unwrap();
    let mut handle: *mut PolyvoicePipeline = ptr::null_mut();
    // SAFETY: handle is non-null; dir is a valid nul-terminated string. The
    // traversal check runs before any registry or model access.
    let rc = unsafe { polyvoice_pipeline_create(0, dir.as_ptr(), &mut handle) };
    assert_eq!(rc, INVALID_ARG);
    assert!(handle.is_null());
}

#[test]
fn create_unwritable_cache_dir_maps_to_registry() {
    // A cache dir nested under a regular file cannot be created, so the
    // registry constructor fails before any download is attempted.
    let tmp = tempfile::tempdir().unwrap();
    let file = tmp.path().join("blocker");
    std::fs::write(&file, b"not a directory").unwrap();
    let bad = file.join("child");
    let dir = CString::new(bad.to_str().unwrap()).unwrap();
    let mut handle: *mut PolyvoicePipeline = ptr::null_mut();
    // SAFETY: handle is non-null; dir is a valid nul-terminated string.
    let rc = unsafe { polyvoice_pipeline_create(0, dir.as_ptr(), &mut handle) };
    assert_eq!(rc, REGISTRY);
    assert!(handle.is_null());
}

#[test]
fn run_null_out_params_return_invalid_arg() {
    let handle = mock_handle(vec![]);
    let samples = [0.0f32; 16000];
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // SAFETY: handle is valid, samples is valid; each null out-param is the
    // condition under test.
    let rc = unsafe {
        polyvoice_pipeline_run(
            handle,
            samples.as_ptr(),
            samples.len(),
            16000,
            ptr::null_mut(),
            &mut out_len,
        )
    };
    assert_eq!(rc, INVALID_ARG);
    // SAFETY: same contract as above.
    let rc = unsafe {
        polyvoice_pipeline_run(
            handle,
            samples.as_ptr(),
            samples.len(),
            16000,
            &mut out,
            ptr::null_mut(),
        )
    };
    assert_eq!(rc, INVALID_ARG);
    assert!(out.is_null());
    // SAFETY: handle was created by mock_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn run_null_pipeline_returns_invalid_arg() {
    let samples = [0.0f32; 16000];
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // SAFETY: null pipeline is the condition under test.
    let rc = unsafe {
        polyvoice_pipeline_run(
            ptr::null_mut(),
            samples.as_ptr(),
            samples.len(),
            16000,
            &mut out,
            &mut out_len,
        )
    };
    assert_eq!(rc, INVALID_ARG);
    assert!(out.is_null());
}

#[test]
fn run_null_samples_returns_invalid_arg() {
    let handle = mock_handle(vec![]);
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // SAFETY: handle is valid; null samples is the condition under test.
    let rc =
        unsafe { polyvoice_pipeline_run(handle, ptr::null(), 0, 16000, &mut out, &mut out_len) };
    assert_eq!(rc, INVALID_ARG);
    assert!(out.is_null());
    // SAFETY: handle was created by mock_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn run_rejects_out_of_range_sample_rate() {
    let handle = mock_handle(vec![]);
    let samples = [0.0f32; 16000];
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // SAFETY: handle and samples are valid; 100 Hz is outside the validated
    // sample-rate range.
    let rc = unsafe {
        polyvoice_pipeline_run(
            handle,
            samples.as_ptr(),
            samples.len(),
            100,
            &mut out,
            &mut out_len,
        )
    };
    assert_eq!(rc, INVALID_ARG);
    assert!(out.is_null());
    // SAFETY: handle was created by mock_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn run_unsupported_sample_rate_maps_to_invalid_arg() {
    // 8 kHz is a valid `SampleRate` but the pipeline only runs at its
    // configured rate, so the UnsupportedSampleRate error maps to InvalidArg.
    let handle = mock_handle(vec![]);
    let samples = [0.0f32; 8000];
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // SAFETY: handle and samples are valid.
    let rc = unsafe {
        polyvoice_pipeline_run(
            handle,
            samples.as_ptr(),
            samples.len(),
            8000,
            &mut out,
            &mut out_len,
        )
    };
    assert_eq!(rc, INVALID_ARG);
    assert!(out.is_null());
    // SAFETY: handle was created by mock_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn run_rejects_too_many_samples() {
    let handle = mock_handle(vec![]);
    let samples = [0.0f32; 16];
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // The length cap is checked before the slice is materialized, so the
    // small buffer is never read out of bounds.
    // SAFETY: handle is valid; the oversized length is the condition under test.
    let rc = unsafe {
        polyvoice_pipeline_run(
            handle,
            samples.as_ptr(),
            16000 * 3600 + 1,
            16000,
            &mut out,
            &mut out_len,
        )
    };
    assert_eq!(rc, AUDIO_TOO_LONG);
    assert!(out.is_null());
    // SAFETY: handle was created by mock_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn run_json_success_round_trip() {
    let handle = mock_handle(vec![
        raw_segment(0.0, 1.0, 0, false),
        raw_segment(1.5, 2.5, 0, false),
    ]);
    let samples = vec![0.0f32; 16000 * 3];
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // SAFETY: handle, samples and out-params are all valid.
    let rc = unsafe {
        polyvoice_pipeline_run(
            handle,
            samples.as_ptr(),
            samples.len(),
            16000,
            &mut out,
            &mut out_len,
        )
    };
    assert_eq!(rc, PolyvoiceStatus::Ok as c_int);
    assert!(!out.is_null());
    // SAFETY: out was returned by polyvoice_pipeline_run and is non-null.
    let json = unsafe { CStr::from_ptr(out) }
        .to_string_lossy()
        .into_owned();
    assert_eq!(json.len(), out_len);
    assert!(
        json.contains("\"num_speakers\":1"),
        "unexpected JSON: {json}"
    );
    assert!(json.contains("turns"));
    // SAFETY: out was returned by polyvoice_pipeline_run.
    unsafe { polyvoice_free_string(out, out_len) };
    // SAFETY: handle was created by mock_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn run_maps_pipeline_failure_to_inference() {
    let handle = failing_handle();
    let samples = vec![0.0f32; 16000 * 3];
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // SAFETY: handle, samples and out-params are all valid; the embedder
    // fails by construction.
    let rc = unsafe {
        polyvoice_pipeline_run(
            handle,
            samples.as_ptr(),
            samples.len(),
            16000,
            &mut out,
            &mut out_len,
        )
    };
    assert_eq!(rc, INFERENCE);
    assert!(out.is_null());
    // SAFETY: handle was created by failing_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn run_format_null_out_params_return_invalid_arg() {
    let samples = [0.0f32; 16000];
    let mut out: *mut c_char = ptr::null_mut();
    let mut out_len: usize = 0;
    // SAFETY: each null out-param is the condition under test; the pipeline
    // handle is never dereferenced because validation fails first.
    let rc = unsafe {
        polyvoice_pipeline_run_format(
            ptr::null_mut(),
            samples.as_ptr(),
            samples.len(),
            16000,
            PolyvoiceFormat::Json as c_int,
            ptr::null_mut(),
            &mut out_len,
        )
    };
    assert_eq!(rc, INVALID_ARG);
    // SAFETY: same contract as above.
    let rc = unsafe {
        polyvoice_pipeline_run_format(
            ptr::null_mut(),
            samples.as_ptr(),
            samples.len(),
            16000,
            PolyvoiceFormat::Json as c_int,
            &mut out,
            ptr::null_mut(),
        )
    };
    assert_eq!(rc, INVALID_ARG);
    assert!(out.is_null());
}

#[test]
fn run_format_rejects_unknown_format() {
    let handle = mock_handle(vec![]);
    let samples = [0.0f32; 16000];
    for format in [-1, 5, 42] {
        let mut out: *mut c_char = ptr::null_mut();
        let mut out_len: usize = 0;
        // SAFETY: handle, samples and out-params are valid; the format value
        // is the condition under test.
        let rc = unsafe {
            polyvoice_pipeline_run_format(
                handle,
                samples.as_ptr(),
                samples.len(),
                16000,
                format,
                &mut out,
                &mut out_len,
            )
        };
        assert_eq!(rc, INVALID_ARG, "format {format} must be rejected");
        assert!(out.is_null());
    }
    // SAFETY: handle was created by mock_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn run_format_renders_every_format() {
    let handle = mock_handle(vec![raw_segment(0.0, 1.0, 0, false)]);
    let samples = vec![0.0f32; 16000 * 2];
    for format in PolyvoiceFormat::Json as c_int..=PolyvoiceFormat::Txt as c_int {
        let mut out: *mut c_char = ptr::null_mut();
        let mut out_len: usize = 0;
        // SAFETY: handle, samples and out-params are all valid.
        let rc = unsafe {
            polyvoice_pipeline_run_format(
                handle,
                samples.as_ptr(),
                samples.len(),
                16000,
                format,
                &mut out,
                &mut out_len,
            )
        };
        assert_eq!(rc, PolyvoiceStatus::Ok as c_int, "format {format} failed");
        assert!(!out.is_null());
        // SAFETY: out was returned by polyvoice_pipeline_run_format and is non-null.
        let s = unsafe { CStr::from_ptr(out) }
            .to_string_lossy()
            .into_owned();
        assert_eq!(s.len(), out_len);
        // SAFETY: out was returned by polyvoice_pipeline_run_format.
        unsafe { polyvoice_free_string(out, out_len) };
    }
    // SAFETY: handle was created by mock_handle and is destroyed exactly once.
    unsafe { polyvoice_pipeline_destroy(handle) };
}

#[test]
fn destroy_null_is_noop() {
    // SAFETY: destroy explicitly accepts null.
    unsafe { polyvoice_pipeline_destroy(ptr::null_mut()) };
}

#[test]
fn free_string_null_is_noop() {
    // SAFETY: free_string explicitly accepts null.
    unsafe { polyvoice_free_string(ptr::null_mut(), 0) };
}