diff --git a/src/device/clock.rs b/src/device/clock.rs index 6aedab97..11a85607 100644 --- a/src/device/clock.rs +++ b/src/device/clock.rs @@ -65,18 +65,6 @@ pub trait HasClock: AsRef + AsMut { /// can be clocked in microseconds with f64 without losing precision. pub trait TimeUnit: InteriorMutable {} -/// Contains memoized renders of clock values. -/// -/// Performance optimization. -#[derive(Debug)] pub struct ClockView { - pub sr: Memo, String>, - pub buf: Memo, String>, - pub lat: Memo, String>, - pub bpm: Memo, String>, - pub beat: Memo, String>, - pub time: Memo, String>, -} - pub const DEFAULT_PPQ: f64 = 96.0; /// FIXME: remove this and use PPQ from timebase everywhere: @@ -377,71 +365,6 @@ impl Act for ClockCommand { } } -impl ClockView { - pub const BEAT_EMPTY: &'static str = "-.-.--"; - pub const TIME_EMPTY: &'static str = "-.---s"; - pub const BPM_EMPTY: &'static str = "---.---"; - pub fn update_clock (cache: &Arc>, 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: &mut String,_,_|{ - 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, "{}", ClockView::BEAT_EMPTY)); - cache.time.update(None, rewrite!(buf, "{}", ClockView::TIME_EMPTY)); - cache.bpm.update(None, rewrite!(buf, "{}", ClockView::BPM_EMPTY)); - } - } -} - -impl_default!(ClockView: { - let mut beat = String::with_capacity(16); - let _ = write!(beat, "{}", Self::BEAT_EMPTY); - let mut time = String::with_capacity(16); - let _ = write!(time, "{}", Self::TIME_EMPTY); - let mut bpm = String::with_capacity(16); - let _ = 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)), - } -}); - #[cfg(feature = "clock")] impl_has!(Clock: |self: Track|self.sequencer.clock); impl_default!(Timebase: Self::new(48000f64, 150f64, DEFAULT_PPQ)); impl_time_unit!(SampleCount); @@ -456,3 +379,4 @@ impl_time_unit!(LaunchSync); mod moment; pub use self::moment::*; mod ticker; pub use self::ticker::*; mod timebase; pub use self::timebase::*; +mod clock_view; pub use self::clock_view::*; diff --git a/src/device/clock/clock_view.rs b/src/device/clock/clock_view.rs new file mode 100644 index 00000000..009982b1 --- /dev/null +++ b/src/device/clock/clock_view.rs @@ -0,0 +1,78 @@ +use crate::*; + +/// Contains memoized renders of clock values. +/// +/// Performance optimization. +#[derive(Debug)] pub struct ClockView { + pub sr: Memo, String>, + pub buf: Memo, String>, + pub lat: Memo, String>, + pub bpm: Memo, String>, + pub beat: Memo, String>, + pub time: Memo, String>, +} + +impl_default!(ClockView: { + let mut beat = String::with_capacity(16); + let _ = write!(beat, "{}", Self::BEAT_EMPTY); + let mut time = String::with_capacity(16); + let _ = write!(time, "{}", Self::TIME_EMPTY); + let mut bpm = String::with_capacity(16); + let _ = 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 ClockView { + pub const BEAT_EMPTY: &'static str = "-.-.--"; + pub const TIME_EMPTY: &'static str = "-.---s"; + pub const BPM_EMPTY: &'static str = "---.---"; + pub fn update_clock (cache: &Arc>, 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: &mut String,_,_|{ + 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, "{}", ClockView::BEAT_EMPTY)); + cache.time.update(None, rewrite!(buf, "{}", ClockView::TIME_EMPTY)); + cache.bpm.update(None, rewrite!(buf, "{}", ClockView::BPM_EMPTY)); + } + } +} diff --git a/src/device/editor.rs b/src/device/editor.rs index 943c8514..822ac099 100644 --- a/src/device/editor.rs +++ b/src/device/editor.rs @@ -24,7 +24,10 @@ pub struct MidiEditor { impl_default!(MidiEditor: Self { mode: PianoHorizontal::new(None), - size: Size(0.into(), 0.into()), + size: Size( + Arc::new(0usize.into()), + Arc::new(0usize.into()), + ), }); impl MidiEditor { diff --git a/src/device/sampler.rs b/src/device/sampler.rs index 61abcf40..da02443b 100644 --- a/src/device/sampler.rs +++ b/src/device/sampler.rs @@ -81,7 +81,7 @@ impl NoteRange for Sampler { &self.note_lo } fn note_axis (&self) -> &AtomicUsize { - &self.size.y + &self.size.1 } } diff --git a/tengri b/tengri index 0273d2ac..cb382cf1 160000 --- a/tengri +++ b/tengri @@ -1 +1 @@ -Subproject commit 0273d2ac75b8a144ea03b4bdd94d08003f3589df +Subproject commit cb382cf12e9169310ddab4eca2ca01e87fde9869