mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
303 lines
11 KiB
Rust
303 lines
11 KiB
Rust
use crate::*;
|
|
use super::*;
|
|
use KeyCode::{Char, Delete, Tab, Up, Down, Left, Right};
|
|
use ClockCommand::{Play, Pause};
|
|
use GrooveboxCommand as Cmd;
|
|
use PhraseCommand::*;
|
|
use PhrasePoolCommand::*;
|
|
|
|
pub struct Groovebox {
|
|
_jack: Arc<RwLock<JackConnection>>,
|
|
|
|
pub player: MidiPlayer,
|
|
pub pool: PoolModel,
|
|
pub editor: MidiEditor,
|
|
pub sampler: Sampler,
|
|
|
|
pub size: Measure<Tui>,
|
|
pub status: bool,
|
|
pub note_buf: Vec<u8>,
|
|
pub midi_buf: Vec<Vec<Vec<u8>>>,
|
|
pub perf: PerfModel,
|
|
}
|
|
impl Groovebox {
|
|
pub fn new (
|
|
jack: &Arc<RwLock<JackConnection>>,
|
|
midi_from: &[impl AsRef<str>],
|
|
midi_to: &[impl AsRef<str>],
|
|
audio_from: &[&[impl AsRef<str>];2],
|
|
audio_to: &[&[impl AsRef<str>];2],
|
|
) -> Usually<Self> {
|
|
let sampler = crate::sampler::Sampler::new(
|
|
jack, &"sampler", midi_from, audio_from, audio_to
|
|
)?;
|
|
let mut player = crate::midi::MidiPlayer::new(
|
|
jack, &"sequencer", &midi_from, &midi_to
|
|
)?;
|
|
jack.read().unwrap().client().connect_ports(&player.midi_outs[0], &sampler.midi_in)?;
|
|
//jack.connect_midi_from(&player.midi_ins[0], &midi_from)?;
|
|
//jack.connect_midi_from(&sampler.midi_in, &midi_from)?;
|
|
//jack.connect_midi_to(&player.midi_outs[0], &midi_to)?;
|
|
//jack.connect_audio_from(&sampler.audio_ins[0], &audio_from[0])?;
|
|
//jack.connect_audio_from(&sampler.audio_ins[1], &audio_from[1])?;
|
|
//jack.connect_audio_to(&sampler.audio_outs[0], &audio_to[0])?;
|
|
//jack.connect_audio_to(&sampler.audio_outs[1], &audio_to[1])?;
|
|
|
|
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::MidiEditor::from(&phrase);
|
|
Ok(Self {
|
|
_jack: jack.clone(),
|
|
player,
|
|
pool,
|
|
editor,
|
|
sampler,
|
|
size: Measure::new(),
|
|
midi_buf: vec![vec![];65536],
|
|
note_buf: vec![],
|
|
perf: PerfModel::default(),
|
|
status: true,
|
|
})
|
|
}
|
|
}
|
|
has_clock!(|self: Groovebox|self.player.clock());
|
|
audio!(|self: Groovebox, 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
|
|
});
|
|
render!(Tui: (self: Groovebox) => {
|
|
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();
|
|
let color = self.player.play_phrase().as_ref()
|
|
.and_then(|(_,p)|p.as_ref().map(|p|p.read().unwrap().color))
|
|
.clone();
|
|
let transport = Fixed::y(3, row!(
|
|
PlayPause(self.clock().is_rolling()),
|
|
TransportView::new(self, color, true),
|
|
));
|
|
let selector = Push::x(sampler_w, Fixed::y(1, row!(
|
|
PhraseSelector::play_phrase(&self.player),
|
|
PhraseSelector::next_phrase(&self.player),
|
|
)));
|
|
let pool = move|x|Split::w(false, pool_w,
|
|
Pull::y(1, Fill::y(Align::e(PoolView(&self.pool)))),
|
|
x);
|
|
let sampler = move|x|Split::e(false, sampler_w, Fill::xy(col!(
|
|
Meters(self.sampler.input_meter.as_ref()),
|
|
GrooveboxSamples(self)
|
|
)), x);
|
|
let status = EditStatus(&self.sampler, &self.editor, note_pt, pool(sampler(&self.editor)));
|
|
Fill::xy(lay!([
|
|
&self.size,
|
|
Fill::xy(Align::s(Fixed::y(2, GrooveboxStatus::from(self)))),
|
|
Shrink::y(2, col!(![
|
|
transport,
|
|
selector,
|
|
status
|
|
]))
|
|
]))
|
|
});
|
|
|
|
struct EditStatus<'a, T: Content<Tui>>(&'a Sampler, &'a MidiEditor, usize, T);
|
|
impl<'a, T: Content<Tui>> Content<Tui> for EditStatus<'a, T> {
|
|
fn content (&self) -> Option<impl Content<Tui>> {
|
|
Some(Split::n(false, 9, col!([
|
|
row!(|add|{
|
|
if let Some(sample) = &self.0.mapped[self.2] {
|
|
add(&format!("Sample {}", sample.read().unwrap().end))?;
|
|
}
|
|
add(&MidiEditStatus(&self.1))?;
|
|
Ok(())
|
|
}),
|
|
lay!([
|
|
Outer(Style::default().fg(TuiTheme::g(128))),
|
|
Fill::x(Fixed::y(8, if let Some((_, sample)) = &self.0.recording {
|
|
SampleViewer(Some(sample.clone()))
|
|
} else if let Some(sample) = &self.0.mapped[self.2] {
|
|
SampleViewer(Some(sample.clone()))
|
|
} else {
|
|
SampleViewer(None)
|
|
})),
|
|
]),
|
|
]), &self.3))
|
|
}
|
|
}
|
|
impl<'a, T: Content<Tui>> Content<Tui> for EditStatus<'a, T> {
|
|
fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
|
|
self.content().unwrap().min_size(to)
|
|
}
|
|
fn render (&self, to: &mut TuiOutput) -> Usually<()> {
|
|
self.content().unwrap().render(to)
|
|
}
|
|
}
|
|
|
|
struct GrooveboxSamples<'a>(&'a Groovebox);
|
|
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::xy(col_iter!((note_lo..=note_hi).rev() => |note| {
|
|
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: Groovebox, input|GrooveboxCommand::execute_with_state(self, input));
|
|
|
|
input_to_command!(GrooveboxCommand: <Tui>|state: Groovebox, 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(' ')) => Cmd::Clock(
|
|
if state.clock().is_stopped() { Play(Some(0)) } else { Pause(Some(0)) }
|
|
),
|
|
|
|
// Tab: Toggle visibility of phrase pool column
|
|
key_pat!(Tab) => Cmd::Pool(PoolCommand::Show(!state.pool.visible)),
|
|
|
|
// q: Enqueue currently edited phrase
|
|
key_pat!(Char('q')) => Cmd::Enqueue(Some(state.pool.phrase().clone())),
|
|
// 0: Enqueue phrase 0 (stop all)
|
|
key_pat!(Char('0')) => Cmd::Enqueue(Some(state.pool.phrases()[0].clone())),
|
|
|
|
key_pat!(Shift-Char('R')) => Cmd::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();
|
|
Cmd::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) {
|
|
Cmd::Editor(command)
|
|
} else if let Some(command) = PoolCommand::input_to_command(&state.pool, input) {
|
|
Cmd::Pool(command)
|
|
} else {
|
|
return None
|
|
}
|
|
});
|
|
|
|
command!(|self: GrooveboxCommand, state: Groovebox|match self {
|
|
Self::Pool(cmd) => {
|
|
let mut default = |cmd: PoolCommand|cmd
|
|
.execute(&mut state.pool)
|
|
.map(|x|x.map(Self::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(Self::Editor));
|
|
match cmd {
|
|
_ => default()?
|
|
}
|
|
},
|
|
Self::Clock(cmd) => cmd.execute(state)?.map(Self::Clock),
|
|
Self::Sampler(cmd) => cmd.execute(&mut state.sampler)?.map(Self::Sampler),
|
|
Self::Enqueue(phrase) => {
|
|
state.player.enqueue_next(phrase.as_ref());
|
|
None
|
|
},
|
|
Self::History(delta) => {
|
|
todo!("undo/redo")
|
|
},
|
|
});
|