title: Using AudioBufferList with Swift
body: <p>I have a bridging function in Swift, one of whose arguments in C is <code>AudioBufferList *</code>.  In Swift this generates an <code>UnsafePointer&lt;AudioBufferList&gt;</code>.  I've manage to deference the pointer  by calling <code>audioData[0]</code> (is there a better way?).  But I'm struggling with the next 2 tiers down: the <code>.mBuffers</code> array of <code>AudioBuffer</code>'s and their <code>void *</code> / <code>UnsafePointer&lt;()&gt;</code> <code>.mData</code> members.  </p>
  
  <p>In C it would simply be</p>
  
  <pre><code>Float32 *audioData = (Float 32*)abl-&gt;mBuffers[0]-&gt;mData;
  output = audioData[sampleNum]...
  </code></pre>
  
  <p>In Swift the first odd thing is that it won't let me access the elements of <code>mBuffers</code> but is perfectly happy when I access it as a property.  In other words, this works and even has correct data (for the first member of <code>mBuffers</code> I presume)...</p>
  
  <pre><code>println(abl[0].mBuffers.mNumberChannels)  // But .mBuffers should be an []!
  </code></pre>
  
  <p>Second, it let's me print out <code>.mData</code> subscripts but the value is always <code>()</code></p>
  
  <pre><code>println(abl[0].mBuffers.mData[10])  // Prints '()'
  </code></pre>
  
  <p>I've tried various casting ops and accessing with multiple indices but to no avail...any ideas?</p>
  
  <p>Here are the C and Swift definitions for <code>AudioBufferList</code> and <code>AudioBuffer</code> for convenience...</p>
  
  <pre><code>// C
  struct AudioBufferList
  {
      UInt32      mNumberBuffers;
      AudioBuffer mBuffers[1]; // this is a variable length array of mNumberBuffers elements
      // ...and a bit more for c++
  }
  
  
  struct AudioBuffer
  {
      UInt32  mNumberChannels;
      UInt32  mDataByteSize;
      void*   mData;
  };
  </code></pre>
  
  <p>... </p>
  
  <pre><code>// SWIFT
  
  struct AudioBufferList {
      var mNumberBuffers: UInt32
      var mBuffers: (AudioBuffer)
  }
  
  struct AudioBuffer {
      var mNumberChannels: UInt32
      var mDataByteSize: UInt32
      var mData: UnsafePointer&lt;()&gt;
  }
  </code></pre>