我试图找出一个人对着麦克风说话时声音的平均频率或范围。它不必是实时的。到目前为止,我的方法是使用 AVAudioEngine 和 AVAudioPCMBuffer,获取缓冲区数据并将其转换为 FFT。
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
self.recognitionRequest?.append(buffer)
let data = buffer.floatChannelData?[0]
let arrayOfData = Array(UnsafeBufferPointer(start: data, count: Int(buffer.frameLength)))
let fftData = self.performFFT(arrayOfData)
}
func performFFT(_ input: [Float]) -> [Float] {
var real = [Float](input)
var imag = [Float](repeating: 0.0, count: input.count)
var splitComplex = DSPSplitComplex(realp: &real, imagp: &imag)
let length = vDSP_Length(floor(log2(Float(input.count))))
let radix = FFTRadix(kFFTRadix2)
let weights = vDSP_create_fftsetup(length, radix)
vDSP_fft_zip(weights!, &splitComplex, 1, length, FFTDirection(FFT_FORWARD))
var magnitudes = [Float](repeating: 0.0, count: input.count)
vDSP_zvmags(&splitComplex, 1, &magnitudes, 1, vDSP_Length(input.count))
var normalizedMagnitudes = [Float](repeating: 0.0, count: input.count)
vDSP_vsmul(sqrt(magnitudes), 1, [2.0 / Float(input.count)], &normalizedMagnitudes, 1, vDSP_Length(input.count))
vDSP_destroy_fftsetup(weights)
return normalizedMagnitudes
}
public func sqrt(_ x: [Float]) -> [Float] {
var results = [Float](repeating: 0.0, count: x.count)
vvsqrtf(&results, x, [Int32(x.count)])
return results
}
我想我正在返回正确的 FFT 数据,打印如下所示:
然而,这不可能是正确的赫兹。是我说话,平均男性声音的范围是 85 到 180 Hz。我只是不确定从这里去哪里。
目标是找到用户通过麦克风讲话时的频率平均值或范围。非常感谢您的帮助!!!