Swift 1.2 beta2 and CoreMIDI
Swift 1.2 beta2 and CoreMIDI
Someone is actually working on CoreMIDI with Swift.
N.B.
Apple has improved Core MIDI support for Swift since this was written.
The Problem
Prior to Swift 1.2, various CoreMIDI types defined in MIDIServices.h were architecture dependent. Like this:
1 2 3 4 5 6 7 8 9 10 11 |
#if __LP64__ typedef UInt32 MIDIObjectRef; typedef MIDIObjectRef MIDIClientRef; #else typedef void * MIDIObjectRef; typedef struct OpaqueMIDIClient * MIDIClientRef; typedef struct OpaqueMIDIPort * MIDIPortRef; typedef struct OpaqueMIDIDevice * MIDIDeviceRef; typedef struct OpaqueMIDIEntity * MIDIEntityRef; typedef struct OpaqueMIDIEndpoint * MIDIEndpointRef; #endif |
This mean that you had to deal with nonsense like this:
1 2 3 4 5 6 7 8 9 |
#if __LP64__ var client = MIDIClientRef() status = MIDIClientCreate(s, MIDINotifyProc( COpaquePointer( [ MyMIDINotifyProc ] ) ), nil, &client) #else //FIXME: abandon all hope, ye who enter here |
I blogged about the problem with creating a MIDI client ref in a 32-bit architecture. (Hint: don’t try).
Now in Swift 1.2 beta wer get this:
1 2 3 4 5 6 7 |
typealias MIDIObjectRef = UInt32 typealias MIDIClientRef = MIDIObjectRef typealias MIDIPortRef = MIDIObjectRef typealias MIDIDeviceRef = MIDIObjectRef typealias MIDIEntityRef = MIDIObjectRef typealias MIDIEndpointRef = MIDIObjectRef typealias MIDITimeStamp = UInt64 |
Those Opaque structs are now history. Yay.
But…
Let’s look at the MIDI Services reference.
Here’s a gem. Note the return type.
1 |
func MIDIGetNumberOfSources() -> ItemCount |
You would use this like so:
1 2 3 4 |
var sourceCount = MIDIGetNumberOfSources() for var i:ItemCount = 0; i < sourceCount; i++ { var endpoint = MIDIGetSource(i) etc. |
ItemCount is defined in MacTypes. Or it was until now. Right now there is a commment for it but no definition.
D’oh!
Workaround. Go ahead and define it yourself as it was pre-Swift 1.2.
1 |
typealias ItemCount = UInt |
Or just don’t use it.
1 2 3 |
for srcIndex in 0 ..< sourceCount { let mep = MIDIGetSource(srcIndex) etc. |
Summary
There is progress with using CoreMIDI from Swift.
There are still potholes though.
One Reply to “Swift 1.2 beta2 and CoreMIDI”