From 692373532d0f4f3b77ecd231d4a1de92afdf61b0 Mon Sep 17 00:00:00 2001 From: PolyMeilex Date: Sun, 17 Dec 2023 02:08:25 +0100 Subject: [PATCH 1/2] Allow piano range configuration --- neothesia-cli/src/main.rs | 13 ++++++++++--- neothesia-core/src/config.rs | 12 ++++++++++++ neothesia/src/scene/playing_scene/keyboard.rs | 9 +++++++-- piano-math/src/lib.rs | 8 -------- piano-math/src/range.rs | 11 ++++++----- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/neothesia-cli/src/main.rs b/neothesia-cli/src/main.rs index 44f22b9a..7574a6eb 100644 --- a/neothesia-cli/src/main.rs +++ b/neothesia-cli/src/main.rs @@ -22,8 +22,11 @@ struct Recorder { height: u32, } -fn get_layout(width: f32, height: f32) -> piano_math::KeyboardLayout { - let range = piano_math::KeyboardRange::standard_88_keys(); +fn get_layout( + width: f32, + height: f32, + range: piano_math::KeyboardRange, +) -> piano_math::KeyboardLayout { let white_count = range.white_count(); let neutral_width = width / white_count as f32; let neutral_height = height * 0.2; @@ -73,7 +76,11 @@ impl Recorder { let quad_pipeline = QuadPipeline::new(&gpu, &transform_uniform); - let keyboard_layout = get_layout(width as f32, height as f32); + let keyboard_layout = get_layout( + width as f32, + height as f32, + piano_math::KeyboardRange::new(config.piano_range()), + ); let mut keyboard = KeyboardRenderer::new(keyboard_layout.clone(), config.vertical_guidelines); diff --git a/neothesia-core/src/config.rs b/neothesia-core/src/config.rs index 5d3dc4bc..fea0f5d0 100644 --- a/neothesia-core/src/config.rs +++ b/neothesia-core/src/config.rs @@ -34,6 +34,9 @@ pub struct Config { pub soundfont_path: Option, pub last_opened_song: Option, + + #[serde(default = "default_piano_range")] + pub piano_range: (u8, u8), } impl Default for Config { @@ -71,9 +74,14 @@ impl Config { input: None, soundfont_path: None, last_opened_song: None, + piano_range: default_piano_range(), }) } + pub fn piano_range(&self) -> std::ops::RangeInclusive { + self.piano_range.0..=self.piano_range.1 + } + pub fn set_output(&mut self, output: Option) { self.output = output; } @@ -94,6 +102,10 @@ impl Drop for Config { } } +fn default_piano_range() -> (u8, u8) { + (21, 108) +} + fn default_speed_multiplier() -> f32 { 1.0 } diff --git a/neothesia/src/scene/playing_scene/keyboard.rs b/neothesia/src/scene/playing_scene/keyboard.rs index d984572f..b50ae8b2 100644 --- a/neothesia/src/scene/playing_scene/keyboard.rs +++ b/neothesia/src/scene/playing_scene/keyboard.rs @@ -9,8 +9,11 @@ pub struct Keyboard { song_config: SongConfig, } -fn get_layout(width: f32, height: f32) -> piano_math::KeyboardLayout { - let range = piano_math::KeyboardRange::standard_88_keys(); +fn get_layout( + width: f32, + height: f32, + range: piano_math::KeyboardRange, +) -> piano_math::KeyboardLayout { let white_count = range.white_count(); let neutral_width = width / white_count as f32; let neutral_height = height * 0.2; @@ -23,6 +26,7 @@ impl Keyboard { let layout = get_layout( target.window_state.logical_size.width, target.window_state.logical_size.height, + piano_math::KeyboardRange::new(target.config.piano_range()), ); let mut renderer = KeyboardRenderer::new(layout, target.config.vertical_guidelines); @@ -54,6 +58,7 @@ impl Keyboard { let keyboard_layout = get_layout( target.window_state.logical_size.width, target.window_state.logical_size.height, + self.renderer.layout().range.clone(), ); self.set_layout(keyboard_layout.clone()); diff --git a/piano-math/src/lib.rs b/piano-math/src/lib.rs index 259ff7c6..074264d8 100644 --- a/piano-math/src/lib.rs +++ b/piano-math/src/lib.rs @@ -20,14 +20,6 @@ pub struct KeyboardLayout { } impl KeyboardLayout { - pub fn standard_88_keys(neutral_width: f32, neutral_height: f32) -> Self { - Self::from_range( - neutral_width, - neutral_height, - KeyboardRange::standard_88_keys(), - ) - } - pub fn from_range(neutral_width: f32, neutral_height: f32, range: KeyboardRange) -> Self { let sharp_width = neutral_width * 0.625; // 62.5% let sharp_height = neutral_height * 0.635; diff --git a/piano-math/src/range.rs b/piano-math/src/range.rs index b7a8f687..986927a3 100644 --- a/piano-math/src/range.rs +++ b/piano-math/src/range.rs @@ -34,7 +34,7 @@ pub struct KeyboardRange { impl KeyboardRange { pub fn new(range: R) -> Self where - R: RangeBounds, + R: RangeBounds, { let mut keys = Vec::new(); let mut white_keys = Vec::new(); @@ -47,15 +47,16 @@ impl KeyboardRange { std::ops::Bound::Included(id) => *id, std::ops::Bound::Excluded(id) => *id + 1, std::ops::Bound::Unbounded => 0, - } as u8; + }; let end = match end { std::ops::Bound::Included(id) => *id + 1, std::ops::Bound::Excluded(id) => *id, - std::ops::Bound::Unbounded => 0, - } as u8; + std::ops::Bound::Unbounded => 128, + }; - let range = start..end; + // 127 is the top of MIDI tuning range + let range = start..end.min(128); for id in range.clone().map(KeyId) { keys.push(id); From 86494bcd3e51379502fbe97a7de282543b1f182b Mon Sep 17 00:00:00 2001 From: PolyMeilex Date: Sun, 17 Dec 2023 02:44:47 +0100 Subject: [PATCH 2/2] Fixup C labels for custom ranges --- neothesia-core/src/render/keyboard/mod.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/neothesia-core/src/render/keyboard/mod.rs b/neothesia-core/src/render/keyboard/mod.rs index e336db11..0aad5434 100644 --- a/neothesia-core/src/render/keyboard/mod.rs +++ b/neothesia-core/src/render/keyboard/mod.rs @@ -152,13 +152,8 @@ impl KeyboardRenderer { quads.instances().push(*quad); } - for (id, key) in self - .layout - .keys - .iter() - .filter(|key| key.note_id() == 0) - .enumerate() - { + let range_start = self.layout.range.start() as usize; + for key in self.layout.keys.iter().filter(|key| key.note_id() == 0) { let x = self.pos.x + key.x(); let y = self.pos.y; @@ -167,13 +162,15 @@ impl KeyboardRenderer { let size = w * 0.7; + let oct_number = (key.id() + range_start) / 12; + let mut buffer = glyphon::Buffer::new(text.font_system(), glyphon::Metrics::new(size, size)); buffer.set_size(text.font_system(), w, h); buffer.set_wrap(text.font_system(), glyphon::Wrap::None); buffer.set_text( text.font_system(), - &format!("C{}", id + 1), + &format!("C{}", oct_number as i8 - 1), glyphon::Attrs::new().family(glyphon::Family::SansSerif), glyphon::Shaping::Basic, );