I am writing an instrument tuner app (for now starting with Guitar). For pitch detection I'm using TarsosDSP. It does detect the pitch correctly, however it is quite shaky - for example, I'll hit the (correctly tuned) D string on my Guitar, it correctly recognizes it as a D, but after a short moment it cycles through a bunch of random notes very quickly. I'm not sure how to best solve this. Here is my code which is responsible for detecting the pitch:
val dispatcher: AudioDispatcher = AudioDispatcherFactory.fromDefaultMicrophone(44100, 4096, 3072)
val pdh = PitchDetectionHandler { res, _ ->
val pitchInHz: Float = res.pitch
runOnUiThread { processing.closestNote(pitchInHz)}
}
val pitchProcessor: AudioProcessor =
PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN,
44100F, 4096, pdh)
dispatcher.addAudioProcessor(pitchProcessor)
val audioThread = Thread(dispatcher, "Audio Thread")
audioThread.start()
I have then written a function which is supposed to detect the closest note to the current pitch. In addition I tried to get the results "less shaky" by also writing a function which is supposed to find the closest pitch in hz and then using that result for the closestNote function thinking that this way I may get less different results (even though it should be the same, and I also don't notice any difference). Here are the two functions:
...
private val allNotes = arrayOf("A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#")
private val concertPitch = 440
...
/** detects closest note in A = 440hz with with equal temperament formula:
* pitch(i) = pitch(0) * 2^(i/12)
* therefore formula to derive interval between two pitches:
* i = 12 * log2 * (pitch(i)/pitch(o))
*/
fun closestNote(pitchInHz: Float) {
(myCallback as MainActivity).noteSize() //adjusts the font size of note
if (pitchInHz != -1F) {
val roundHz = closestPitch(pitchInHz)
val i = (round(log2(roundHz / concertPitch) * 12)).toInt()
val closestNote = allNotes[(i % 12 + 12) % 12]
myCallback?.updateNote(closestNote) // updates note text
}
}
private fun closestPitch(pitchInHz: Float): Float {
val i = (round(log2(pitchInHz / concertPitch) * 12)).toInt()
val closestPitch = concertPitch * 2.toDouble().pow(i.toDouble() / 12)
return closestPitch.toFloat()
}
Any ideas how I can get more consistent results? Thanks!
Solved it myself: TarsosDSP calculates a probability with every note being played. I set my closestNote
function to only update the text if the probability is > 0.91 (I found that value to offer "stability" in terms of text not changing after hitting a string and still correctly recognizing the note without hitting the string multiple times/too hard, also tested it with an unplugged, non hollow body electric Guitar)