我正在编写一个乐器调音器应用程序(现在从吉他开始)。对于音高检测,我使用的是 TarsosDSP。它确实可以正确检测音高,但是它很不稳定——例如,我会在我的吉他上敲击(正确调音的)D 弦,它会正确地将其识别为 D,但片刻之后它会循环播放一堆随机笔记很快。我不确定如何最好地解决这个问题。这是我负责检测音高的代码:
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()
然后我编写了一个函数,该函数应该检测与当前音高最近的音符。此外,我还尝试通过编写一个函数来获得“不那么不稳定”的结果,该函数应该以 hz 为单位找到最接近的音高,然后将该结果用于最接近的音符函数,认为这样我可能会得到更少不同的结果(即使它应该是一样的,我也没有注意到任何区别)。下面是两个函数:
...
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()
}
有什么想法可以让我获得更一致的结果吗?谢谢!