Skip to content

Commit

Permalink
updated to latest PGMidi
Browse files Browse the repository at this point in the history
  • Loading branch information
danomatika committed Jan 19, 2015
1 parent 0860587 commit 9d5ec8a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 42 deletions.
5 changes: 4 additions & 1 deletion libs/pgmidi/PGArc.h
Expand Up @@ -33,7 +33,10 @@
inline
OBJC_TYPE *arc_cast(SOURCE_TYPE *source)
{
return (__bridge OBJC_TYPE*)source;
@autoreleasepool
{
return (__bridge OBJC_TYPE*)source;
}
}

#define PG_RELEASE(a) a = nil;
Expand Down
9 changes: 8 additions & 1 deletion libs/pgmidi/PGMidi.h
Expand Up @@ -3,7 +3,12 @@
// PGMidi
//

#import <UIKit/UIKit.h>
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#import <Foundation/Foundation.h>
#endif

#import <CoreMIDI/CoreMIDI.h>

#import "PGArc.h"
Expand Down Expand Up @@ -130,6 +135,8 @@ extern NSString * const PGMidiConnectionKey;
@property (nonatomic,readonly) PGMidiDestination *virtualSourceDestination;
@property (nonatomic,retain) NSString *virtualEndpointName;
@property (nonatomic,assign) BOOL networkEnabled;

/// Remember to set the UIBackgroundModes plist property for virtual sources to work
@property (nonatomic,assign) BOOL virtualSourceEnabled;
@property (nonatomic,assign) BOOL virtualDestinationEnabled;

Expand Down
57 changes: 34 additions & 23 deletions libs/pgmidi/PGMidi.mm
Expand Up @@ -7,8 +7,9 @@
#import "PGArc.h"
#import <mach/mach_time.h>

// For some reason, this is nut pulled in by the umbrella header
#import <CoreMIDI/MIDINetworkSession.h>
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#import <CoreMIDI/MIDINetworkSession.h>
#endif

/// A helper that NSLogs an error message if "c" is an error code
#define NSLogError(c,str) do{if (c) NSLog(@"Error (%@): %ld:%@", str, (long)c,[NSError errorWithDomain:NSMachErrorDomain code:c userInfo:nil]);}while(false)
Expand Down Expand Up @@ -94,7 +95,7 @@ - (id) initWithMidi:(PGMidi*)m endpoint:(MIDIEndpointRef)e
//==============================================================================

@interface PGMidiSource ()
@property (strong, readwrite) NSArray *delegates;
@property (strong, nonatomic, readwrite) NSArray *delegates;
@end

@implementation PGMidiSource
Expand Down Expand Up @@ -145,16 +146,22 @@ - (void) midiRead:(const MIDIPacketList *)pktlist
static
void PGMIDIReadProc(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon)
{
PGMidiSource *self = arc_cast<PGMidiSource>(srcConnRefCon);
[self midiRead:pktlist];
@autoreleasepool
{
PGMidiSource *self = arc_cast<PGMidiSource>(srcConnRefCon);
[self midiRead:pktlist];
}
}

static
void PGMIDIVirtualDestinationReadProc(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon)
{
PGMidi *midi = (__bridge PGMidi*)readProcRefCon;
PGMidiSource *self = midi.virtualDestinationSource;
[self midiRead:pktlist];
@autoreleasepool
{
PGMidi *midi = (__bridge PGMidi*)readProcRefCon;
PGMidiSource *self = midi.virtualDestinationSource;
[self midiRead:pktlist];
}
}

@end
Expand Down Expand Up @@ -236,7 +243,11 @@ @implementation PGMidi

+ (BOOL)midiAvailable
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
return [[[UIDevice currentDevice] systemVersion] floatValue] >= 4.2;
#else
return YES;
#endif
}

- (id) init
Expand Down Expand Up @@ -307,16 +318,24 @@ - (MIDIPortRef) outputPort
return outputPort;
}

-(BOOL)networkEnabled
-(BOOL)networkEnabled
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
return [MIDINetworkSession defaultSession].enabled;
#else
return NO;
#endif
}

