Skip to content

Instantly share code, notes, and snippets.

@denisb411
Last active June 19, 2017 12:26
Show Gist options
  • Save denisb411/3e832a592ff8b79cca2afdbed432a0d0 to your computer and use it in GitHub Desktop.
Save denisb411/3e832a592ff8b79cca2afdbed432a0d0 to your computer and use it in GitHub Desktop.
Custom AKFFTTap. Improved the FFT resolution by increasing hte bufferSize.
import Foundation
import AudioKit
/// FFT Calculation for any node
@objc open class CustomAKFFTTap: NSObject, EZAudioFFTDelegate {
internal let bufferSize: UInt32 = 512 //old: 1024
internal var fft: EZAudioFFT?
/// Array of FFT data
open var fftData = [Double](zeros: 256) //old: 512
/// Initialze the FFT calculation on a given node
///
/// - parameter input: Node on whose output the FFT will be computed
///
public init(_ input: AKNode) {
super.init()
fft = EZAudioFFT(maximumBufferSize: vDSP_Length(bufferSize), sampleRate: Float(AKSettings.sampleRate), delegate: self)
input.avAudioNode.installTap(onBus: 0, bufferSize: bufferSize, format: AudioKit.format) { [weak self] (buffer, time) -> Void in
guard let strongSelf = self else { return }
buffer.frameLength = strongSelf.bufferSize
let offset = Int(buffer.frameCapacity - buffer.frameLength)
let tail = buffer.floatChannelData?[0]
strongSelf.fft!.computeFFT(withBuffer: &tail![offset],
withBufferSize: strongSelf.bufferSize)
}
}
/// Callback function for FFT computation
@objc open func fft(_ fft: EZAudioFFT!, updatedWithFFTData fftData: UnsafeMutablePointer<Float>, bufferSize: vDSP_Length) {
DispatchQueue.main.async { () -> Void in
for i in 0..<256 { //old: 512
self.fftData[i] = Double(fftData[i])
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment