mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
168 lines
6 KiB
Rust
168 lines
6 KiB
Rust
use crate::*;
|
|
use ClockCommand::{Play, Pause};
|
|
use KeyCode::{Tab, Char};
|
|
use SequencerCommand as Cmd;
|
|
use MidiEditCommand::*;
|
|
use PhrasePoolCommand::*;
|
|
/// Root view for standalone `tek_sequencer`.
|
|
pub struct SequencerTui {
|
|
_jack: Arc<RwLock<JackConnection>>,
|
|
|
|
pub pool: PoolModel,
|
|
pub editor: MidiEditor,
|
|
pub player: MidiPlayer,
|
|
|
|
pub transport: bool,
|
|
pub selectors: bool,
|
|
pub compact: bool,
|
|
|
|
pub clock: Clock,
|
|
pub size: Measure<Tui>,
|
|
pub status: bool,
|
|
pub note_buf: Vec<u8>,
|
|
pub midi_buf: Vec<Vec<Vec<u8>>>,
|
|
pub perf: PerfModel,
|
|
}
|
|
from_jack!(|jack|SequencerTui {
|
|
let clock = Clock::from(jack);
|
|
let phrase = Arc::new(RwLock::new(MidiClip::new(
|
|
"Clip", true, 4 * clock.timebase.ppq.get() as usize,
|
|
None, Some(ItemColor::random().into())
|
|
)));
|
|
Self {
|
|
_jack: jack.clone(),
|
|
|
|
pool: PoolModel::from(&phrase),
|
|
editor: MidiEditor::from(&phrase),
|
|
player: MidiPlayer::from((&clock, &phrase)),
|
|
|
|
compact: true,
|
|
transport: true,
|
|
selectors: true,
|
|
size: Measure::new(),
|
|
midi_buf: vec![vec![];65536],
|
|
note_buf: vec![],
|
|
perf: PerfModel::default(),
|
|
status: true,
|
|
clock,
|
|
}
|
|
});
|
|
render!(Tui: (self: SequencerTui) => self.size.of(
|
|
Bsp::s(self.toolbar_view(),
|
|
Bsp::n(self.status_view(),
|
|
Bsp::w(self.pool_view(), Fill::xy(&self.editor))))));
|
|
impl SequencerTui {
|
|
fn toolbar_view (&self) -> impl Content<Tui> + use<'_> {
|
|
self.transport.then(||TransportView::new(true, &self.clock))
|
|
}
|
|
fn status_view (&self) -> impl Content<Tui> + use<'_> {
|
|
let edit_clip = MidiEditClip(&self.editor);
|
|
let selectors = When(self.selectors, Bsp::e(ClipSelected::play_phrase(&self.player), ClipSelected::next_phrase(&self.player)));
|
|
row!(selectors, edit_clip, MidiEditStatus(&self.editor))
|
|
}
|
|
fn pool_view (&self) -> impl Content<Tui> + use<'_> {
|
|
let w = self.size.w();
|
|
let phrase_w = if w > 60 { 20 } else if w > 40 { 15 } else { 10 };
|
|
let pool_w = if self.pool.visible { phrase_w } else { 0 };
|
|
let pool = Pull::y(1, Fill::y(Align::e(PoolView(self.pool.visible, &self.pool))));
|
|
Fixed::x(pool_w, Align::e(Fill::y(PoolView(self.compact, &self.pool))))
|
|
}
|
|
}
|
|
audio!(|self:SequencerTui, client, scope|{
|
|
// Start profiling cycle
|
|
let t0 = self.perf.get_t0();
|
|
// Update transport clock
|
|
if Control::Quit == ClockAudio(self).process(client, scope) {
|
|
return Control::Quit
|
|
}
|
|
// Update MIDI sequencer
|
|
if Control::Quit == PlayerAudio(
|
|
&mut self.player, &mut self.note_buf, &mut self.midi_buf
|
|
).process(client, scope) {
|
|
return Control::Quit
|
|
}
|
|
// End profiling cycle
|
|
self.perf.update(t0, scope);
|
|
Control::Continue
|
|
});
|
|
has_size!(<Tui>|self:SequencerTui|&self.size);
|
|
has_clock!(|self:SequencerTui|&self.clock);
|
|
has_phrases!(|self:SequencerTui|self.pool.phrases);
|
|
has_editor!(|self:SequencerTui|self.editor);
|
|
handle!(<Tui>|self:SequencerTui,input|SequencerCommand::execute_with_state(self, input.event()));
|
|
#[derive(Clone, Debug)] pub enum SequencerCommand {
|
|
Compact(bool),
|
|
History(isize),
|
|
Clock(ClockCommand),
|
|
Pool(PoolCommand),
|
|
Editor(MidiEditCommand),
|
|
Enqueue(Option<Arc<RwLock<MidiClip>>>),
|
|
}
|
|
keymap!(KEYS_SEQUENCER = |state: SequencerTui, input: Event| SequencerCommand {
|
|
// TODO: k: toggle on-screen keyboard
|
|
ctrl(key(Char('k'))) => { todo!("keyboard") },
|
|
// Transport: Play/pause
|
|
key(Char(' ')) => Cmd::Clock(if state.clock().is_stopped() { Play(None) } else { Pause(None) }),
|
|
// Transport: Play from start or rewind to start
|
|
shift(key(Char(' '))) => Cmd::Clock(if state.clock().is_stopped() { Play(Some(0)) } else { Pause(Some(0)) }),
|
|
// u: undo
|
|
key(Char('u')) => Cmd::History(-1),
|
|
// Shift-U: redo
|
|
key(Char('U')) => Cmd::History( 1),
|
|
// Tab: Toggle compact mode
|
|
key(Tab) => Cmd::Compact(!state.compact),
|
|
// q: Enqueue currently edited phrase
|
|
key(Char('q')) => Cmd::Enqueue(Some(state.pool.phrase().clone())),
|
|
// 0: Enqueue phrase 0 (stop all)
|
|
key(Char('0')) => Cmd::Enqueue(Some(state.phrases()[0].clone())),
|
|
// e: Toggle between editing currently playing or other phrase
|
|
key(Char('e')) => if let Some((_, Some(playing))) = state.player.play_phrase() {
|
|
let editing = state.editor.phrase().as_ref().map(|p|p.read().unwrap().clone());
|
|
let selected = state.pool.phrase().clone();
|
|
Cmd::Editor(Show(Some(if Some(selected.read().unwrap().clone()) != editing {
|
|
selected
|
|
} else {
|
|
playing.clone()
|
|
})))
|
|
} else {
|
|
return None
|
|
}
|
|
}, if let Some(command) = MidiEditCommand::input_to_command(&state.editor, input) {
|
|
Cmd::Editor(command)
|
|
} else if let Some(command) = PoolCommand::input_to_command(&state.pool, input) {
|
|
Cmd::Pool(command)
|
|
} else {
|
|
return None
|
|
});
|
|
command!(|self: SequencerCommand, state: SequencerTui|match self {
|
|
Self::Enqueue(phrase) => {
|
|
state.player.enqueue_next(phrase.as_ref());
|
|
None
|
|
},
|
|
Self::Pool(cmd) => match cmd {
|
|
// autoselect: automatically load selected phrase in editor
|
|
PoolCommand::Select(_) => {
|
|
let undo = cmd.delegate(&mut state.pool, Self::Pool)?;
|
|
state.editor.set_phrase(Some(state.pool.phrase()));
|
|
undo
|
|
},
|
|
// update color in all places simultaneously
|
|
PoolCommand::Phrase(SetColor(index, _)) => {
|
|
let undo = cmd.delegate(&mut state.pool, Self::Pool)?;
|
|
state.editor.set_phrase(Some(state.pool.phrase()));
|
|
undo
|
|
},
|
|
_ => cmd.delegate(&mut state.pool, Self::Pool)?
|
|
},
|
|
Self::Editor(cmd) => cmd.delegate(&mut state.editor, Self::Editor)?,
|
|
Self::Clock(cmd) => cmd.delegate(state, Self::Clock)?,
|
|
Self::History(delta) => {
|
|
todo!("undo/redo")
|
|
},
|
|
Self::Compact(compact) => if state.compact != compact {
|
|
state.compact = compact;
|
|
Some(Self::Compact(!compact))
|
|
} else {
|
|
None
|
|
},
|
|
});
|