title: core audio: how can one packet = one byte when clearly one packet = 4 bytes
body: <p>I was going over core audio conversion services in the <em><a href="http://www.informit.com/store/learning-core-audio-a-hands-on-guide-to-audio-programming-9780321636843" rel="nofollow">Learning Core Audio</a></em> and I was struck by this example in their <a href="http://www.informit.com/store/learning-core-audio-a-hands-on-guide-to-audio-programming-9780321636843" rel="nofollow">sample code</a>: </p>
  
  <pre><code>while(1)
  {
      // wrap the destination buffer in an AudioBufferList
      AudioBufferList convertedData;
      convertedData.mNumberBuffers = 1;
      convertedData.mBuffers[0].mNumberChannels = mySettings-&gt;outputFormat.mChannelsPerFrame;
      convertedData.mBuffers[0].mDataByteSize = outputBufferSize;
      convertedData.mBuffers[0].mData = outputBuffer;
  
      UInt32 frameCount = packetsPerBuffer;
  
      // read from the extaudiofile
      CheckResult(ExtAudioFileRead(mySettings-&gt;inputFile,
                                   &amp;frameCount,
                                   &amp;convertedData),
                  "Couldn't read from input file");
  
      if (frameCount == 0) {
          printf ("done reading from file");
          return;
      }
  
      // write the converted data to the output file
      CheckResult (AudioFileWritePackets(mySettings-&gt;outputFile,
                                         FALSE,
                                         frameCount,
                                         NULL,
                                         outputFilePacketPosition / mySettings-&gt;outputFormat.mBytesPerPacket, 
                                         &amp;frameCount,
                                         convertedData.mBuffers[0].mData),
                   "Couldn't write packets to file");
  
      // advance the output file write location
      outputFilePacketPosition += (frameCount * mySettings-&gt;outputFormat.mBytesPerPacket);
  }
  </code></pre>
  
  <p>notice how <code>frameCount</code> is defined as <code>packetsPerBuffer</code>.. <code>packetsPerBuffer</code> is defined here:</p>
  
  <pre><code>UInt32 outputBufferSize = 32 * 1024; // 32 KB is a good starting point
  UInt32 sizePerPacket = mySettings-&gt;outputFormat.mBytesPerPacket;    
  UInt32 packetsPerBuffer = outputBufferSize / sizePerPacket;
  </code></pre>
  
  <p>the part that stumped me is <code>AudioFileWritePackets</code> is called.. in the <a href="https://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/AudioFileConvertRef/Reference/reference.html#//apple_ref/c/func/AudioFileWritePackets" rel="nofollow">documentation</a> AudioFileWritePackets third and fifth parameters are defined as:</p>
  
  <p><b>inNumBytes</b>
  <em>The number of bytes of audio data being written.</em></p>
  
  <p><b>ioNumPackets</b>
  <em>On input, a pointer to the number of packets to write. On output, a pointer to the number of packets actually written..</em></p>
  
  <p>yet in the code both parameters are given frameCount.. how is this possible?? I know with PCM data 1 frame = 1 packet:</p>
  
  <pre><code>// define the ouput format. AudioConverter requires that one of the data formats be LPCM
  audioConverterSettings.outputFormat.mSampleRate = 44100.0;
  audioConverterSettings.outputFormat.mFormatID = kAudioFormatLinearPCM;
  audioConverterSettings.outputFormat.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
  audioConverterSettings.outputFormat.mBytesPerPacket = 4;
  audioConverterSettings.outputFormat.mFramesPerPacket = 1;
  audioConverterSettings.outputFormat.mBytesPerFrame = 4;
  audioConverterSettings.outputFormat.mChannelsPerFrame = 2;
  audioConverterSettings.outputFormat.mBitsPerChannel = 16;
  </code></pre>
  
  <p>but the same lPCM formatting also clearly states that there are 4 bytes per packet (= 4 bytes per frame).. </p>
  
  <p>so how does this work? (the same applies to the other example in the same chapter that uses <code>AudioConverterFillComplexBuffer</code> instead of <code>ExtAudioFileRead</code>, and uses packets instead of frames.. but it's the same thing)</p>