wip: compiles again, after extensive jack rework
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-21 01:45:23 +03:00
parent cb7e4f7a95
commit 0192d85a19
18 changed files with 526 additions and 525 deletions

View file

@ -26,6 +26,8 @@ pub struct Clock {
pub midi_out: Arc<RwLock<Option<MidiOutput>>>,
/// For emitting a metronome
pub click_out: Arc<RwLock<Option<AudioOutput>>>,
// Cache of formatted strings
pub view_cache: Arc<RwLock<ViewCache>>,
}
impl std::fmt::Debug for Clock {
@ -59,6 +61,7 @@ impl Clock {
midi_in: Arc::new(RwLock::new(Some(MidiInput::new(jack, &"M/clock", &[])?))),
midi_out: Arc::new(RwLock::new(Some(MidiOutput::new(jack, &"clock/M", &[])?))),
click_out: Arc::new(RwLock::new(Some(AudioOutput::new(jack, &"click", &[])?))),
..Default::default()
};
if let Some(bpm) = bpm {
clock.timebase.bpm.set(bpm);

View file

@ -1,4 +1,5 @@
use crate::*;
use std::fmt::Write;
pub fn view_transport (
play: bool,
@ -49,3 +50,86 @@ pub(crate) fn button_play_pause (playing: bool) -> impl Content<TuiOut> {
)
)
}
#[derive(Debug)] pub struct ViewCache {
pub sr: Memo<Option<(bool, f64)>, String>,
pub buf: Memo<Option<f64>, String>,
pub lat: Memo<Option<f64>, String>,
pub bpm: Memo<Option<f64>, String>,
pub beat: Memo<Option<f64>, String>,
pub time: Memo<Option<f64>, String>,
}
impl Default for ViewCache {
fn default () -> Self {
let mut beat = String::with_capacity(16);
write!(beat, "{}", Self::BEAT_EMPTY);
let mut time = String::with_capacity(16);
write!(time, "{}", Self::TIME_EMPTY);
let mut bpm = String::with_capacity(16);
write!(bpm, "{}", Self::BPM_EMPTY);
Self {
beat: Memo::new(None, beat),
time: Memo::new(None, time),
bpm: Memo::new(None, bpm),
sr: Memo::new(None, String::with_capacity(16)),
buf: Memo::new(None, String::with_capacity(16)),
lat: Memo::new(None, String::with_capacity(16)),
}
}
}
impl ViewCache {
pub const BEAT_EMPTY: &'static str = "-.-.--";
pub const TIME_EMPTY: &'static str = "-.---s";
pub const BPM_EMPTY: &'static str = "---.---";
//pub fn track_counter (cache: &Arc<RwLock<Self>>, track: usize, tracks: usize)
//-> Arc<RwLock<String>>
//{
//let data = (track, tracks);
//cache.write().unwrap().trks.update(Some(data), rewrite!(buf, "{}/{}", data.0, data.1));
//cache.read().unwrap().trks.view.clone()
//}
//pub fn scene_add (cache: &Arc<RwLock<Self>>, scene: usize, scenes: usize, is_editing: bool)
//-> impl Content<TuiOut>
//{
//let data = (scene, scenes);
//cache.write().unwrap().scns.update(Some(data), rewrite!(buf, "({}/{})", data.0, data.1));
//button_3("S", "add scene", cache.read().unwrap().scns.view.clone(), is_editing)
//}
pub fn update_clock (cache: &Arc<RwLock<Self>>, clock: &Clock, compact: bool) {
let rate = clock.timebase.sr.get();
let chunk = clock.chunk.load(Relaxed) as f64;
let lat = chunk / rate * 1000.;
let delta = |start: &Moment|clock.global.usec.get() - start.usec.get();
let mut cache = cache.write().unwrap();
cache.buf.update(Some(chunk), rewrite!(buf, "{chunk}"));
cache.lat.update(Some(lat), rewrite!(buf, "{lat:.1}ms"));
cache.sr.update(Some((compact, rate)), |buf,_,_|{
buf.clear();
if compact {
write!(buf, "{:.1}kHz", rate / 1000.)
} else {
write!(buf, "{:.0}Hz", rate)
}
});
if let Some(now) = clock.started.read().unwrap().as_ref().map(delta) {
let pulse = clock.timebase.usecs_to_pulse(now);
let time = now/1000000.;
let bpm = clock.timebase.bpm.get();
cache.beat.update(Some(pulse), |buf, _, _|{
buf.clear();
clock.timebase.format_beats_1_to(buf, pulse)
});
cache.time.update(Some(time), rewrite!(buf, "{:.3}s", time));
cache.bpm.update(Some(bpm), rewrite!(buf, "{:.3}", bpm));
} else {
cache.beat.update(None, rewrite!(buf, "{}", ViewCache::BEAT_EMPTY));
cache.time.update(None, rewrite!(buf, "{}", ViewCache::TIME_EMPTY));
cache.bpm.update(None, rewrite!(buf, "{}", ViewCache::BPM_EMPTY));
}
}
}