Skip to content

Instantly share code, notes, and snippets.

@bzztbomb
Created January 22, 2013 19:36
Show Gist options
  • Save bzztbomb/4597659 to your computer and use it in GitHub Desktop.
Save bzztbomb/4597659 to your computer and use it in GitHub Desktop.
Cinder + CoreMidi sketch
CinderCoreMidi.h
class CinderCoreMidi
{
public:
static void init();
static void destroy();
};
CinderCoreMidi.cpp
//
// Internal to this file pgmidi stuff
//
@interface CoreMidiDelegate <PGMidiDelegate, PGMidiSourceDelegate>
{
}
@property (strong, nonatomic) PGMidi* midi;
@end
@implementation CoreMidiDelegate
-(id) init
{
self = [super init];
if (self != nil)
{
self.midi = [[PGMidi alloc] init];
self.midi.delegate = self;
[self attachToAllExistingSources];
}
return self;
}
- (void) attachToAllExistingSources
{
for (PGMidiSource *source in self.midi.sources)
{
source.delegate = self;
}
}
- (void) midi:(PGMidi*)midi sourceAdded:(PGMidiSource *)source
{
source.delegate = self;
}
- (void) midi:(PGMidi*)midi sourceRemoved:(PGMidiSource *)source
{
}
- (void) midi:(PGMidi*)midi_param destinationAdded:(PGMidiDestination *)destination
{
}
- (void) midi:(PGMidi*)midi destinationRemoved:(PGMidiDestination *)destination
{
}
- (void) midiSource:(PGMidiSource*)midi midiReceived:(const MIDIPacketList *)packetList
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
const MIDIPacket *packet = &packetList->packet[0];
for (int i = 0; i < packetList->numPackets; ++i)
{
// Send packets to Cinder somehow, remember that this is called in another thread, so you'll
// need to make it threadsafe with Grand Central Dispatch, or pthread primitives etc..
packet = MIDIPacketNext(packet);
}
[pool release];
}
@end
//
// C++ Cinder interface
//
static CoreMidiDelegate* cm_delegate = nil;
void CinderCoreMidi::init()
{
cm_delegate = [[CoreMidiDelegate alloc] init];
[cm_delegate retain];
}
void CinderCoreMidi::destroy()
{
[cm_delegate release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment