rename phrase -> clip mostly everywhere

This commit is contained in:
🪞👃🪞 2025-01-10 02:12:31 +01:00
parent 709391ff0a
commit 08f7a62692
24 changed files with 426 additions and 423 deletions

View file

@ -1,5 +1,42 @@
use crate::*;
use super::*;
/// A clip, rendered as a horizontal piano roll.
pub struct PianoHorizontal {
pub clip: Option<Arc<RwLock<MidiClip>>>,
/// Buffer where the whole clip is rerendered on change
pub buffer: Arc<RwLock<BigBuffer>>,
/// Size of actual notes area
pub size: Measure<TuiOut>,
/// The display window
pub range: MidiRangeModel,
/// The note cursor
pub point: MidiPointModel,
/// The highlight color palette
pub color: ItemPalette,
/// Width of the keyboard
pub keys_width: u16,
}
impl PianoHorizontal {
pub fn new (clip: Option<&Arc<RwLock<MidiClip>>>) -> Self {
let size = Measure::new();
let mut range = MidiRangeModel::from((24, true));
range.time_axis = size.x.clone();
range.note_axis = size.y.clone();
let mut piano = Self {
keys_width: 5,
size,
range,
buffer: RwLock::new(Default::default()).into(),
point: MidiPointModel::default(),
clip: clip.cloned(),
color: clip.as_ref()
.map(|p|p.read().unwrap().color)
.unwrap_or(ItemPalette::from(ItemColor::from(TuiTheme::g(64)))),
};
piano.redraw();
piano
}
}
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))
}
@ -10,17 +47,22 @@ render!(TuiOut: (self: PianoHorizontal) => Bsp::s( // the freeze is in the piano
)),
Fill::xy(Bsp::e(
Fixed::x(self.keys_width, PianoHorizontalKeys(self)),
Fill::xy(self.size.of(lay!(self.notes(), self.cursor()))),
Fill::xy(self.size.of("")),
//"",
//Fill::xy(self.size.of(lay!(
////self.notes(),
////self.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) {
fn draw_bg (buf: &mut BigBuffer, clip: &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_bg(clip.color.darkest.rgb);
cell.set_fg(clip.color.darker.rgb);
cell.set_char(if time % 384 == 0 {
'│'
} else if time % 96 == 0 {
@ -38,10 +80,10 @@ impl PianoHorizontal {
}
}
/// 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));
fn draw_fg (buf: &mut BigBuffer, clip: &MidiClip, zoom: usize) {
let style = Style::default().fg(clip.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 (x, time_start) in (0..clip.length).step_by(zoom).enumerate() {
for (y, note) in (0..=127).rev().enumerate() {
if let Some(cell) = buf.get_mut(x, note) {
@ -53,8 +95,8 @@ impl PianoHorizontal {
}
let time_end = time_start + zoom;
for time in time_start..time_end.min(phrase.length) {
for event in phrase.notes[time].iter() {
for time in time_start..time_end.min(clip.length) {
for event in clip.notes[time].iter() {
match event {
MidiMessage::NoteOn { key, .. } => {
let note = key.as_int() as usize;
@ -80,31 +122,32 @@ impl PianoHorizontal {
let note_hi = self.note_hi();
let note_point = self.note_point();
let buffer = self.buffer.clone();
RenderThunk::new(move|render: &mut TuiOut|{
let source = buffer.read().unwrap();
let [x0, y0, w, h] = render.area().xywh();
if h as usize != note_axis {
panic!("area height mismatch: {h} <> {note_axis}");
}
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) {
if let Some(cell) = render.buffer.cell_mut(ratatui::prelude::Position::from((screen_x, screen_y))) {
*cell = source_cell.clone();
}
}
}
}
}
})
return ""
//RenderThunk::new(move|render: &mut TuiOut|{
//let source = buffer.read().unwrap();
//let [x0, y0, w, h] = render.area().xywh();
//if h as usize != note_axis {
//panic!("area height mismatch: {h} <> {note_axis}");
//}
//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) {
//if let Some(cell) = render.buffer.cell_mut(ratatui::prelude::Position::from((screen_x, screen_y))) {
//*cell = source_cell.clone();
//}
//}
//}
//}
//}
//})
}
fn cursor (&self) -> impl Content<TuiOut> {
let style = Some(Style::default().fg(self.color.lightest.rgb));
@ -115,27 +158,28 @@ impl PianoHorizontal {
let time_point = self.time_point();
let time_start = self.time_start().get();
let time_zoom = self.time_zoom().get();
RenderThunk::new(move|render: &mut TuiOut|{
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
}
}
})
""
//RenderThunk::new(move|render: &mut TuiOut|{
////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
////}
////}
//})
}
}
@ -163,35 +207,35 @@ impl TimePoint for PianoHorizontal {
fn set_time_point (&self, x: usize) { self.point.set_time_point(x) }
}
impl MidiViewer for PianoHorizontal {
fn phrase (&self) -> &Option<Arc<RwLock<MidiClip>>> {
&self.phrase
fn clip (&self) -> &Option<Arc<RwLock<MidiClip>>> {
&self.clip
}
fn phrase_mut (&mut self) -> &mut Option<Arc<RwLock<MidiClip>>> {
&mut self.phrase
fn clip_mut (&mut self) -> &mut Option<Arc<RwLock<MidiClip>>> {
&mut self.clip
}
/// Determine the required space to render the phrase.
fn buffer_size (&self, phrase: &MidiClip) -> (usize, usize) {
(phrase.length / self.range.time_zoom().get(), 128)
/// Determine the required space to render the clip.
fn buffer_size (&self, clip: &MidiClip) -> (usize, usize) {
(clip.length / self.range.time_zoom().get(), 128)
}
fn redraw (&self) {
let buffer = if let Some(phrase) = self.phrase.as_ref() {
let phrase = phrase.read().unwrap();
let buf_size = self.buffer_size(&phrase);
let buffer = if let Some(clip) = self.clip.as_ref() {
let clip = clip.read().unwrap();
let buf_size = self.buffer_size(&clip);
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);
self.time_len().set(clip.length);
PianoHorizontal::draw_bg(&mut buffer, &clip, time_zoom, note_len);
PianoHorizontal::draw_fg(&mut buffer, &clip, time_zoom);
buffer
} else {
Default::default()
};
*self.buffer.write().unwrap() = 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)
fn set_clip (&mut self, clip: Option<&Arc<RwLock<MidiClip>>>) {
*self.clip_mut() = clip.cloned();
self.color = clip.map(|p|p.read().unwrap().color)
.unwrap_or(ItemPalette::from(ItemColor::from(TuiTheme::g(64))));
self.redraw();
}
@ -208,12 +252,12 @@ impl std::fmt::Debug for PianoHorizontal {
}
// 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 {
//if let Some((ref started_at, Some(ref playing))) = self.player.play_clip {
//let clip = clip.read().unwrap();
//if *playing.read().unwrap() == *clip {
//let pulse = self.current().pulse.get();
//let start = started_at.pulse.get();
//let now = (pulse - start) % phrase.length as f64;
//let now = (pulse - start) % clip.length as f64;
//self.now().set(now);
//}
//}