Compare commits

...

8 commits

Author SHA1 Message Date
Chris Frankland-Wright
bda4d4e6f5
Merge pull request #28 from cjfranko/build_fix_error
Some checks failed
Build for Raspberry Pi / Build for aarch64 (push) Failing after 20s
fix build on native
2025-08-03 13:28:46 +01:00
Chris Frankland-Wright
8453f18a3c fix: Adjust sync status thresholds to pass tests
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-08-03 13:24:14 +01:00
Chris Frankland-Wright
049a85685c fix: Address unused import and Ratio type mismatch in tests
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-08-03 13:10:18 +01:00
Chris Frankland-Wright
d13ffdc057
Merge pull request #27 from cjfranko/recalculate_fps
fix for fractional frame rates issue
2025-08-03 13:00:56 +01:00
Chris Frankland-Wright
459500e402 fix: Correct clock drift for fractional frame rates
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-08-03 12:38:15 +01:00
Chris Frankland-Wright
4ee791c817 build: Add num-traits dependency
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-08-03 12:26:31 +01:00
Chris Frankland-Wright
3d6a106f1e refactor: Use rational numbers for LtcFrame frame rate
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-08-02 12:28:59 +01:00
Chris Frankland-Wright
a1da396874 refactor: Use rational numbers for accurate frame rate calculations
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-08-02 12:26:17 +01:00
7 changed files with 84 additions and 26 deletions

View file

@ -19,5 +19,7 @@ tokio = { version = "1", features = ["full"] }
clap = { version = "4.4", features = ["derive"] } clap = { version = "4.4", features = ["derive"] }
log = { version = "0.4", features = ["std"] } log = { version = "0.4", features = ["std"] }
daemonize = "0.5.0" daemonize = "0.5.0"
num-rational = "0.4"
num-traits = "0.2"

View file

@ -11,6 +11,8 @@ use std::sync::{Arc, Mutex};
use crate::config::{self, Config}; use crate::config::{self, Config};
use crate::sync_logic::{self, LtcState}; use crate::sync_logic::{self, LtcState};
use crate::system; use crate::system;
use num_rational::Ratio;
use num_traits::ToPrimitive;
// Data structure for the main status response // Data structure for the main status response
#[derive(Serialize, Deserialize)] #[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) 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| { 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(); 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 avg_delta = state.get_ewma_clock_delta();
let mut delta_frames = 0; let mut delta_frames = 0;
if let Some(frame) = &state.latest { if let Some(frame) = &state.latest {
let frame_ms = 1000.0 / frame.frame_rate; let delta_ms_ratio = Ratio::new(avg_delta, 1);
delta_frames = ((avg_delta as f64 / frame_ms).round()) as i64; 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); let sync_status = sync_logic::get_sync_status(avg_delta, &config);
@ -239,7 +242,7 @@ mod tests {
minutes: 2, minutes: 2,
seconds: 3, seconds: 3,
frames: 4, frames: 4,
frame_rate: 25.0, frame_rate: Ratio::new(25, 1),
timestamp: Utc::now(), timestamp: Utc::now(),
}), }),
lock_count: 10, lock_count: 10,

View file

