mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-07-17 15:56:57 +02:00
wip: nomralize
This commit is contained in:
parent
35197fb826
commit
244e2b388e
16 changed files with 1880 additions and 1866 deletions
530
src/clock.rs
530
src/clock.rs
|
|
@ -1,3 +1,4 @@
|
|||
use crate::*;
|
||||
use ::std::sync::{Arc, RwLock, atomic::{AtomicUsize, Ordering::*}};
|
||||
use ::atomic_float::AtomicF64;
|
||||
use ::tengri::{draw::*, term::*};
|
||||
|
|
@ -425,33 +426,276 @@ impl Microsecond {
|
|||
|
||||
/// Define and implement a unit of time
|
||||
#[macro_export] macro_rules! impl_time_unit {
|
||||
($T:ident) => {
|
||||
impl Gettable<f64> for $T {
|
||||
fn get (&self) -> f64 { self.0.load(Relaxed) }
|
||||
}
|
||||
impl InteriorMutable<f64> for $T {
|
||||
fn set (&self, value: f64) -> f64 {
|
||||
let old = self.get();
|
||||
self.0.store(value, Relaxed);
|
||||
old
|
||||
($T:ident) => {
|
||||
impl Gettable<f64> for $T {
|
||||
fn get (&self) -> f64 { self.0.load(Relaxed) }
|
||||
}
|
||||
impl InteriorMutable<f64> for $T {
|
||||
fn set (&self, value: f64) -> f64 {
|
||||
let old = self.get();
|
||||
self.0.store(value, Relaxed);
|
||||
old
|
||||
}
|
||||
}
|
||||
impl TimeUnit for $T {}
|
||||
impl_op!($T, Add, add, |a, b|{a + b});
|
||||
impl_op!($T, Sub, sub, |a, b|{a - b});
|
||||
impl_op!($T, Mul, mul, |a, b|{a * b});
|
||||
impl_op!($T, Div, div, |a, b|{a / b});
|
||||
impl_op!($T, Rem, rem, |a, b|{a % b});
|
||||
impl From<f64> for $T { fn from (value: f64) -> Self { Self(value.into()) } }
|
||||
impl From<usize> for $T { fn from (value: usize) -> Self { Self((value as f64).into()) } }
|
||||
impl From<$T> for f64 { fn from (value: $T) -> Self { value.get() } }
|
||||
impl From<$T> for usize { fn from (value: $T) -> Self { value.get() as usize } }
|
||||
impl From<&$T> for f64 { fn from (value: &$T) -> Self { value.get() } }
|
||||
impl From<&$T> for usize { fn from (value: &$T) -> Self { value.get() as usize } }
|
||||
impl Clone for $T { fn clone (&self) -> Self { Self(self.get().into()) } }
|
||||
}
|
||||
impl TimeUnit for $T {}
|
||||
impl_op!($T, Add, add, |a, b|{a + b});
|
||||
impl_op!($T, Sub, sub, |a, b|{a - b});
|
||||
impl_op!($T, Mul, mul, |a, b|{a * b});
|
||||
impl_op!($T, Div, div, |a, b|{a / b});
|
||||
impl_op!($T, Rem, rem, |a, b|{a % b});
|
||||
impl From<f64> for $T { fn from (value: f64) -> Self { Self(value.into()) } }
|
||||
impl From<usize> for $T { fn from (value: usize) -> Self { Self((value as f64).into()) } }
|
||||
impl From<$T> for f64 { fn from (value: $T) -> Self { value.get() } }
|
||||
impl From<$T> for usize { fn from (value: $T) -> Self { value.get() as usize } }
|
||||
impl From<&$T> for f64 { fn from (value: &$T) -> Self { value.get() } }
|
||||
impl From<&$T> for usize { fn from (value: &$T) -> Self { value.get() as usize } }
|
||||
impl Clone for $T { fn clone (&self) -> Self { Self(self.get().into()) } }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Clock {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
||||
f.debug_struct("Clock")
|
||||
.field("timebase", &self.timebase)
|
||||
.field("chunk", &self.chunk)
|
||||
.field("quant", &self.quant)
|
||||
.field("sync", &self.sync)
|
||||
.field("global", &self.global)
|
||||
.field("playhead", &self.playhead)
|
||||
.field("started", &self.started)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl Clock {
|
||||
pub fn new (jack: &Jack<'static>, bpm: Option<f64>) -> Usually<Self> {
|
||||
let (chunk, transport) = jack.with_client(|c|(c.buffer_size(), c.transport()));
|
||||
let timebase = Arc::new(Timebase::default());
|
||||
let clock = Self {
|
||||
quant: Arc::new(24.into()),
|
||||
sync: Arc::new(384.into()),
|
||||
transport: Arc::new(Some(transport)),
|
||||
chunk: Arc::new((chunk as usize).into()),
|
||||
global: Arc::new(Moment::zero(&timebase)),
|
||||
playhead: Arc::new(Moment::zero(&timebase)),
|
||||
offset: Arc::new(Moment::zero(&timebase)),
|
||||
started: RwLock::new(None).into(),
|
||||
timebase,
|
||||
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);
|
||||
}
|
||||
Ok(clock)
|
||||
}
|
||||
pub fn timebase (&self) -> &Arc<Timebase> {
|
||||
&self.timebase
|
||||
}
|
||||
/// Current sample rate
|
||||
pub fn sr (&self) -> &SampleRate {
|
||||
&self.timebase.sr
|
||||
}
|
||||
/// Current tempo
|
||||
pub fn bpm (&self) -> &Bpm {
|
||||
&self.timebase.bpm
|
||||
}
|
||||
/// Current MIDI resolution
|
||||
pub fn ppq (&self) -> &Ppq {
|
||||
&self.timebase.ppq
|
||||
}
|
||||
/// Next pulse that matches launch sync (for phrase switchover)
|
||||
pub fn next_launch_pulse (&self) -> usize {
|
||||
let sync = self.sync.get() as usize;
|
||||
let pulse = self.playhead.pulse.get() as usize;
|
||||
if pulse % sync == 0 {
|
||||
pulse
|
||||
} else {
|
||||
(pulse / sync + 1) * sync
|
||||
}
|
||||
}
|
||||
/// Start playing, optionally seeking to a given location beforehand
|
||||
pub fn play_from (&self, start: Option<u32>) -> Usually<()> {
|
||||
if let Some(transport) = self.transport.as_ref() {
|
||||
if let Some(start) = start {
|
||||
transport.locate(start)?;
|
||||
}
|
||||
transport.start()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Pause, optionally seeking to a given location afterwards
|
||||
pub fn pause_at (&self, pause: Option<u32>) -> Usually<()> {
|
||||
if let Some(transport) = self.transport.as_ref() {
|
||||
transport.stop()?;
|
||||
if let Some(pause) = pause {
|
||||
transport.locate(pause)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Is currently paused?
|
||||
pub fn is_stopped (&self) -> bool {
|
||||
self.started.read().unwrap().is_none()
|
||||
}
|
||||
/// Is currently playing?
|
||||
pub fn is_rolling (&self) -> bool {
|
||||
self.started.read().unwrap().is_some()
|
||||
}
|
||||
/// Update chunk size
|
||||
pub fn set_chunk (&self, n_frames: usize) {
|
||||
self.chunk.store(n_frames, Relaxed);
|
||||
}
|
||||
pub fn update_from_scope (&self, scope: &ProcessScope) -> Usually<()> {
|
||||
// Store buffer length
|
||||
self.set_chunk(scope.n_frames() as usize);
|
||||
|
||||
// Store reported global frame and usec
|
||||
let CycleTimes { current_frames, current_usecs, .. } = scope.cycle_times()?;
|
||||
self.global.sample.set(current_frames as f64);
|
||||
self.global.usec.set(current_usecs as f64);
|
||||
|
||||
let mut started = self.started.write().unwrap();
|
||||
|
||||
// If transport has just started or just stopped,
|
||||
// update starting point:
|
||||
if let Some(transport) = self.transport.as_ref() {
|
||||
match (transport.query_state()?, started.as_ref()) {
|
||||
(TransportState::Rolling, None) => {
|
||||
let moment = Moment::zero(&self.timebase);
|
||||
moment.sample.set(current_frames as f64);
|
||||
moment.usec.set(current_usecs as f64);
|
||||
*started = Some(moment);
|
||||
},
|
||||
(TransportState::Stopped, Some(_)) => {
|
||||
*started = None;
|
||||
},
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
self.playhead.update_from_sample(started.as_ref()
|
||||
.map(|started|current_frames as f64 - started.sample.get())
|
||||
.unwrap_or(0.));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn bbt (&self) -> PositionBBT {
|
||||
let pulse = self.playhead.pulse.get() as i32;
|
||||
let ppq = self.timebase.ppq.get() as i32;
|
||||
let bpm = self.timebase.bpm.get();
|
||||
let bar = (pulse / ppq) / 4;
|
||||
PositionBBT {
|
||||
bar: 1 + bar,
|
||||
beat: 1 + (pulse / ppq) % 4,
|
||||
tick: (pulse % ppq),
|
||||
bar_start_tick: (bar * 4 * ppq) as f64,
|
||||
beat_type: 4.,
|
||||
beats_per_bar: 4.,
|
||||
beats_per_minute: bpm,
|
||||
ticks_per_beat: ppq as f64
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_launch_instant (&self) -> Moment {
|
||||
Moment::from_pulse(self.timebase(), self.next_launch_pulse() as f64)
|
||||
}
|
||||
|
||||
/// Get index of first sample to populate.
|
||||
///
|
||||
/// Greater than 0 means that the first pulse of the clip
|
||||
/// falls somewhere in the middle of the chunk.
|
||||
pub fn get_sample_offset (&self, scope: &ProcessScope, started: &Moment) -> usize{
|
||||
(scope.last_frame_time() as usize).saturating_sub(
|
||||
started.sample.get() as usize +
|
||||
self.started.read().unwrap().as_ref().unwrap().sample.get() as usize
|
||||
)
|
||||
}
|
||||
|
||||
// Get iterator that emits sample paired with pulse.
|
||||
//
|
||||
// * Sample: index into output buffer at which to write MIDI event
|
||||
// * Pulse: index into clip from which to take the MIDI event
|
||||
//
|
||||
// Emitted for each sample of the output buffer that corresponds to a MIDI pulse.
|
||||
pub fn get_pulses (&self, scope: &ProcessScope, offset: usize) -> Ticker {
|
||||
self.timebase().pulses_between_samples(offset, offset + scope.n_frames() as usize)
|
||||
}
|
||||
}
|
||||
impl Clock {
|
||||
fn _todo_provide_u32 (&self) -> u32 {
|
||||
todo!()
|
||||
}
|
||||
fn _todo_provide_opt_u32 (&self) -> Option<u32> {
|
||||
todo!()
|
||||
}
|
||||
fn _todo_provide_f64 (&self) -> f64 {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl<T: HasClock> Command<T> for ClockCommand {
|
||||
fn execute (&self, state: &mut T) -> Perhaps<Self> {
|
||||
self.execute(state.clock_mut()) // awesome
|
||||
}
|
||||
}
|
||||
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<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, "{}", 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);
|
||||
impl_time_unit!(SampleRate);
|
||||
impl_time_unit!(Microsecond);
|
||||
|
|
@ -460,243 +704,3 @@ impl_time_unit!(Ppq);
|
|||
impl_time_unit!(Pulse);
|
||||
impl_time_unit!(Bpm);
|
||||
impl_time_unit!(LaunchSync);
|
||||
impl std::fmt::Debug for Clock {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
||||
f.debug_struct("Clock")
|
||||
.field("timebase", &self.timebase)
|
||||
.field("chunk", &self.chunk)
|
||||
.field("quant", &self.quant)
|
||||
.field("sync", &self.sync)
|
||||
.field("global", &self.global)
|
||||
.field("playhead", &self.playhead)
|
||||
.field("started", &self.started)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
impl Clock {
|
||||
pub fn new (jack: &Jack<'static>, bpm: Option<f64>) -> Usually<Self> {
|
||||
let (chunk, transport) = jack.with_client(|c|(c.buffer_size(), c.transport()));
|
||||
let timebase = Arc::new(Timebase::default());
|
||||
let clock = Self {
|
||||
quant: Arc::new(24.into()),
|
||||
sync: Arc::new(384.into()),
|
||||
transport: Arc::new(Some(transport)),
|
||||
chunk: Arc::new((chunk as usize).into()),
|
||||
global: Arc::new(Moment::zero(&timebase)),
|
||||
playhead: Arc::new(Moment::zero(&timebase)),
|
||||
offset: Arc::new(Moment::zero(&timebase)),
|
||||
started: RwLock::new(None).into(),
|
||||
timebase,
|
||||
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);
|
||||
}
|
||||
Ok(clock)
|
||||
}
|
||||
pub fn timebase (&self) -> &Arc<Timebase> {
|
||||
&self.timebase
|
||||
}
|
||||
/// Current sample rate
|
||||
pub fn sr (&self) -> &SampleRate {
|
||||
&self.timebase.sr
|
||||
}
|
||||
/// Current tempo
|
||||
pub fn bpm (&self) -> &Bpm {
|
||||
&self.timebase.bpm
|
||||
}
|
||||
/// Current MIDI resolution
|
||||
pub fn ppq (&self) -> &Ppq {
|
||||
&self.timebase.ppq
|
||||
}
|
||||
/// Next pulse that matches launch sync (for phrase switchover)
|
||||
pub fn next_launch_pulse (&self) -> usize {
|
||||
let sync = self.sync.get() as usize;
|
||||
let pulse = self.playhead.pulse.get() as usize;
|
||||
if pulse % sync == 0 {
|
||||
pulse
|
||||
} else {
|
||||
(pulse / sync + 1) * sync
|
||||
}
|
||||
}
|
||||
/// Start playing, optionally seeking to a given location beforehand
|
||||
pub fn play_from (&self, start: Option<u32>) -> Usually<()> {
|
||||
if let Some(transport) = self.transport.as_ref() {
|
||||
if let Some(start) = start {
|
||||
transport.locate(start)?;
|
||||
}
|
||||
transport.start()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Pause, optionally seeking to a given location afterwards
|
||||
pub fn pause_at (&self, pause: Option<u32>) -> Usually<()> {
|
||||
if let Some(transport) = self.transport.as_ref() {
|
||||
transport.stop()?;
|
||||
if let Some(pause) = pause {
|
||||
transport.locate(pause)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Is currently paused?
|
||||
pub fn is_stopped (&self) -> bool {
|
||||
self.started.read().unwrap().is_none()
|
||||
}
|
||||
/// Is currently playing?
|
||||
pub fn is_rolling (&self) -> bool {
|
||||
self.started.read().unwrap().is_some()
|
||||
}
|
||||
/// Update chunk size
|
||||
pub fn set_chunk (&self, n_frames: usize) {
|
||||
self.chunk.store(n_frames, Relaxed);
|
||||
}
|
||||
pub fn update_from_scope (&self, scope: &ProcessScope) -> Usually<()> {
|
||||
// Store buffer length
|
||||
self.set_chunk(scope.n_frames() as usize);
|
||||
|
||||
// Store reported global frame and usec
|
||||
let CycleTimes { current_frames, current_usecs, .. } = scope.cycle_times()?;
|
||||
self.global.sample.set(current_frames as f64);
|
||||
self.global.usec.set(current_usecs as f64);
|
||||
|
||||
let mut started = self.started.write().unwrap();
|
||||
|
||||
// If transport has just started or just stopped,
|
||||
// update starting point:
|
||||
if let Some(transport) = self.transport.as_ref() {
|
||||
match (transport.query_state()?, started.as_ref()) {
|
||||
(TransportState::Rolling, None) => {
|
||||
let moment = Moment::zero(&self.timebase);
|
||||
moment.sample.set(current_frames as f64);
|
||||
moment.usec.set(current_usecs as f64);
|
||||
*started = Some(moment);
|
||||
},
|
||||
(TransportState::Stopped, Some(_)) => {
|
||||
*started = None;
|
||||
},
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
self.playhead.update_from_sample(started.as_ref()
|
||||
.map(|started|current_frames as f64 - started.sample.get())
|
||||
.unwrap_or(0.));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn bbt (&self) -> PositionBBT {
|
||||
let pulse = self.playhead.pulse.get() as i32;
|
||||
let ppq = self.timebase.ppq.get() as i32;
|
||||
let bpm = self.timebase.bpm.get();
|
||||
let bar = (pulse / ppq) / 4;
|
||||
PositionBBT {
|
||||
bar: 1 + bar,
|
||||
beat: 1 + (pulse / ppq) % 4,
|
||||
tick: (pulse % ppq),
|
||||
bar_start_tick: (bar * 4 * ppq) as f64,
|
||||
beat_type: 4.,
|
||||
beats_per_bar: 4.,
|
||||
beats_per_minute: bpm,
|
||||
ticks_per_beat: ppq as f64
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_launch_instant (&self) -> Moment {
|
||||
Moment::from_pulse(self.timebase(), self.next_launch_pulse() as f64)
|
||||
}
|
||||
|
||||
/// Get index of first sample to populate.
|
||||
///
|
||||
/// Greater than 0 means that the first pulse of the clip
|
||||
/// falls somewhere in the middle of the chunk.
|
||||
pub fn get_sample_offset (&self, scope: &ProcessScope, started: &Moment) -> usize{
|
||||
(scope.last_frame_time() as usize).saturating_sub(
|
||||
started.sample.get() as usize +
|
||||
self.started.read().unwrap().as_ref().unwrap().sample.get() as usize
|
||||
)
|
||||
}
|
||||
|
||||
// Get iterator that emits sample paired with pulse.
|
||||
//
|
||||
// * Sample: index into output buffer at which to write MIDI event
|
||||
// * Pulse: index into clip from which to take the MIDI event
|
||||
//
|
||||
// Emitted for each sample of the output buffer that corresponds to a MIDI pulse.
|
||||
pub fn get_pulses (&self, scope: &ProcessScope, offset: usize) -> Ticker {
|
||||
self.timebase().pulses_between_samples(offset, offset + scope.n_frames() as usize)
|
||||
}
|
||||
}
|
||||
impl Clock {
|
||||
fn _todo_provide_u32 (&self) -> u32 {
|
||||
todo!()
|
||||
}
|
||||
fn _todo_provide_opt_u32 (&self) -> Option<u32> {
|
||||
todo!()
|
||||
}
|
||||
fn _todo_provide_f64 (&self) -> f64 {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl<T: HasClock> Command<T> for ClockCommand {
|
||||
fn execute (&self, state: &mut T) -> Perhaps<Self> {
|
||||
self.execute(state.clock_mut()) // awesome
|
||||
}
|
||||
}
|
||||
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<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, "{}", 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)),
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue