我正在编写一个需要检测音频流中频率的应用程序。我已经读了大约一百万篇文章,但在跨越终点线时遇到了问题。我通过Apple的AVFoundation Framework通过此功能将音频数据传给我。
我正在使用Swift 4.2,并尝试使用FFT函数,但是目前它们有点烦人。
有什么想法吗?
// get's the data as a call back for the AVFoundation framework.
public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
// prints the whole sample buffer and tells us alot of information about what's inside
print(sampleBuffer);
// create a buffer, ready out the data, and use the CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer method to put
// it into a buffer
var buffer: CMBlockBuffer? = nil
var audioBufferList = AudioBufferList(mNumberBuffers: 1,
mBuffers: AudioBuffer(mNumberChannels: 1, mDataByteSize: 0, mData: nil))
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, bufferListSizeNeededOut: nil, bufferListOut: &audioBufferList, bufferListSize: MemoryLayout<AudioBufferList>.size, blockBufferAllocator: nil, blockBufferMemoryAllocator: nil, flags: UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment), blockBufferOut: &buffer);
let abl = UnsafeMutableAudioBufferListPointer(&audioBufferList)
var sum:Int64 = 0
var count:Int = 0
var bufs:Int = 0
var max:Int64 = 0;
var min:Int64 = 0
// loop through the samples and check for min's and maxes.
for buff in abl {
let samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(OpaquePointer(buff.mData)),
count: Int(buff.mDataByteSize)/MemoryLayout<Int16>.size)
for sample in samples {
let s = Int64(sample)
sum = (sum + s*s)
count += 1
if(s > max) {
max = s;
}
if(s < min) {
min = s;
}
print(sample)
}
bufs += 1
}
// debug
print("min - \(min), max = \(max)");
// update the interface
DispatchQueue.main.async {
self.frequencyDataOutLabel.text = "min - \(min), max = \(max)";
}
// stop the capture session
self.captureSession.stopRunning();
}
答案 0 :(得分:0)
经过大量研究,我发现答案是使用FFT方法(快速傅立叶变换)。它从上面的iPhone代码中获取原始输入,并将其转换为代表频带中每个频率幅度的值数组。
此处https://github.com/jscalo/tempi-fft的开放代码有很多支持,这些代码创建了一个可视化工具,用于捕获数据并显示数据。从那里开始,就需要对其进行操作以满足需求。就我而言,我一直在寻找高于人类听力(20kHz范围)的频率。通过在tempi-fft代码中扫描阵列的后半部分,我能够确定我正在寻找的频率是否存在并且足够响亮。