diff --git a/crates/cuepool-core/src/cue/mod.rs b/crates/cuepool-core/src/cue/mod.rs index cadf15f..93604e1 100644 --- a/crates/cuepool-core/src/cue/mod.rs +++ b/crates/cuepool-core/src/cue/mod.rs @@ -74,6 +74,8 @@ pub struct CueTriggers { pub wall_clock: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub timecode: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub time_window: Option, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -114,6 +116,18 @@ pub enum RepeatMode { Once, } +/// Daily on-air window. While the clock is inside `from..to` the cue fires +/// once (so a machine that boots late still starts it) and it stops at `to`. +/// `to` at or before `from` wraps past midnight. Times use the wall-clock +/// trigger's formats. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct TimeWindowTrigger { + pub from: String, + pub to: String, + #[serde(default)] + pub mode: ClockMode, +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct WallClockTrigger { pub time: String, @@ -944,6 +958,11 @@ mod tests { timecode: Some(TimecodeTrigger { time: Timespan::from_secs_f64(10.0), }), + time_window: Some(TimeWindowTrigger { + from: "09:30:00".into(), + to: "17:00:00".into(), + mode: ClockMode::TwentyFourHour, + }), }; let cue = Cue::Dummy { base: CueBase { diff --git a/crates/cuepool-core/src/lib.rs b/crates/cuepool-core/src/lib.rs index 88950d2..e6a30a6 100644 --- a/crates/cuepool-core/src/lib.rs +++ b/crates/cuepool-core/src/lib.rs @@ -21,7 +21,7 @@ pub use cue::{ }; pub use cue::{ ClockMode, CueTriggers, FadeType, HotkeyTrigger, LoopMode, MidiTrigger, MidiTriggerKind, - RepeatMode, StopMode, TimecodeTrigger, TriggerMode, WallClockTrigger, + RepeatMode, StopMode, TimeWindowTrigger, TimecodeTrigger, TriggerMode, WallClockTrigger, }; pub use eq::{EQBand, EQBandShape, EQFilter, EQFilterOrder, EQSettings}; pub use lighting::{ diff --git a/crates/cuepool-gui/src/app/mod.rs b/crates/cuepool-gui/src/app/mod.rs index fff7459..da6cf37 100644 --- a/crates/cuepool-gui/src/app/mod.rs +++ b/crates/cuepool-gui/src/app/mod.rs @@ -3282,6 +3282,7 @@ impl CuePoolApp { triggers.hotkey.is_some().then_some("hotkey"), triggers.midi.is_some().then_some("MIDI"), triggers.wall_clock.is_some().then_some("wall clock"), + triggers.time_window.is_some().then_some("time window"), triggers.timecode.is_some().then_some("timecode"), ] }) diff --git a/crates/cuepool-gui/src/cue_list/mod.rs b/crates/cuepool-gui/src/cue_list/mod.rs index 489057d..bf7e5ca 100644 --- a/crates/cuepool-gui/src/cue_list/mod.rs +++ b/crates/cuepool-gui/src/cue_list/mod.rs @@ -879,6 +879,9 @@ fn trigger_badges(triggers: &cuepool_core::CueTriggers) -> Vec<&'static str> { if triggers.wall_clock.is_some() { badges.push("clk"); } + if triggers.time_window.is_some() { + badges.push("win"); + } if triggers.timecode.is_some() { badges.push("tc"); } @@ -921,6 +924,9 @@ fn describe_triggers(triggers: &cuepool_core::CueTriggers, tc_fps: f32) -> Strin } parts.push(s); } + if let Some(window) = &triggers.time_window { + parts.push(format!("Time window {}–{}", window.from, window.to)); + } if let Some(tc) = &triggers.timecode { parts.push(format!( "Timecode ≥ {}", diff --git a/crates/cuepool-gui/src/inspector/mod.rs b/crates/cuepool-gui/src/inspector/mod.rs index d44511c..ec94267 100644 --- a/crates/cuepool-gui/src/inspector/mod.rs +++ b/crates/cuepool-gui/src/inspector/mod.rs @@ -1590,6 +1590,37 @@ fn triggers_editor( } } + // Time window + { + let mut enabled = triggers.time_window.is_some(); + if ui + .checkbox(&mut enabled, "Time Window") + .on_hover_text( + "Daily on-air window. While the clock is inside it the cue fires once, \ + even if CuePool starts late, and it stops at To. To at or before From \ + wraps past midnight.", + ) + .changed() + { + *changed = true; + triggers.time_window = enabled.then(|| cuepool_core::TimeWindowTrigger { + from: "09:00:00".into(), + to: "17:00:00".into(), + mode: cuepool_core::ClockMode::TwentyFourHour, + }); + } + if let Some(window) = triggers.time_window.as_mut() { + ui.horizontal(|ui| { + ui.label("From:"); + *changed |= ui.text_edit_singleline(&mut window.from).changed(); + }); + ui.horizontal(|ui| { + ui.label("To:"); + *changed |= ui.text_edit_singleline(&mut window.to).changed(); + }); + } + } + // Timecode { let mut enabled = triggers.timecode.is_some(); diff --git a/crates/cuepool/src/engine.rs b/crates/cuepool/src/engine.rs index b9ec85a..5d1781f 100644 --- a/crates/cuepool/src/engine.rs +++ b/crates/cuepool/src/engine.rs @@ -33,6 +33,9 @@ pub enum EngineCommand { volume: f32, pan: f32, }, + /// Stop one cue (a group with its members) using the cue's own fade-out, + /// as a Stop cue aimed at it with that fade would. + StopCue(Decimal), } #[derive(Debug, Clone)] @@ -333,6 +336,7 @@ impl ShowEngine { EngineCommand::Preload => self.preload(), EngineCommand::Seek { instance_id, secs } => self.seek(instance_id, secs), EngineCommand::SetLevel { qid, volume, pan } => self.set_level(qid, volume, pan), + EngineCommand::StopCue(qid) => self.stop_cue(qid), } self.take_actions() } @@ -1004,6 +1008,35 @@ impl ShowEngine { Some(instance_id) } + /// Whether the cue is playing (or paused) here: audio, video or a still. + pub fn is_cue_active(&self, qid: Decimal) -> bool { + self.cue_is_active(qid) + } + + fn stop_cue(&mut self, qid: Decimal) { + let (fade_out, fade_type) = self + .state + .lock_unpoisoned() + .show_file + .cues + .iter() + .find(|cue| cue.base().qid == qid) + .map_or((0.0, Default::default()), |cue| match cue { + Cue::Sound { + fade_out, + fade_type, + .. + } + | Cue::Video { + fade_out, + fade_type, + .. + } => (*fade_out, *fade_type), + _ => (0.0, Default::default()), + }); + self.stop_target(qid, StopMode::Immediate, fade_out, fade_type); + } + fn cue_is_active(&self, qid: Decimal) -> bool { self.is_video_playing(qid) || self.current_picture_qid == Some(qid) @@ -3380,4 +3413,32 @@ mod tests { assert!(engine.group_loops.is_empty()); assert!(engine.snapshot().videos.is_empty()); } + + #[test] + fn stop_cue_uses_the_cues_own_fade_out() { + let app = cuepool_gui::CuePoolApp::new(); + let mut engine = ShowEngine::new(app.state().clone(), None); + let (cue, input) = active_audio(1, Decimal::ONE); + engine.active_cues.push(cue); + engine.state.lock_unpoisoned().show_file.cues = vec![Cue::Sound { + base: cuepool_core::CueBase { + qid: Decimal::ONE, + ..Default::default() + }, + path: "tone.wav".into(), + start_time: Timespan::ZERO, + duration: Timespan::ZERO, + volume: 1.0, + pan: 0.0, + fade_in: 0.0, + fade_out: 2.0, + fade_type: Default::default(), + eq: None, + routing: Default::default(), + }]; + assert!(engine.is_cue_active(Decimal::ONE)); + + engine.command(EngineCommand::StopCue(Decimal::ONE), Duration::ZERO); + assert!(input.is_fading(), "a cue with a fade-out fades, not cuts"); + } } diff --git a/crates/cuepool/src/main.rs b/crates/cuepool/src/main.rs index 5df7885..55b8176 100644 --- a/crates/cuepool/src/main.rs +++ b/crates/cuepool/src/main.rs @@ -444,9 +444,7 @@ fn due_wall_clock_cues( .filter(|c| c.enabled()) .filter_map(|c| { let trigger = c.base().triggers.wall_clock.as_ref()?; - let target = chrono::NaiveTime::parse_from_str(&trigger.time, "%H:%M:%S") - .or_else(|_| chrono::NaiveTime::parse_from_str(&trigger.time, "%I:%M:%S %p")) - .ok()?; + let target = parse_clock_time(&trigger.time)?; if now.signed_duration_since(target).num_seconds().abs() > 1 { return None; } @@ -460,6 +458,78 @@ fn due_wall_clock_cues( .collect() } +/// A wall-clock time in either trigger format ("21:30:00" or "09:30:00 PM"). +fn parse_clock_time(text: &str) -> Option { + chrono::NaiveTime::parse_from_str(text, "%H:%M:%S") + .or_else(|_| chrono::NaiveTime::parse_from_str(text, "%I:%M:%S %p")) + .ok() +} + +/// The date the `from..to` window containing `now` opened, or None outside +/// it. `to` at or before `from` wraps midnight, so 01:00 inside 22:00–02:00 +/// belongs to the window that opened yesterday. +fn time_window_opened( + from: chrono::NaiveTime, + to: chrono::NaiveTime, + today: chrono::NaiveDate, + now: chrono::NaiveTime, +) -> Option { + if from < to { + (from <= now && now < to).then_some(today) + } else if now >= from { + Some(today) + } else if now < to { + today.pred_opt() + } else { + None + } +} + +#[derive(Debug, PartialEq)] +enum TimeWindowAction { + Fire(rust_decimal::Decimal), + Stop(rust_decimal::Decimal), +} + +/// Time-window triggers at `now`. `windows` records, per cue, the window it +/// is inside (by opening date). Entering a window fires the cue unless it is +/// already playing — once per window, so an operator's Stop sticks; leaving +/// it stops the cue if it is still playing. +// ponytail: `windows` lives in memory, so a restart mid-window fires the cue +// again — wanted after a crash. Persist it if a manual Stop must survive one. +fn time_window_actions( + cues: &[cuepool_core::Cue], + today: chrono::NaiveDate, + now: chrono::NaiveTime, + windows: &mut std::collections::HashMap, + is_active: impl Fn(rust_decimal::Decimal) -> bool, +) -> Vec { + let mut actions = Vec::new(); + for cue in cues.iter().filter(|cue| cue.enabled()) { + let qid = cue.base().qid; + let Some(window) = cue.base().triggers.time_window.as_ref() else { + continue; + }; + let (Some(from), Some(to)) = (parse_clock_time(&window.from), parse_clock_time(&window.to)) + else { + continue; + }; + match time_window_opened(from, to, today, now) { + Some(opened) if windows.get(&qid) != Some(&opened) => { + windows.insert(qid, opened); + if !is_active(qid) { + actions.push(TimeWindowAction::Fire(qid)); + } + } + None if windows.remove(&qid).is_some() && is_active(qid) => { + actions.push(TimeWindowAction::Stop(qid)); + } + _ => {} + } + } + actions +} + /// Timecode triggers due at `elapsed` show seconds that have not fired. fn due_timecode_cues( cues: &[cuepool_core::Cue], @@ -742,6 +812,8 @@ struct App { // ── trigger state ── wall_clock_fired: std::collections::HashMap, + /// Time-window trigger state: the window each cue is inside, by opening date. + time_windows: std::collections::HashMap, timecode_fired: std::collections::HashSet, // ── polish ── @@ -1149,6 +1221,7 @@ impl App { pixmap_pause_flag: Arc::new(AtomicBool::new(false)), current_pixmap_qid: None, wall_clock_fired: std::collections::HashMap::new(), + time_windows: std::collections::HashMap::new(), timecode_fired: std::collections::HashSet::new(), }; app.apply_audio_settings(); @@ -3552,6 +3625,41 @@ impl App { } } + /// Fire cues entering their time window and stop cues leaving it. + fn poll_time_window_triggers(&mut self, event_loop: &ActiveEventLoop) { + let now = chrono::Local::now(); + let actions = { + let Ok(state) = self.cuepool.state().lock() else { + return; + }; + let engine = &self.show_engine; + let (text, pixmap) = (self.current_text_qid, self.current_pixmap_qid); + time_window_actions( + &state.show_file.cues, + now.date_naive(), + now.time(), + &mut self.time_windows, + |qid| engine.is_cue_active(qid) || text == Some(qid) || pixmap == Some(qid), + ) + }; + for action in actions { + match action { + TimeWindowAction::Fire(qid) => { + log::info!("Time window opened: firing Q{qid}"); + self.play_cue_by_qid(qid, event_loop); + } + TimeWindowAction::Stop(qid) => { + log::info!("Time window closed: stopping Q{qid}"); + if let Err(error) = + self.run_engine_command(EngineCommand::StopCue(qid), event_loop) + { + log::error!("Stopping Q{qid} at its window end failed: {error}"); + } + } + } + } + } + /// Poll timecode triggers against the show clock; publishes the clock and /// the next armed trigger to the GUI. The clock is frozen while paused /// (see [`Self::show_elapsed`]) and triggers never fire mid-pause. @@ -4927,6 +5035,7 @@ impl ApplicationHandler for App { return; } self.poll_wall_clock_triggers(event_loop); + self.poll_time_window_triggers(event_loop); self.poll_timecode_triggers(event_loop); self.tick_engine(event_loop); self.upload_pixmap_frames(); @@ -5970,6 +6079,84 @@ mod tests { cuepool_core::Cue::Dummy { base } } + fn window_cue(qid: i64, from: &str, to: &str) -> cuepool_core::Cue { + let mut base = cuepool_core::CueBase { + qid: rust_decimal::Decimal::from(qid), + ..Default::default() + }; + base.triggers.time_window = Some(cuepool_core::TimeWindowTrigger { + from: from.into(), + to: to.into(), + mode: Default::default(), + }); + cuepool_core::Cue::Dummy { base } + } + + fn at(hms: &str) -> chrono::NaiveTime { + chrono::NaiveTime::parse_from_str(hms, "%H:%M:%S").unwrap() + } + + #[test] + fn a_time_window_fires_once_on_a_late_start_and_stops_at_its_end() { + use std::cell::Cell; + let q = rust_decimal::Decimal::from(7); + let cues = vec![window_cue(7, "09:30:00", "17:00:00")]; + let today = chrono::NaiveDate::from_ymd_opt(2026, 11, 19).unwrap(); + let mut windows = Default::default(); + let playing = Cell::new(false); + + // Before the window: nothing. + let early = time_window_actions(&cues, today, at("09:29:59"), &mut windows, |_| false); + assert!(early.is_empty()); + // The machine boots at 9:31, inside the window: fire it. + let late = time_window_actions(&cues, today, at("09:31:00"), &mut windows, |_| { + playing.get() + }); + assert_eq!(late, [TimeWindowAction::Fire(q)]); + // The operator stops it mid-window: the Stop sticks. + let again = time_window_actions(&cues, today, at("12:00:00"), &mut windows, |_| false); + assert!(again.is_empty(), "fires once per window"); + // Playing at To: stop it. + playing.set(true); + let end = time_window_actions(&cues, today, at("17:00:00"), &mut windows, |_| { + playing.get() + }); + assert_eq!(end, [TimeWindowAction::Stop(q)]); + // Next day's window fires again. + let tomorrow = today.succ_opt().unwrap(); + let next = time_window_actions(&cues, tomorrow, at("09:30:00"), &mut windows, |_| false); + assert_eq!(next, [TimeWindowAction::Fire(q)]); + } + + #[test] + fn a_time_window_does_not_refire_a_playing_cue_and_wraps_midnight() { + let q = rust_decimal::Decimal::from(3); + let today = chrono::NaiveDate::from_ymd_opt(2026, 11, 19).unwrap(); + + // Already playing (fired by hand) when the window opens: leave it. + let cues = vec![window_cue(3, "09:30:00", "17:00:00")]; + let mut windows = Default::default(); + assert!( + time_window_actions(&cues, today, at("09:30:00"), &mut windows, |_| true).is_empty() + ); + + // 22:00–02:00: 01:00 belongs to the window that opened yesterday, so + // a cue fired at 23:00 is not fired again after midnight. + let overnight = vec![window_cue(3, "22:00:00", "02:00:00")]; + let mut windows = Default::default(); + let open = time_window_actions(&overnight, today, at("23:00:00"), &mut windows, |_| false); + assert_eq!(open, [TimeWindowAction::Fire(q)]); + let tomorrow = today.succ_opt().unwrap(); + let after_midnight = + time_window_actions(&overnight, tomorrow, at("01:00:00"), &mut windows, |_| { + false + }); + assert!(after_midnight.is_empty()); + let closed = + time_window_actions(&overnight, tomorrow, at("02:00:00"), &mut windows, |_| true); + assert_eq!(closed, [TimeWindowAction::Stop(q)]); + } + #[test] fn a_wall_clock_trigger_fires_once_per_target_even_across_the_whole_window() { use chrono::{NaiveDate, NaiveTime};