use activityspan; use gaphold; fn linnet_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn linnet_magnitudes(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= 0.0 && value - value == 0.0) { return false; } i += 1; } true } fn linnet_compute(levels: vector, threshold: f64, gap: f64, frame_ms: f64) -> vector { let span = activityspan_bounds(levels, threshold); let segments = gaphold_segments(levels, threshold, gap); if len(span) == 0 || len(segments) == 0 { return []; } if at(span, 0) < 0.0 { return [-1, -1, 0, 0]; } [at(span, 0) * frame_ms, at(span, 1) * frame_ms, at(segments, 0), (at(span, 2) + at(segments, 1)) * frame_ms] } // Summarize speech-activity intervals from a precomputed frame-level envelope. // Returns [first active millisecond, exclusive active end, segment count, retained active-or-bridged milliseconds]. A silent trace returns [-1,-1,0,0]. pub fn linnet_segment(levels: vector, threshold: f64, gap: f64, frame_ms: f64) -> vector { if !linnet_magnitudes(levels) { return []; } if !(threshold >= 0.0 && threshold - threshold == 0.0) { return []; } if !(gap >= 0.0 && gap <= 9007199254740991.0 && gap % 1.0 == 0.0) { return []; } if !(frame_ms > 0.0 && frame_ms - frame_ms == 0.0) { return []; } let result = linnet_compute(levels, threshold, gap, frame_ms); if !linnet_finite(result) { return []; } result }