I'm just getting started with CoreAudio. Just trying to create an audio file, but getting a kAudioFileUnsupportedDataFormatError with the following.
Can any give me an idea why? It all looks okay to me, but I must be doing something wrong.
// Prepare the format
AudioStreamBasicDescription asbd;
memset(&asbd, 0, sizeof(asbd));
asbd.mSampleRate = SAMPLE_RATE; // 44100
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mFormatFlags = kAudioFormatFlagIsBigEndian;
asbd.mBitsPerChannel = 16;
asbd.mChannelsPerFrame = 1;
asbd.mFramesPerPacket = 1;
asbd.mBytesPerFrame = 2;
asbd.mBytesPerPacket = 2;
// Set up the file
AudioFileID audioFile;
OSStatus audioErr = noErr;
audioErr = AudioFileCreateWithURL((CFURLRef)fileURL,
kAudioFileAIFFType,
&asbd,
kAudioFileFlags_EraseFile,
&audioFile);
The "Core Audio Data Types Reference" contains the reference material for AudioStreamBasicDescription. But it's pretty dense and difficult to understand.
"Audio Unit Hosting Guide for iOS" has a section called "Working with the AudioStreamBasicDescription structure" that's a bit more helpful.
d.
Well, I got it to work by changing mFormatFlags to:
asbd.mFormatFlags = kLinearPCMFormatFlagIsBigEndian |
kLinearPCMFormatFlagIsSignedInteger |
kLinearPCMFormatFlagIsPacked;
I'm now looking for an Apple doc or other resource that tells you what flags are needed for which format, and why.
mFormatFlags are bit-field flags so they can be combined using bitwise logical operators, hence the density and possible difficulty of understanding. Another useful document to look at before "going for the flags" may be this one:
https://developer.apple.com/library/mac/documentation/MusicAudio/Conceptual/CoreAudioOverview/SupportedAudioFormatsMacOSX/SupportedAudioFormatsMacOSX.html
Else, this objective-C utility for getting a human readable form of the flags has also been posted:
https://gist.github.com/eppz/11272305
Regards!