Swift CoreMIDI without Objective-C (Updated for Swift 3)

Update: Code has been added for Swift 3 and XCode 8

These examples do little to no error checking and it is assumed your system has at least one source and one destination. Run them in a macOS Playground.

List the MIDI sources and destinations (Swift 3)

 

List the MIDI sources and destinations (Swift 2)

 

Create a client, connect a destination, and play a note (Swift 3)

 

Create a client, connect a destination, and play a note (Swift 2)

 

Connect an input source and receive data via a callback (Swift 3)

 

Connect an input source and receive data via a callback (Swift 2)

8 thoughts on “Swift CoreMIDI without Objective-C (Updated for Swift 3)”

  1. Hi,
    Thanks for this page, I do get an EXC_BAD_ACCESS error on the “MIDISend(outPort, dest, &packetList)” line of the “Create a client, connect a destination, and play a note” code. I can’t figure out what this is due to. Any idea?

    1. The problem was that I working with a playground for iOS and not for OS X. It works fine when working with a playground for OS X and using Cocoa.

    1. Hello,
      Thanks for this page ! i would like to send midi message, in playground everything is right, no error, but i’m trying to send midi to my DAW Ableton and they don’t receive anything, What i miss ?

  2. Hello Matt,

    very helpful, thanks.

    I have one problem. In a playground, the code for sending MIDI notes (and other events) works only, if the destination is “Internalcomm”. For all other devices (Network, an CoremMIDI compliant interface attached to the computer’s USB port etc.) there is no effect. A MIDI event monitoring program I have purchased does not show any events. There are no error messages and the code runs to completion.

    Also, if I rund the exact same code on an iOS device, no messages are sent.

    Did I miss out something?

    christoph

    1. I have no way to test on iOS but I added code to display the destinations to the example code. I set destNum to each destination on my system and they all worked. I tried to use Swift.readLine() to read the destination number from the command line but apparently it doesn’t work in playgrounds.

Comments are closed.