EXC_BAD_ACCESS from ExtAudioFileWriteAsync

2.2k views Asked by At

I am using audio unit to record our voice. In my callback function I am getting data in AudioBufferList. I have to store this buffer in a file.

Issue is; When I try to store the buffer in a .caf file, the application is crashing. Below is the code.

AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate         = 44100.00;
audioFormat.mFormatID           = kAudioFormatLinearPCM;
audioFormat.mFormatFlags        = kAudioFormatFlagsCanonical;
audioFormat.mBytesPerPacket     = 2;
audioFormat.mFramesPerPacket    = 1;
audioFormat.mBytesPerFrame      = 2;
audioFormat.mChannelsPerFrame   = 1;
audioFormat.mBitsPerChannel     = 16;
audioFormat.mReserved           = 0;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
if(![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"audio.caf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:path])
    NSLog(@"file not exist.");
NSLog(@"Path : %@", path);
THIS->url = [NSURL fileURLWithPath:[path retain]];
err = ExtAudioFileCreateWithURL((CFURLRef)THIS->url, kAudioFileCAFType, &audioFormat, NULL, kAudioFileFlags_EraseFile, &THIS->fOutputAudioFile);
if(err != noErr){
    printf("create with url: error %d\n", (int)err);
}
THIS->state = 2;
err = ExtAudioFileSetProperty(THIS->fOutputAudioFile, kExtAudioFileProperty_ClientDataFormat, (UInt32)sizeof(audioFormat), &audioFormat);
if(err != noErr){
    printf("file set property: error %d\n", (int)err);
}
err = ExtAudioFileWriteAsync(THIS->fOutputAudioFile, 0, NULL);
if(err != noErr){
    printf("write async: error %d\n", (int)err);
    return err;
}
[pool release];

err = ExtAudioFileWriteAsync(THIS->fOutputAudioFile, ioData->mNumberBuffers, ioData);
if(err != noErr){
    printf("file write async: error %d\n", (int)err);
    return err;
}

ERROR:

Program received signal:  “EXC_BAD_ACCESS”.
#0 0x30d04bb2 in memmove () #1 0x33ba1d1e in AudioRingBuffer::Store () #2 0x33c2269e in ExtAudioFile::WriteFramesAsync () #3 0x33c278c8 in ExtAudioFileWriteAsync () #4 0x00002e74 in PerformThru (inRefCon=0x14d9f0, ioActionFlags=0x2ffe77d4, inTimeStamp=0xa896fc, inBusNumber=0, inNumberFrames=256, ioData=0x14f680) at /Alex/project/ProStudio/TestAudioUnit/Classes/TestAudioUnitAppDelegate.mm:399 #5 0x33b70896 in AUInputElement::PullInput () #6 0x33b794a0 in AUInputFormatConverter2::InputProc () #7 0x33b5560e in AudioConverterChain::CallInputProc () #8 0x33b5555e in AudioConverterChain::FillBufferFromInputProc () #9 0x33b5537c in BufferedAudioConverter::GetInputBytes () #10 0x33b79390 in CBRConverter::RenderOutput () #11 0x33b5505a in BufferedAudioConverter::FillBuffer () #12 0x33b55362 in BufferedAudioConverter::GetInputBytes () #13 0x33b79390 in CBRConverter::RenderOutput () #14 0x33b5505a in BufferedAudioConverter::FillBuffer () #15 0x33b551ae in AudioConverterChain::RenderOutput () #16 0x33b5505a in BufferedAudioConverter::FillBuffer () #17 0x33b54e2a in AudioConverterFillComplexBuffer () #18 0x33b78f58 in AUConverterBase::RenderBus () #19 0x33c08eea in AURemoteIO::RenderBus () #20 0x33b5666e in AUBase::DoRender () #21 0x33c09698 in AURemoteIO::PerformIO () #22 0x33c09962 in AURIOCallbackReceiver_PerformIO () #23 0x33c02448 in _XPerformIO () #24 0x33b71bea in mshMIGPerform () #25 0x33bd7de0 in MSHMIGDispatchMessage () #26 0x33c0ebae in AURemoteIO::IOThread::Entry () #27 0x33b4a1d8 in CAPThread::Entry () #28 0x30d7d88c in _pthread_start () #29 0x30d72a90 in thread_start ()

Please help some one to fix this issue.

1

There are 1 answers

0
jfalexvijay On BEST ANSWER

I got the solution for this issue;

Problem is; I am setting the AudioStreamBasicDescription for audio unit and also ExtAudioFileCreateWithURL. It was having some different setting. I have changed as same. Now it is working fine.

Previous ASBD is;


audioFormat.mSampleRate        = 44100.00;
audioFormat.mFormatID          = kAudioFormatLinearPCM;
audioFormat.mFormatFlags       = kAudioFormatFlagsCanonical | (kAudioUnitSampleFractionBits << kLinearPCMFormatFlagsSampleFractionShift) | kAudioFormatFlagIsNonInterleaved;
audioFormat.mChannelsPerFrame  = 2;
audioFormat.mFramesPerPacket   = 1;
audioFormat.mBitsPerChannel    = 8 * sizeof(AudioUnitSampleType);
audioFormat.mBytesPerPacket    = sizeof(AudioUnitSampleType);
audioFormat.mBytesPerFrame     = sizeof(AudioUnitSampleType);;
audioFormat.mReserved          = 0;

now i am using following ASBD;


AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate         = 44100.00;
audioFormat.mFormatID           = kAudioFormatLinearPCM;
audioFormat.mFormatFlags        = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket    = 1;
audioFormat.mChannelsPerFrame   = 1;
audioFormat.mBitsPerChannel     = 16;
audioFormat.mBytesPerPacket     = 2;
audioFormat.mBytesPerFrame      = 2;
audioFormat.mReserved           = 0;