refactor: Use rational numbers for accurate frame rate calculations

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
Chris Frankland-Wright 2025-08-02 12:26:17 +01:00
parent b71e13d4c4
commit a1da396874
6 changed files with 41 additions and 17 deletions

View file

@ -129,8 +129,9 @@ impl LtcState {
/// Convert average jitter into frames (rounded).
pub fn average_frames(&self) -> i64 {
if let Some(frame) = &self.latest {
let ms_per_frame = 1000.0 / frame.frame_rate;
(self.average_jitter() as f64 / ms_per_frame).round() as i64
let jitter_ms_ratio = Ratio::new(self.average_jitter(), 1);
let frames_ratio = jitter_ms_ratio * frame.frame_rate / Ratio::new(1000, 1);
frames_ratio.round().to_integer()
} else {
0
}
@ -192,7 +193,7 @@ mod tests {
minutes: m,
seconds: s,
frames: 0,
frame_rate: 25.0,
frame_rate: Ratio::new(25, 1),
timestamp: Utc::now(),
}
}