mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
flatten arranger and piano modules
This commit is contained in:
parent
7a4fa1692b
commit
1723826cc2
19 changed files with 120 additions and 130 deletions
186
src/piano/piano_h.rs
Normal file
186
src/piano/piano_h.rs
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
use crate::*;
|
||||
use super::*;
|
||||
|
||||
pub(crate) fn note_y_iter (note_lo: usize, note_hi: usize, y0: u16) -> impl Iterator<Item=(usize, u16, usize)> {
|
||||
(note_lo..=note_hi).rev().enumerate().map(move|(y, n)|(y, y0 + y as u16, n))
|
||||
}
|
||||
|
||||
render!(Tui: (self: PianoHorizontal) => {
|
||||
let (color, name, length, looped) = if let Some(phrase) = self.phrase().as_ref().map(|p|p.read().unwrap()) {
|
||||
(phrase.color, phrase.name.clone(), phrase.length, phrase.looped)
|
||||
} else {
|
||||
(ItemPalette::from(TuiTheme::g(64)), String::new(), 0, false)
|
||||
};
|
||||
//let field = move|x, y|row!(
|
||||
//Tui::fg_bg(color.lighter.rgb, color.darker.rgb, Tui::bold(true, x)),
|
||||
//Tui::fg_bg(color.lighter.rgb, color.dark.rgb, y),
|
||||
//);
|
||||
let keys_width = 5;
|
||||
let keys = move||PianoHorizontalKeys(self);
|
||||
let timeline = move||PianoHorizontalTimeline(self);
|
||||
let notes = move||PianoHorizontalNotes(self);
|
||||
let cursor = move||PianoHorizontalCursor(self);
|
||||
Outer(Style::default().fg(self.color.dark.rgb).bg(self.color.darkest.rgb)).enclose("kyp");
|
||||
//with_border(lay!(
|
||||
//Push::x(0, row!(
|
||||
////" ",
|
||||
//field("Edit:", name.to_string()), " ",
|
||||
//field("Length:", length.to_string()), " ",
|
||||
//field("Loop:", looped.to_string())
|
||||
//)),
|
||||
//Padding::xy(0, 1, Fill::xy(Bsp::s(
|
||||
//Fixed::y(1, Bsp::e(
|
||||
//Fixed::x(self.keys_width, ""),
|
||||
//Fill::x(timeline()),
|
||||
//)),
|
||||
//Bsp::e(
|
||||
//Fixed::x(self.keys_width, keys()),
|
||||
//Fill::xy(lay!(
|
||||
//&self.size,
|
||||
//Fill::xy(lay!(
|
||||
//Fill::xy(notes()),
|
||||
//Fill::xy(cursor()),
|
||||
//))
|
||||
//)),
|
||||
//),
|
||||
//)))
|
||||
//))
|
||||
});
|
||||
|
||||
impl PianoHorizontal {
|
||||
/// Draw the piano roll foreground using full blocks on note on and half blocks on legato: █▄ █▄ █▄
|
||||
fn draw_bg (buf: &mut BigBuffer, phrase: &MidiClip, zoom: usize, note_len: usize) {
|
||||
for (y, note) in (0..127).rev().enumerate() {
|
||||
for (x, time) in (0..buf.width).map(|x|(x, x*zoom)) {
|
||||
let cell = buf.get_mut(x, y).unwrap();
|
||||
cell.set_bg(phrase.color.darkest.rgb);
|
||||
cell.set_fg(phrase.color.darker.rgb);
|
||||
cell.set_char(if time % 384 == 0 {
|
||||
'│'
|
||||
} else if time % 96 == 0 {
|
||||
'╎'
|
||||
} else if time % note_len == 0 {
|
||||
'┊'
|
||||
} else if (127 - note) % 12 == 1 {
|
||||
'='
|
||||
} else {
|
||||
'·'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Draw the piano roll background using full blocks on note on and half blocks on legato: █▄ █▄ █▄
|
||||
fn draw_fg (buf: &mut BigBuffer, phrase: &MidiClip, zoom: usize) {
|
||||
let style = Style::default().fg(phrase.color.base.rgb);//.bg(Color::Rgb(0, 0, 0));
|
||||
let mut notes_on = [false;128];
|
||||
for (x, time_start) in (0..phrase.length).step_by(zoom).enumerate() {
|
||||
|
||||
for (y, note) in (0..127).rev().enumerate() {
|
||||
if let Some(cell) = buf.get_mut(x, note) {
|
||||
if notes_on[note] {
|
||||
cell.set_char('▂');
|
||||
cell.set_style(style);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let time_end = time_start + zoom;
|
||||
for time in time_start..time_end.min(phrase.length) {
|
||||
for event in phrase.notes[time].iter() {
|
||||
match event {
|
||||
MidiMessage::NoteOn { key, .. } => {
|
||||
let note = key.as_int() as usize;
|
||||
let cell = buf.get_mut(x, note).unwrap();
|
||||
cell.set_char('█');
|
||||
cell.set_style(style);
|
||||
notes_on[note] = true
|
||||
},
|
||||
MidiMessage::NoteOff { key, .. } => {
|
||||
notes_on[key.as_int() as usize] = false
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
has_size!(<Tui>|self:PianoHorizontal|&self.size);
|
||||
|
||||
impl TimeRange for PianoHorizontal {
|
||||
fn time_len (&self) -> &AtomicUsize { self.range.time_len() }
|
||||
fn time_zoom (&self) -> &AtomicUsize { self.range.time_zoom() }
|
||||
fn time_lock (&self) -> &AtomicBool { self.range.time_lock() }
|
||||
fn time_start (&self) -> &AtomicUsize { self.range.time_start() }
|
||||
fn time_axis (&self) -> &AtomicUsize { self.range.time_axis() }
|
||||
}
|
||||
impl NoteRange for PianoHorizontal {
|
||||
fn note_lo (&self) -> &AtomicUsize { self.range.note_lo() }
|
||||
fn note_axis (&self) -> &AtomicUsize { self.range.note_axis() }
|
||||
}
|
||||
impl NotePoint for PianoHorizontal {
|
||||
fn note_len (&self) -> usize { self.point.note_len() }
|
||||
fn set_note_len (&self, x: usize) { self.point.set_note_len(x) }
|
||||
fn note_point (&self) -> usize { self.point.note_point() }
|
||||
fn set_note_point (&self, x: usize) { self.point.set_note_point(x) }
|
||||
}
|
||||
impl TimePoint for PianoHorizontal {
|
||||
fn time_point (&self) -> usize { self.point.time_point() }
|
||||
fn set_time_point (&self, x: usize) { self.point.set_time_point(x) }
|
||||
}
|
||||
impl PhraseViewMode for PianoHorizontal {
|
||||
fn phrase (&self) -> &Option<Arc<RwLock<MidiClip>>> {
|
||||
&self.phrase
|
||||
}
|
||||
fn phrase_mut (&mut self) -> &mut Option<Arc<RwLock<MidiClip>>> {
|
||||
&mut self.phrase
|
||||
}
|
||||
/// Determine the required space to render the phrase.
|
||||
fn buffer_size (&self, phrase: &MidiClip) -> (usize, usize) {
|
||||
(phrase.length / self.range.time_zoom().get(), 128)
|
||||
}
|
||||
fn redraw (&mut self) {
|
||||
let buffer = if let Some(phrase) = self.phrase.as_ref() {
|
||||
let phrase = phrase.read().unwrap();
|
||||
let buf_size = self.buffer_size(&phrase);
|
||||
let mut buffer = BigBuffer::from(buf_size);
|
||||
let note_len = self.note_len();
|
||||
let time_zoom = self.time_zoom().get();
|
||||
self.time_len().set(phrase.length);
|
||||
PianoHorizontal::draw_bg(&mut buffer, &phrase, time_zoom, note_len);
|
||||
PianoHorizontal::draw_fg(&mut buffer, &phrase, time_zoom);
|
||||
buffer
|
||||
} else {
|
||||
Default::default()
|
||||
};
|
||||
self.buffer = buffer
|
||||
}
|
||||
fn set_phrase (&mut self, phrase: Option<&Arc<RwLock<MidiClip>>>) {
|
||||
*self.phrase_mut() = phrase.cloned();
|
||||
self.color = phrase.map(|p|p.read().unwrap().color)
|
||||
.unwrap_or(ItemPalette::from(ItemColor::from(TuiTheme::g(64))));
|
||||
self.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for PianoHorizontal {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
||||
f.debug_struct("PianoHorizontal")
|
||||
.field("time_zoom", &self.range.time_zoom)
|
||||
.field("buffer", &format!("{}x{}", self.buffer.width, self.buffer.height))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
// Update sequencer playhead indicator
|
||||
//self.now().set(0.);
|
||||
//if let Some((ref started_at, Some(ref playing))) = self.player.play_phrase {
|
||||
//let phrase = phrase.read().unwrap();
|
||||
//if *playing.read().unwrap() == *phrase {
|
||||
//let pulse = self.current().pulse.get();
|
||||
//let start = started_at.pulse.get();
|
||||
//let now = (pulse - start) % phrase.length as f64;
|
||||
//self.now().set(now);
|
||||
//}
|
||||
//}
|
||||
33
src/piano/piano_h_cursor.rs
Normal file
33
src/piano/piano_h_cursor.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use crate::*;
|
||||
use super::*;
|
||||
|
||||
pub struct PianoHorizontalCursor<'a>(pub(crate) &'a PianoHorizontal);
|
||||
render!(Tui: |self: PianoHorizontalCursor<'a>, render|{
|
||||
let style = Some(Style::default().fg(self.0.color.lightest.rgb));
|
||||
let note_hi = self.0.note_hi();
|
||||
let note_len = self.0.note_len();
|
||||
let note_lo = self.0.note_lo().get();
|
||||
let note_point = self.0.note_point();
|
||||
let time_point = self.0.time_point();
|
||||
let time_start = self.0.time_start().get();
|
||||
let time_zoom = self.0.time_zoom().get();
|
||||
let [x0, y0, w, _] = render.area().xywh();
|
||||
for (area_y, screen_y, note) in note_y_iter(note_lo, note_hi, y0) {
|
||||
if note == note_point {
|
||||
for x in 0..w {
|
||||
let screen_x = x0 + x;
|
||||
let time_1 = time_start + x as usize * time_zoom;
|
||||
let time_2 = time_1 + time_zoom;
|
||||
if time_1 <= time_point && time_point < time_2 {
|
||||
render.blit(&"█", screen_x, screen_y, style);
|
||||
let tail = note_len as u16 / time_zoom as u16;
|
||||
for x_tail in (screen_x + 1)..(screen_x + tail) {
|
||||
render.blit(&"▂", x_tail, screen_y, style);
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
});
|
||||
59
src/piano/piano_h_keys.rs
Normal file
59
src/piano/piano_h_keys.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use crate::*;
|
||||
use super::*;
|
||||
|
||||
pub struct PianoHorizontalKeys<'a>(pub(crate) &'a PianoHorizontal);
|
||||
|
||||
render!(Tui: |self: PianoHorizontalKeys<'a>, to|{
|
||||
let state = self.0;
|
||||
let color = state.color;
|
||||
let note_lo = state.note_lo().get();
|
||||
let note_hi = state.note_hi();
|
||||
let note_point = state.note_point();
|
||||
let [x, y0, w, h] = to.area().xywh();
|
||||
let key_style = Some(Style::default().fg(Color::Rgb(192, 192, 192)).bg(Color::Rgb(0, 0, 0)));
|
||||
let off_style = Some(Style::default().fg(TuiTheme::g(160)));
|
||||
let on_style = Some(Style::default().fg(TuiTheme::g(255)).bg(color.base.rgb).bold());
|
||||
for (area_y, screen_y, note) in note_y_iter(note_lo, note_hi, y0) {
|
||||
to.blit(&to_key(note), x, screen_y, key_style);
|
||||
if note > 127 {
|
||||
continue
|
||||
}
|
||||
if note == note_point {
|
||||
to.blit(&format!("{:<5}", to_note_name(note)), x, screen_y, on_style)
|
||||
} else {
|
||||
to.blit(&to_note_name(note), x, screen_y, off_style)
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
has_color!(|self: PianoHorizontalKeys<'a>|self.0.color.base);
|
||||
|
||||
impl<'a> NoteRange for PianoHorizontalKeys<'a> {
|
||||
fn note_lo (&self) -> &AtomicUsize { &self.0.note_lo() }
|
||||
fn note_axis (&self) -> &AtomicUsize { &self.0.note_axis() }
|
||||
}
|
||||
|
||||
impl<'a> NotePoint for PianoHorizontalKeys<'a> {
|
||||
fn note_len (&self) -> usize { self.0.note_len() }
|
||||
fn set_note_len (&self, x: usize) { self.0.set_note_len(x) }
|
||||
fn note_point (&self) -> usize { self.0.note_point() }
|
||||
fn set_note_point (&self, x: usize) { self.0.set_note_point(x) }
|
||||
}
|
||||
|
||||
fn to_key (note: usize) -> &'static str {
|
||||
match note % 12 {
|
||||
11 => "████▌",
|
||||
10 => " ",
|
||||
9 => "████▌",
|
||||
8 => " ",
|
||||
7 => "████▌",
|
||||
6 => " ",
|
||||
5 => "████▌",
|
||||
4 => "████▌",
|
||||
3 => " ",
|
||||
2 => "████▌",
|
||||
1 => " ",
|
||||
0 => "████▌",
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
32
src/piano/piano_h_notes.rs
Normal file
32
src/piano/piano_h_notes.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use crate::*;
|
||||
use super::*;
|
||||
|
||||
pub struct PianoHorizontalNotes<'a>(pub(crate) &'a PianoHorizontal);
|
||||
|
||||
render!(Tui: |self: PianoHorizontalNotes<'a>, render|{
|
||||
let time_start = self.0.time_start().get();
|
||||
let note_lo = self.0.note_lo().get();
|
||||
let note_hi = self.0.note_hi();
|
||||
let note_point = self.0.note_point();
|
||||
let source = &self.0.buffer;
|
||||
let [x0, y0, w, h] = render.area().xywh();
|
||||
if h as usize != self.0.note_axis().get() {
|
||||
panic!("area height mismatch");
|
||||
}
|
||||
for (area_x, screen_x) in (x0..x0+w).enumerate() {
|
||||
for (area_y, screen_y, note) in note_y_iter(note_lo, note_hi, y0) {
|
||||
let source_x = time_start + area_x;
|
||||
let source_y = note_hi - area_y;
|
||||
// TODO: enable loop rollover:
|
||||
//let source_x = (time_start + area_x) % source.width.max(1);
|
||||
//let source_y = (note_hi - area_y) % source.height.max(1);
|
||||
let is_in_x = source_x < source.width;
|
||||
let is_in_y = source_y < source.height;
|
||||
if is_in_x && is_in_y {
|
||||
if let Some(source_cell) = source.get(source_x, source_y) {
|
||||
*render.buffer.get_mut(screen_x, screen_y) = source_cell.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
15
src/piano/piano_h_time.rs
Normal file
15
src/piano/piano_h_time.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
use crate::*;
|
||||
use super::*;
|
||||
|
||||
pub struct PianoHorizontalTimeline<'a>(pub(crate) &'a PianoHorizontal);
|
||||
render!(Tui: |self: PianoHorizontalTimeline<'a>, render|{
|
||||
let [x, y, w, h] = render.area();
|
||||
let style = Some(Style::default().dim());
|
||||
let length = self.0.phrase.as_ref().map(|p|p.read().unwrap().length).unwrap_or(1);
|
||||
for (area_x, screen_x) in (0..w).map(|d|(d, d+x)) {
|
||||
let t = area_x as usize * self.0.time_zoom().get();
|
||||
if t < length {
|
||||
render.blit(&"|", screen_x, y, style);
|
||||
}
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue