tuner: a held reading no longer impersonates a live in-tune one
- Commit
e8d2dbd94a8091bf095282f436b9fef83b9f2240- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 08:27 UTC (5 days ago)
- Parents
1e61704
holdOrClearNote marks the readout stale from the first silent frame (held ~1.2 s so the display does not flicker), but NoteScreen/TuningMeter never read the flag: an A4 at 440.0 Hz, 0.0 cents, IN_TUNE followed by silence rendered 'A4 / In tune / 440.0 Hz' with the live-coloured needle for the whole hold — indistinguishable from a string still sounding, against the contract's stated 'the UI must make the difference visible'. The UI now branches on staleness: the note letter dims, the status line shows 'Held — last heard, not live' instead of the verdict, and the meter needle fades to 35% alpha at its held position. Three regression tests assert live in-tune shows the verdict, stale in-tune shows held (not 'In tune'), and stale out-of-tune shows held (not the cents verdict) — the stale cases failed pre-fix. Cents/reference math and session clearing were audited and cleared. Verified: :tuner:testDevUnitTest 149 tests pass; :tuner:lint BUILD SUCCESSFUL.
Diff
tuner/src/main/java/com/vayunmathur/tuner/ui/NoteScreen.kt
diff --git a/tuner/src/main/java/com/vayunmathur/tuner/ui/NoteScreen.kt b/tuner/src/main/java/com/vayunmathur/tuner/ui/NoteScreen.ktindex 9acace6e83..e07183f881 100644--- a/tuner/src/main/java/com/vayunmathur/tuner/ui/NoteScreen.kt+++ b/tuner/src/main/java/com/vayunmathur/tuner/ui/NoteScreen.kt@@ -50,14 +50,18 @@ fun NoteScreen(state: TunerUiState) { Text( text = readout?.let { "${it.letter}${it.octave}" } ?: EMPTY_NOTE, style = MaterialTheme.typography.displayLarge,- color = if (readout == null) {+ color = if (readout == null || readout.stale) { MaterialTheme.colorScheme.onSurfaceVariant } else { MaterialTheme.colorScheme.onSurface }, ) - TuningMeter(cents = readout?.cents, band = readout?.band ?: TuningBand.OUT)+ TuningMeter(+ cents = readout?.cents,+ band = readout?.band ?: TuningBand.OUT,+ stale = readout?.stale == true,+ ) if (readout == null) { Text(@@ -78,16 +82,20 @@ fun NoteScreen(state: TunerUiState) { ) } else { Text(- text = if (readout.band == TuningBand.IN_TUNE) {- stringResource(R.string.in_tune)- } else {- stringResource(R.string.cents_value, formatSigned(readout.cents))+ text = when {+ readout.stale -> stringResource(R.string.reading_held)+ readout.band == TuningBand.IN_TUNE -> stringResource(R.string.in_tune)+ else -> stringResource(R.string.cents_value, formatSigned(readout.cents)) }, style = MaterialTheme.typography.headlineSmall,- color = when (readout.band) {- TuningBand.IN_TUNE -> MaterialTheme.colorScheme.primary- TuningBand.CLOSE -> MaterialTheme.colorScheme.tertiary- TuningBand.OUT -> MaterialTheme.colorScheme.error+ color = if (readout.stale) {+ MaterialTheme.colorScheme.onSurfaceVariant+ } else {+ when (readout.band) {+ TuningBand.IN_TUNE -> MaterialTheme.colorScheme.primary+ TuningBand.CLOSE -> MaterialTheme.colorScheme.tertiary+ TuningBand.OUT -> MaterialTheme.colorScheme.error+ } }, ) Text(
tuner/src/main/java/com/vayunmathur/tuner/ui/TuningMeter.kt
diff --git a/tuner/src/main/java/com/vayunmathur/tuner/ui/TuningMeter.kt b/tuner/src/main/java/com/vayunmathur/tuner/ui/TuningMeter.ktindex e0ed858d03..191758a985 100644--- a/tuner/src/main/java/com/vayunmathur/tuner/ui/TuningMeter.kt+++ b/tuner/src/main/java/com/vayunmathur/tuner/ui/TuningMeter.kt@@ -24,17 +24,21 @@ fun TuningMeter( cents: Double?, band: TuningBand, modifier: Modifier = Modifier,+ stale: Boolean = false, ) { val scheme = MaterialTheme.colorScheme val trackColour = scheme.surfaceVariant val tickColour = scheme.onSurfaceVariant.copy(alpha = 0.5f) val centreColour = scheme.outline val inTuneZone = scheme.primary.copy(alpha = 0.18f)- val needleColour = when (band) {+ val bandColour = when (band) { TuningBand.IN_TUNE -> scheme.primary TuningBand.CLOSE -> scheme.tertiary TuningBand.OUT -> scheme.error }+ // A held reading keeps its last position but must not look live: fade the needle rather+ // than recolour it, which would claim a tuning band the stale value no longer measures.+ val needleColour = if (stale) bandColour.copy(alpha = 0.35f) else bandColour Canvas(modifier.fillMaxWidth().height(96.dp)) { val width = size.width
tuner/src/main/res/values/strings.xml
diff --git a/tuner/src/main/res/values/strings.xml b/tuner/src/main/res/values/strings.xmlindex 3df53291f3..936a39c504 100644--- a/tuner/src/main/res/values/strings.xml+++ b/tuner/src/main/res/values/strings.xml@@ -25,6 +25,9 @@ <string name="cents_value">%1$s cents</string> <!-- Shown when the note is within a cent of its target. --> <string name="in_tune">In tune</string>+ <!-- Shown instead of the live verdict when the last measured reading is being held after+ detection stopped: it is the last thing heard, not a measurement of what is sounding now. -->+ <string name="reading_held">Held — last heard, not live</string> <!-- Title of the instrument chooser. --> <!-- Instrument family names. These are the only names that need a code change to add. --> <string name="instrument_guitar">Guitar</string>
tuner/src/test/java/com/vayunmathur/tuner/ui/StaleReadoutTest.kt
diff --git a/tuner/src/test/java/com/vayunmathur/tuner/ui/StaleReadoutTest.kt b/tuner/src/test/java/com/vayunmathur/tuner/ui/StaleReadoutTest.ktnew file mode 100644index 0000000000..8447c87140--- /dev/null+++ b/tuner/src/test/java/com/vayunmathur/tuner/ui/StaleReadoutTest.kt@@ -0,0 +1,81 @@+package com.vayunmathur.tuner.ui++import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.tuner.R+import com.vayunmathur.tuner.data.AnalysisRange+import com.vayunmathur.tuner.data.Instrument+import com.vayunmathur.tuner.data.InstrumentKind+import com.vayunmathur.tuner.data.StringSpec+import com.vayunmathur.tuner.domain.TuningBand+import com.vayunmathur.tuner.platform.NoteReadout+import com.vayunmathur.tuner.platform.NoteUiState+import com.vayunmathur.tuner.platform.TunerUiState+import kotlin.test.Test+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * A held reading must not be presented as a live one.+ *+ * The ViewModel marks a readout [NoteReadout.stale] from the first silent frame after a+ * detection until the hold expires (~112 hops at 512 samples / 48 kHz, so about 1.2 s).+ * Before the fix the Note screen never read that flag: an exactly in-tune A4 (440.0 Hz,+ * 0.0 cents) that had stopped sounding kept rendering "In tune" in the live colour for the+ * whole hold, indistinguishable from a string still sounding at 440.0 Hz.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class StaleReadoutTest {++ @get:Rule+ val composeRule = createComposeRule()++ private val context = ApplicationProvider.getApplicationContext<android.content.Context>()+ private fun str(id: Int, vararg a: Any): String = context.getString(id, *a)++ private val guitar = Instrument(+ id = "guitar", familyId = "guitar", kind = InstrumentKind.FRETTED,+ strings = listOf(+ StringSpec(40, "E"), StringSpec(45, "A"), StringSpec(50, "D"),+ StringSpec(55, "G"), StringSpec(59, "B"), StringSpec(64, "E"),+ ),+ fretCount = 12, analysis = AnalysisRange(80.0, 1200.0),+ )++ private fun state(readout: NoteReadout) = TunerUiState(+ instrument = guitar, instruments = listOf(guitar),+ listening = true,+ note = NoteUiState(readout),+ )++ @Test+ fun liveInTuneReadoutShowsTheLiveVerdict() {+ val readout = NoteReadout(69, "A", 4, 440.0, 0.0, TuningBand.IN_TUNE)+ composeRule.setContent { NoteScreen(state(readout)) }+ composeRule.onNodeWithText("A4").assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.in_tune)).assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.reading_held)).assertDoesNotExist()+ }++ @Test+ fun staleInTuneReadoutIsMarkedHeldNotLive() {+ val readout = NoteReadout(69, "A", 4, 440.0, 0.0, TuningBand.IN_TUNE, stale = true)+ composeRule.setContent { NoteScreen(state(readout)) }+ composeRule.onNodeWithText("A4").assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.reading_held)).assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.in_tune)).assertDoesNotExist()+ }++ @Test+ fun staleOutOfTuneReadoutIsMarkedHeldNotCents() {+ val readout = NoteReadout(69, "A", 4, 445.0, 19.6, TuningBand.OUT, stale = true)+ composeRule.setContent { NoteScreen(state(readout)) }+ composeRule.onNodeWithText(str(R.string.reading_held)).assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.cents_value, "+19.6")).assertDoesNotExist()+ }+}