#[derive(Clone, Debug, PartialEq)]
pub struct FrameTimes {
times: Vec<f64>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Gap {
pub after: usize,
pub missing: usize,
pub seconds: f64,
}
impl FrameTimes {
pub fn constant(frames: usize, rate: f64, start: f64) -> Self {
let rate = if rate.is_finite() && rate > 0.0 { rate } else { 30.0 };
Self { times: (0..frames).map(|i| start + i as f64 / rate).collect() }
}
pub fn from_times(mut times: Vec<f64>) -> Self {
times.retain(|t| t.is_finite());
times.sort_by(f64::total_cmp);
Self { times }
}
pub fn len(&self) -> usize {
self.times.len()
}
pub fn is_empty(&self) -> bool {
self.times.is_empty()
}
pub fn time(&self, index: usize) -> Option<f64> {
self.times.get(index).copied()
}
pub fn span(&self) -> Option<(f64, f64)> {
Some((*self.times.first()?, *self.times.last()?))
}
pub fn frame_at(&self, t: f64) -> Option<usize> {
if self.times.is_empty() {
return None;
}
Some(self.times.partition_point(|&x| x <= t).saturating_sub(1))
}
pub fn median_interval(&self) -> Option<f64> {
let mut d: Vec<f64> = self.times.windows(2).map(|w| w[1] - w[0]).filter(|d| *d > 0.0).collect();
if d.is_empty() {
return None;
}
d.sort_by(f64::total_cmp);
Some(d[d.len() / 2])
}
pub fn rate(&self) -> Option<f64> {
self.median_interval().map(|d| 1.0 / d)
}
pub fn gaps(&self) -> Vec<Gap> {
let Some(m) = self.median_interval() else { return Vec::new() };
self.times
.windows(2)
.enumerate()
.filter_map(|(i, w)| {
let d = w[1] - w[0];
(d > 1.5 * m).then(|| Gap { after: i + 1, missing: ((d / m).round() as usize).saturating_sub(1).max(1), seconds: d })
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn frames_are_found_by_time_and_gaps_are_seen() {
let times: Vec<f64> = (0..20).filter(|i| *i != 5 && *i != 6).map(|i| i as f64 * 0.01).collect();
let t = FrameTimes::from_times(times);
assert_eq!(t.len(), 18);
assert_eq!(t.frame_at(-1.0), Some(0));
assert_eq!(t.frame_at(0.045), Some(4));
assert_eq!(t.frame_at(0.065), Some(4), "frames 5 and 6 are missing: frame 4 is still shown");
assert_eq!(t.frame_at(0.07), Some(5));
assert_eq!(t.frame_at(99.0), Some(17));
assert!((t.rate().unwrap() - 100.0).abs() < 1e-6);
assert_eq!(t.gaps(), vec![Gap { after: 5, missing: 2, seconds: t.time(5).unwrap() - t.time(4).unwrap() }]);
assert_eq!(FrameTimes::constant(3, 50.0, 1.0).span(), Some((1.0, 1.04)));
}
}