title: AudioConverterFillComplexBuffer return -50 (paramErr)
body: <p>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.</p>
  
  <p>I set up my input and output formats and create the converter:</p>
  
  <pre><code>UInt32 numChannels = 2;
  
  // Describe the input stream
  AudioStreamBasicDescription inputFormat;
  memset(&amp;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(&amp;outputFormat, 0, sizeof(AudioStreamBasicDescription));
  outputFormat.mSampleRate = 44100;
  outputFormat.mFormatID = kAudioFormatAppleLossless;
  outputFormat.mFramesPerPacket = 4096;
  
  OSStatus err = AudioConverterNew(&amp;inputFormat, &amp;outputFormat, &amp;_alacConverter);
  </code></pre>
  
  <p>Then I get the maximum output packet size and allocate a buffer to hold the converted data:</p>
  
  <pre><code>size = sizeof(UInt32);
  UInt32 maxOutputSize;
  AudioConverterGetProperty(_alacConverter, 
                            kAudioConverterPropertyMaximumOutputPacketSize, 
                            &amp;size, 
                            &amp;maxOutputSize);
  
  _outputBuffer = [[NSMutableData dataWithCapacity:maxOutputSize] retain];
  </code></pre>
  
  <p>Finally, I call AudioConverterFillComplexBuffer to get some data:</p>
  
  <pre><code>AudioBufferList bufferList; 
  memset(&amp;bufferList, 0, sizeof(AudioBufferList));
  memset(&amp;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, 
                                        &amp;numPackets, 
                                        &amp;bufferList, 
                                        &amp;streamDesc);
  </code></pre>
  
  <p>...where I always get the -50 error. Can anyone give me a clue where I'm going wrong? Many (many) thanks!</p>
  
  <p><strong>Update:</strong> I'm just using a stub function as the data source, which is never called:</p>
  
  <pre><code>OSStatus _encoderDataProc(AudioConverterRef inAudioConverter, 
                            UInt32* ioNumberDataPackets, 
                            AudioBufferList* ioData, 
                            AudioStreamPacketDescription** outDataPacketDescription,
                            void* inUserData)
  {
      TraceDebug(@"_encoderDataProc has been called");
      return -1;
  }
  </code></pre>
  
  <p>The maxOutputSize comes back as 32776. And for good measure, here's what I get from CAShow(_alacConverter):</p>
  
  <pre><code>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
  </code></pre>
  
  <p>As ever, any help is greatly appreciated. I've made no headway on this since the original post.</p>