mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
flatten workspace into 1 crate
This commit is contained in:
parent
7c4e1e2166
commit
d926422c67
147 changed files with 66 additions and 126 deletions
268
src/groovebox.rs
Normal file
268
src/groovebox.rs
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
use crate::*;
|
||||
use super::*;
|
||||
use KeyCode::{Char, Delete, Tab, Up, Down, Left, Right};
|
||||
use ClockCommand::{Play, Pause};
|
||||
use GrooveboxCommand::*;
|
||||
use PhraseCommand::*;
|
||||
use PhrasePoolCommand::*;
|
||||
|
||||
pub struct GrooveboxTui {
|
||||
_jack: Arc<RwLock<JackClient>>,
|
||||
|
||||
pub player: MidiPlayer,
|
||||
pub pool: PoolModel,
|
||||
pub editor: MidiEditorModel,
|
||||
pub sampler: crate::sampler::Sampler,
|
||||
|
||||
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|GrooveboxTui {
|
||||
let mut player = MidiPlayer::new(jack, "sequencer")?;
|
||||
let phrase = Arc::new(RwLock::new(MidiClip::new(
|
||||
"New", true, 4 * player.clock.timebase.ppq.get() as usize,
|
||||
None, Some(ItemColor::random().into())
|
||||
)));
|
||||
player.play_phrase = Some((Moment::zero(&player.clock.timebase), Some(phrase.clone())));
|
||||
let pool = crate::pool::PoolModel::from(&phrase);
|
||||
let editor = crate::midi::MidiEditorModel::from(&phrase);
|
||||
let sampler = crate::sampler::Sampler::new(jack, "sampler")?;
|
||||
Self {
|
||||
_jack: jack.clone(),
|
||||
player,
|
||||
pool,
|
||||
editor,
|
||||
sampler,
|
||||
size: Measure::new(),
|
||||
midi_buf: vec![vec![];65536],
|
||||
note_buf: vec![],
|
||||
perf: PerfModel::default(),
|
||||
status: true,
|
||||
}
|
||||
});
|
||||
audio!(|self: GrooveboxTui, client, scope|{
|
||||
let t0 = self.perf.get_t0();
|
||||
if Control::Quit == ClockAudio(&mut self.player).process(client, scope) {
|
||||
return Control::Quit
|
||||
}
|
||||
if Control::Quit == PlayerAudio(
|
||||
&mut self.player, &mut self.note_buf, &mut self.midi_buf
|
||||
).process(client, scope) {
|
||||
return Control::Quit
|
||||
}
|
||||
if Control::Quit == SamplerAudio(&mut self.sampler).process(client, scope) {
|
||||
return Control::Quit
|
||||
}
|
||||
for RawMidi { time, bytes } in self.player.midi_ins[0].iter(scope) {
|
||||
if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() {
|
||||
match message {
|
||||
MidiMessage::NoteOn { ref key, .. } => {
|
||||
self.editor.set_note_point(key.as_int() as usize);
|
||||
},
|
||||
MidiMessage::Controller { controller, value } => {
|
||||
if let Some(sample) = &self.sampler.mapped[self.editor.note_point()] {
|
||||
let mut sample = sample.write().unwrap();
|
||||
let percentage = value.as_int() as f64 / 127.;
|
||||
match controller.as_int() {
|
||||
20 => {
|
||||
sample.start = (percentage * sample.end as f64) as usize;
|
||||
},
|
||||
21 => {
|
||||
let length = sample.channels[0].len();
|
||||
sample.end = sample.start + (percentage * (length as f64 - sample.start as f64)) as usize;
|
||||
sample.end = sample.end.min(length);
|
||||
},
|
||||
24 => {
|
||||
sample.gain = percentage as f32 * 2.0;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.perf.update(t0, scope);
|
||||
Control::Continue
|
||||
});
|
||||
has_clock!(|self:GrooveboxTui|&self.player.clock);
|
||||
render!(<Tui>|self:GrooveboxTui|{
|
||||
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 sampler_w = 11;
|
||||
let note_pt = self.editor.note_point();
|
||||
Fill::wh(lay!([
|
||||
&self.size,
|
||||
Fill::wh(Align::s(Fixed::h(2, GrooveboxStatus::from(self)))),
|
||||
Tui::shrink_y(2, col!([
|
||||
Fixed::h(2, row!([
|
||||
Fixed::wh(5, 2, PlayPause(self.clock().is_rolling())),
|
||||
Fixed::h(2, TransportView::from((self, self.player.play_phrase().as_ref().map(|(_,p)|
|
||||
p.as_ref().map(|p|p.read().unwrap().color)
|
||||
).flatten().clone(), true))),
|
||||
])),
|
||||
Tui::push_x(sampler_w, Fixed::h(1, row!([
|
||||
PhraseSelector::play_phrase(&self.player),
|
||||
PhraseSelector::next_phrase(&self.player),
|
||||
]))),
|
||||
row!([
|
||||
Tui::split_n(false, 9,
|
||||
col!([
|
||||
row!(|add|{
|
||||
if let Some(sample) = &self.sampler.mapped[note_pt] {
|
||||
add(&format!("Sample {}", sample.read().unwrap().end))?;
|
||||
}
|
||||
add(&MidiEditStatus(&self.editor))?;
|
||||
Ok(())
|
||||
}),
|
||||
lay!([
|
||||
Outer(Style::default().fg(TuiTheme::g(128))),
|
||||
Fill::w(Fixed::h(8, if let Some((_, sample)) = &self.sampler.recording {
|
||||
SampleViewer(Some(sample.clone()))
|
||||
} else if let Some(sample) = &self.sampler.mapped[note_pt] {
|
||||
SampleViewer(Some(sample.clone()))
|
||||
} else {
|
||||
SampleViewer(None)
|
||||
})),
|
||||
]),
|
||||
]),
|
||||
Tui::split_w(false, pool_w,
|
||||
Tui::pull_y(1, Fill::h(Align::e(PoolView(&self.pool)))),
|
||||
Tui::split_e(false, sampler_w, Fill::wh(col!([
|
||||
Meters(self.sampler.input_meter.as_ref()),
|
||||
GrooveboxSamples(self),
|
||||
])), Fill::h(&self.editor))
|
||||
)
|
||||
),
|
||||
]),
|
||||
]))
|
||||
]))
|
||||
});
|
||||
|
||||
struct GrooveboxSamples<'a>(&'a GrooveboxTui);
|
||||
render!(<Tui>|self: GrooveboxSamples<'a>|{
|
||||
let note_lo = self.0.editor.note_lo().load(Relaxed);
|
||||
let note_pt = self.0.editor.note_point();
|
||||
let note_hi = self.0.editor.note_hi();
|
||||
Fill::wh(col!(note in (note_lo..=note_hi).rev() => {
|
||||
let mut bg = if note == note_pt { TuiTheme::g(64) } else { Color::Reset };
|
||||
let mut fg = TuiTheme::g(160);
|
||||
if let Some((index, _)) = self.0.sampler.recording {
|
||||
if note == index {
|
||||
bg = Color::Rgb(64,16,0);
|
||||
fg = Color::Rgb(224,64,32)
|
||||
}
|
||||
} else if self.0.sampler.mapped[note].is_some() {
|
||||
fg = TuiTheme::g(224);
|
||||
}
|
||||
Tui::bg(bg, if let Some(sample) = &self.0.sampler.mapped[note] {
|
||||
Tui::fg(fg, format!("{note:3} ?????? "))
|
||||
} else {
|
||||
Tui::fg(fg, format!("{note:3} (none) "))
|
||||
})
|
||||
}))
|
||||
});
|
||||
|
||||
pub enum GrooveboxCommand {
|
||||
History(isize),
|
||||
Clock(ClockCommand),
|
||||
Pool(PoolCommand),
|
||||
Editor(PhraseCommand),
|
||||
Enqueue(Option<Arc<RwLock<MidiClip>>>),
|
||||
Sampler(SamplerCommand),
|
||||
}
|
||||
|
||||
handle!(<Tui>|self: GrooveboxTui, input|GrooveboxCommand::execute_with_state(self, input));
|
||||
|
||||
input_to_command!(GrooveboxCommand: <Tui>|state: GrooveboxTui, input|match input.event() {
|
||||
// TODO: k: toggle on-screen keyboard
|
||||
key_pat!(Ctrl-Char('k')) => {
|
||||
todo!("keyboard")
|
||||
},
|
||||
|
||||
// Transport: Play from start or rewind to start
|
||||
key_pat!(Char(' ')) => Clock(
|
||||
if state.clock().is_stopped() { Play(Some(0)) } else { Pause(Some(0)) }
|
||||
),
|
||||
|
||||
// Tab: Toggle visibility of phrase pool column
|
||||
key_pat!(Tab) => Pool(PoolCommand::Show(!state.pool.visible)),
|
||||
|
||||
// q: Enqueue currently edited phrase
|
||||
key_pat!(Char('q')) => Enqueue(Some(state.pool.phrase().clone())),
|
||||
// 0: Enqueue phrase 0 (stop all)
|
||||
key_pat!(Char('0')) => Enqueue(Some(state.pool.phrases()[0].clone())),
|
||||
|
||||
key_pat!(Shift-Char('R')) => Sampler(if state.sampler.recording.is_some() {
|
||||
SamplerCommand::RecordFinish
|
||||
} else {
|
||||
SamplerCommand::RecordBegin(u7::from(state.editor.note_point() as u8))
|
||||
}),
|
||||
|
||||
// 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.pool.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.pool, input) {
|
||||
Pool(command)
|
||||
} else {
|
||||
return None
|
||||
}
|
||||
});
|
||||
|
||||
command!(|self:GrooveboxCommand,state:GrooveboxTui|match self {
|
||||
Self::Pool(cmd) => {
|
||||
let mut default = |cmd: PoolCommand|cmd
|
||||
.execute(&mut state.pool)
|
||||
.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.pool.phrase()));
|
||||
undo
|
||||
},
|
||||
// update color in all places simultaneously
|
||||
PoolCommand::Phrase(SetColor(index, _)) => {
|
||||
let undo = default(cmd)?;
|
||||
state.editor.set_phrase(Some(state.pool.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::Sampler(cmd) => cmd.execute(&mut state.sampler)?.map(Sampler),
|
||||
Self::Enqueue(phrase) => {
|
||||
state.player.enqueue_next(phrase.as_ref());
|
||||
None
|
||||
},
|
||||
Self::History(delta) => {
|
||||
todo!("undo/redo")
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue