Skip to content

Commit

Permalink
Make key layout more mathematically accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Jan 28, 2024
1 parent aa8e51d commit f509797
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions piano-math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,25 @@ fn partial_octave(sizing: &Sizing, range: std::ops::Range<u8>) -> Octave {
let sharp_ids: [u8; 5] = [1, 3, 6, 8, 10];

#[inline(always)]
fn sharp_id_to_x(id: u8, mult: f32) -> f32 {
(id + 1) as f32 * mult
fn sharp_id_to_x(id: u8, cde_mult: f32, fgab_mult: f32) -> f32 {
let mult = if matches!(id, 1 | 3) {
cde_mult
} else {
fgab_mult
};

(id + 1) as f32 * mult - mult / 2.0
}

let mult = width / 12.0;
let last_x = sharp_id_to_x(sharp_ids[4], mult);
let offset = (width - last_x) / 2.0;
// Mathematically there is no correct™ way to position keys, but doing it separately for cde and fgh
// is quite popular, and gives decently accurate results, so let's do that
let cde_width = sizing.neutral_width * 3.0;
let fgab_width = sizing.neutral_width * 4.0;
let cde_mult = cde_width / 5.0;
let fgab_mult = fgab_width / 7.0;

for note_id in sharp_ids {
let x = sharp_id_to_x(note_id, mult);
let x = x - offset;
let x = sharp_id_to_x(note_id, cde_mult, fgab_mult);

let w = sizing.sharp_width;
let hw = w / 2.0;
Expand Down

0 comments on commit f509797

Please sign in to comment.