From b5a724399eec455715bd4c24f364bfe41e086cea Mon Sep 17 00:00:00 2001 From: zishounekonanoda Date: Wed, 12 Aug 2026 02:41:22 +0900 Subject: [PATCH] Fix negative fine pitch across octave boundaries --- pom.xml | 6 +++++ .../NoteBlockAPI/utils/NoteUtils.java | 4 ++-- .../NoteBlockAPI/utils/NoteUtilsTest.java | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/xxmicloxx/NoteBlockAPI/utils/NoteUtilsTest.java diff --git a/pom.xml b/pom.xml index ee2757d..37efb96 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,12 @@ 3.0.0 compile + + junit + junit + 4.13.2 + test + diff --git a/src/main/java/com/xxmicloxx/NoteBlockAPI/utils/NoteUtils.java b/src/main/java/com/xxmicloxx/NoteBlockAPI/utils/NoteUtils.java index 4b5994e..8432212 100644 --- a/src/main/java/com/xxmicloxx/NoteBlockAPI/utils/NoteUtils.java +++ b/src/main/java/com/xxmicloxx/NoteBlockAPI/utils/NoteUtils.java @@ -44,7 +44,7 @@ public static float getPitchInOctave(Note note) { public static float getPitchInOctave(byte key, short pitch) { // Apply pitch to key key = applyPitchToKey(key, pitch); - pitch %= 100; + pitch = (short) Math.floorMod(pitch, 100); // -15 base_-2 // 9 base_-1 @@ -62,7 +62,7 @@ public static float getPitchInOctave(byte key, short pitch) { } public static byte applyPitchToKey(byte key, short pitch) { - key += pitch / 100; + key += Math.floorDiv(pitch, 100); return key; } diff --git a/src/test/java/com/xxmicloxx/NoteBlockAPI/utils/NoteUtilsTest.java b/src/test/java/com/xxmicloxx/NoteBlockAPI/utils/NoteUtilsTest.java new file mode 100644 index 0000000..913bf07 --- /dev/null +++ b/src/test/java/com/xxmicloxx/NoteBlockAPI/utils/NoteUtilsTest.java @@ -0,0 +1,24 @@ +package com.xxmicloxx.NoteBlockAPI.utils; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class NoteUtilsTest { + + @Test + public void negativeFinePitchMovesToPreviousKeyWithoutIndexUnderflow() { + assertEquals(32, NoteUtils.applyPitchToKey((byte) 33, (short) -30)); + assertEquals(expectedPitch(2370), NoteUtils.getPitchInOctave((byte) 33, (short) -30), 0.000001f); + } + + @Test + public void negativeFinePitchSelectsThePreviousOctaveSample() { + assertEquals("test_-1", InstrumentUtils.warpNameOutOfRange("test", (byte) 33, (short) -30)); + assertEquals("test", InstrumentUtils.warpNameOutOfRange("test", (byte) 57, (short) -30)); + } + + private float expectedPitch(int index) { + return (float) Math.pow(2, (index - 1200d) / 1200d); + } +}