tek/crates/tek_sequencer/src/sequencer.rs

686 lines
23 KiB
Rust

use crate::*;
use tek_core::Direction;
/// Phrase editor.
pub struct Sequencer<E: Engine> {
pub name: Arc<RwLock<String>>,
pub mode: bool,
pub focused: bool,
pub entered: bool,
pub phrase: Option<Arc<RwLock<Phrase>>>,
pub transport: Option<Arc<RwLock<TransportToolbar<E>>>>,
pub buffer: BigBuffer,
pub keys: Buffer,
/// Highlight input keys
pub keys_in: [bool; 128],
/// Highlight output keys
pub keys_out: [bool; 128],
pub now: usize,
pub ppq: usize,
pub note_axis: FixedAxis<usize>,
pub time_axis: ScaledAxis<usize>,
/// Play input through output.
pub monitoring: bool,
/// Write input to sequence.
pub recording: bool,
/// Overdub input to sequence.
pub overdub: bool,
/// Map: tick -> MIDI events at tick
pub phrases: Vec<Arc<RwLock<Phrase>>>,
/// Phrase selector
pub sequence: Option<usize>,
/// Output from current sequence.
pub midi_out: Option<Port<MidiOut>>,
/// MIDI output buffer
midi_out_buf: Vec<Vec<Vec<u8>>>,
/// Send all notes off
pub reset: bool, // TODO?: after Some(nframes)
/// Highlight keys on piano roll.
pub notes_in: [bool;128],
/// Highlight keys on piano roll.
pub notes_out: [bool;128],
}
impl<E: Engine> Sequencer<E> {
pub fn new (name: &str) -> Self {
Self {
name: Arc::new(RwLock::new(name.into())),
monitoring: false,
recording: false,
overdub: true,
phrases: vec![],
sequence: None,
midi_out: None,
midi_out_buf: vec![Vec::with_capacity(16);16384],
reset: true,
notes_in: [false;128],
notes_out: [false;128],
buffer: Default::default(),
keys: keys_vert(),
entered: false,
focused: false,
mode: false,
keys_in: [false;128],
keys_out: [false;128],
phrase: None,
now: 0,
ppq: 96,
transport: None,
note_axis: FixedAxis { start: 12, point: Some(36) },
time_axis: ScaledAxis { start: 0, scale: 24, point: Some(0) },
}
}
pub fn toggle_monitor (&mut self) {
self.monitoring = !self.monitoring;
}
pub fn toggle_record (&mut self) {
self.recording = !self.recording;
}
pub fn toggle_overdub (&mut self) {
self.overdub = !self.overdub;
}
pub fn process (
&mut self,
input: Option<MidiIter>,
timebase: &Arc<Timebase>,
playing: Option<TransportState>,
started: Option<(usize, usize)>,
quant: usize,
reset: bool,
scope: &ProcessScope,
(frame0, frames): (usize, usize),
(_usec0, _usecs): (usize, usize),
period: f64,
) {
if self.midi_out.is_some() {
// Clear the section of the output buffer that we will be using
for frame in &mut self.midi_out_buf[0..frames] {
frame.clear();
}
// Emit "all notes off" at start of buffer if requested
if self.reset {
all_notes_off(&mut self.midi_out_buf);
self.reset = false;
} else if reset {
all_notes_off(&mut self.midi_out_buf);
}
}
if let (
Some(TransportState::Rolling), Some((start_frame, _)), Some(phrase)
) = (
playing, started, self.sequence.and_then(|id|self.phrases.get_mut(id))
) {
phrase.read().map(|phrase|{
if self.midi_out.is_some() {
phrase.process_out(
&mut self.midi_out_buf,
&mut self.notes_out,
timebase,
(frame0.saturating_sub(start_frame), frames, period)
);
}
}).unwrap();
let mut phrase = phrase.write().unwrap();
let length = phrase.length;
// Monitor and record input
if input.is_some() && (self.recording || self.monitoring) {
// For highlighting keys and note repeat
for (frame, event, bytes) in parse_midi_input(input.unwrap()) {
match event {
LiveEvent::Midi { message, .. } => {
if self.monitoring {
self.midi_out_buf[frame].push(bytes.to_vec())
}
if self.recording {
phrase.record_event({
let pulse = timebase.frame_to_pulse(
(frame0 + frame - start_frame) as f64
);
let quantized = (
pulse / quant as f64
).round() as usize * quant;
let looped = quantized % length;
looped
}, message);
}
match message {
MidiMessage::NoteOn { key, .. } => {
self.notes_in[key.as_int() as usize] = true;
}
MidiMessage::NoteOff { key, .. } => {
self.notes_in[key.as_int() as usize] = false;
},
_ => {}
}
},
_ => {}
}
}
}
} else if input.is_some() && self.midi_out.is_some() && self.monitoring {
for (frame, event, bytes) in parse_midi_input(input.unwrap()) {
self.process_monitor_event(frame, &event, bytes)
}
}
if let Some(out) = &mut self.midi_out {
write_midi_output(&mut out.writer(scope), &self.midi_out_buf, frames);
}
}
#[inline]
fn process_monitor_event (&mut self, frame: usize, event: &LiveEvent, bytes: &[u8]) {
match event {
LiveEvent::Midi { message, .. } => {
self.write_to_output_buffer(frame, bytes);
self.process_monitor_message(&message);
},
_ => {}
}
}
#[inline] fn write_to_output_buffer (&mut self, frame: usize, bytes: &[u8]) {
self.midi_out_buf[frame].push(bytes.to_vec());
}
#[inline]
fn process_monitor_message (&mut self, message: &MidiMessage) {
match message {
MidiMessage::NoteOn { key, .. } => {
self.notes_in[key.as_int() as usize] = true;
}
MidiMessage::NoteOff { key, .. } => {
self.notes_in[key.as_int() as usize] = false;
},
_ => {}
}
}
}
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_rect([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)
}
}
}