From 6cfed5675f2e460adf15f754e7a0f91f75f71759 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 2 Jul 2024 00:25:04 +0200 Subject: [PATCH 1/2] Changed interpolateNoteX to be a bit faster Removed Lerp since its now no longer needed --- src/core/PianoRenderer.ts | 40 +++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/core/PianoRenderer.ts b/src/core/PianoRenderer.ts index 1c41e18..997b0c1 100644 --- a/src/core/PianoRenderer.ts +++ b/src/core/PianoRenderer.ts @@ -81,6 +81,14 @@ export function drawKeys(from: Audio.Note, to: Audio.Note) { notePositions.set(note, { x, y }); } } + + // For InterpolateNoteX + rangeBase = from; + rangeOffset = + Math.floor((1 / 12) * from + 1) + Math.floor((1 / 12) * (from - 5)); + if (!isWhiteKey(from)) { + rangeOffset += 1; + } } export function drawNote( @@ -161,22 +169,30 @@ export function isWhiteKey(note: Audio.Note) { ); } +let rangeBase = 0; +let rangeOffset = 0; export function interpolateNoteX(note: Audio.Note) { - // TODO: Think of a better way to do this - const lowerNote = Math.floor(note); - const noteMod1 = note - lowerNote; - - const lowerPos = notePositions.get(lowerNote); - const upperPos = notePositions.get(lowerNote + 1); - if (!lowerPos || !upperPos) { - return Infinity; - } + // Piano Key is the index of the key on the Piano, pretending there are no gap in the black keys + const pianoKey = calculateNoteX(note) - rangeBase; - return lerp(lowerPos.x, upperPos.x, noteMod1); + return ((pianoKey - rangeOffset) * whiteKeyWidth) / 2; } -function lerp(a: number, b: number, t: number) { - return a + (b - a) * t; +function calculateNoteX(note: Audio.Note) { + // Desmos View and a bit of explaination: https://www.desmos.com/calculator/40j3sx9jsq + const noteFloor = Math.floor(note); + // Instead of working with floors like the Desmos view, we instead take the mod 12 of note and adjust the multiplier from that + const noteMod12 = noteFloor % 12; + const mul1 = noteMod12 == 4 ? 1 : 0; + const mul2 = noteMod12 == 11 ? 1 : 0; + const multiplier = 1 + mul1 + mul2; + + const offsetBase = + Math.floor((1 / 12) * note + 1) + Math.floor((1 / 12) * (note - 5)); + const offsetBetween = -(multiplier - 1) * noteFloor; + const offset = offsetBase + offsetBetween; + + return multiplier * note + offset + 1; } export function resize(yPos: number, width: number, height: number) { From 6c946c7238eb7a57d1b940fe08fdb28e00d1d131 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 5 Jul 2024 15:53:51 +0200 Subject: [PATCH 2/2] Move variables to the top --- src/core/PianoRenderer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/PianoRenderer.ts b/src/core/PianoRenderer.ts index 997b0c1..f90022d 100644 --- a/src/core/PianoRenderer.ts +++ b/src/core/PianoRenderer.ts @@ -13,6 +13,8 @@ const outOfRangeNoteSize = 10; let whiteKeyWidth: number; let blackKeyWidth: number; +let rangeBase = 0; +let rangeOffset = 0; export function init() { pianoCanvas = document.getElementById("pianoCanvas") as HTMLCanvasElement; @@ -169,8 +171,6 @@ export function isWhiteKey(note: Audio.Note) { ); } -let rangeBase = 0; -let rangeOffset = 0; export function interpolateNoteX(note: Audio.Note) { // Piano Key is the index of the key on the Piano, pretending there are no gap in the black keys const pianoKey = calculateNoteX(note) - rangeBase;