From bb736b367b2a764e481ded03ed4cfb665f858b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Goran=20Meki=C4=87?= Date: Sun, 1 Nov 2026 13:59:52 +0100 Subject: [PATCH] Hide out of scale notes --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/note_area.rs | 31 ++++++++-- src/piano_roll.rs | 143 ++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 165 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e1ab834..ef6bb1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2084,7 +2084,7 @@ dependencies = [ [[package]] name = "maolan-widgets" -version = "0.1.2" +version = "0.1.3" dependencies = [ "iced", "iced_aw", diff --git a/Cargo.toml b/Cargo.toml index 6aff488..4cac8e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "maolan-widgets" -version = "0.1.2" +version = "0.1.3" edition = "2024" description = "Widgets used for Maolan DAW" license = "BSD-2-Clause" diff --git a/src/note_area.rs b/src/note_area.rs index 2467c70..1a0618c 100644 --- a/src/note_area.rs +++ b/src/note_area.rs @@ -1,7 +1,7 @@ use crate::{ midi::{ - KEYBOARD_WIDTH, KEYS_SCROLL_ID, MIDI_NOTE_COUNT, NOTES_PER_OCTAVE, NOTES_SCROLL_ID, - PITCH_MAX, RIGHT_SCROLL_GUTTER_WIDTH, WHITE_KEY_HEIGHT, WHITE_KEYS_PER_OCTAVE, + KEYBOARD_WIDTH, KEYS_SCROLL_ID, NOTES_PER_OCTAVE, NOTES_SCROLL_ID, + RIGHT_SCROLL_GUTTER_WIDTH, WHITE_KEY_HEIGHT, WHITE_KEYS_PER_OCTAVE, }, piano, }; @@ -24,7 +24,15 @@ pub struct NoteArea { impl NoteArea { pub fn view<'a, Message: 'a>(self, content: Vec>) -> Element<'a, Message> { - let pitch_count = MIDI_NOTE_COUNT; + self.view_with_rows(content, &crate::piano_roll::PitchRows::new(None)) + } + + pub fn view_with_rows<'a, Message: 'a>( + self, + content: Vec>, + rows: &crate::piano_roll::PitchRows, + ) -> Element<'a, Message> { + let pitch_count = rows.pitches().len(); let row_h = ((WHITE_KEY_HEIGHT * WHITE_KEYS_PER_OCTAVE as f32 / NOTES_PER_OCTAVE as f32) * self.zoom_y) .max(1.0); @@ -34,8 +42,7 @@ impl NoteArea { let mut layers: Vec> = vec![]; - for i in 0..pitch_count { - let pitch = PITCH_MAX.saturating_sub(i as u8); + for (i, &pitch) in rows.pitches().iter().enumerate() { let is_black = piano::is_black_key(pitch); layers.push( pin(container("") @@ -52,6 +59,20 @@ impl NoteArea { .position(Point::new(0.0, i as f32 * row_h)) .into(), ); + + if pitch_count < crate::midi::MIDI_NOTE_COUNT { + layers.push( + pin(container("") + .width(Length::Fixed(notes_w)) + .height(Length::Fixed(1.0)) + .style(|_| container::Style { + background: Some(Background::Color(Color::from_rgb(0.25, 0.25, 0.28))), + ..container::Style::default() + })) + .position(Point::new(0.0, (i + 1) as f32 * row_h - 1.0)) + .into(), + ); + } } if let Some(samples_per_bar) = self.samples_per_bar { diff --git a/src/piano_roll.rs b/src/piano_roll.rs index 0941268..8c25d05 100644 --- a/src/piano_roll.rs +++ b/src/piano_roll.rs @@ -7,6 +7,44 @@ use iced::{ widget::{container, pin}, }; +/// Descending MIDI pitches displayed by the piano roll. All geometry and editing +/// must use this mapping when scale rows are collapsed. +#[derive(Clone, Debug)] +pub struct PitchRows { + pitches: Vec, +} + +impl PitchRows { + pub fn new(scale: Option<(u8, &'static [u8])>) -> Self { + let pitches = (0..=PITCH_MAX) + .rev() + .filter(|&pitch| { + scale.is_none_or(|(root, pattern)| { + pattern.contains(&((i16::from(pitch) - i16::from(root)).rem_euclid(12) as u8)) + }) + }) + .collect(); + Self { pitches } + } + + pub fn pitches(&self) -> &[u8] { + &self.pitches + } + + pub fn row(&self, pitch: u8) -> Option { + self.pitches.iter().position(|&p| p == pitch) + } + + pub fn pitch_at(&self, row: f32) -> u8 { + self.pitches[row.floor().clamp(0.0, (self.pitches.len() - 1) as f32) as usize] + } + + pub fn shift(&self, pitch: u8, rows: f32) -> u8 { + self.row(pitch) + .map_or(pitch, |row| self.pitch_at(row as f32 + rows.round())) + } +} + pub struct PianoRoll<'a, Message> { notes: Vec, clip_length_samples: usize, @@ -14,6 +52,7 @@ pub struct PianoRoll<'a, Message> { pixels_per_sample: f32, zoom_x: f32, interaction: Element<'a, Message>, + scale_filter: Option<(u8, &'static [u8])>, } impl<'a, Message: 'a> PianoRoll<'a, Message> { @@ -32,21 +71,35 @@ impl<'a, Message: 'a> PianoRoll<'a, Message> { pixels_per_sample, zoom_x, interaction, + scale_filter: None, } } + pub fn with_scale_filter(mut self, root_semitone: u8, pattern: &'static [u8]) -> Self { + self.scale_filter = Some((root_semitone, pattern)); + self + } + + fn visible_notes(&self) -> impl Iterator { + self.notes.iter().filter(|note| { + note.pitch <= PITCH_MAX + && self.scale_filter.is_none_or(|(root, pattern)| { + let relative = (note.pitch as i16 - root as i16).rem_euclid(12) as u8; + pattern.contains(&relative) + }) + }) + } + pub fn into_element(self) -> Element<'a, Message> { let row_h = row_height(self.zoom_y); let pps = (self.pixels_per_sample * self.zoom_x).max(0.0001); let notes_w = (self.clip_length_samples as f32 * pps).max(1.0); - let notes_h = crate::midi::MIDI_NOTE_COUNT as f32 * row_h; + let rows = PitchRows::new(self.scale_filter); + let notes_h = rows.pitches().len() as f32 * row_h; let mut layers: Vec> = vec![]; - for note in &self.notes { - if note.pitch > PITCH_MAX { - continue; - } - let y_idx = usize::from(PITCH_MAX.saturating_sub(note.pitch)); + for note in self.visible_notes() { + let y_idx = rows.row(note.pitch).expect("visible pitch"); let y = y_idx as f32 * row_h + 1.0; let x = note.start_sample as f32 * pps; let w = (note.length_samples as f32 * pps).max(2.0); @@ -72,3 +125,81 @@ impl<'a, Message: 'a> PianoRoll<'a, Message> { .into() } } + +#[cfg(test)] +mod tests { + use super::*; + + fn note(pitch: u8) -> PianoNote { + PianoNote { + start_sample: 0, + length_samples: 16, + pitch, + velocity: 100, + channel: 0, + mpe: Default::default(), + } + } + + fn piano_roll() -> PianoRoll<'static, ()> { + PianoRoll::new( + vec![note(60), note(61), note(63)], + 256, + 1.0, + 1.0, + 1.0, + container("").into(), + ) + } + + #[test] + fn collapsed_rows_round_trip_for_scale_patterns() { + for pattern in [ + &[0, 2, 4, 5, 7, 9, 11][..], + &[0, 2, 3, 5, 7, 8, 10][..], + &[0, 3, 5, 6, 7, 10][..], + ] { + for root in 0..12 { + let rows = PitchRows::new(Some((root, pattern))); + for (row, &pitch) in rows.pitches().iter().enumerate() { + assert_eq!(rows.row(pitch), Some(row)); + assert_eq!(rows.pitch_at(row as f32 + 0.5), pitch); + } + assert_eq!(rows.pitch_at(-100.0), rows.pitches()[0]); + assert_eq!(rows.pitch_at(200.0), *rows.pitches().last().unwrap()); + } + } + } + + #[test] + fn dragging_moves_by_visible_scale_steps() { + let rows = PitchRows::new(Some((0, &[0, 2, 4, 5, 7, 9, 11]))); + assert_eq!(rows.row(61), None); + assert_eq!(rows.shift(60, -1.0), 62); + assert_eq!(rows.shift(60, 1.0), 59); + assert_eq!(rows.shift(127, -10.0), 127); + assert_eq!(rows.shift(0, 10.0), 0); + let chromatic = PitchRows::new(None); + assert_eq!(chromatic.pitches().len(), 128); + assert_eq!(chromatic.shift(60, -1.0), 61); + } + + #[test] + fn scale_filter_controls_the_notes_used_for_rendering() { + assert_eq!(piano_roll().visible_notes().count(), 3); + + let major: Vec = piano_roll() + .with_scale_filter(0, &[0, 2, 4, 5, 7, 9, 11]) + .visible_notes() + .map(|note| note.pitch) + .collect(); + assert_eq!(major, vec![60]); + + let minor: Vec = piano_roll() + .with_scale_filter(0, &[0, 2, 3, 5, 7, 8, 10]) + .visible_notes() + .map(|note| note.pitch) + .collect(); + assert_eq!(minor, vec![60, 63]); + } +}