Modern Apps git

← Modern-Apps.git

tuner: nearest note respects the temperament, not just equal temperament

Commit
34d20bfa000db28e52fc589892c84a6645ad4a30
Author
Vayun Mathur <[email protected]>
When
2026-11-30 21:46 UTC (5 days ago)
Parents
da30793
PitchReference.nearestMidi rounded on the pure equal-tempered
grid while referenceHz/centsFrom apply per-pitch-class
temperament offsets. With C stretched +60 cents, a tone exactly
on C3's tempered reference mapped to C#3 at -40 cents instead of
C3 at 0 — any temperament beyond ±50 cents flipped note names a
semitone off. Production always passes an all-zero temperament
today, but the class exists so a temperament is a data change
rather than a refactor; wiring one in would have silently
corrupted every readout.

nearestMidi now walks from the equal-tempered rounding to the
neighbour centsFrom actually minimises; with zero temperament
the walk never moves, so current behaviour is bit-identical.
Regression tests cover the tempered-centre mapping and a
12-class historical temperament round-trip. Pitch detection
paths were audited and cleared. Verified: :tuner:testDevUnitTest
141 tests and lint pass.

Diff

tuner/src/main/java/com/vayunmathur/tuner/domain/Cents.kt
diff --git a/tuner/src/main/java/com/vayunmathur/tuner/domain/Cents.kt b/tuner/src/main/java/com/vayunmathur/tuner/domain/Cents.ktindex dfa2c486fb..9df21be16a 100644--- a/tuner/src/main/java/com/vayunmathur/tuner/domain/Cents.kt+++ b/tuner/src/main/java/com/vayunmathur/tuner/domain/Cents.kt@@ -69,7 +69,16 @@ class PitchReference(     }      /** The MIDI number whose reference frequency is closest to [hz], in cents. */-    fun nearestMidi(hz: Double): Int = (69.0 + 12.0 * log2(hz / a4Hz)).roundToInt()+    fun nearestMidi(hz: Double): Int {+        var midi = (69.0 + 12.0 * log2(hz / a4Hz)).roundToInt()+        // The equal-tempered grid is only the starting point: a temperament offset moves a+        // note's reference off that grid, so the closest reference can be the neighbouring+        // note. Walk to the midi [centsFrom] actually minimises - with an all-zero+        // temperament the walk never moves and this is the plain grid rounding above.+        while (abs(centsFrom(hz, midi + 1)) < abs(centsFrom(hz, midi))) midi+++        while (abs(centsFrom(hz, midi - 1)) < abs(centsFrom(hz, midi))) midi--+        return midi+    }      /** Signed deviation of [hz] from [midi]'s reference, in cents. */     fun centsFrom(hz: Double, midi: Int): Double = 1200.0 * log2(hz / referenceHz(midi))
tuner/src/test/java/com/vayunmathur/tuner/domain/CentsTest.kt
diff --git a/tuner/src/test/java/com/vayunmathur/tuner/domain/CentsTest.kt b/tuner/src/test/java/com/vayunmathur/tuner/domain/CentsTest.ktindex d84b5d0ffa..eac266f1f2 100644--- a/tuner/src/test/java/com/vayunmathur/tuner/domain/CentsTest.kt+++ b/tuner/src/test/java/com/vayunmathur/tuner/domain/CentsTest.kt@@ -59,6 +59,42 @@ class CentsTest {         assertEquals(31.766654, baroqueish.centsFrom(440.0, 69), 1e-5)     } +    @Test+    fun nearestMidiHonoursTheTemperament() {+        // C stretched +60 cents, A flattened -60 cents; every other pitch class equal.+        val temperament = DoubleArray(12)+        temperament[0] = 60.0+        temperament[9] = -60.0+        val reference = PitchReference(DEFAULT_A4_HZ, temperament)+        for (midi in listOf(48, 57, 60, 69, 72)) {+            val hz = reference.referenceHz(midi)+            assertEquals(+                midi, reference.nearestMidi(hz),+                "a tone exactly on ${spell(midi)}'s tempered reference was mapped elsewhere",+            )+            assertEquals(0.0, reference.centsFrom(hz, midi), 1e-9)+        }+        // Near the tempered centre the note must hold: +10 cents sharp of it is still C4,+        // even though equal temperament alone would round it up to C#4 (+70 cents).+        val sharp = reference.referenceHz(60) * Math.pow(2.0, 10.0 / 1200.0)+        assertEquals(60, reference.nearestMidi(sharp))+        assertEquals(10.0, reference.centsFrom(sharp, 60), 1e-6)+    }++    @Test+    fun nearestMidiRoundTripsEveryTemperedReference() {+        // A historical-style temperament: every pitch class offset, none beyond +/-40 cents.+        val temperament = doubleArrayOf(+            -10.0, 5.0, -15.0, 20.0, -5.0, 10.0, -20.0, 15.0, -25.0, 30.0, -30.0, 40.0,+        )+        val reference = PitchReference(DEFAULT_A4_HZ, temperament)+        for (midi in 21..108) {+            val hz = reference.referenceHz(midi)+            assertEquals(midi, reference.nearestMidi(hz), "tempered reference lost at midi $midi")+            assertEquals(0.0, reference.centsFrom(hz, midi), 1e-9)+        }+    }+     @Test     fun temperamentOffsetsShiftOnlyTheirOwnPitchClass() {         val stretched = DoubleArray(12).also { it[0] = 5.0 }