I'm trying to convert a PCM 32-bit float audio stream to ALAC. I found some working examples to build from, but my own code keeps getting a -50 (paramErr) from AudioConverterFillComplexBuffer. My eyes are crossing from looking at this code; I can't see what's different from my examples.
I set up my input and output formats and create the converter:
UInt32 numChannels = 2;
// Describe the input stream
AudioStreamBasicDescription inputFormat;
memset(&inputFormat, 0, sizeof(AudioStreamBasicDescription));
inputFormat.mSampleRate = 44100;
inputFormat.mFormatID = kAudioFormatLinearPCM;
inputFormat.mFramesPerPacket = 1;
inputFormat.mChannelsPerFrame = numChannels;
inputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked;
inputFormat.mBitsPerChannel = sizeof(Float32) * 8;
inputFormat.mBytesPerPacket = numChannels * (inputFormat.mBitsPerChannel / 8);
inputFormat.mBytesPerFrame = inputFormat.mBytesPerPacket * inputFormat.mFramesPerPacket;
// Describe the output stream
AudioStreamBasicDescription outputFormat;
memset(&outputFormat, 0, sizeof(AudioStreamBasicDescription));
outputFormat.mSampleRate = 44100;
outputFormat.mFormatID = kAudioFormatAppleLossless;
outputFormat.mFramesPerPacket = 4096;
OSStatus err = AudioConverterNew(&inputFormat, &outputFormat, &_alacConverter);
Then I get the maximum output packet size and allocate a buffer to hold the converted data:
size = sizeof(UInt32);
UInt32 maxOutputSize;
AudioConverterGetProperty(_alacConverter,
kAudioConverterPropertyMaximumOutputPacketSize,
&size,
开发者_高级运维 &maxOutputSize);
_outputBuffer = [[NSMutableData dataWithCapacity:maxOutputSize] retain];
Finally, I call AudioConverterFillComplexBuffer to get some data:
AudioBufferList bufferList;
memset(&bufferList, 0, sizeof(AudioBufferList));
memset(&bufferList.mBuffers[0], 0, sizeof(AudioBuffer));
bufferList.mNumberBuffers = 1;
bufferList.mBuffers[0].mNumberChannels = numChannels;
bufferList.mBuffers[0].mDataByteSize = [_outputBuffer length];
bufferList.mBuffers[0].mData = [_outputBuffer mutableBytes];
AudioStreamPacketDescription streamDesc = {0};
UInt32 numPackets = 1;
err = AudioConverterFillComplexBuffer(_alacConverter,
_encoderDataProc,
self,
&numPackets,
&bufferList,
&streamDesc);
...where I always get the -50 error. Can anyone give me a clue where I'm going wrong? Many (many) thanks!
Update: I'm just using a stub function as the data source, which is never called:
OSStatus _encoderDataProc(AudioConverterRef inAudioConverter,
UInt32* ioNumberDataPackets,
AudioBufferList* ioData,
AudioStreamPacketDescription** outDataPacketDescription,
void* inUserData)
{
TraceDebug(@"_encoderDataProc has been called");
return -1;
}
The maxOutputSize comes back as 32776. And for good measure, here's what I get from CAShow(_alacConverter):
AudioConverter 0xa8404e (0x101c449f0):
PCMConverter2 0x101c34370
Input: 2 ch, 44100 Hz, 'lpcm' (0x00000009) 32-bit little-endian float
Output: 2 ch, 44100 Hz, 'lpcm' (0x0000000C) 32-bit little-endian signed integer
CodecConverter 0x101c44b90
Input: 2 ch, 44100 Hz, 'lpcm' (0x0000000C) 32-bit little-endian signed integer
Output: 2 ch, 44100 Hz, 'alac' (0x00000004) from 32-bit source, 4096 frames/packet
codec: 'aenc'/'alac'/'appl'
Input layout tag: 0x650002
Output layout tag: 0x650002
As ever, any help is greatly appreciated. I've made no headway on this since the original post.
I was stuck in the same situation (basically after using your code as a template). Solution seems to be in allocation of memory for the AudioBufferList. Implementing this answer did the trick for me (although my objective-C is not good enough to see the exact difference :))
I experienced the same problem (AudioConverterFillComplexBuffer returning -50)
-
but it appeared only after restarting the same converter with a second file after the first one played to the end.
The first file converted perfectly without problems.
With the help of Claude's answer (THANKS!) I found out that the converter actually changes the size of the AudioBufferList to 0 if the end of the file is reached.
After resetting the AudioBufferList's sizes to the original values everthing works now as expected.
精彩评论