title: Reading audio with Extended Audio File Services (ExtAudioFileRead)
body: <p>I am working on understanding Core Audio, or rather: <a href="http://developer.apple.com/iphone/library/documentation/MusicAudio/Reference/ExtendedAudioFileServicesReference/Reference/reference.html" rel="nofollow noreferrer">Extended Audio File Services</a></p>
  
  <p>Here, I want to use <code>ExtAudioFileRead()</code> to read some audio data from a file.<br>
  This works fine as long as I use one single huge buffer to store my audio data (that is, one <code>AudioBuffer</code>). As soon as I use more than one <code>AudioBuffer</code>, <code>ExtAudioFileRead()</code> returns the error code <code>-50</code> ("error in parameter list"). As far as I can figure out, this means that one of the arguments of <code>ExtAudioFileRead()</code> is wrong. Probably the <code>audioBufferList</code>.</p>
  
  <p>I can not use one huge buffer because then, <code>dataByteSize</code> would overflow its <code>UInt32</code>-integer range with huge files.</p>
  
  <p>Here is the code to create the <code>audioBufferList</code>:</p>
  
  <pre><code>AudioBufferList *audioBufferList;
  audioBufferList = malloc(sizeof(AudioBufferList) + (numBuffers-1)*sizeof(AudioBuffer));
  audioBufferList-&gt;mNumberBuffers = numBuffers;
  for (int bufferIdx = 0; bufferIdx&lt;numBuffers; bufferIdx++ ) {
      audioBufferList-&gt;mBuffers[bufferIdx].mNumberChannels = numChannels;
      audioBufferList-&gt;mBuffers[bufferIdx].mDataByteSize = dataByteSize;
      audioBufferList-&gt;mBuffers[bufferIdx].mData = malloc(dataByteSize);
  }
  </code></pre>
  
  <p>And here is the working, but overflowing code:</p>
  
  <pre><code>UInt32 dataByteSize = fileLengthInFrames * bytesPerFrame; // this will overflow
  AudioBufferList *audioBufferList = malloc(sizeof(audioBufferList));
  audioBufferList-&gt;mNumberBuffers = 1;
  audioBufferList-&gt;mBuffers[0].mNumberChannels = numChannels;
  audioBufferList-&gt;mBuffers[0].mDataByteSize = dataByteSize;
  audioBufferList-&gt;mBuffers[0].mData = malloc(dataByteSize);
  </code></pre>
  
  <p>And finally, the call of <code>ExtAudioFileRead()</code> (should work with both versions):</p>
  
  <pre><code>UInt32 numFrames = fileLengthInFrames;
  error = ExtAudioFileRead(extAudioFileRef,
                           &amp;numFrames,
                           audioBufferList);
  </code></pre>
  
  <p>Do you know what I am doing wrong here?</p>