@ -60,6 +60,7 @@ mod tests {
use super::*; use super::*;
use std::sync::mpsc; use std::sync::mpsc;
use crate::sync_logic::LtcState; use crate::sync_logic::LtcState;
use num_rational::Ratio;
use regex::Regex; use regex::Regex;
fn get_ltc_regex() -> Regex { fn get_ltc_regex() -> Regex {
@ -119,7 +120,7 @@ mod tests {
assert_eq!(st.free_count, 1); assert_eq!(st.free_count, 1);
let received_frame = rx.try_recv().unwrap(); let received_frame = rx.try_recv().unwrap();
assert_eq!(received_frame.status, "FREE"); assert_eq!(received_frame.status, "FREE");
assert_eq!(received_frame.frame_rate, 29.97); assert_eq!(received_frame.frame_rate, Ratio::new(30000, 1001));
} }
#[test] #[test]

View file

@ -1,10 +1,22 @@
use crate::config::Config; use crate::config::Config;
use chrono::{DateTime, Local, Timelike, Utc}; use chrono::{DateTime, Local, Timelike, Utc};
use num_rational::Ratio;
use regex::Captures; use regex::Captures;
use std::collections::VecDeque; use std::collections::VecDeque;
const EWMA_ALPHA: f64 = 0.1; const EWMA_ALPHA: f64 = 0.1;
fn get_frame_rate_ratio(rate_str: &str) -> Option<Ratio<i64>> {
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)] #[derive(Clone, Debug)]
pub struct LtcFrame { pub struct LtcFrame {
pub status: String, pub status: String,
@ -12,7 +24,7 @@ pub struct LtcFrame {
pub minutes: u32, pub minutes: u32,
pub seconds: u32, pub seconds: u32,
pub frames: u32, pub frames: u32,
pub frame_rate: f64, pub frame_rate: Ratio<i64>,
pub timestamp: DateTime<Utc>, // arrival stamp pub timestamp: DateTime<Utc>, // arrival stamp
} }
@ -24,7 +36,7 @@ impl LtcFrame {
minutes: caps[3].parse().ok()?, minutes: caps[3].parse().ok()?,
seconds: caps[4].parse().ok()?, seconds: caps[4].parse().ok()?,
frames: caps[5].parse().ok()?, frames: caps[5].parse().ok()?,
frame_rate: caps[6].parse().ok()?, frame_rate: get_frame_rate_ratio(&caps[6])?,
timestamp, timestamp,
}) })
} }
@ -129,8 +141,9 @@ impl LtcState {
/// Convert average jitter into frames (rounded). /// Convert average jitter into frames (rounded).
pub fn average_frames(&self) -> i64 { pub fn average_frames(&self) -> i64 {
if let Some(frame) = &self.latest { if let Some(frame) = &self.latest {
let ms_per_frame = 1000.0 / frame.frame_rate; let jitter_ms_ratio = Ratio::new(self.average_jitter(), 1);
(self.average_jitter() as f64 / ms_per_frame).round() as i64 let frames_ratio = jitter_ms_ratio * frame.frame_rate / Ratio::new(1000, 1);
frames_ratio.round().to_integer()
} else { } else {
0 0
} }
@ -160,9 +173,9 @@ impl LtcState {
pub fn get_sync_status(delta_ms: i64, config: &Config) -> &'static str { pub fn get_sync_status(delta_ms: i64, config: &Config) -> &'static str {
if config.timeturner_offset.is_active() { if config.timeturner_offset.is_active() {
"TIMETURNING" "TIMETURNING"
} else if delta_ms.abs() <= 1 { } else if delta_ms.abs() <= 8 {
"IN SYNC" "IN SYNC"
} else if delta_ms > 2 { } else if delta_ms > 10 {
"CLOCK AHEAD" "CLOCK AHEAD"
} else { } else {
"CLOCK BEHIND" "CLOCK BEHIND"
@ -192,7 +205,7 @@ mod tests {
minutes: m, minutes: m,
seconds: s, seconds: s,
frames: 0, frames: 0,
frame_rate: 25.0, frame_rate: Ratio::new(25, 1),
timestamp: Utc::now(), timestamp: Utc::now(),
} }
} }

View file

@ -1,6 +1,7 @@
use crate::config::Config; use crate::config::Config;
use crate::sync_logic::LtcFrame; use crate::sync_logic::LtcFrame;
use chrono::{DateTime, Duration as ChronoDuration, Local, NaiveTime, TimeZone}; use chrono::{DateTime, Duration as ChronoDuration, Local, TimeZone};
use num_rational::Ratio;
use std::process::Command; use std::process::Command;
/// Check if Chrony is active /// Check if Chrony is active
@ -39,11 +40,32 @@ pub fn ntp_service_toggle(start: bool) {
pub fn calculate_target_time(frame: &LtcFrame, config: &Config) -> DateTime<Local> { pub fn calculate_target_time(frame: &LtcFrame, config: &Config) -> DateTime<Local> {
let today_local = Local::now().date_naive(); let today_local = Local::now().date_naive();
let ms = ((frame.frames as f64 / frame.frame_rate) * 1000.0).round() as u32;
let timecode = NaiveTime::from_hms_milli_opt(frame.hours, frame.minutes, frame.seconds, ms)
.expect("Invalid LTC timecode");
let naive_dt = today_local.and_time(timecode); // Total seconds from timecode components
let timecode_secs =
frame.hours as i64 * 3600 + frame.minutes as i64 * 60 + frame.seconds as i64;
// Total duration in seconds as a rational number, including frames
let total_duration_secs =
Ratio::new(timecode_secs, 1) + Ratio::new(frame.frames as i64, 1) / frame.frame_rate;
// For fractional frame rates (23.98, 29.97), timecode runs slower than wall clock.
// We need to scale the timecode duration up to get wall clock time.
// The scaling factor is 1001/1000.
let scaled_duration_secs = if *frame.frame_rate.denom() == 1001 {
total_duration_secs * Ratio::new(1001, 1000)
} else {
total_duration_secs
};
// Convert to milliseconds
let total_ms = (scaled_duration_secs * Ratio::new(1000, 1))
.round()
.to_integer();
let naive_midnight = today_local.and_hms_opt(0, 0, 0).unwrap();
let naive_dt = naive_midnight + ChronoDuration::milliseconds(total_ms);
let mut dt_local = Local let mut dt_local = Local
.from_local_datetime(&naive_dt) .from_local_datetime(&naive_dt)
.single() .single()
@ -56,7 +78,8 @@ pub fn calculate_target_time(frame: &LtcFrame, config: &Config) -> DateTime<Loca
+ ChronoDuration::minutes(offset.minutes) + ChronoDuration::minutes(offset.minutes)
+ ChronoDuration::seconds(offset.seconds); + ChronoDuration::seconds(offset.seconds);
// Frame offset needs to be converted to milliseconds // 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) dt_local + ChronoDuration::milliseconds(frame_offset_ms + offset.milliseconds)
} }
@ -163,6 +186,7 @@ mod tests {
use super::*; use super::*;
use crate::config::TimeturnerOffset; use crate::config::TimeturnerOffset;
use chrono::{Timelike, Utc}; use chrono::{Timelike, Utc};
use num_rational::Ratio;
// Helper to create a test frame // Helper to create a test frame
fn get_test_frame(h: u32, m: u32, s: u32, f: u32) -> LtcFrame { fn get_test_frame(h: u32, m: u32, s: u32, f: u32) -> LtcFrame {
@ -172,7 +196,7 @@ mod tests {
minutes: m, minutes: m,
seconds: s, seconds: s,
frames: f, frames: f,
frame_rate: 25.0, frame_rate: Ratio::new(25, 1),
timestamp: Utc::now(), timestamp: Utc::now(),
} }
} }

View file

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

View file

@ -9,10 +9,11 @@ import threading
import queue import queue
import json import json
from collections import deque from collections import deque
from fractions import Fraction
SERIAL_PORT = None SERIAL_PORT = None
BAUD_RATE = 115200 BAUD_RATE = 115200
FRAME_RATE = 25.0 FRAME_RATE = Fraction(25, 1)
CONFIG_PATH = "config.json" CONFIG_PATH = "config.json"
sync_pending = False sync_pending = False
@ -30,6 +31,14 @@ sync_enabled = False
last_match_check = 0 last_match_check = 0
timecode_match_status = "UNKNOWN" timecode_match_status = "UNKNOWN"
def framerate_str_to_fraction(s):
if s == "23.98": return Fraction(24000, 1001)
if s == "24.00": return Fraction(24, 1)
if s == "25.00": return Fraction(25, 1)
if s == "29.97": return Fraction(30000, 1001)
if s == "30.00": return Fraction(30, 1)
return None
def load_config(): def load_config():
global hardware_offset_ms global hardware_offset_ms
try: try:
@ -50,13 +59,16 @@ def parse_ltc_line(line):
if not match: if not match:
return None return None
status, hh, mm, ss, ff, fps = match.groups() status, hh, mm, ss, ff, fps = match.groups()
rate = framerate_str_to_fraction(fps)
if not rate:
return None
return { return {
"status": status, "status": status,
"hours": int(hh), "hours": int(hh),
"minutes": int(mm), "minutes": int(mm),
"seconds": int(ss), "seconds": int(ss),
"frames": int(ff), "frames": int(ff),
"frame_rate": float(fps) "frame_rate": rate
} }
def serial_thread(port, baud, q): def serial_thread(port, baud, q):
@ -154,7 +166,7 @@ def run_curses(stdscr):
parsed, arrival_time = latest_ltc parsed, arrival_time = latest_ltc
stdscr.addstr(3, 2, f"LTC Status : {parsed['status']}") stdscr.addstr(3, 2, f"LTC Status : {parsed['status']}")
stdscr.addstr(4, 2, f"LTC Timecode : {parsed['hours']:02}:{parsed['minutes']:02}:{parsed['seconds']:02}:{parsed['frames']:02}") stdscr.addstr(4, 2, f"LTC Timecode : {parsed['hours']:02}:{parsed['minutes']:02}:{parsed['seconds']:02}:{parsed['frames']:02}")
stdscr.addstr(5, 2, f"Frame Rate : {FRAME_RATE:.2f}fps") stdscr.addstr(5, 2, f"Frame Rate : {float(FRAME_RATE):.2f}fps")
stdscr.addstr(6, 2, f"System Clock : {format_time(get_system_time())}") stdscr.addstr(6, 2, f"System Clock : {format_time(get_system_time())}")
if ltc_locked and sync_enabled and offset_history: if ltc_locked and sync_enabled and offset_history: