use crate::midi::{MIDI_NOTE_COUNT, NOTES_PER_OCTAVE, WHITE_KEY_HEIGHT, WHITE_KEYS_PER_OCTAVE};
use iced::{
Background, Color, Event, Point, Rectangle, Renderer, Size, Theme, gradient, mouse,
widget::canvas::{self, Action as CanvasAction, Frame, Geometry, Path, Program},
};
use std::collections::{HashMap, HashSet};
pub fn is_black_key(pitch: u8) -> bool {
matches!(pitch % 12, 1 | 3 | 6 | 8 | 10)
}
pub fn note_color(velocity: u8, channel: u8) -> Color {
let t = (velocity as f32 / 127.0).clamp(0.0, 1.0);
let c = (channel as f32 / 15.0).clamp(0.0, 1.0);
Color {
r: 0.25 + 0.45 * t,
g: 0.35 + 0.4 * (1.0 - c),
b: 0.65 + 0.3 * c,
a: 0.9,
}
}
pub fn brighten(color: Color, amount: f32) -> Color {
Color {
r: (color.r + amount).min(1.0),
g: (color.g + amount).min(1.0),
b: (color.b + amount).min(1.0),
a: color.a,
}
}
pub fn darken(color: Color, amount: f32) -> Color {
Color {
r: (color.r - amount).max(0.0),
g: (color.g - amount).max(0.0),
b: (color.b - amount).max(0.0),
a: color.a,
}
}
pub fn note_two_edge_gradient(base: Color) -> Background {
let edge = brighten(base, 0.08);
let middle = darken(base, 0.08);
Background::Gradient(
gradient::Linear::new(0.0)
.add_stop(0.0, edge)
.add_stop(0.5, middle)
.add_stop(1.0, edge)
.into(),
)
}
pub fn octave_note_count(octave: u8) -> u8 {
let start = usize::from(octave) * NOTES_PER_OCTAVE;
if start >= MIDI_NOTE_COUNT {
0
} else {
(MIDI_NOTE_COUNT - start).min(NOTES_PER_OCTAVE) as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Orientation {
#[default]
Degree0,
Degree90,
Degree180,
Degree270,
}
const BLACK_KEY_DEPTH_RATIO: f32 = 0.5;
fn white_key_end(note: u8, note_count: u8) -> f32 {
let next = note + 1;
if next < note_count && is_black_key(next) {
f32::from(next) + 0.5
} else {
f32::from(next)
}
}
fn velocity_from_v(v: f32, depth: f32) -> u8 {
let normalized = (v / depth).clamp(0.0, 1.0);
(normalized * 126.0).round() as u8 + 1
}
pub fn note_at_in_range(
cursor: Point,
bounds: Rectangle,
orientation: Orientation,
note_count: u8,
) -> Option<(u8, u8)> {
if note_count == 0 {
return None;
}
let natural_cursor = to_natural_cursor(orientation, bounds, cursor);
let natural = natural_bounds(orientation, bounds);
let height = natural.height;
let width = natural.width;
let note_size = height / f32::from(note_count);
let black_key_width = width * BLACK_KEY_DEPTH_RATIO;
let u = natural_cursor.x.clamp(0.0, height);
let v = natural_cursor.y;
if v <= black_key_width {
let note_id = (u / note_size).floor().min(f32::from(note_count - 1)) as u8;
if is_black_key(note_id) {
return Some((note_id, velocity_from_v(v, black_key_width)));
}
}
let note_id = (0..note_count)
.filter(|¬e| !is_black_key(note))
.find(|¬e| u <= white_key_end(note, note_count) * note_size)?;
let velocity = velocity_from_v(v, width);
Some((note_id, velocity))
}
pub fn map_point(orientation: Orientation, bounds: Rectangle, u: f32, v: f32) -> Point {
let x = bounds.x;
let y = bounds.y;
let w = bounds.width;
let h = bounds.height;
match orientation {
Orientation::Degree0 => Point::new(x + u, y + v),
Orientation::Degree90 => Point::new(x + w - v, y + u),
Orientation::Degree180 => Point::new(x + u, y + h - v),
Orientation::Degree270 => Point::new(x + v, y + h - u),
}
}
fn map_rect(
orientation: Orientation,
bounds: Rectangle,
u_low: f32,
u_high: f32,
v_low: f32,
v_high: f32,
) -> (Point, Size) {
let x = bounds.x;
let y = bounds.y;
match orientation {
Orientation::Degree0 => (
Point::new(x + u_low, y + v_low),
Size::new(u_high - u_low, v_high - v_low),
),
Orientation::Degree90 => (
Point::new(x + bounds.width - v_high, y + u_low),
Size::new(v_high - v_low, u_high - u_low),
),
Orientation::Degree180 => (
Point::new(x + u_low, y + bounds.height - v_high),
Size::new(u_high - u_low, v_high - v_low),
),
Orientation::Degree270 => (
Point::new(x + v_low, y + bounds.height - u_high),
Size::new(v_high - v_low, u_high - u_low),
),
}
}
fn natural_bounds(orientation: Orientation, bounds: Rectangle) -> Rectangle {
match orientation {
Orientation::Degree90 | Orientation::Degree270 => bounds,
Orientation::Degree0 | Orientation::Degree180 => Rectangle {
x: bounds.x,
y: bounds.y,
width: bounds.height,
height: bounds.width,
},
}
}
fn to_natural_cursor(orientation: Orientation, bounds: Rectangle, cursor: Point) -> Point {
let w = bounds.width;
let h = bounds.height;
match orientation {
Orientation::Degree0 => Point::new(cursor.x, cursor.y),
Orientation::Degree90 => Point::new(cursor.y, w - cursor.x),
Orientation::Degree180 => Point::new(cursor.x, h - cursor.y),
Orientation::Degree270 => Point::new(h - cursor.y, cursor.x),
}
}
fn text_align_x(orientation: Orientation) -> iced::advanced::text::Alignment {
use iced::advanced::text::Alignment;
match orientation {
Orientation::Degree0 => Alignment::Center,
Orientation::Degree90 => Alignment::Left,
Orientation::Degree180 => Alignment::Center,
Orientation::Degree270 => Alignment::Right,
}
}
fn text_align_y(orientation: Orientation) -> iced::alignment::Vertical {
use iced::alignment::Vertical;
match orientation {
Orientation::Degree0 => Vertical::Top,
Orientation::Degree90 => Vertical::Center,
Orientation::Degree180 => Vertical::Bottom,
Orientation::Degree270 => Vertical::Center,
}
}
fn draw_keyboard_range_into(
frame: &mut Frame<Renderer>,
bounds: Rectangle,
pressed_notes: &HashSet<u8>,
octave: u8,
midnam_note_names: &HashMap<u8, String>,
orientation: Orientation,
note_count: u8,
) {
if note_count == 0 {
return;
}
let natural = natural_bounds(orientation, bounds);
let height = natural.height;
let width = natural.width;
let note_size = height / f32::from(note_count);
let black_key_width = width * BLACK_KEY_DEPTH_RATIO;
let stroke = canvas::Stroke::default().with_width(1.0);
let h_align = text_align_x(orientation);
let v_align = text_align_y(orientation);
let mut u_low = 0.0;
for note_id in (0..note_count).filter(|¬e| !is_black_key(note)) {
let midi_note = octave * 12 + note_id;
let is_pressed = pressed_notes.contains(¬e_id);
let color = if is_pressed {
Color::from_rgb(0.0, 0.5, 1.0)
} else {
Color::WHITE
};
let u_high = white_key_end(note_id, note_count) * note_size;
let (origin, size) = map_rect(orientation, bounds, u_low, u_high, 0.0, width);
let rect = Path::rectangle(origin, size);
frame.fill(&rect, color);
frame.stroke(&rect, stroke);
if let Some(note_name) = midnam_note_names.get(&midi_note) {
use iced::widget::canvas::Text;
let text_pos = map_point(orientation, bounds, (u_low + u_high) * 0.5, width - 4.0);
frame.fill_text(Text {
content: note_name.clone(),
position: text_pos,
color: Color::BLACK,
size: 10.0.into(),
align_x: h_align,
align_y: v_align,
..Text::default()
});
}
u_low = u_high;
}
for note_id in (0..note_count).filter(|¬e| is_black_key(note)) {
let is_pressed = pressed_notes.contains(¬e_id);
let color = if is_pressed {
Color::from_rgb(0.0, 0.4, 0.8)
} else {
Color::BLACK
};
let (origin, size) = map_rect(
orientation,
bounds,
f32::from(note_id) * note_size,
f32::from(note_id + 1) * note_size,
0.0,
black_key_width,
);
let rect = Path::rectangle(origin, size);
frame.fill(&rect, color);
}
}
pub fn draw_octave_into(
frame: &mut Frame<Renderer>,
bounds: Rectangle,
pressed_notes: &HashSet<u8>,
octave: u8,
midnam_note_names: &HashMap<u8, String>,
orientation: Orientation,
) {
draw_keyboard_range_into(
frame,
bounds,
pressed_notes,
octave,
midnam_note_names,
orientation,
12,
);
}
pub fn draw_partial_octave_into(
frame: &mut Frame<Renderer>,
bounds: Rectangle,
pressed_notes: &HashSet<u8>,
octave: u8,
midnam_note_names: &HashMap<u8, String>,
orientation: Orientation,
) {
draw_keyboard_range_into(
frame,
bounds,
pressed_notes,
octave,
midnam_note_names,
orientation,
octave_note_count(octave),
);
}
pub fn draw_octave(
renderer: &Renderer,
bounds: Rectangle,
pressed_notes: &HashSet<u8>,
octave: u8,
midnam_note_names: &HashMap<u8, String>,
orientation: Orientation,
) -> Vec<canvas::Geometry> {
let mut frame = Frame::new(renderer, bounds.size());
let local_bounds = Rectangle {
x: 0.0,
y: 0.0,
width: bounds.width,
height: bounds.height,
};
draw_octave_into(
&mut frame,
local_bounds,
pressed_notes,
octave,
midnam_note_names,
orientation,
);
vec![frame.into_geometry()]
}
pub fn draw_partial_octave(
renderer: &Renderer,
bounds: Rectangle,
pressed_notes: &HashSet<u8>,
octave: u8,
midnam_note_names: &HashMap<u8, String>,
orientation: Orientation,
) -> Vec<canvas::Geometry> {
let mut frame = Frame::new(renderer, bounds.size());
let local_bounds = Rectangle {
x: 0.0,
y: 0.0,
width: bounds.width,
height: bounds.height,
};
draw_partial_octave_into(
&mut frame,
local_bounds,
pressed_notes,
octave,
midnam_note_names,
orientation,
);
vec![frame.into_geometry()]
}
#[derive(Debug, Clone)]
pub struct OctaveKeyboard<Message, Press, Release>
where
Press: Fn(u8, u8) -> Message + Clone,
Release: Fn(u8) -> Message + Clone,
{
pub octave: u8,
pub note_count: u8,
pub midnam_note_names: HashMap<u8, String>,
pub orientation: Orientation,
on_press: Press,
on_release: Release,
}
impl<Message, Press, Release> OctaveKeyboard<Message, Press, Release>
where
Press: Fn(u8, u8) -> Message + Clone,
Release: Fn(u8) -> Message + Clone,
{
pub fn new(
octave: u8,
midnam_note_names: HashMap<u8, String>,
on_press: Press,
on_release: Release,
) -> Self {
Self {
octave,
note_count: octave_note_count(octave),
midnam_note_names,
orientation: Orientation::Degree0,
on_press,
on_release,
}
}
pub fn with_orientation(mut self, orientation: Orientation) -> Self {
self.orientation = orientation;
self
}
fn note_at(&self, cursor: Point, bounds: Rectangle) -> Option<(u8, u8)> {
note_at_in_range(cursor, bounds, self.orientation, self.note_count)
}
fn midi_note(&self, note_class: u8) -> u8 {
(usize::from(self.octave) * 12 + usize::from(note_class)) as u8
}
}
#[derive(Default, Debug)]
pub struct OctaveKeyboardState {
pub pressed_notes: HashSet<u8>,
pub active_note_class: Option<u8>,
}
impl<Message, Press, Release> Program<Message> for OctaveKeyboard<Message, Press, Release>
where
Message: 'static,
Press: Fn(u8, u8) -> Message + Clone,
Release: Fn(u8) -> Message + Clone,
{
type State = OctaveKeyboardState;
fn update(
&self,
state: &mut Self::State,
event: &Event,
bounds: Rectangle,
cursor: mouse::Cursor,
) -> Option<CanvasAction<Message>> {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
if let Some(position) = cursor.position_in(bounds)
&& let Some((note_class, velocity)) = self.note_at(position, bounds)
{
state.active_note_class = Some(note_class);
state.pressed_notes.clear();
state.pressed_notes.insert(note_class);
return Some(
CanvasAction::publish((self.on_press.clone())(
self.midi_note(note_class),
velocity,
))
.and_capture(),
);
}
}
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
if let Some(note_class) = state.active_note_class.take() {
state.pressed_notes.clear();
return Some(CanvasAction::publish((self.on_release.clone())(
self.midi_note(note_class),
)));
}
}
_ => {}
}
None
}
fn draw(
&self,
state: &Self::State,
renderer: &Renderer,
_theme: &Theme,
bounds: Rectangle,
_cursor: mouse::Cursor,
) -> Vec<Geometry> {
if self.note_count == NOTES_PER_OCTAVE as u8 {
draw_octave(
renderer,
bounds,
&state.pressed_notes,
self.octave,
&self.midnam_note_names,
self.orientation,
)
} else {
draw_partial_octave(
renderer,
bounds,
&state.pressed_notes,
self.octave,
&self.midnam_note_names,
self.orientation,
)
}
}
}
pub fn row_height(zoom_y: f32) -> f32 {
((WHITE_KEY_HEIGHT * WHITE_KEYS_PER_OCTAVE as f32 / NOTES_PER_OCTAVE as f32) * zoom_y).max(1.0)
}
#[cfg(test)]
mod tests {
use super::*;
use iced::widget::canvas::Program;
use iced::{Point, Rectangle, Size, event, mouse};
#[derive(Debug, Clone, PartialEq, Eq)]
enum TestMessage {
Pressed(u8, u8),
Released(u8),
}
fn action_message(action: CanvasAction<TestMessage>) -> (Option<TestMessage>, event::Status) {
let (message, _redraw, status) = action.into_inner();
(message, status)
}
#[test]
fn keyboard_hits_match_semitone_columns() {
for note_count in [8, 12] {
for orientation in [
Orientation::Degree0,
Orientation::Degree90,
Orientation::Degree180,
Orientation::Degree270,
] {
let length = f32::from(note_count) * 24.0;
let size = match orientation {
Orientation::Degree0 | Orientation::Degree180 => Size::new(length, 48.0),
_ => Size::new(48.0, length),
};
let bounds = Rectangle::new(Point::ORIGIN, size);
for note in 0..note_count {
for fraction in [0.01, 0.5, 0.99] {
let cursor = map_point(
orientation,
bounds,
(f32::from(note) + fraction) * 24.0,
12.0,
);
assert_eq!(
note_at_in_range(cursor, bounds, orientation, note_count)
.map(|(note, _)| note),
Some(note)
);
}
}
}
}
}
#[test]
fn octave_keyboard_update_publishes_pressed_and_released_notes() {
let keyboard = OctaveKeyboard::new(
4,
HashMap::new(),
TestMessage::Pressed,
TestMessage::Released,
)
.with_orientation(Orientation::Degree270);
let bounds = Rectangle::new(Point::ORIGIN, Size::new(20.0, 70.0));
let press_cursor = mouse::Cursor::Available(Point::new(15.0, 65.0));
let release_cursor = mouse::Cursor::Available(Point::new(15.0, 65.0));
let mut state = OctaveKeyboardState::default();
let press = keyboard
.update(
&mut state,
&Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
bounds,
press_cursor,
)
.expect("press action");
let (message, status) = action_message(press);
assert_eq!(message, Some(TestMessage::Pressed(48, 96)));
assert_eq!(status, event::Status::Captured);
assert_eq!(state.active_note_class, Some(0));
assert!(state.pressed_notes.contains(&0));
let release = keyboard
.update(
&mut state,
&Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)),
bounds,
release_cursor,
)
.expect("release action");
let (message, status) = action_message(release);
assert_eq!(message, Some(TestMessage::Released(48)));
assert_eq!(status, event::Status::Ignored);
assert!(state.pressed_notes.is_empty());
}
#[test]
fn partial_octave_keyboard_maps_top_note_to_midi_127() {
let keyboard = OctaveKeyboard::new(
10,
HashMap::new(),
TestMessage::Pressed,
TestMessage::Released,
)
.with_orientation(Orientation::Degree270);
let bounds = Rectangle::new(Point::ORIGIN, Size::new(20.0, 80.0));
let cursor = mouse::Cursor::Available(Point::new(15.0, 5.0));
let mut state = OctaveKeyboardState::default();
let press = keyboard
.update(
&mut state,
&Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
bounds,
cursor,
)
.expect("press action");
let (message, status) = action_message(press);
assert_eq!(message, Some(TestMessage::Pressed(127, 96)));
assert_eq!(status, event::Status::Captured);
assert_eq!(state.active_note_class, Some(7));
}
}