【发布时间】:2015-12-29 17:02:49
【问题描述】:
我正在尝试从 Swift 中的 AVAudioPCMBuffer 生成频谱图。我在AVAudioMixerNode 上安装了一个水龙头,并接收到带有音频缓冲区的回调。我想将缓冲区中的信号转换为[Float:Float] 字典,其中键代表频率,值代表相应频率上的音频幅度。
我尝试使用 Apple 的 Accelerate 框架,但我得到的结果似乎令人怀疑。我确定这只是我转换信号的方式。
我查看了this blog post 以供参考。
这是我所拥有的:
self.audioEngine.mainMixerNode.installTapOnBus(0, bufferSize: 1024, format: nil, block: { buffer, when in
let bufferSize: Int = Int(buffer.frameLength)
// Set up the transform
let log2n = UInt(round(log2(Double(bufferSize))))
let fftSetup = vDSP_create_fftsetup(log2n, Int32(kFFTRadix2))
// Create the complex split value to hold the output of the transform
var realp = [Float](count: bufferSize/2, repeatedValue: 0)
var imagp = [Float](count: bufferSize/2, repeatedValue: 0)
var output = DSPSplitComplex(realp: &realp, imagp: &imagp)
// Now I need to convert the signal from the buffer to complex value, this is what I'm struggling to grasp.
// The complexValue should be UnsafePointer<DSPComplex>. How do I generate it from the buffer's floatChannelData?
vDSP_ctoz(complexValue, 2, &output, 1, UInt(bufferSize / 2))
// Do the fast Fournier forward transform
vDSP_fft_zrip(fftSetup, &output, 1, log2n, Int32(FFT_FORWARD))
// Convert the complex output to magnitude
var fft = [Float](count:Int(bufferSize / 2), repeatedValue:0.0)
vDSP_zvmags(&output, 1, &fft, 1, vDSP_length(bufferSize / 2))
// Release the setup
vDSP_destroy_fftsetup(fftsetup)
// TODO: Convert fft to [Float:Float] dictionary of frequency vs magnitude. How?
})
我的问题是
- 如何将
buffer.floatChannelData转换为UnsafePointer<DSPComplex>以传递给vDSP_ctoz函数?有没有不同/更好的方法可以绕过vDSP_ctoz? - 如果缓冲区包含来自多个通道的音频,这会有所不同吗?缓冲音频通道数据交错或不交错有何不同?
- 如何将
fft数组中的索引转换为以Hz 为单位的频率? - 还有什么我做错了吗?
更新
感谢大家的建议。我最终按照接受的答案中的建议填充了复杂的数组。当我绘制值并在音叉上播放 440 Hz 音调时,它会准确记录它应该在哪里。
这是填充数组的代码:
var channelSamples: [[DSPComplex]] = []
for var i=0; i<channelCount; ++i {
channelSamples.append([])
let firstSample = buffer.format.interleaved ? i : i*bufferSize
for var j=firstSample; j<bufferSize; j+=buffer.stride*2 {
channelSamples[i].append(DSPComplex(real: buffer.floatChannelData.memory[j], imag: buffer.floatChannelData.memory[j+buffer.stride]))
}
}
channelSamples 数组为每个通道保存单独的样本数组。
为了计算大小,我使用了这个:
var spectrum = [Float]()
for var i=0; i<bufferSize/2; ++i {
let imag = out.imagp[i]
let real = out.realp[i]
let magnitude = sqrt(pow(real,2)+pow(imag,2))
spectrum.append(magnitude)
}
【问题讨论】:
-
嘿,刚刚发现你的堆栈溢出问题,我得说:谢谢!你无疑为我节省了大量的研究时间。我仍然对这个答案的工作原理很感兴趣,但我想表达一些赞赏,因为它似乎还没有被发现(或者可能与大多数人无关)
-
这个问题已经很老了,但是第二部分的“out”变量是什么?你是怎么得到的?
-
@Logan:
out变量是DSPSplitComplex 的一个实例。它包含一个复数,其中实部和虚部存储在单独的数组中。它由 FFT 函数填充。 -
@Jakub 谢谢,我知道如何让它工作了。你为我节省了大量时间!这是一个赞成票!
标签: ios swift audio accelerate-framework avaudioengine