Wednesday, 5 January 2011

More iOS MIDI (using networked MIDI connections)

My iOS MIDI I/O example code has been incredibly popular. It's great to know that many music apps out there have incorporated my code to get their MIDI interfacing working.

Thanks for all the kind feedback.

I have been updating the MIDI code with a few structural tidy-ups, and an often requested feature - the ability to use networked MIDI sessions (connections).

Doing it manually: Using networked MIDI connections

iOS provides some a neat networked MIDI connection facility. However, despite being easy to set up, network MIDI sessions aren't initially obvious.

First, you must include:
#import <CoreMIDI/MIDINetworkSession.h>
Because network sessions are not included in the umbrella CoreMIDI.h include file. Sigh.

Next you must establish a network session by winking the (evil) singleton instance into existence:
MIDINetworkSession* session = [MIDINetworkSession defaultSession];
Then, merely turn it on:
session.enabled = YES;
session.connectionPolicy = MIDINetworkConnectionPolicy_Anyone;
Yes, that is all.

Documentation for MIDINetworkSession is available here.

The easy way: using PGMidi

If you're using my MIDI classes, then you can just call the PGMidi class' enableNetwork: method with YES or NO.

Get the code

The latest version of my MIDI interface code, and a sample project that shows it in action is available from the GitHub project page.

Feedback, as ever, is welcomed.


giku said...

Great post, one question on timing when sending MIDI out.

I can't get it precise enough, even when I use mach_wait_until (exact time periods) in thread with high priority I get random delays up to 0.02 s. It's unacceptable....

Pete Goodliffe said...

The API I provide you with is clearly simplified.

If you want to write a sequencer-like app you'll really have to move down to CoreMIDI itself.

In that API you associate a MIDITimeStamp with each MIDIPacket you send that determines exactly when to send the data. This is the host clock time as returned by mach_absolute_time.

Pete Goodliffe said...

Please note that I have updated this post as the location of the PGMidi repo has changed (by popular request) to GitHub:

Andrew said...

Hi Pete!

I am hoping to set up a MIDINetworkSession to link my iOS device with my laptop to send 'Play' and 'Pause' messages to Logic Pro. Any tips?