polyvoice 0.14.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
//! Sliding-window utilities for batch and streaming pipelines.
//!
//! [`WindowIter`] iterates over `(start, end)` sample ranges.
//! [`WindowBuffer`] buffers incoming samples and yields full windows.

/// Invalid window geometry rejected by [`WindowIter::try_new`] and
/// [`WindowBuffer::try_new`].
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowError {
    /// Window size must be positive.
    #[error("win must be > 0")]
    ZeroWindow,
    /// Hop size must be positive.
    #[error("hop must be > 0")]
    ZeroHop,
    /// Hop must not exceed the window size.
    #[error("hop ({hop}) must be <= win ({win})")]
    HopExceedsWindow {
        /// Requested hop size in samples.
        hop: usize,
        /// Requested window size in samples.
        win: usize,
    },
}

/// Iterator over fixed-size (or partial trailing) windows.
///
/// Yields `(start_sample, end_sample)` pairs.  `end_sample` is exclusive.
#[derive(Debug, Clone)]
pub struct WindowIter {
    start: usize,
    total: usize,
    win: usize,
    hop: usize,
    include_partial: bool,
}

impl WindowIter {
    /// { win > 0 && hop > 0 }
    /// `pub fn new(total: usize, win: usize, hop: usize) -> Self`
    /// { true }
    /// Create a new iterator.
    ///
    /// * `total` — total number of samples in the audio region.
    /// * `win`   — window size in samples.
    /// * `hop`   — hop size in samples.
    ///
    /// # Panics
    ///
    /// Panics if `win == 0`, `hop == 0`, or `hop > win`.
    /// Use [`try_new`](Self::try_new) for a fallible alternative.
    #[allow(clippy::panic)] // Documented convenience over `try_new`.
    pub fn new(total: usize, win: usize, hop: usize) -> Self {
        match Self::try_new(total, win, hop) {
            Ok(iter) => iter,
            Err(e) => panic!("WindowIter::new: {e}"),
        }
    }

    /// { true }
    /// `pub fn try_new(total: usize, win: usize, hop: usize) -> Result<Self, WindowError>`
    /// { ret.is_ok() == (win > 0 && hop > 0 && hop <= win) }
    /// Fallible constructor: validates the window geometry and returns
    /// [`WindowError`] instead of panicking.
    ///
    /// * `total` — total number of samples in the audio region.
    /// * `win`   — window size in samples (must be > 0).
    /// * `hop`   — hop size in samples (must be > 0 and <= `win`).
    pub fn try_new(total: usize, win: usize, hop: usize) -> Result<Self, WindowError> {
        if win == 0 {
            return Err(WindowError::ZeroWindow);
        }
        if hop == 0 {
            return Err(WindowError::ZeroHop);
        }
        if hop > win {
            return Err(WindowError::HopExceedsWindow { hop, win });
        }
        Ok(Self {
            start: 0,
            total,
            win,
            hop,
            include_partial: false,
        })
    }

    /// { true }
    /// `pub fn include_partial(mut self) -> Self`
    /// { ret.include_partial }
    /// Include a final partial window if the region does not divide evenly.
    pub fn include_partial(mut self) -> Self {
        self.include_partial = true;
        self
    }
}

impl Iterator for WindowIter {
    type Item = (usize, usize);

    fn next(&mut self) -> Option<Self::Item> {
        if self.start >= self.total {
            return None;
        }
        let end = if self.include_partial {
            (self.start + self.win).min(self.total)
        } else if self.start + self.win > self.total {
            return None;
        } else {
            self.start + self.win
        };
        let item = (self.start, end);
        self.start += self.hop;
        Some(item)
    }
}

/// Streaming buffer that accumulates samples and yields full windows.
///
/// Maintains an internal ring-like buffer.  Call [`extend`](Self::extend)
/// with incoming chunks, then repeatedly call [`try_pop`](Self::try_pop)
/// to consume every window that is ready.
#[derive(Debug, Clone)]
pub struct WindowBuffer {
    buf: Vec<f32>,
    win: usize,
    hop: usize,
    next_start: usize,
}

impl WindowBuffer {
    /// { win > 0 && hop > 0 }
    /// `pub fn new(win: usize, hop: usize) -> Self`
    /// { true }
    /// Create a new buffer.
    ///
    /// * `win` — window size in samples.
    /// * `hop` — hop size in samples.
    ///
    /// # Panics
    ///
    /// Panics if `win == 0`, `hop == 0`, or `hop > win`.
    /// Use [`try_new`](Self::try_new) for a fallible alternative.
    #[allow(clippy::panic)] // Documented convenience over `try_new`.
    pub fn new(win: usize, hop: usize) -> Self {
        match Self::try_new(win, hop) {
            Ok(buf) => buf,
            Err(e) => panic!("WindowBuffer::new: {e}"),
        }
    }

