Last active
November 22, 2021 22:24
-
-
Save arildojr7/37d590546bf3b9bbe4b3212aba051109 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getCurrentPitchState(pitchHz: Double): TunerState { | |
val closestFrequency = getClosestFrequencyInAllNotes(pitchHz) | |
val note = getNoteByFrequency(closestFrequency) | |
val diff = if (closestFrequency > pitchHz) { | |
abs(closestFrequency - pitchHz).unaryMinus() | |
} else { | |
abs(pitchHz - closestFrequency) | |
} | |
return when { | |
diff.isInPermittedTolerance() -> TunerState.Tuned(note) | |
diff < -0.5 -> TunerState.Down(note) | |
else -> TunerState.Up(note) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment