EXC_BAS_ACCESS in Core Audio - writing mic data to file w/ Extended AudioFile Services

51 Views Asked by At

I am attempting to write incoming mic audio to a file. Because the audio samples are delivered 4096 frames (the set frame rate for my project) at a time in a time-critical callback I cannot simply write the bytes to a file with AudioFileWriteBytes. I also did not wish to go through the effort and complexity of setting up my own ring buffer to store samples to write elsewhere. So I am using Extended Audio File API for its ExtAudioFileWriteAsync function.

As per instructed by the documentation I create the ExtAudioFileRef with a CFURL and than run it once with a null buffer and 0 frames in main. Then I initiate my AUHAL unit and the input callback begins to be called.

ExtAudioFileWriteAsync(player.recordFile, 0, NULL);

There I have my code to write to this file asynchronously. I have the call nested in a dispatch queue so that it runs after the callback function exits scope (tho not sure if that is necessary but I get this error with all without the enclosing dispatch block). This is the callback as it is right now.

OSStatus InputRenderProc(void *inRefCon,
                         AudioUnitRenderActionFlags *ioActionFlags,
                         const AudioTimeStamp *inTimeStamp,
                         UInt32 inBusNumber,
                         UInt32 inNumberFrames,
                         AudioBufferList * ioData)
{
   MyAUGraphPlayer *player = (MyAUGraphPlayer*) inRefCon;

   // rendering incoming mic samples to player->inputBuffer
   OSStatus inputProcErr = noErr;
   inputProcErr = AudioUnitRender(player->inputUnit,
                                  ioActionFlags,
                                  inTimeStamp,
                                  inBusNumber,
                                  inNumberFrames,
                                  player->inputBuffer);


   printf("%i", inNumberFrames);
   dispatch_async(player->fileWritingQueue, ^{
      ExtAudioFileWriteAsync(player->recordFile, 4096, player->inputBuffer);
   });

   return inputProcErr;
}

It immediately bails out with the bad access exception on the first callback invocation. For clarity these are the settings I have for creating the file to begin with.

   // describe a PCM format for audio file
   AudioStreamBasicDescription format =  { 0 };
   format.mBytesPerFrame = 4;
   format.mBytesPerPacket = 4;
   format.mChannelsPerFrame = 2;
   format.mBitsPerChannel = 16;
   format.mFramesPerPacket = 1;
   format.mFormatFlags = kAudioFormatFlagIsPacked | kAudioFormatFlagIsFloat;
   format.mFormatID = kAudioFormatLinearPCM;

   CFURLRef myFileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("./test2.wav"), kCFURLPOSIXPathStyle, false);


   ExtAudioFileCreateWithURL(myFileURL,
                             kAudioFileWAVEType,
                             &format,
                             NULL,
                             kAudioFileFlags_EraseFile,
                             &player.recordFile);
   player.fileWritingQueue = dispatch_queue_create("myQueue", NULL);

   ExtAudioFileWriteAsync(player.recordFile, 0, NULL);
0

There are 0 best solutions below