Skip to content

Instantly share code, notes, and snippets.

@welbesw
Last active March 18, 2016 14:38
Show Gist options
  • Save welbesw/82a5cfbea2366ed361de to your computer and use it in GitHub Desktop.
Save welbesw/82a5cfbea2366ed361de to your computer and use it in GitHub Desktop.
FFT on the audio buffer in the AURenderCallback method:
static void performFFT(float* data, UInt32 numberOfFrames, SoundBufferPtr soundBuffer, UInt32 inBusNumber) {
int bufferLog2 = round(log2(numberOfFrames));
float fftNormFactor = 1.0/( 2 * numberOfFrames);
FFTSetup fftSetup = vDSP_create_fftsetup(bufferLog2, kFFTRadix2);
int numberOfFramesOver2 = numberOfFrames / 2;
float outReal[numberOfFramesOver2];
float outImaginary[numberOfFramesOver2];
COMPLEX_SPLIT output = { .realp = outReal, .imagp = outImaginary };
//Put all of the even numbered elements into outReal and odd numbered into outImaginary
vDSP_ctoz((COMPLEX *)data, 2, &output, 1, numberOfFramesOver2);
//Perform the FFT via Accelerate
//Use FFT forward for standard PCM audio
vDSP_fft_zrip(fftSetup, &output, 1, bufferLog2, FFT_FORWARD);
//Scale the FFT data
vDSP_vsmul(output.realp, 1, &fftNormFactor, output.realp, 1, numberOfFramesOver2);
vDSP_vsmul(output.imagp, 1, &fftNormFactor, output.imagp, 1, numberOfFramesOver2);
//Take the absolute value of the output to get in range of 0 to 1
vDSP_zvabs(&output, 1, soundBuffer[inBusNumber].frequencyData, 1, numberOfFramesOver2);
vDSP_destroy_fftsetup(fftSetup);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment