-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathMIKMIDIClientSourceEndpoint.m
73 lines (58 loc) · 1.99 KB
/
MIKMIDIClientSourceEndpoint.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// MIKMIDIClientSourceEndpoint.m
// MIKMIDI
//
// Created by Dan Rosenstark on 2015-01-07
//
#import "MIKMIDIClientSourceEndpoint.h"
#import "MIKMIDICommand.h"
#import "MIKMIDIErrors.h"
@implementation MIKMIDIClientSourceEndpoint
+ (NSArray *)representedMIDIObjectTypes; { return @[@(kMIDIObjectType_Source)]; }
- (instancetype)initWithName:(NSString*)name error:(NSError **)error
{
error = error ?: &(NSError *__autoreleasing){ nil };
if (!name || name.length == 0) {
[NSException raise:@"Problem instantiating MIKMIDIClientSourceEndpoint" format:@"Virtual endpoint needs name"];
*error = [NSError MIKMIDIErrorWithCode:MIKMIDIInvalidArgumentError userInfo:nil];
return nil;
}
MIDIClientRef midiClient;
MIDIEndpointRef midiOut;
MIDIClientCreate((__bridge CFStringRef)name, NULL, NULL, &midiClient);
OSStatus err = MIDISourceCreate(midiClient, (__bridge CFStringRef)name, &midiOut);
if (err != noErr) {
NSLog(@"%s failed. Unable to create MIDISource.", __PRETTY_FUNCTION__);
#if TARGET_OS_IPHONE
if (err == kMIDINotPermitted) {
NSLog(@"MIKMIDI's use of some CoreMIDI functions requires that your app have the audio key in its UIBackgroundModes.\n"
"Please see https://github.com/mixedinkey-opensource/MIKMIDI/wiki/Adding-Audio-to-UIBackgroundModes");
}
#endif
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
return nil;
}
self = [super initWithObjectRef:midiOut];
if (self) {
}
return self;
}
- (BOOL)sendCommands:(NSArray *)commands error:(NSError **)error
{
if (![commands count]) return NO;
error = error ? error : &(NSError *__autoreleasing){ nil };
MIDIPacketList *packetList;
if (!MIKCreateMIDIPacketListFromCommands(&packetList, commands)) return NO;
OSStatus err = MIDIReceived(self.objectRef, packetList);
free(packetList);
if (err != noErr) {
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
return NO;
}
return YES;
}
-(void)dealloc
{
MIDIEndpointDispose(self.objectRef);
}
@end