mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
106 lines
5.2 KiB
Rust
106 lines
5.2 KiB
Rust
use crate::*;
|
|
use KeyCode::*;
|
|
use ClockCommand::{Play, Pause};
|
|
/// Transport clock app.
|
|
pub struct ClockTui { pub jack: Arc<RwLock<JackConnection>>, pub clock: Clock, }
|
|
handle!(TuiIn: |self: ClockTui, input|ClockCommand::execute_with_state(self, input.event()));
|
|
keymap!(TRANSPORT_KEYS = |state: ClockTui, input: Event| ClockCommand {
|
|
key(Char(' ')) =>
|
|
if state.clock().is_stopped() { Play(None) } else { Pause(None) },
|
|
shift(key(Char(' '))) =>
|
|
if state.clock().is_stopped() { Play(Some(0)) } else { Pause(Some(0)) }
|
|
});
|
|
has_clock!(|self: ClockTui|&self.clock);
|
|
audio!(|self: ClockTui, client, scope|ClockAudio(self).process(client, scope));
|
|
content!(TuiOut:|self: ClockTui|ClockView { compact: false, clock: &self.clock });
|
|
pub struct ClockView<'a> { pub compact: bool, pub clock: &'a Clock }
|
|
content!(TuiOut: |self: ClockView<'a>| Outer(Style::default().fg(TuiTheme::g(255))).enclose(row!(
|
|
OutputStats::new(self.compact, self.clock),
|
|
" ",
|
|
PlayPause { compact: false, playing: self.clock.is_rolling() },
|
|
" ",
|
|
BeatStats::new(self.compact, self.clock),
|
|
)));
|
|
impl<'a> ClockView<'a> {
|
|
pub fn new (compact: bool, clock: &'a Clock) -> Self { Self { compact, clock } }
|
|
}
|
|
pub struct PlayPause { pub compact: bool, pub playing: bool }
|
|
content!(TuiOut: |self: PlayPause| Tui::bg(
|
|
if self.playing{Color::Rgb(0,128,0)}else{Color::Rgb(128,64,0)},
|
|
Either::new(self.compact,
|
|
Thunk::new(||Fixed::x(9, Either::new(self.playing,
|
|
Tui::fg(Color::Rgb(0, 255, 0), " PLAYING "),
|
|
Tui::fg(Color::Rgb(255, 128, 0), " STOPPED ")))),
|
|
Thunk::new(||Fixed::x(5, Either::new(self.playing,
|
|
Tui::fg(Color::Rgb(0, 255, 0), Bsp::s(" 🭍🭑🬽 ", " 🭞🭜🭘 ",)),
|
|
Tui::fg(Color::Rgb(255, 128, 0), Bsp::s(" ▗▄▖ ", " ▝▀▘ ",))))))));
|
|
pub struct BeatStats { compact: bool, bpm: Arc<str>, beat: Arc<str>, time: Arc<str>, }
|
|
content!(TuiOut: |self: BeatStats| Either::new(self.compact,
|
|
row!(FieldV(TuiTheme::g(128).into(), "BPM", &self.bpm),
|
|
FieldV(TuiTheme::g(128).into(), "Beat", &self.beat),
|
|
FieldV(TuiTheme::g(128).into(), "Time", &self.time),),
|
|
col!(Bsp::e(Tui::fg(TuiTheme::g(255), &self.bpm), " BPM"),
|
|
Bsp::e("Beat ", Tui::fg(TuiTheme::g(255), &self.beat)),
|
|
Bsp::e("Time ", Tui::fg(TuiTheme::g(255), &self.time)))));
|
|
impl BeatStats {
|
|
fn new (compact: bool, clock: &Clock) -> Self {
|
|
let bpm = format!("{:.3}", clock.timebase.bpm.get()).into();
|
|
let (beat, time) = if let Some(started) = clock.started.read().unwrap().as_ref() {
|
|
let now = clock.global.usec.get() - started.usec.get();
|
|
(
|
|
clock.timebase.format_beats_1(clock.timebase.usecs_to_pulse(now)).into(),
|
|
format!("{:.3}s", now/1000000.).into()
|
|
)
|
|
} else {
|
|
("-.-.--".to_string().into(), "-.---s".to_string().into())
|
|
};
|
|
Self { compact, bpm, beat, time }
|
|
}
|
|
}
|
|
pub struct OutputStats { compact: bool, sample_rate: Arc<str>, buffer_size: Arc<str>, latency: Arc<str>, }
|
|
content!(TuiOut: |self: OutputStats| Either::new(self.compact,
|
|
row!(FieldV(TuiTheme::g(128).into(), "SR", &self.sample_rate),
|
|
FieldV(TuiTheme::g(128).into(), "Buf", &self.buffer_size),
|
|
FieldV(TuiTheme::g(128).into(), "Lat", &self.latency)),
|
|
col!(Bsp::e(Tui::fg(TuiTheme::g(255), format!("{}", self.sample_rate)), " sample rate"),
|
|
Bsp::e(Tui::fg(TuiTheme::g(255), format!("{}", self.buffer_size)), " sample buffer"),
|
|
Bsp::e(Tui::fg(TuiTheme::g(255), format!("{:.3}ms", self.latency)), " latency"))));
|
|
impl OutputStats {
|
|
fn new (compact: bool, clock: &Clock) -> Self {
|
|
let rate = clock.timebase.sr.get();
|
|
let chunk = clock.chunk.load(Relaxed);
|
|
let sr = if compact {format!("{:.1}kHz", rate / 1000.)} else {format!("{:.0}Hz", rate)};
|
|
Self {
|
|
compact,
|
|
sample_rate: sr.into(),
|
|
buffer_size: format!("{chunk}").into(),
|
|
latency: format!("{:.1}ms", chunk as f64 / rate * 1000.).into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO:
|
|
//keymap!(TRANSPORT_BPM_KEYS = |state: Clock, input: Event| ClockCommand {
|
|
//key(Char(',')) => SetBpm(state.bpm().get() - 1.0),
|
|
//key(Char('.')) => SetBpm(state.bpm().get() + 1.0),
|
|
//key(Char('<')) => SetBpm(state.bpm().get() - 0.001),
|
|
//key(Char('>')) => SetBpm(state.bpm().get() + 0.001),
|
|
//});
|
|
//keymap!(TRANSPORT_QUANT_KEYS = |state: Clock, input: Event| ClockCommand {
|
|
//key(Char(',')) => SetQuant(state.quant.prev()),
|
|
//key(Char('.')) => SetQuant(state.quant.next()),
|
|
//key(Char('<')) => SetQuant(state.quant.prev()),
|
|
//key(Char('>')) => SetQuant(state.quant.next()),
|
|
//});
|
|
//keymap!(TRANSPORT_SYNC_KEYS = |sync: Clock, input: Event | ClockCommand {
|
|
//key(Char(',')) => SetSync(state.sync.prev()),
|
|
//key(Char('.')) => SetSync(state.sync.next()),
|
|
//key(Char('<')) => SetSync(state.sync.prev()),
|
|
//key(Char('>')) => SetSync(state.sync.next()),
|
|
//});
|
|
//keymap!(TRANSPORT_SEEK_KEYS = |state: Clock, input: Event| ClockCommand {
|
|
//key(Char(',')) => todo!("transport seek bar"),
|
|
//key(Char('.')) => todo!("transport seek bar"),
|
|
//key(Char('<')) => todo!("transport seek beat"),
|
|
//key(Char('>')) => todo!("transport seek beat"),
|
|
//});
|