wip: removing format calls from render loop

This commit is contained in:
🪞👃🪞 2025-01-20 16:52:07 +01:00
parent 7ad574cf2a
commit 209f35440a
2 changed files with 31 additions and 10 deletions

View file

@ -22,6 +22,7 @@ pub use ::tek_tui::{
Event, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers, KeyCode::{self, *},
},
};
use std::fmt::Write;
use clap::{self, Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
@ -149,6 +150,10 @@ impl TekCli {
pub keys_track: SourceIter<'static>,
pub keys_scene: SourceIter<'static>,
pub keys_mix: SourceIter<'static>,
pub fmt_beat: String,
pub fmt_time: String,
pub fmt_bpm: String,
}
has_size!(<TuiOut>|self: Tek|&self.size);
has_clock!(|self: Tek|self.clock);
@ -224,6 +229,9 @@ impl Tek {
keys_track: SourceIter(KEYS_TRACK),
keys_scene: SourceIter(KEYS_SCENE),
keys_mix: SourceIter(KEYS_MIX),
fmt_beat: String::with_capacity(16),
fmt_time: String::with_capacity(16),
fmt_bpm: String::with_capacity(16),
..Default::default()
};
tek.sync_lead(sync_lead);
@ -373,17 +381,24 @@ impl Tek {
}
fn view_beat_stats (&self) -> impl Content<TuiOut> + use<'_> {
let compact = self.size.w() > 80;
let clock = self.clock();
let now = clock.started.read().unwrap().as_ref().map(|start|clock.global.usec.get() - start.usec.get());
let beat = ||now.map(|now|clock.timebase.format_beats_1(clock.timebase.usecs_to_pulse(now)))
.unwrap_or("-.-.--".into());
let time = ||now.map(|now|format!("{:.3}s", now/1000000.))
.unwrap_or("-.---s".into());
let bpm = ||format!("{:.3}", clock.timebase.bpm.get());
let clock = self.clock();
let delta = |start: &Moment|clock.global.usec.get() - start.usec.get();
if let Some(now) = clock.started.read().unwrap().as_ref().map(delta) {
clock.timebase.format_beats_1_to(&mut self.fmt_beat, clock.timebase.usecs_to_pulse(now));
write!(&mut self.fmt_time, "{:.3}s", now/1000000.);
write!(&mut self.fmt_bpm, "{:.3}", clock.timebase.bpm.get());
} else {
write!(&mut self.fmt_beat, "-.-.--");
write!(&mut self.fmt_time, "-.---s");
write!(&mut self.fmt_bpm, "---.---");
}
let theme = ItemPalette::G[128];
let fh = |name, value|Field(theme, name, value);
let fv = |name, value|FieldV(theme, name, value);
Either::new(compact,
row!(Field(theme, "BPM", bpm()), Field(theme, "Beat", beat()), Field(theme, "Time", time())),
row!(FieldV(theme, "BPM", bpm()), FieldV(theme, "Beat", beat()), FieldV(theme, "Time", time())))
row!(fh("BPM", &self.fmt_bpm), fh("Beat", &self.fmt_beat), fh("Time", &self.fmt_time)),
row!(fv("BPM", &self.fmt_bpm), fv("Beat", &self.fmt_beat), fv("Time", &self.fmt_time))
)
}
fn view_engine_stats (&self) -> impl Content<TuiOut> + use<'_> {
let compact = self.size.w() > 80;

View file

@ -93,10 +93,16 @@ impl Timebase {
}
/// Format a number of pulses into Beat.Bar.Pulse starting from 1
#[inline] pub fn format_beats_1 (&self, pulse: f64) -> Arc<str> {
let mut string = String::with_capacity(16);
self.format_beats_1_to(&mut string, pulse).expect("failed to format {pulse} into beat");
string.into()
}
/// Format a number of pulses into Beat.Bar.Pulse starting from 1
#[inline] pub fn format_beats_1_to (&self, w: &mut impl std::fmt::Write, pulse: f64) -> Result<(), std::fmt::Error> {
let pulse = pulse as usize;
let ppq = self.ppq.get() as usize;
let (beats, pulses) = if ppq > 0 { (pulse / ppq, pulse % ppq) } else { (0, 0) };
format!("{}.{}.{pulses:02}", beats / 4 + 1, beats % 4 + 1).into()
write!(w, "{}.{}.{pulses:02}", beats / 4 + 1, beats % 4 + 1)
}
/// Format a number of pulses into Beat.Bar.Pulse starting from 1
#[inline] pub fn format_beats_1_short (&self, pulse: f64) -> Arc<str> {