From 3d6a106f1eb97b481aabce7c834425b1d1de81f1 Mon Sep 17 00:00:00 2001 From: Chris Frankland-Wright Date: Sat, 2 Aug 2025 12:28:59 +0100 Subject: [PATCH] refactor: Use rational numbers for LtcFrame frame rate Co-authored-by: aider (gemini/gemini-2.5-pro) --- src/sync_logic.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/sync_logic.rs b/src/sync_logic.rs index 87e3d3d..06b14f0 100644 --- a/src/sync_logic.rs +++ b/src/sync_logic.rs @@ -1,10 +1,22 @@ use crate::config::Config; use chrono::{DateTime, Local, Timelike, Utc}; +use num_rational::Ratio; use regex::Captures; use std::collections::VecDeque; const EWMA_ALPHA: f64 = 0.1; +fn get_frame_rate_ratio(rate_str: &str) -> Option> { + match rate_str { + "23.98" => Some(Ratio::new(24000, 1001)), + "24.00" => Some(Ratio::new(24, 1)), + "25.00" => Some(Ratio::new(25, 1)), + "29.97" => Some(Ratio::new(30000, 1001)), + "30.00" => Some(Ratio::new(30, 1)), + _ => None, + } +} + #[derive(Clone, Debug)] pub struct LtcFrame { pub status: String, @@ -12,7 +24,7 @@ pub struct LtcFrame { pub minutes: u32, pub seconds: u32, pub frames: u32, - pub frame_rate: f64, + pub frame_rate: Ratio, pub timestamp: DateTime, // arrival stamp } @@ -24,7 +36,7 @@ impl LtcFrame { minutes: caps[3].parse().ok()?, seconds: caps[4].parse().ok()?, frames: caps[5].parse().ok()?, - frame_rate: caps[6].parse().ok()?, + frame_rate: get_frame_rate_ratio(&caps[6])?, timestamp, }) }