    /// { true }
    /// `pub fn try_new(win: usize, hop: usize) -> Result<Self, WindowError>`
    /// { ret.is_ok() == (win > 0 && hop > 0 && hop <= win) }
    /// Fallible constructor: validates the window geometry and returns
    /// [`WindowError`] instead of panicking.
    ///
    /// * `win` — window size in samples (must be > 0).
    /// * `hop` — hop size in samples (must be > 0 and <= `win`).
    pub fn try_new(win: usize, hop: usize) -> Result<Self, WindowError> {
        if win == 0 {
            return Err(WindowError::ZeroWindow);
        }
        if hop == 0 {
            return Err(WindowError::ZeroHop);
        }
        if hop > win {
            return Err(WindowError::HopExceedsWindow { hop, win });
        }
        Ok(Self {
            buf: Vec::new(),
            win,
            hop,
            next_start: 0,
        })
    }

    /// { true }
    /// `pub fn extend(&mut self, samples: &[f32])`
    /// { true }
    /// Append samples to the buffer.
    pub fn extend(&mut self, samples: &[f32]) {
        self.buf.extend_from_slice(samples);
    }

    /// { true }
    /// `pub fn try_pop(&mut self) -> Option<(usize, Vec<f32>)>`
    /// { ret.as_ref().map(|(_, w)| w.len() == self.win) }
    /// Return the next full window if one is available.
    ///
    /// Returns `Some((global_start, buf[..win].to_vec()))` where `global_start` is the
    /// sample offset of this window relative to the start of the stream.
    /// The window is cloned so that the internal buffer can advance immediately.
    pub fn try_pop(&mut self) -> Option<(usize, Vec<f32>)> {
        if self.buf.len() < self.win {
            return None;
        }
        let start = self.next_start;
        let window = self.buf[..self.win].to_vec();
        self.next_start += self.hop;
        self.buf.drain(..self.hop);
        Some((start, window))
    }

    /// { true }
    /// `pub fn flush(&mut self) -> Option<(usize, Vec<f32>)>`
    /// { true }
    /// Zero-pad the remaining buffer to `win` and return the final window.
    ///
    /// Returns `None` if the buffer is empty.
    pub fn flush(&mut self) -> Option<(usize, Vec<f32>)> {
        if self.buf.is_empty() {
            return None;
        }
        let start = self.next_start;
        let mut padded = self.buf.clone();
        if padded.len() < self.win {
            padded.resize(self.win, 0.0f32);
        }
        self.buf.clear();
        Some((start, padded))
    }

    /// { true }
    /// `pub fn is_empty(&self) -> bool`
    /// { ret == (self.buf.len() == 0) }
    /// Whether the buffer is currently empty.
    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    /// { true }
    /// `pub fn len(&self) -> usize`
    /// { ret == self.buf.len() }
    /// Current length of the buffered samples.
    pub fn len(&self) -> usize {
        self.buf.len()
    }

    /// { true }
    /// `pub fn clear(&mut self)`
    /// { self.buf.is_empty() }
    /// Clear all buffered samples and reset the next-start offset.
    pub fn clear(&mut self) {
        self.buf.clear();
    }

