Skip to content

Commit

Permalink
Allow piano range configuration (#115)
Browse files Browse the repository at this point in the history
* Allow piano range configuration

* Fixup C labels for custom ranges
  • Loading branch information
PolyMeilex authored Dec 17, 2023
1 parent 7a2aa61 commit f9320ae
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
13 changes: 10 additions & 3 deletions neothesia-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions neothesia-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct Config {

pub soundfont_path: Option<PathBuf>,
pub last_opened_song: Option<PathBuf>,

#[serde(default = "default_piano_range")]
pub piano_range: (u8, u8),
}

impl Default for Config {
Expand Down Expand Up @@ -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<u8> {
self.piano_range.0..=self.piano_range.1
}

pub fn set_output(&mut self, output: Option<String>) {
self.output = output;
}
Expand All @@ -94,6 +102,10 @@ impl Drop for Config {
}
}

fn default_piano_range() -> (u8, u8) {
(21, 108)
}

fn default_speed_multiplier() -> f32 {
1.0
}
Expand Down
13 changes: 5 additions & 8 deletions neothesia-core/src/render/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
);
Expand Down
9 changes: 7 additions & 2 deletions neothesia/src/scene/playing_scene/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
8 changes: 0 additions & 8 deletions piano-math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions piano-math/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct KeyboardRange {
impl KeyboardRange {
pub fn new<R>(range: R) -> Self
where
R: RangeBounds<usize>,
R: RangeBounds<u8>,
{
let mut keys = Vec::new();
let mut white_keys = Vec::new();
Expand All @@ -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);
Expand Down

0 comments on commit f9320ae

Please sign in to comment.