refactor: atomic timebase

This commit is contained in:
🪞👃🪞 2024-06-20 19:15:26 +03:00
parent 87c5e47b43
commit f77c84a99c
5 changed files with 116 additions and 99 deletions

View file

@ -3,7 +3,9 @@ use crate::prelude::*;
pub struct Transport {
name: String,
/// Holds info about tempo
timebase: Arc<Mutex<Timebase>>,
timebase: Arc<Timebase>,
transport: ::jack::Transport,
}
impl Transport {
@ -12,11 +14,17 @@ impl Transport {
let transport = client.transport();
DynamicDevice::new(render, handle, process, Self {
name: name.into(),
timebase: Timebase {
rate: transport.query()?.pos.frame_rate(),
tempo: 113000,
ppq: 96,
},
timebase: Arc::new(Timebase {
rate: AtomicUsize::new(
transport.query()?.pos.frame_rate().map(|x|x as usize).unwrap_or(0)
),
tempo: AtomicUsize::new(
113000
),
ppq: AtomicUsize::new(
96
),
}),
transport
}).activate(client)
}
@ -39,6 +47,10 @@ impl Transport {
pub fn stop (&mut self) -> Result<(), Box<dyn Error>> {
Ok(self.transport.stop()?)
}
pub fn timebase (&self) -> Arc<Timebase> {
self.timebase.clone()
}
}
pub fn process (_: &mut Transport, _: &Client, _: &ProcessScope) -> Control {
@ -63,8 +75,8 @@ pub fn render (state: &Transport, buf: &mut Buffer, mut area: Rect)
"0.0.00",
"0:00.000",
&format!("BPM {:03}.{:03}",
state.bpm as u64,
((state.bpm % 1.0) * 1000.0) as u64
state.timebase.tempo() / 1000,
state.timebase.tempo() % 1000,
)
].iter() {
buf.set_string(area.x + x, area.y + 2, button, label);