unify arranger, sequencer, transport

This commit is contained in:
🪞👃🪞 2024-09-12 15:30:38 +03:00
parent cd2555ffc7
commit 77519dbb5c
18 changed files with 1618 additions and 1639 deletions

View file

@ -197,3 +197,490 @@ impl<E: Engine> Sequencer<E> {
}
}
}
impl Widget for Sequencer<Tui> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
self.horizontal_draw(to)?;
if self.focused && self.entered {
Corners(Style::default().green().not_dim()).draw(to)?;
}
Ok(Some(to.area()))
}
}
impl<E: Engine> Sequencer<E> {
/// Select which pattern to display. This pre-renders it to the buffer at full resolution.
pub fn show (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) -> Usually<()> {
self.phrase = phrase.map(Clone::clone);
if let Some(ref phrase) = self.phrase {
let width = usize::MAX.min(phrase.read().unwrap().length);
let mut buffer = BigBuffer::new(width, 64);
let phrase = phrase.read().unwrap();
fill_seq_bg(&mut buffer, phrase.length, self.ppq)?;
fill_seq_fg(&mut buffer, &phrase)?;
self.buffer = buffer;
} else {
self.buffer = Default::default();
}
Ok(())
}
pub(crate) fn style_focus (&self) -> Option<Style> {
Some(if self.focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
})
}
pub(crate) fn style_timer_step (now: usize, step: usize, next_step: usize) -> Style {
if step <= now && now < next_step {
Style::default().yellow().bold().not_dim()
} else {
Style::default()
}
}
fn index_to_color (&self, index: u16, default: Color) -> Color {
let index = index as usize;
if self.keys_in[index] && self.keys_out[index] {
Color::Yellow
} else if self.keys_in[index] {
Color::Red
} else if self.keys_out[index] {
Color::Green
} else {
default
}
}
}
fn nth_octave (index: u16) -> &'static str {
match index {
0 => "-1",
1 => "0",
2 => "1",
3 => "2",
4 => "3",
5 => "4",
6 => "5",
7 => "6",
8 => "7",
9 => "8",
10 => "9",
_ => unreachable!()
}
}
fn key_colors (index: u16) -> (Color, Color) {
match index % 6 {
0 => (Color::White, Color::Black),
1 => (Color::White, Color::Black),
2 => (Color::White, Color::White),
3 => (Color::Black, Color::White),
4 => (Color::Black, Color::White),
5 => (Color::Black, Color::White),
_ => unreachable!()
}
}
fn fill_seq_bg (buf: &mut BigBuffer, length: usize, ppq: usize) -> Usually<()> {
for x in 0..buf.width {
if x as usize >= length {
break
}
let style = Style::default();
buf.get_mut(x, 0).map(|cell|{
cell.set_char('-');
cell.set_style(style);
});
for y in 0 .. buf.height {
buf.get_mut(x, y).map(|cell|{
cell.set_char(char_seq_bg(ppq, x as u16));
cell.set_fg(Color::Gray);
cell.modifier = Modifier::DIM;
});
}
}
Ok(())
}
fn char_seq_bg (ppq: usize, x: u16) -> char {
if ppq == 0 {
'·'
} else if x % (4 * ppq as u16) == 0 {
'│'
} else if x % ppq as u16 == 0 {
'╎'
} else {
'·'
}
}
fn fill_seq_fg (buf: &mut BigBuffer, phrase: &Phrase) -> Usually<()> {
let mut notes_on = [false;128];
for x in 0..buf.width {
if x as usize >= phrase.length {
break
}
if let Some(notes) = phrase.notes.get(x as usize) {
if phrase.percussive {
for note in notes {
match note {
MidiMessage::NoteOn { key, .. } =>
notes_on[key.as_int() as usize] = true,
_ => {}
}
}
} else {
for note in notes {
match note {
MidiMessage::NoteOn { key, .. } =>
notes_on[key.as_int() as usize] = true,
MidiMessage::NoteOff { key, .. } =>
notes_on[key.as_int() as usize] = false,
_ => {}
}
}
}
for y in 0..buf.height/2 {
if y >= 64 {
break
}
if let Some(block) = half_block(
notes_on[y as usize * 2],
notes_on[y as usize * 2 + 1],
) {
buf.get_mut(x, y).map(|cell|{
cell.set_char(block);
cell.set_fg(Color::White);
});
}
}
if phrase.percussive {
notes_on.fill(false);
}
}
}
Ok(())
}
pub(crate) fn keys_vert () -> Buffer {
let area = [0, 0, 5, 64];
let mut buffer = Buffer::empty(Rect {
x: area.x(), y: area.y(), width: area.w(), height: area.h()
});
buffer_update(&mut buffer, area, &|cell, x, y| {
let y = 63 - y;
match x {
0 => {
cell.set_char('▀');
let (fg, bg) = key_colors(6 - y % 6);
cell.set_fg(fg);
cell.set_bg(bg);
},
1 => {
cell.set_char('▀');
cell.set_fg(Color::White);
cell.set_bg(Color::White);
},
2 => if y % 6 == 0 {
cell.set_char('C');
},
3 => if y % 6 == 0 {
cell.set_symbol(nth_octave(y / 6));
},
_ => {}
}
});
buffer
}
impl Sequencer<Tui> {
const H_KEYS_OFFSET: usize = 5;
pub(crate) fn horizontal_draw <'a> (&self, to: &mut Tui) -> Usually<()> {
let area = to.area();
Split::down(|add|{
add(&SequenceName(&self))?;
add(&SequenceRange)?;
add(&SequenceLoopRange)?;
add(&SequenceNoteRange)?;
Ok(())
}).render(to.with_area(area.x(), area.y(), 10, area.h()))?;
let area = [area.x() + 10, area.y(), area.w().saturating_sub(10), area.h().min(66)];
Lozenge(Style::default().fg(Nord::BG2)).draw(to.with_rect(area))?;
let area = [area.x() + 1, area.y(), area.w().saturating_sub(1), area.h()];
Layers::new(|add|{
add(&SequenceKeys(&self))?;
add(&self.phrase.as_ref().map(|phrase|SequenceTimer(&self, phrase.clone())))?;
add(&SequenceNotes(&self))?;
add(&SequenceCursor(&self))?;
add(&SequenceZoom(&self))
}).render(to.with_rect(area))?;
Ok(())
}
}
const STYLE_LABEL: Option<Style> = Some(Style {
fg: Some(Color::Reset),
bg: None,
underline_color: None,
add_modifier: Modifier::empty(),
sub_modifier: Modifier::BOLD,
});
const STYLE_VALUE: Option<Style> = Some(Style {
fg: Some(Color::White),
bg: None,
underline_color: None,
add_modifier: Modifier::BOLD,
sub_modifier: Modifier::DIM,
});
struct SequenceName<'a>(&'a Sequencer<Tui>);
impl<'a> Widget for SequenceName<'a> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let [x, y, ..] = to.area();
let frame = [x, y, 10, 4];
Lozenge(Style::default().fg(Nord::BG2)).draw(to.with_rect(frame))?;
to.blit(&"Name:", x + 1, y + 1, STYLE_LABEL)?;
to.blit(&*self.0.name.read().unwrap(), x + 1, y + 2, STYLE_VALUE)?;
Ok(Some(frame))
}
}
struct SequenceRange;
impl Widget for SequenceRange {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let [x, y, ..] = to.area();
let frame = [x, y, 10, 6];
Lozenge(Style::default().fg(Nord::BG2)).draw(to.with_rect(frame))?;
to.blit(&"Start: ", x + 1, y + 1, STYLE_LABEL)?;
to.blit(&" 1.1.1", x + 1, y + 2, STYLE_VALUE)?;
to.blit(&"End: ", x + 1, y + 3, STYLE_LABEL)?;
to.blit(&" 2.1.1", x + 1, y + 4, STYLE_VALUE)?;
Ok(Some(frame))
}
}
struct SequenceLoopRange;
impl Widget for SequenceLoopRange {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let [x, y, ..] = to.area();
let range = [x, y, 10, 7];
Lozenge(Style::default().fg(Nord::BG2)).draw(to.with_rect(range))?;
to.blit(&"Loop [ ]", x + 1, y + 1, STYLE_LABEL)?;
to.blit(&"From: ", x + 1, y + 2, STYLE_LABEL)?;
to.blit(&" 1.1.1", x + 1, y + 3, STYLE_VALUE)?;
to.blit(&"Length: ", x + 1, y + 4, STYLE_LABEL)?;
to.blit(&" 1.0.0", x + 1, y + 5, STYLE_VALUE)?;
Ok(Some(range))
}
}
struct SequenceNoteRange;
impl Widget for SequenceNoteRange {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let [x, y, ..] = to.area();
let range = [x, y, 10, 9];
Lozenge(Style::default().fg(Nord::BG2)).draw(to.with_rect(range))?;
to.blit(&"Notes: ", x + 1, y + 1, STYLE_LABEL)?;
to.blit(&"C#0-C#9 ", x + 1, y + 2, STYLE_VALUE)?;
to.blit(&"[ /2 ]", x + 1, y + 3, STYLE_LABEL)?;
to.blit(&"[ x2 ]", x + 1, y + 4, STYLE_LABEL)?;
to.blit(&"[ Rev ]", x + 1, y + 5, STYLE_LABEL)?;
to.blit(&"[ Inv ]", x + 1, y + 6, STYLE_LABEL)?;
to.blit(&"[ Dup ]", x + 1, y + 7, STYLE_LABEL)?;
Ok(Some(to.area()))
}
}
struct SequenceKeys<'a>(&'a Sequencer<Tui>);
impl<'a> Widget for SequenceKeys<'a> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let area = to.area();
if area.h() < 2 {
return Ok(Some(area))
}
let area = [area.x(), area.y() + 1, 5, area.h() - 2];
buffer_update(to.buffer(), area, &|cell, x, y|{
let y = y + self.0.note_axis.start as u16;
if x < self.0.keys.area.width && y < self.0.keys.area.height {
*cell = self.0.keys.get(x, y).clone()
}
});
Ok(Some(area))
}
}
struct SequenceNotes<'a>(&'a Sequencer<Tui>);
impl<'a> Widget for SequenceNotes<'a> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let area = to.area();
if area.h() < 2 {
return Ok(Some(area))
}
let area = [
area.x() + Sequencer::H_KEYS_OFFSET as u16,
area.y() + 1,
area.w().saturating_sub(Sequencer::H_KEYS_OFFSET as u16),
area.h().saturating_sub(2),
];
to.buffer_update(area, &move |cell, x, y|{
let src_x = ((x as usize + self.0.time_axis.start) * self.0.time_axis.scale) as usize;
let src_y = (y as usize + self.0.note_axis.start) as usize;
if src_x < self.0.buffer.width && src_y < self.0.buffer.height - 1 {
let src = self.0.buffer.get(src_x, self.0.buffer.height - src_y);
src.map(|src|{
cell.set_symbol(src.symbol());
cell.set_fg(src.fg);
});
}
});
Ok(Some(area))
}
}
struct SequenceCursor<'a>(&'a Sequencer<Tui>);
impl<'a> Widget for SequenceCursor<'a> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let area = to.area();
if let (Some(time), Some(note)) = (self.0.time_axis.point, self.0.note_axis.point) {
let x = area.x() + Sequencer::H_KEYS_OFFSET as u16 + time as u16;
let y = area.y() + 1 + note as u16 / 2;
let c = if note % 2 == 0 { "" } else { "" };
to.blit(&c, x, y, self.0.style_focus())
} else {
Ok(Some([0,0,0,0]))
}
}
}
struct SequenceZoom<'a>(&'a Sequencer<Tui>);
impl<'a> Widget for SequenceZoom<'a> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let area = to.area();
let quant = ppq_to_name(self.0.time_axis.scale);
let quant_x = area.x() + area.w() - 1 - quant.len() as u16;
let quant_y = area.y() + area.h() - 2;
to.blit(&quant, quant_x, quant_y, self.0.style_focus())
}
}
struct SequenceTimer<'a>(&'a Sequencer<Tui>, Arc<RwLock<Phrase>>);
impl<'a> Widget for SequenceTimer<'a> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let area = to.area();
let phrase = self.1.read().unwrap();
let (time0, time_z, now) = (
self.0.time_axis.start, self.0.time_axis.scale, self.0.now % phrase.length
);
let [x, _, width, _] = area;
let x2 = x as usize + Sequencer::H_KEYS_OFFSET;
let x3 = x as usize + width as usize;
for x in x2..x3 {
let step = (time0 + x2) * time_z;
let next_step = (time0 + x2 + 1) * time_z;
let style = Sequencer::<Tui>::style_timer_step(now, step as usize, next_step as usize);
to.blit(&"-", x as u16, area.y(), Some(style))?;
}
return Ok(Some([area.x(), area.y(), area.w(), 1]))
}
}
impl Handle<Tui> for Sequencer<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
match from.event() {
// NONE, "seq_cursor_up", "move cursor up", |sequencer: &mut Sequencer| {
key!(KeyCode::Up) => {
match self.entered {
true => { self.note_axis.point_dec(); },
false => { self.note_axis.start_dec(); },
}
Ok(Some(true))
},
// NONE, "seq_cursor_down", "move cursor down", |self: &mut Sequencer| {
key!(KeyCode::Down) => {
match self.entered {
true => { self.note_axis.point_inc(); },
false => { self.note_axis.start_inc(); },
}
Ok(Some(true))
},
// NONE, "seq_cursor_left", "move cursor up", |self: &mut Sequencer| {
key!(KeyCode::Left) => {
match self.entered {
true => { self.time_axis.point_dec(); },
false => { self.time_axis.start_dec(); },
}
Ok(Some(true))
},
// NONE, "seq_cursor_right", "move cursor up", |self: &mut Sequencer| {
key!(KeyCode::Right) => {
match self.entered {
true => { self.time_axis.point_inc(); },
false => { self.time_axis.start_inc(); },
}
Ok(Some(true))
},
// NONE, "seq_mode_switch", "switch the display mode", |self: &mut Sequencer| {
key!(KeyCode::Char('`')) => {
self.mode = !self.mode;
Ok(Some(true))
},
_ => Ok(None)
}
}
}