Skip to content

Instantly share code, notes, and snippets.

@MaxBareiss
Created May 27, 2014 15:16
Show Gist options
  • Save MaxBareiss/3868b8c3eaaff45bcaea to your computer and use it in GitHub Desktop.
Save MaxBareiss/3868b8c3eaaff45bcaea to your computer and use it in GitHub Desktop.
-(void) audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
static FFTSetupD fft_weights;
static DSPDoubleSplitComplex input;
static double *magnitudes;
ExtAudioFileRef fileRef;
ExtAudioFileOpenURL((__bridge CFURLRef)[recorder url], &fileRef);
// 2) Set up audio format // DATA LEAK NOT HERE
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = Fs;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
audioFormat.mBitsPerChannel = sizeof(Float32) * 8;
audioFormat.mChannelsPerFrame = 1; // set this to 2 for stereo
audioFormat.mBytesPerFrame = audioFormat.mChannelsPerFrame * sizeof(Float32);
audioFormat.mFramesPerPacket = 1;
audioFormat.mBytesPerPacket = audioFormat.mFramesPerPacket * audioFormat.mBytesPerFrame;
// 3) Apply audio format to my Extended Audio File
ExtAudioFileSetProperty(fileRef, kExtAudioFileProperty_ClientDataFormat, sizeof (AudioStreamBasicDescription), &audioFormat);
// 4) Set up an AudioBufferList
UInt32 outputBufferSize = 32 * 1024; // 32 KB
UInt32 sizePerPacket = audioFormat.mBytesPerPacket;
UInt32 packetsPerBuffer = outputBufferSize / sizePerPacket;
UInt8 *outputBuffer = (UInt8 *)malloc(sizeof(UInt8 *) * outputBufferSize);
AudioBufferList convertedData;
convertedData.mNumberBuffers = 1;
convertedData.mBuffers[0].mNumberChannels = audioFormat.mChannelsPerFrame;
convertedData.mBuffers[0].mDataByteSize = outputBufferSize;
convertedData.mBuffers[0].mData = outputBuffer;
// 5) Read Extended Audio File into AudioBufferList with ExtAudioFileRead()
UInt32 frameCount = packetsPerBuffer;
ExtAudioFileRead(fileRef, &frameCount, &convertedData);
// 6) Log float values of AudioBufferList
/* Setup weights (twiddle factors) */
fft_weights = vDSP_create_fftsetupD(two_exp, kFFTRadix2);
/* Allocate memory to store split-complex input and output data */
input.realp = (double *)malloc(size_buffer * sizeof(double));
input.imagp = (double *)malloc(size_buffer * sizeof(double));
magnitudes = (double *)malloc(size_buffer * sizeof(double));
int bufferSize;
// THIS IS WHERE THE MEMORY LEAK IS BELOW
for(int y = 0; y < convertedData.mNumberBuffers; y++)
{
AudioBuffer audioBuffer = convertedData.mBuffers[y];
bufferSize = audioBuffer.mDataByteSize / sizeof(Float32);
Float32 *frame = audioBuffer.mData;
for (int i = 0; i < bufferSize; i++)
{
input.realp[i] = (double)frame[i];
input.imagp[i] = 0.0f;
}
}
// There is more stuff here but it is unimportant
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment