Modern Apps git

← Modern-Apps.git

tuner: a note change no longer inherits the previous note's cents

Commit
0372aaae949a2967324b26e4840a34306e8b53fe
Author
Vayun Mathur <[email protected]>
When
2026-12-01 01:49 UTC (5 days ago)
Parents
17bab0a
Cents are relative to the nearest MIDI note, but
CentsSmoother kept no note identity: it smoothed the raw
relative value keyed only on its delta, treating any jump
within the 20-cent snap threshold as a glide. Settling on A4
at +18c and then playing an exactly-in-tune B4 (raw 0c) took
the glide branch and displayed +15.75c OUT for that frame —
still above 5c for ~9 frames and outside IN_TUNE for ~21
frames (~224 ms), coloured out-of-tune throughout.

The smoother now owns its note identity: update takes the
frame's midi and starts clean on any note change (even
inside the snap threshold), reset forgets the note too,
while same-note glide and large-jump snap are unchanged.
Five new tests pin the cross-note case, the 20c boundary,
same-note glide/snap values and reset. Sign/scale, note
naming, A4 handling, chord history and mic lifecycle were
audited and cleared. Verified: :tuner:testDevUnitTest 146
tests and lint pass.

Diff

tuner/src/main/java/com/vayunmathur/tuner/domain/PitchDetector.kt
diff --git a/tuner/src/main/java/com/vayunmathur/tuner/domain/PitchDetector.kt b/tuner/src/main/java/com/vayunmathur/tuner/domain/PitchDetector.ktindex 2bcb86e5b3..da5996833b 100644--- a/tuner/src/main/java/com/vayunmathur/tuner/domain/PitchDetector.kt+++ b/tuner/src/main/java/com/vayunmathur/tuner/domain/PitchDetector.kt@@ -211,26 +211,35 @@ class YinPhaseSlopeAnalyzer(val sampleRate: Double) : PitchAnalyzer {  *  * Raw frames jitter by a couple of tenths of a cent, which makes a needle look nervous. Large  * jumps snap instead of gliding, because a jump means a new note rather than a drifting one.+ *+ * The state is per note: cents are defined relative to the nearest MIDI note, so carrying a+ * smoothed value across a change of that note would display the previous note's deviation+ * against the new one. A change of [midi][update] therefore starts clean even when the two+ * notes' deviations happen to be within [SNAP_CENTS] of each other - the old snap test alone+ * cannot see that case, because it only ever compares the relative numbers.  */ class CentsSmoother(private val timeConstantSeconds: Double = 0.080) {     private var value: Double? = null+    private var midi: Int? = null -    /** Feeds a raw measurement and returns the value to display. */-    fun update(cents: Double, hopSeconds: Double): Double {+    /** Feeds a raw measurement of [midi] and returns the value to display. */+    fun update(midi: Int, cents: Double, hopSeconds: Double): Double {         val previous = value-        val next = if (previous == null || abs(cents - previous) > SNAP_CENTS) {+        val next = if (previous == null || this.midi != midi || abs(cents - previous) > SNAP_CENTS) {             cents         } else {             val alpha = 1.0 - exp(-hopSeconds / timeConstantSeconds)             previous + alpha * (cents - previous)         }         value = next+        this.midi = midi         return next     }      /** Drops the smoothed value, so the next reading starts clean. */     fun reset() {         value = null+        midi = null     }      private companion object {
tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.kt
diff --git a/tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.kt b/tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.ktindex 1e7a9cd370..360359633d 100644--- a/tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.kt+++ b/tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.kt@@ -209,7 +209,7 @@ class TunerViewModel(application: Application) : AndroidViewModel(application) {         val estimate = frame.estimate         val midi = reference.nearestMidi(estimate.frequencyHz)         val raw = reference.centsFrom(estimate.frequencyHz, midi)-        val smoothed = smoother.update(raw, NOTE_HOP.toDouble() / AudioCapture.SAMPLE_RATE)+        val smoothed = smoother.update(midi, raw, NOTE_HOP.toDouble() / AudioCapture.SAMPLE_RATE)         val spelling = spell(midi)         val readout = NoteReadout(             midi = midi,
tuner/src/test/java/com/vayunmathur/tuner/domain/CentsSmootherTest.kt
diff --git a/tuner/src/test/java/com/vayunmathur/tuner/domain/CentsSmootherTest.kt b/tuner/src/test/java/com/vayunmathur/tuner/domain/CentsSmootherTest.ktnew file mode 100644index 0000000000..bfd3ace878--- /dev/null+++ b/tuner/src/test/java/com/vayunmathur/tuner/domain/CentsSmootherTest.kt@@ -0,0 +1,72 @@+package com.vayunmathur.tuner.domain++import kotlin.math.exp+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * The display smoother, and specifically what it must not do across a note change.+ *+ * Cents are relative to the nearest note, so the smoothed state is only meaningful for the+ * note it was measured against. Before the fix the smoother keyed solely on the cents jump:+ * a previous note at +18 cents followed by a new note measured exactly in tune (a jump of+ * 18, inside the 20-cent snap threshold) displayed about +15.8 cents - band OUT - for the+ * new note, and stayed outside the in-tune band for roughly twenty frames while it decayed,+ * even though the detector had measured 0 cents on every one of those frames.+ */+class CentsSmootherTest {++    /** One Note-tab hop: 512 samples at 48 kHz, as the ViewModel feeds it. */+    private val hopSeconds = 512.0 / 48_000.0++    @Test+    fun aNewNoteDoesNotInheritThePreviousNotesDeviation() {+        val smoother = CentsSmoother()+        // Settle on A4 played 18 cents sharp.+        repeat(5) { smoother.update(69, 18.0, hopSeconds) }+        // The player moves to B4, measured exactly in tune. The display must say so at once:+        // this is a new note, not the old one drifting.+        assertEquals(0.0, smoother.update(71, 0.0, hopSeconds), 1e-9)+        assertEquals(TuningBand.IN_TUNE, bandFor(smoother.update(71, 0.0, hopSeconds)))+    }++    @Test+    fun aNewNoteAlsoStartsCleanWhenItsDeviationIsCloseToTheOldOne() {+        val smoother = CentsSmoother()+        repeat(5) { smoother.update(69, -14.0, hopSeconds) }+        // C5 at +6 cents: exactly 20 cents from the old reading, so the old strictly-greater+        // snap test would not fire either; a note change must return the measured value,+        // not a blend of two notes.+        assertEquals(6.0, smoother.update(72, 6.0, hopSeconds), 1e-9)+    }++    @Test+    fun theSameNoteStillSmoothsSmallJitter() {+        val smoother = CentsSmoother()+        assertEquals(18.0, smoother.update(69, 18.0, hopSeconds), 1e-9)+        // Same note, next frame measured in tune: one-pole glide, not a snap. This is the+        // behaviour the smoother exists for, and a note change must not be confused with it.+        val alpha = 1.0 - exp(-hopSeconds / 0.080)+        val expected = 18.0 + alpha * (0.0 - 18.0)+        val displayed = smoother.update(69, 0.0, hopSeconds)+        assertEquals(expected, displayed, 1e-9)+        assertTrue(displayed > 5.0, "same-note jitter was snapped instead of smoothed: $displayed")+    }++    @Test+    fun aLargeJumpOnTheSameNoteStillSnaps() {+        val smoother = CentsSmoother()+        repeat(3) { smoother.update(69, 2.0, hopSeconds) }+        assertEquals(30.0, smoother.update(69, 30.0, hopSeconds), 1e-9)+    }++    @Test+    fun resetForgetsTheNoteAsWellAsTheValue() {+        val smoother = CentsSmoother()+        repeat(5) { smoother.update(69, 18.0, hopSeconds) }+        smoother.reset()+        // After a reset the next reading is taken at face value, whichever note it is.+        assertEquals(0.0, smoother.update(69, 0.0, hopSeconds), 1e-9)+    }+}