Search code examples
ioscore-audio

Recording Data from Callbacks using ExtAudioFileWrite


I'm currently recording stereo audio from the microphone of the iPhone and I have to record the data from the callbacks for analysis.

Currently my AudioStreamBasicDescription format is

AudioStreamBasicDescription format;
format.mSampleRate          = 0;
format.mFormatID            = kAudioFormatLinearPCM;
format.mFormatFlags         = kAudioFormatFlagIsFloat | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved;
format.mFramesPerPacket     = 1;
format.mChannelsPerFrame    = 2;
format.mBitsPerChannel      = 32;
format.mBytesPerPacket      = 4;
format.mBytesPerFrame       = 4;

and the buffer list I render data into is

inputBufferList->mNumberBuffers = NUMCHANNELS;
for (size_t n = 0; n < NUMCHANNELS; n++) {
    inputBufferList->mBuffers[n].mDataByteSize = inNumberFrames * sizeof(float);
    inputBufferList->mBuffers[n].mNumberChannels = 1;
    inputBufferList->mBuffers[n].mData = malloc(inputBufferList->mBuffers[n].mDataByteSize);
}

When I try to write this data into the ExtAudioFileWrite, it gives an error and it was said that the format is wrong. Is there any tutorial on how to write stereo audio using ExtAudioFileWrite?

Edit:

Here is how I'm setting it up

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* destinationFilePath = [[NSString alloc] initWithFormat: @"%@/testrecording.wav", documentsDirectory];

CFURLRef destinationURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)destinationFilePath, kCFURLPOSIXPathStyle, false);

OSStatus status;

ExtAudioFileRef cfref;

status = ExtAudioFileCreateWithURL(destinationURL, kAudioFileWAVEType,
                                   &format, NULL, kAudioFileFlags_EraseFile,
                                   &cfref);

The status shows an exception in this


Solution

  • 1718449215 is kAudioConverterErr_FormatNotSupported ('fmt?'), so I'm guessing that WAVE might not support float LPCM. You could try changing to kAudioFormatFlagIsSignedInteger or switching file format, e.g. kAudioFileM4AType, kAudioFileCAFType, or (maybe?) kAudioFileAIFFType.

    Don't forget to update format sizes for the former, and filename extension for the latter.