    /// { true }
    /// `pub fn set_next_start(&mut self, start: usize)`
    /// { self.next_start == start }
    /// Set the next-start offset to a specific value.
    pub fn set_next_start(&mut self, start: usize) {
        self.next_start = start;
    }
}

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

    #[test]
    fn window_iter_complete_only() {
        let ranges: Vec<_> = WindowIter::new(10, 3, 2).collect();
        assert_eq!(ranges, vec![(0, 3), (2, 5), (4, 7), (6, 9)]);
    }

    #[test]
    fn window_iter_include_partial() {
        let ranges: Vec<_> = WindowIter::new(10, 3, 2).include_partial().collect();
        assert_eq!(ranges, vec![(0, 3), (2, 5), (4, 7), (6, 9), (8, 10)]);
    }

    #[test]
    fn window_iter_empty() {
        let ranges: Vec<_> = WindowIter::new(0, 3, 2).collect();
        assert!(ranges.is_empty());
    }

    #[test]
    fn window_iter_shorter_than_win() {
        let ranges: Vec<_> = WindowIter::new(2, 3, 2).collect();
        assert!(ranges.is_empty());
        let ranges: Vec<_> = WindowIter::new(2, 3, 2).include_partial().collect();
        assert_eq!(ranges, vec![(0, 2)]);
    }

    #[test]
    fn window_buffer_pop_and_flush() {
        let mut buf = WindowBuffer::new(4, 2);
        buf.extend(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
        let (s1, w1) = buf.try_pop().unwrap();
        assert_eq!(s1, 0);
        assert_eq!(w1, vec![1.0, 2.0, 3.0, 4.0]);
        let (s2, w2) = buf.try_pop().unwrap();
        assert_eq!(s2, 2);
        assert_eq!(w2, vec![3.0, 4.0, 5.0, 6.0]);
        assert!(buf.try_pop().is_none());

        let (s3, w3) = buf.flush().unwrap();
        assert_eq!(s3, 4);
        assert_eq!(w3, vec![5.0, 6.0, 0.0, 0.0]);
    }

    #[test]
    fn window_buffer_flush_empty() {
        let mut buf = WindowBuffer::new(4, 2);
        assert!(buf.flush().is_none());
    }

    #[test]
    #[should_panic(expected = "WindowIter::new: win must be > 0")]
    fn window_iter_rejects_zero_win() {
        let _ = WindowIter::new(10, 0, 1);
    }

    #[test]
    #[should_panic(expected = "WindowIter::new: hop must be > 0")]
    fn window_iter_rejects_zero_hop() {
        let _ = WindowIter::new(10, 2, 0);
    }

    #[test]
    #[should_panic(expected = "WindowIter::new: hop (3) must be <= win (2)")]
    fn window_iter_rejects_hop_greater_than_win() {
        let _ = WindowIter::new(10, 2, 3);
    }

    #[test]
    #[should_panic(expected = "WindowBuffer::new: win must be > 0")]
    fn window_buffer_rejects_zero_win() {
        let _ = WindowBuffer::new(0, 1);
    }

    #[test]
    #[should_panic(expected = "WindowBuffer::new: hop must be > 0")]
    fn window_buffer_rejects_zero_hop() {
        let _ = WindowBuffer::new(4, 0);
    }

    #[test]
    #[should_panic(expected = "WindowBuffer::new: hop (5) must be <= win (4)")]
    fn window_buffer_rejects_hop_greater_than_win() {
        let _ = WindowBuffer::new(4, 5);
    }

    #[test]
    fn window_iter_try_new_reports_geometry_errors() {
        assert_eq!(
            WindowIter::try_new(10, 0, 1).unwrap_err(),
            WindowError::ZeroWindow
        );
        assert_eq!(
            WindowIter::try_new(10, 2, 0).unwrap_err(),
            WindowError::ZeroHop
        );
        assert_eq!(
            WindowIter::try_new(10, 2, 3).unwrap_err(),
            WindowError::HopExceedsWindow { hop: 3, win: 2 }
        );
        assert!(WindowIter::try_new(10, 2, 1).is_ok());
    }

    #[test]
    fn window_buffer_try_new_reports_geometry_errors() {
        assert_eq!(
            WindowBuffer::try_new(0, 1).unwrap_err(),
            WindowError::ZeroWindow
        );
        assert_eq!(
            WindowBuffer::try_new(4, 0).unwrap_err(),
            WindowError::ZeroHop
        );
        assert_eq!(
            WindowBuffer::try_new(4, 5).unwrap_err(),
            WindowError::HopExceedsWindow { hop: 5, win: 4 }
        );
        assert!(WindowBuffer::try_new(4, 2).is_ok());
    }
}

#[allow(clippy::unwrap_used)]
#[cfg(test)]
mod prop_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: 1000,
            ..ProptestConfig::default()
        })]

        /// All yielded (start, end) pairs satisfy start < end.
        #[test]
        fn window_iter_start_less_than_end(
            total in 0usize..10000,
            (win, hop) in (1usize..5000)
                .prop_flat_map(|w| (Just(w), 1usize..=w)),
            include_partial in any::<bool>(),
        ) {
            let iter = if include_partial {
                WindowIter::new(total, win, hop).include_partial()
            } else {
                WindowIter::new(total, win, hop)
            };
            for (start, end) in iter {
                prop_assert!(start < end, "start {} must be < end {}", start, end);
            }
        }

        /// All yielded end values are <= total.
        #[test]
        fn window_iter_end_le_total(
            total in 0usize..10000,
            (win, hop) in (1usize..5000)
                .prop_flat_map(|w| (Just(w), 1usize..=w)),
            include_partial in any::<bool>(),
        ) {
            let iter = if include_partial {
                WindowIter::new(total, win, hop).include_partial()
            } else {
                WindowIter::new(total, win, hop)
            };
            for (_, end) in iter {
                prop_assert!(end <= total, "end {} must be <= total {}", end, total);
            }
        }

        /// When include_partial = false, every yielded window has end - start == win.
        #[test]
        fn window_iter_no_partial(
            total in 0usize..10000,
            (win, hop) in (1usize..5000)
                .prop_flat_map(|w| (Just(w), 1usize..=w)),
        ) {
            for (start, end) in WindowIter::new(total, win, hop) {
                prop_assert_eq!(
                    end - start,
                    win,
                    "window [{}..{}] must be full size {} when include_partial=false",
                    start, end, win
                );
            }
        }

        /// When include_partial = true, every yielded window satisfies
        /// end == (start + win).min(total). The final window reaches total
        /// when total > 0.
        #[test]
        fn window_iter_partial_last(
            total in 0usize..10000,
            (win, hop) in (1usize..5000)
                .prop_flat_map(|w| (Just(w), 1usize..=w)),
        ) {
            let ranges: Vec<_> = WindowIter::new(total, win, hop).include_partial().collect();
            for (start, end) in &ranges {
                prop_assert!(
                    *end == (*start + win).min(total),
                    "window [{}..{}] must equal ({} + {}).min({})",
                    start, end, start, win, total
                );
            }
        }
    }
}