0

i am trying to capture raw buffer data into an audio file. Like this:

dstFormat.mSampleRate = 224000; 
dstFormat.mFormatID = kAudioFormatMPEG4AAC;
dstFormat.mChannelsPerFrame = 1;
dstFormat.mBitsPerChannel = 16;
dstFormat.mBytesPerPacket = dstFormat.mBytesPerFrame = 2 * dstFormat.mChannelsPerFrame;
dstFormat.mFramesPerPacket = 1;
dstFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;      
OSStatus result = ExtAudioFileCreateWithURL((CFURLRef) inURL,
                                            kAudioFileM4AType,
                                            &dstFormat,
                                            0,
                                            kAudioFileFlags_EraseFile,
                                            &audioFileRef)

I am hooked into a function with the following parameters: AudioUnit inUnit, AudioUnitRenderActionFlags * ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inOutputBusNumber, UInt32 inNumberFrames, AudioBufferList * ioData

Inside it I am trying to write the data into file, but a bad parameter -50 error occurs. result = ExtAudioFileWrite(audioFileRef, inNumberFrames, ioData);

If I replace ioData parameter with my own:

 AudioBufferList *bufferList = (AudioBufferList*) malloc(sizeof(AudioBufferList));
    bufferList->mNumberBuffers = 1;// ioData->mNumberBuffers;
    for(UInt32 i=0;i<bufferList->mNumberBuffers;i++)
    {
        bufferList->mBuffers[i].mNumberChannels = 1; 
        bufferList->mBuffers[i].mDataByteSize = ioData->mBuffers[i].mDataByteSize; //ioData->mBuffers[i].mDataByteSize; // (1024*2) * dstFormat.mBytesPerFrame;
        bufferList->mBuffers[i].mData = ioData->mBuffers[i].mData;
    }

.. i get file around 260MB for the first second. What is wrong here?

4

2 に答える 2

1

I'm surprised that the iPhone will even create an audio file with those parameters, though I suspect that's because you have only tested it so far in the simulator and not on hardware. Anyways, your stream format is the problem here.

If you are capturing raw data, then why set the format ID to m4a, but then set the format flags to use linear PCM? I'm also not sure how you arrived at a sample rate of 224kHz, but it's going to get you into trouble and at least partially explains why the output file size is so big.

I'd suggest googling around a bit to see some examples of the stream description structure, as it is a bit complicated. This question might also help you get on the right track:

CoreAudio - kAudioFileUnsupportedDataFormatError

于 2012-04-13T09:36:29.413 に答える
1

I just figured out how to write to the AAC format a couple of days ago. Details + code here: Recording to AAC from RemoteIO: data is getting written but file unplayable

I've only tested this on iOS 5 in the iPad Simulator + iPad 2. Performance is great and I have massive respect for the Core Audio capabilities but geez... Apple could document it a bit more.

于 2012-04-13T12:44:02.350 に答える