-(void)setNetworkEnabled:(BOOL)networkEnabled
-(void)setNetworkEnabled:(BOOL)networkEnabled
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
MIDINetworkSession* session = [MIDINetworkSession defaultSession];
session.enabled = networkEnabled;
session.connectionPolicy = MIDINetworkConnectionPolicy_Anyone;
#else
NSLog(@"MIDINetworkSession not available on Mac OS X");
#endif
}

-(BOOL)virtualSourceEnabled
Expand All @@ -330,11 +349,7 @@ -(void)setVirtualSourceEnabled:(BOOL)virtualSourceEnabled

if (virtualSourceEnabled)
{
NSString *name
= virtualEndpointName
? virtualEndpointName
: [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString*)kCFBundleNameKey];
OSStatus s = MIDISourceCreate(client, (__bridge CFStringRef)name, &virtualSourceEndpoint);
OSStatus s = MIDISourceCreate(client, (__bridge CFStringRef)@"MidiMonitor Source", &virtualSourceEndpoint);
NSLogError(s, @"Create MIDI virtual source");
if (s) return;

Expand All @@ -359,7 +374,7 @@ -(void)setVirtualSourceEnabled:(BOOL)virtualSourceEnabled
PG_RELEASE(virtualSourceDestination);
OSStatus s = MIDIEndpointDispose(virtualSourceEndpoint);
NSLogError(s, @"Dispose MIDI virtual source");
virtualSourceEndpoint = NULL;
virtualSourceEndpoint = 0;
}
}

Expand All @@ -374,16 +389,12 @@ -(void)setVirtualDestinationEnabled:(BOOL)virtualDestinationEnabled

if (virtualDestinationEnabled)
{
NSString *name
= virtualEndpointName
? virtualEndpointName
: [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString*)kCFBundleNameKey];
OSStatus s = MIDIDestinationCreate(client, (__bridge CFStringRef)name, PGMIDIVirtualDestinationReadProc, (__bridge void*)self, &virtualDestinationEndpoint);
OSStatus s = MIDIDestinationCreate(client, (__bridge CFStringRef)@"MidiMonitor Destination", PGMIDIVirtualDestinationReadProc, (__bridge void*)self, &virtualDestinationEndpoint);
NSLogError(s, @"Create MIDI virtual destination");
if (s) return;

// Attempt to use saved unique ID
SInt32 uniqueID = [[NSUserDefaults standardUserDefaults] integerForKey:@"PGMIDI Saved Virtual Destination ID"];
SInt32 uniqueID = (SInt32)[[NSUserDefaults standardUserDefaults] integerForKey:@"PGMIDI Saved Virtual Destination ID"];
if (uniqueID)
{
s = MIDIObjectSetIntegerProperty(virtualDestinationEndpoint, kMIDIPropertyUniqueID, uniqueID);
Expand All @@ -407,7 +418,7 @@ -(void)setVirtualDestinationEnabled:(BOOL)virtualDestinationEnabled

[delegate midi:self sourceAdded:virtualDestinationSource];
[[NSNotificationCenter defaultCenter] postNotificationName:PGMidiSourceAddedNotification
object:self
object:self
userInfo:[NSDictionary dictionaryWithObject:virtualDestinationSource
forKey:PGMidiConnectionKey]];
}
Expand Down
18 changes: 1 addition & 17 deletions libs/pgmidi/README.md
Expand Up @@ -40,20 +40,4 @@ Feel free to incorporate this code in your own applications.

I'd appreciate hearing from you if you do so. It's nice to know that I've been helpful. Attribution is welcomed, but not required.

Copyright (c) 2010-2011 Pete Goodliffe. All rights reserved.


Release Notes
-------------

* Latest
Take the HEAD of this repo. It contains the latest and greatest stuff;
the library is always under development.
* Build 3 (2011-02-04)
Updated the PGMidi API to make it a lot more featured.
* Build 2 (2010-11-02)
Added MIDI output to the project.
* Build 1 (2010-10-14)
Initial version


Copyright (c) 2010-2015 Pete Goodliffe. All rights reserved.

0 comments on commit 9d5ec8a

Please sign in to comment.