use burn::config::Config;
use crate::kits::speech::diarization::SpeakerActivity;
#[derive(Config, Debug, PartialEq)]
pub struct SpeakerCutConfig {
#[config(default = "2.0")]
pub min_window_seconds: f64,
#[config(default = "0.5")]
pub min_turn_seconds: f64,
#[config(default = "0.5")]
pub threshold: f64,
}
impl Default for SpeakerCutConfig {
fn default() -> Self {
Self::new()
}
}
pub fn speaker_cut(
activity: &SpeakerActivity,
start: usize,
hop: usize,
frames: usize,
sample_rate: usize,
config: &SpeakerCutConfig,
) -> Option<usize> {
let to_frames = |seconds: f64| ((seconds * sample_rate as f64) / hop as f64).round() as usize;
let min_window = to_frames(config.min_window_seconds).max(1);
let min_turn = to_frames(config.min_turn_seconds).max(1);
let mut runs: Vec<(Option<usize>, usize, usize)> = Vec::new();
for k in 0..frames {
let who = dominant(activity, start + k * hop, hop, config.threshold);
match runs.last_mut() {
Some((speaker, _, len)) if *speaker == who => *len += 1,
_ => runs.push((who, k, 1)),
}
}
let mut current = None;
for (speaker, at, len) in runs {
let Some(speaker) = speaker else {
continue;
};
if len < min_turn {
continue;
}
match current {
None => current = Some(speaker),
Some(c) if c == speaker => {}
Some(_) if at >= min_window => return Some(at),
Some(_) => current = Some(speaker),
}
}
None
}
fn dominant(
activity: &SpeakerActivity,
start: usize,
len: usize,
threshold: f64,
) -> Option<usize> {
let window = activity.window(start, start + len);
if window.is_empty() {
return None;
}
let n = window.frames() as f32;
let mut means = vec![0.0f32; window.n_speakers];
for i in 0..window.frames() {
for (m, p) in means.iter_mut().zip(window.frame(i)) {
*m += p / n;
}
}
let (speaker, &best) = means.iter().enumerate().max_by(|a, b| a.1.total_cmp(b.1))?;
(best as f64 >= threshold).then_some(speaker)
}
#[cfg(test)]
mod tests {
use super::*;
fn activity(stretches: &[(Option<usize>, usize)]) -> SpeakerActivity {
let mut probs = Vec::new();
for &(who, n) in stretches {
for _ in 0..n {
probs.extend((0..2).map(|k| if who == Some(k) { 0.9 } else { 0.05 }));
}
}
SpeakerActivity::try_new(0, 10, 2, probs).unwrap()
}
fn config() -> SpeakerCutConfig {
SpeakerCutConfig::new()
}
#[test]
fn test_cuts_at_the_first_turn_past_the_minimum() {
let a = activity(&[(Some(0), 300), (Some(1), 100), (Some(0), 100)]);
assert_eq!(speaker_cut(&a, 0, 10, 500, 1000, &config()), Some(300));
}
#[test]
fn test_short_stretches_and_silence_do_not_cut() {
let a = activity(&[(Some(0), 250), (Some(1), 30), (None, 50), (Some(0), 170)]);
assert_eq!(speaker_cut(&a, 0, 10, 500, 1000, &config()), None);
}
#[test]
fn test_an_early_turn_moves_the_current_speaker() {
let a = activity(&[(Some(0), 100), (Some(1), 200), (Some(0), 200)]);
assert_eq!(speaker_cut(&a, 0, 10, 500, 1000, &config()), Some(300));
}
#[test]
fn test_frames_past_the_activity_are_nobodys() {
let a = activity(&[(Some(0), 250)]);
assert_eq!(speaker_cut(&a, 0, 10, 500, 1000, &config()), None);
let a = activity(&[(Some(0), 300), (Some(1), 100)]);
assert_eq!(speaker_cut(&a, 1000, 10, 300, 1000, &config()), Some(200));
}
}