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

@ -11,6 +11,8 @@ use std::sync::{Arc, Mutex};
use crate::config::{self, Config};
use crate::sync_logic::{self, LtcState};
use crate::system;
use num_rational::Ratio;
use num_traits::ToPrimitive;
// Data structure for the main status response
#[derive(Serialize, Deserialize)]
@ -48,7 +50,7 @@ async fn get_status(data: web::Data<AppState>) -> impl Responder {
format!("{:02}:{:02}:{:02}:{:02}", f.hours, f.minutes, f.seconds, f.frames)
});
let frame_rate = state.latest.as_ref().map_or("".to_string(), |f| {
format!("{:.2}fps", f.frame_rate)
format!("{:.2}fps", f.frame_rate.to_f64().unwrap_or(0.0))
});
let now_local = Local::now();
@ -64,8 +66,9 @@ async fn get_status(data: web::Data<AppState>) -> impl Responder {
let avg_delta = state.get_ewma_clock_delta();
let mut delta_frames = 0;
if let Some(frame) = &state.latest {
let frame_ms = 1000.0 / frame.frame_rate;
delta_frames = ((avg_delta as f64 / frame_ms).round()) as i64;
let delta_ms_ratio = Ratio::new(avg_delta, 1);
let frames_ratio = delta_ms_ratio * frame.frame_rate / Ratio::new(1000, 1);
delta_frames = frames_ratio.round().to_integer();
}
let sync_status = sync_logic::get_sync_status(avg_delta, &config);
@ -239,7 +242,7 @@ mod tests {
minutes: 2,
seconds: 3,
frames: 4,
frame_rate: 25.0,
frame_rate: Ratio::new(25, 1),
timestamp: Utc::now(),
}),
lock_count: 10,

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(),
}
}

View file

@ -1,6 +1,7 @@
use crate::config::Config;
use crate::sync_logic::LtcFrame;
use chrono::{DateTime, Duration as ChronoDuration, Local, NaiveTime, TimeZone};
use num_rational::Ratio;
use std::process::Command;
/// Check if Chrony is active
@ -39,7 +40,8 @@ pub fn ntp_service_toggle(start: bool) {
pub fn calculate_target_time(frame: &LtcFrame, config: &Config) -> DateTime<Local> {
let today_local = Local::now().date_naive();
let ms = ((frame.frames as f64 / frame.frame_rate) * 1000.0).round() as u32;
let ms_ratio = Ratio::new(frame.frames as i64 * 1000, 1) / frame.frame_rate;
let ms = ms_ratio.round().to_integer() as u32;
let timecode = NaiveTime::from_hms_milli_opt(frame.hours, frame.minutes, frame.seconds, ms)
.expect("Invalid LTC timecode");
@ -56,7 +58,8 @@ pub fn calculate_target_time(frame: &LtcFrame, config: &Config) -> DateTime<Loca
+ ChronoDuration::minutes(offset.minutes)
+ ChronoDuration::seconds(offset.seconds);
// Frame offset needs to be converted to milliseconds
let frame_offset_ms = (offset.frames as f64 / frame.frame_rate * 1000.0).round() as i64;
let frame_offset_ms_ratio = Ratio::new(offset.frames * 1000, 1) / frame.frame_rate;
let frame_offset_ms = frame_offset_ms_ratio.round().to_integer();
dt_local + ChronoDuration::milliseconds(frame_offset_ms + offset.milliseconds)
}
@ -163,6 +166,7 @@ mod tests {
use super::*;
use crate::config::TimeturnerOffset;
use chrono::{Timelike, Utc};
use num_rational::Ratio;
// Helper to create a test frame
fn get_test_frame(h: u32, m: u32, s: u32, f: u32) -> LtcFrame {
@ -172,7 +176,7 @@ mod tests {
minutes: m,
seconds: s,
frames: f,
frame_rate: 25.0,
frame_rate: Ratio::new(25, 1),
timestamp: Utc::now(),
}
}

View file

@ -19,9 +19,11 @@ use crossterm::{
};
use crate::config::Config;
use get_if_addrs::get_if_addrs;
use crate::sync_logic::{get_jitter_status, get_sync_status, LtcState};
use crate::system;
use get_if_addrs::get_if_addrs;
use num_rational::Ratio;
use num_traits::ToPrimitive;
pub fn start_ui(
@ -82,8 +84,9 @@ pub fn start_ui(
if last_delta_update.elapsed() >= Duration::from_secs(1) {
cached_delta_ms = avg_delta;
if let Some(frame) = &state.lock().unwrap().latest {
let frame_ms = 1000.0 / frame.frame_rate;
cached_delta_frames = ((avg_delta as f64 / frame_ms).round()) as i64;
let delta_ms_ratio = Ratio::new(avg_delta, 1);
let frames_ratio = delta_ms_ratio * frame.frame_rate / Ratio::new(1000, 1);
cached_delta_frames = frames_ratio.round().to_integer();
} else {
cached_delta_frames = 0;
}
@ -104,7 +107,7 @@ pub fn start_ui(
None => "LTC Timecode : …".to_string(),
};
let fr_str = match opt {
Some(f) => format!("Frame Rate : {:.2}fps", f.frame_rate),
Some(f) => format!("Frame Rate : {:.2}fps", f.frame_rate.to_f64().unwrap_or(0.0)),
None => "Frame Rate : …".to_string(),
};