title: Using MIDIPacketList in swift
body: <p>I am looking at some of the examples of midi output using core midi.</p>
  
  <p>Specifically  <a href="https://stackoverflow.com/questions/16223893/passing-a-bytearray-as-a-parameter-of-a-method-coremidi-implementation?rq=1">this question</a></p>
  
  <p>and <a href="https://stackoverflow.com/questions/10572747/why-doesnt-this-simple-coremidi-program-produce-midi-output?rq=1">this</a> </p>
  
  <p>I have code that works based on these in objC and I now want to try to translate that to swift.</p>
  
  <p>the line I least understand is this one: <code>MIDIPacketList* pktList = (MIDIPacketList*) pktBuffer;</code></p>
  
  <p>I read this as declaring a pointer pktlist of type MIDIPacketList and assigning it the value of pktBuffer, cast to the type MIDIPacketList</p>
  
  <p>I'm new to C and objC and this makes no sense to me.</p>
  
  <p>MIDIPacketList is a struct defined <a href="https://developer.apple.com/library/IOs/documentation/CoreMidi/Reference/MIDIServices_Reference/index.html#//apple_ref/c/tdef/MIDIPacketList" rel="nofollow noreferrer">here</a>:</p>
  
  <p>in what way does it make sense to cast a bytearray to the struct type MIDIPacketList and what is this trying to achieve? I guess it is trying to define the size of the packet list, but why do we need to do that here, and how does this do it anyway? why is it not sufficient to do it with MIDIPacketListAdd as happens a few lines later?</p>
  
  <p>here is my attempt at a midi output class in swift - can anyone see what is going wrong? this code gives no errors in Xcode until it is run. I have a working version of this in objC, but i can't get the size of the packet list defined in swift. (at least I think that's the problem)</p>
  
  <pre><code>import Foundation
  import CoreMIDI
  
  class MidiOutClass {
  
  var midiClient = MIDIClientRef()
  var midiSource = MIDIEndpointRef()
  
  
  func openOutput() {
      MIDIClientCreate("MIDI client", nil, nil, &amp;midiClient)
      MIDISourceCreate(midiClient, "MIDI Source",&amp;midiSource)
      println("midi out opened")//should only do this if successful
  }
  
  func noteOn(channel: Int, note: Int, velocity:Int) {
      midisend((0x90+channel), note: note, value: velocity)
  }
  
  func polyAfter(channel: Int, note: Int, value:Int) {
      midisend((0xA0+channel), note: note, value: value)
  }
  
  func noteOff(channel: Int, note: Int) {
      midisend((0x90+channel), note: note, value: 0 )
  }
  
  func midisend(status:Int, note: Int, value:Int) {
  
      var packet: UnsafeMutablePointer&lt;MIDIPacket&gt; = nil
      //var buffer = [Byte](count:1024, repeatedValue: 0)
  //this is the array I'm trying to use in a similar way to the obj C.
  
      var packetList: UnsafeMutablePointer&lt;MIDIPacketList&gt; = nil
      let midiDataToSend:[Byte] = [Byte(status), Byte(note), Byte(value)];
      packet = MIDIPacketListInit(packetList);
      packet = MIDIPacketListAdd(packetList, 1024, packet, 0, 3, midiDataToSend);
  
      if (packet == nil ) {
          println("failed to send the midi.")
      } else {
          MIDIReceived(midiSource, packetList)
          println("sent some stuff")
      }
  }
  
  }//end MidiOutClass
  </code></pre>