浏览器音频的 Hz 检测不准确


import PitchFinder from 'pitchfinder'
const detectPitch = PitchFinder.AMDF()
const notes = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'G', 'G#']
export default {
data () {
return {
note: 'A',
register: 4,
cents: 0
}
},
mounted () {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
const context = new AudioContext()
const source = context.createMediaStreamSource(stream)
const processor = context.createScriptProcessor()
source.connect(processor)
processor.connect(context.destination)
processor.onaudioprocess = e => {
const hz = detectPitch(e.inputBuffer.getChannelData(0))
if (hz) {
console.log(hz)
// ¢ or c = 1200 × log2 (f2 / f1), 1 semitone = 100 cents
const semitones = 12 * (Math.log2((hz) / 440))
const cents = semitones * 100
// TODO: update component
}
}
})
.catch(e => {
// TODO: handle error
})
}
}
}

我的 Vue组件中有上面的代码(注意,只有一些与 Vue 相关的代码附加用于上下文。我遇到了打印到控制台的值不准确的问题。我使用了一架无人机,并用其他信誉良好的调谐器(A = 440 Hz(验证了它的音高。当用我的代码打印到控制台时,Hz 始终为 ~404,其他音高也会偏移。为什么?谢谢。

代码中其他地方的采样率错误。

440 * 44100.0/48000.0 = 404.25

我的猜测是,您以 48 kHz 运行音频输入,但音高检测器认为采样率为 44.1 kHz。

最新更新