mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
182 lines
6.4 KiB
Rust
182 lines
6.4 KiB
Rust
use crate::*;
|
|
use ClockCommand::{Play, Pause};
|
|
use KeyCode::{Tab, Char};
|
|
use SequencerCommand::*;
|
|
use PhraseCommand::*;
|
|
use PhrasePoolCommand::*;
|
|
/// Root view for standalone `tek_sequencer`.
|
|
pub struct SequencerTui {
|
|
_jack: Arc<RwLock<JackConnection>>,
|
|
pub transport: bool,
|
|
pub selectors: bool,
|
|
pub clock: ClockModel,
|
|
pub phrases: PoolModel,
|
|
pub player: MidiPlayer,
|
|
pub editor: MidiEditor,
|
|
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 = ClockModel::from(jack);
|
|
let phrase = Arc::new(RwLock::new(MidiClip::new(
|
|
"New", true, 4 * clock.timebase.ppq.get() as usize,
|
|
None, Some(ItemColor::random().into())
|
|
)));
|
|
Self {
|
|
_jack: jack.clone(),
|
|
transport: true,
|
|
selectors: true,
|
|
phrases: PoolModel::from(&phrase),
|
|
editor: MidiEditor::from(&phrase),
|
|
player: MidiPlayer::from((&clock, &phrase)),
|
|
size: Measure::new(),
|
|
midi_buf: vec![vec![];65536],
|
|
note_buf: vec![],
|
|
perf: PerfModel::default(),
|
|
status: true,
|
|
clock,
|
|
}
|
|
});
|
|
render!(Tui: (self: SequencerTui) => {
|
|
let w = self.size.w();
|
|
let phrase_w = if w > 60 { 20 } else if w > 40 { 15 } else { 10 };
|
|
let pool_w = if self.phrases.visible { phrase_w } else { 0 };
|
|
let pool = Pull::y(1, Fill::y(Align::e(PoolView(&self.phrases))));
|
|
let with_pool = move|x|Split::w(false, pool_w, pool, x);
|
|
let status = SequencerStatus::from(self);
|
|
let with_status = |x|Split::n(false, if self.status { 2 } else { 0 }, status, x);
|
|
let with_editbar = |x|Split::n(false, 1, MidiEditStatus(&self.editor), x);
|
|
let with_size = |x|lay!([self.size, x]);
|
|
let editor = with_editbar(with_pool(Fill::xy(&self.editor)));
|
|
|
|
let color = self.player.play_phrase().as_ref().map(|(_,p)|
|
|
p.as_ref().map(|p|p.read().unwrap().color)
|
|
).flatten().clone();
|
|
|
|
let toolbar = Cond::when(self.transport, row!([
|
|
PlayPause(self.clock.is_rolling()),
|
|
TransportView::new(self, color, true),
|
|
]));
|
|
|
|
let play_queue = Cond::when(self.selectors, row!([
|
|
PhraseSelector::play_phrase(&self.player),
|
|
PhraseSelector::next_phrase(&self.player),
|
|
]));
|
|
|
|
Min::y(15, with_size(with_status(col!([
|
|
toolbar,
|
|
play_queue,
|
|
editor,
|
|
]))))
|
|
});
|
|
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.phrases.phrases);
|
|
has_editor!(|self:SequencerTui|self.editor);
|
|
handle!(<Tui>|self:SequencerTui,input|SequencerCommand::execute_with_state(self, input));
|
|
#[derive(Clone, Debug)] pub enum SequencerCommand {
|
|
History(isize),
|
|
Clock(ClockCommand),
|
|
Pool(PoolCommand),
|
|
Editor(PhraseCommand),
|
|
Enqueue(Option<Arc<RwLock<MidiClip>>>),
|
|
}
|
|
input_to_command!(SequencerCommand: <Tui>|state: SequencerTui, input|match input.event() {
|
|
// TODO: k: toggle on-screen keyboard
|
|
key_pat!(Ctrl-Char('k')) => { todo!("keyboard") },
|
|
// Transport: Play/pause
|
|
key_pat!(Char(' ')) => Clock(
|
|
if state.clock().is_stopped() { Play(None) } else { Pause(None) }
|
|
),
|
|
// Transport: Play from start or rewind to start
|
|
key_pat!(Shift-Char(' ')) => Clock(
|
|
if state.clock().is_stopped() { Play(Some(0)) } else { Pause(Some(0)) }
|
|
),
|
|
// u: undo
|
|
key_pat!(Char('u')) => History(-1),
|
|
// Shift-U: redo
|
|
key_pat!(Char('U')) => History( 1),
|
|
// Tab: Toggle visibility of phrase pool column
|
|
key_pat!(Tab) => Pool(PoolCommand::Show(!state.phrases.visible)),
|
|
// q: Enqueue currently edited phrase
|
|
key_pat!(Char('q')) => Enqueue(Some(state.phrases.phrase().clone())),
|
|
// 0: Enqueue phrase 0 (stop all)
|
|
key_pat!(Char('0')) => Enqueue(Some(state.phrases.phrases()[0].clone())),
|
|
// e: Toggle between editing currently playing or other phrase
|
|
key_pat!(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.phrases.phrase().clone();
|
|
Editor(Show(Some(if Some(selected.read().unwrap().clone()) != editing {
|
|
selected
|
|
} else {
|
|
playing.clone()
|
|
})))
|
|
} else {
|
|
return None
|
|
},
|
|
// For the rest, use the default keybindings of the components.
|
|
// The ones defined above supersede them.
|
|
_ => if let Some(command) = PhraseCommand::input_to_command(&state.editor, input) {
|
|
Editor(command)
|
|
} else if let Some(command) = PoolCommand::input_to_command(&state.phrases, input) {
|
|
Pool(command)
|
|
} else {
|
|
return None
|
|
}
|
|
});
|
|
command!(|self: SequencerCommand, state: SequencerTui|match self {
|
|
Self::Pool(cmd) => {
|
|
let mut default = |cmd: PoolCommand|cmd
|
|
.execute(&mut state.phrases)
|
|
.map(|x|x.map(Pool));
|
|
match cmd {
|
|
// autoselect: automatically load selected phrase in editor
|
|
PoolCommand::Select(_) => {
|
|
let undo = default(cmd)?;
|
|
state.editor.set_phrase(Some(state.phrases.phrase()));
|
|
undo
|
|
},
|
|
// update color in all places simultaneously
|
|
PoolCommand::Phrase(SetColor(index, _)) => {
|
|
let undo = default(cmd)?;
|
|
state.editor.set_phrase(Some(state.phrases.phrase()));
|
|
undo
|
|
},
|
|
_ => default(cmd)?
|
|
}
|
|
},
|
|
Self::Editor(cmd) => {
|
|
let default = ||cmd.execute(&mut state.editor).map(|x|x.map(Editor));
|
|
match cmd {
|
|
_ => default()?
|
|
}
|
|
},
|
|
Self::Clock(cmd) => cmd.execute(state)?.map(Clock),
|
|
Self::Enqueue(phrase) => {
|
|
state.player.enqueue_next(phrase.as_ref());
|
|
None
|
|
},
|
|
Self::History(delta) => {
|
|
todo!("undo/redo")
|
|
},
|
|
});
|