1

I have added the following code at the end of the Mic / Line In Audio Rendering Callback.

But the app keeps crashing with EXC_BAD_ACCESS on :

err = ExtAudioFileWriteAsync(mOutputAudioFile, inNumberFrames, ioData);

The rest of the code is as follows :

ExtAudioFileRef mOutputAudioFile;
    AudioFileID mAfid;
    NSLog(@"Writing output to file ....");

    NSArray *dirPaths;
    NSString *docsDir;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSLog(@"docDir = %@", docsDir);
    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];
    NSURL *inPath = [NSURL fileURLWithPath:soundFilePath];
    NSLog(@"Output file path is : %@", inPath);

    AudioStreamBasicDescription mStreamFormat;

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


    err = AudioFileCreateWithURL((CFURLRef)inPath, kAudioFileCAFType, &mStreamFormat, kAudioFileFlags_EraseFile, &mAfid);
    if (err != noErr) {
        NSLog(@"ERROR : Audio file could not be created !! %d", (int)err);

    }
    err = ExtAudioFileWrapAudioFileID(mAfid, true, &mOutputAudioFile);
    err = ExtAudioFileSetProperty(mOutputAudioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &mStreamFormat);
    if (ioData) {
        err = ExtAudioFileWriteAsync(mOutputAudioFile, inNumberFrames, ioData);
        if (err != noErr) {
            NSLog(@"ERROR : Audio file could not be written !! %d", (int)err);
        }
    }
    else {

        NSLog(@"No ioData found");
    }
    NSLog(@"Done writing output to file ....");

I have tried almost everything with this. I would be extremely thankful if somebody could help me resolve it. Thanks !

UPDATE 1:

If I use ExtAudioFileWrite instead of ExtAudioFileWriteAsync, the app doesn't crash, but returns error -50 for ExtAudioFileWrite.

I still have no idea what's happening with it. Any help in this regard is much appreciated.

Thanks.

Myxtic
  • 5,679
  • 5
  • 48
  • 69
  • What is the entire error message from the console? – zaph Feb 14 '12 at 14:01
  • 1
    You are missing check for errors in the calls before, maybe if you check the return value you will have the needed info. Also, how are the following defined: inNumberFrames, ioData – MByD Feb 14 '12 at 14:03
  • @CocoaFu There are no messages in the console. That's the sad part. It only says: [Switching to process 6191 thread 0x18203] [Switching to process 6191 thread 0x18203] Current language: auto; currently objective-c – Myxtic Feb 14 '12 at 14:08
  • @BinyaminSharet - Are you talking about the return value of the callback ? – Myxtic Feb 14 '12 at 14:11
  • I'm talking about `ExtAudioFileWrapAudioFileID` and the line after – MByD Feb 14 '12 at 14:12
  • Btw inNumberFrames is a UINT32 var with value 512 when the app crashes. And ioData is an AudioBufferList and contains 0x8abe990 at the time of crash. – Myxtic Feb 14 '12 at 14:14
  • @BinyaminSharet - So I applied the error check on err for ExtAudioFileWrapAudioFileID and ExtAudioFileSetProperty. And no error was indicated there. Please let me know if I am missing something here. Thanks so much for your help. – Myxtic Feb 14 '12 at 14:22

2 Answers2

0

Check the ioData.mNumberBuffers value. For me it was set to 2 despite there being only 1 buffer in the bufferlist. This was causing the EXC_BAD_ACCESS.

Pescolly
  • 922
  • 11
  • 18
0

The sample code posted in the answer here answered all my questions :

Can anybody help me in recording iPhone output sound through Audio Unit

Thanks so much everyone for your help.

Community
  • 1
  • 1
Myxtic
  • 5,679
  • 5
  • 48
  • 69