forked from mixedinkey-opensource/MIKMIDI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIKMIDISynthesizer.m
551 lines (455 loc) · 19.1 KB
/
MIKMIDISynthesizer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//
// MIKMIDISynthesizer.m
//
//
// Created by Andrew Madsen on 2/19/15.
//
//
#import "MIKMIDISynthesizer.h"
#import "MIKMIDICommand.h"
#import "MIKMIDISynthesizer_SubclassMethods.h"
#import "MIKMIDIErrors.h"
#import "MIKMIDIClock.h"
@interface MIKMIDISynthesizer ()
{
CFMutableDictionaryRef _scheduledCommandsByTimeStamp;
CFMutableSetRef _scheduledCommandTimeStampsSet;
CFMutableArrayRef _scheduledCommandTimeStampsArray;
dispatch_queue_t _scheduledCommandQueue;
}
@end
@implementation MIKMIDISynthesizer
- (instancetype)init
{
return [self initWithAudioUnitDescription:[[self class] appleSynthComponentDescription]];
}
- (instancetype)initWithAudioUnitDescription:(AudioComponentDescription)componentDescription
{
self = [super init];
if (self) {
_componentDescription = componentDescription;
if (![self setupAUGraph]) return nil;
self.sendMIDICommand = ^(MIKMIDISynthesizer *synth, MusicDeviceComponent inUnit, UInt32 inStatus, UInt32 inData1, UInt32 inData2, UInt32 inOffsetSampleFrame) {
return MusicDeviceMIDIEvent(inUnit, inStatus, inData1, inData2, inOffsetSampleFrame);
};
}
return self;
}
- (void)dealloc
{
self.instrumentUnit = NULL;
self.graph = NULL;
if (_scheduledCommandsByTimeStamp) {
CFRelease(_scheduledCommandsByTimeStamp);
_scheduledCommandsByTimeStamp = NULL;
}
if (_scheduledCommandTimeStampsSet) {
CFRelease(_scheduledCommandTimeStampsSet);
_scheduledCommandTimeStampsSet = NULL;
}
if (_scheduledCommandTimeStampsArray) {
CFRelease(_scheduledCommandTimeStampsArray);
_scheduledCommandTimeStampsArray = NULL;
}
}
#pragma mark - Public
- (NSArray *)availableInstruments
{
#if TARGET_OS_IPHONE
return @[];
#else
AudioUnit audioUnit = [self instrumentUnit];
NSMutableArray *result = [NSMutableArray array];
UInt32 instrumentCount;
UInt32 instrumentCountSize = sizeof(instrumentCount);
OSStatus err = AudioUnitGetProperty(audioUnit, kMusicDeviceProperty_InstrumentCount, kAudioUnitScope_Global, 0, &instrumentCount, &instrumentCountSize);
if (err) {
NSLog(@"AudioUnitGetProperty() (Instrument Count) failed with error %@ in %s.", @(err), __PRETTY_FUNCTION__);
return @[];
}
#if !TARGET_OS_IPHONE
if (self.componentDescription.componentSubType == kAudioUnitSubType_DLSSynth) {
for (UInt32 i = 0; i < instrumentCount; i++) {
MusicDeviceInstrumentID instrumentID;
UInt32 idSize = sizeof(instrumentID);
err = AudioUnitGetProperty(audioUnit, kMusicDeviceProperty_InstrumentNumber, kAudioUnitScope_Global, i, &instrumentID, &idSize);
if (err) {
NSLog(@"AudioUnitGetProperty() (Instrument Number) failed with error %@ in %s.", @(err), __PRETTY_FUNCTION__);
continue;
}
char cName[256];
UInt32 cNameSize = sizeof(cName);
OSStatus err = AudioUnitGetProperty(audioUnit, kMusicDeviceProperty_InstrumentName, kAudioUnitScope_Global, instrumentID, &cName, &cNameSize);
if (err) {
NSLog(@"AudioUnitGetProperty() failed with error %@ in %s.", @(err), __PRETTY_FUNCTION__);
return @[];
}
NSString *name = [NSString stringWithCString:cName encoding:NSASCIIStringEncoding];
MIKMIDISynthesizerInstrument *instrument = [MIKMIDISynthesizerInstrument instrumentWithID:instrumentID name:name];
if (instrument) [result addObject:instrument];
}
} else if (self.componentDescription.componentSubType == kAudioUnitSubType_MIDISynth)
#endif
{
}
return result;
#endif
}
- (BOOL)selectInstrument:(MIKMIDISynthesizerInstrument *)instrument error:(NSError **)error
{
error = error ? error : &(NSError *__autoreleasing){ nil };
if (!instrument) {
*error = [NSError MIKMIDIErrorWithCode:MIKMIDIInvalidArgumentError userInfo:nil];
return NO;
}
return [self sendBankSelectAndProgramChangeForInstrumentID:instrument.instrumentID error:error];
}
- (BOOL)loadSoundfontFromFileAtURL:(NSURL *)fileURL error:(NSError **)error
{
error = error ? error : &(NSError *__autoreleasing){ nil };
OSStatus err = noErr;
if (self.componentDescription.componentSubType == kAudioUnitSubType_Sampler) {
// fill out a bank preset data structure
NSDictionary *typesByFileExtension = @{@"sf2" : @(kInstrumentType_SF2Preset),
@"dls" : @(kInstrumentType_DLSPreset),
@"aupreset" : @(kInstrumentType_AUPreset)};
AUSamplerInstrumentData instrumentData;
instrumentData.fileURL = (__bridge CFURLRef)fileURL;
instrumentData.instrumentType = [typesByFileExtension[[fileURL pathExtension]] intValue];
instrumentData.bankMSB = kAUSampler_DefaultMelodicBankMSB;
instrumentData.bankLSB = kAUSampler_DefaultBankLSB;
instrumentData.presetID = 0;
// set the kAUSamplerProperty_LoadPresetFromBank property
err = AudioUnitSetProperty(self.instrumentUnit,
kAUSamplerProperty_LoadInstrument,
kAudioUnitScope_Global,
0,
&instrumentData,
sizeof(instrumentData));
if (err != noErr) {
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
return NO;
}
return YES;
} else {
#if TARGET_OS_IPHONE
return NO;
}
#else
FSRef fsRef;
err = FSPathMakeRef((const UInt8*)[[fileURL path] cStringUsingEncoding:NSUTF8StringEncoding], &fsRef, 0);
if (err != noErr) {
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
return NO;
}
err = AudioUnitSetProperty(self.instrumentUnit,
kMusicDeviceProperty_SoundBankFSRef,
kAudioUnitScope_Global, 0,
&fsRef, sizeof(fsRef));
if (err != noErr) {
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
return NO;
}
return YES;
}
#endif
}
#pragma mark - Private
- (BOOL)sendBankSelectAndProgramChangeForInstrumentID:(MusicDeviceInstrumentID)instrumentID error:(NSError **)error
{
error = error ?: &(NSError *__autoreleasing){ nil };
for (UInt8 channel = 0; channel < 16; channel++) {
// http://lists.apple.com/archives/coreaudio-api/2002/Sep/msg00015.html
CFURLRef loadedSoundfontURL = NULL;
UInt32 size = sizeof(loadedSoundfontURL);
OSStatus err = AudioUnitGetProperty(self.instrumentUnit,
kMusicDeviceProperty_SoundBankURL,
kAudioUnitScope_Global,
0,
&loadedSoundfontURL,
&size);
if (err && err != kAudioUnitErr_InvalidProperty) {
NSLog(@"AudioUnitGetProperty() (kMusicDeviceProperty_SoundBankURL) failed with error %@ in %s.", @(err), __PRETTY_FUNCTION__);
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil];
return NO;
}
if (loadedSoundfontURL || [self isUsingAppleDLSSynth]) {
UInt32 bankSelectStatus = 0xB0 | channel;
UInt8 bankSelectMSB = (instrumentID >> 16) & 0x7F;
err = _sendMIDICommand(self, self.instrumentUnit, bankSelectStatus, 0x00, bankSelectMSB, 0);
if (err) {
NSLog(@"MusicDeviceMIDIEvent() (MSB Bank Select) failed with error %@ in %s.", @(err), __PRETTY_FUNCTION__);
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil];
return NO;
}
UInt8 bankSelectLSB = (instrumentID >> 8) & 0x7F;
err = _sendMIDICommand(self, self.instrumentUnit, bankSelectStatus, 0x20, bankSelectLSB, 0);
if (err) {
NSLog(@"MusicDeviceMIDIEvent() (LSB Bank Select) failed with error %@ in %s.", @(err), __PRETTY_FUNCTION__);
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil];
return NO;
}
}
UInt32 programChangeStatus = 0xC0 | channel;
UInt8 programChange = instrumentID & 0x7F;
err = _sendMIDICommand(self, self.instrumentUnit, programChangeStatus, programChange, 0, 0);
if (err) {
NSLog(@"MusicDeviceMIDIEvent() (Program Change) failed with error %@ in %s.", @(err), __PRETTY_FUNCTION__);
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil];
return NO;
}
}
return YES;
}
#pragma mark Audio Graph
- (BOOL)setupAUGraph
{
AUGraph graph;
OSStatus err = 0;
if ((err = NewAUGraph(&graph))) {
NSLog(@"Unable to create AU graph: %@", @(err));
return NO;
}
AudioComponentDescription outputcd = {0};
outputcd.componentType = kAudioUnitType_Output;
#if TARGET_OS_IPHONE
outputcd.componentSubType = kAudioUnitSubType_RemoteIO;
#else
outputcd.componentSubType = kAudioUnitSubType_DefaultOutput;
#endif
outputcd.componentManufacturer = kAudioUnitManufacturer_Apple;
AUNode outputNode;
if ((err = AUGraphAddNode(graph, &outputcd, &outputNode))) {
NSLog(@"Unable to add ouptput node to graph: %@", @(err));
return NO;
}
AudioComponentDescription instrumentcd = self.componentDescription;
AUNode instrumentNode;
if ((err = AUGraphAddNode(graph, &instrumentcd, &instrumentNode))) {
NSLog(@"Unable to add instrument node to AU graph: %@", @(err));
return NO;
}
if ((err = AUGraphOpen(graph))) {
NSLog(@"Unable to open AU graph: %@", @(err));
return NO;
}
AudioUnit instrumentUnit;
if ((err = AUGraphNodeInfo(graph, instrumentNode, NULL, &instrumentUnit))) {
NSLog(@"Unable to get instrument AU unit: %@", @(err));
return NO;
}
if ((err = AUGraphConnectNodeInput(graph, instrumentNode, 0, outputNode, 0))) {
NSLog(@"Unable to connect instrument to output: %@", @(err));
return NO;
}
if ((err = AUGraphInitialize(graph))) {
NSLog(@"Unable to initialize AU graph: %@", @(err));
return NO;
}
#if !TARGET_OS_IPHONE
// Turn down reverb which is way too high by default
if (instrumentcd.componentSubType == kAudioUnitSubType_DLSSynth) {
if ((err = AudioUnitSetParameter(instrumentUnit, kMusicDeviceParam_ReverbVolume, kAudioUnitScope_Global, 0, -120, 0))) {
NSLog(@"Unable to set reverb level to -120: %@", @(err));
}
}
#endif
if ((err = AUGraphStart(graph))) {
NSLog(@"Unable to start AU graph: %@", @(err));
return NO;
}
self.graph = graph;
self.instrumentUnit = instrumentUnit;
return YES;
}
#pragma mark - Instruments
- (BOOL)isUsingAppleSynth
{
AudioComponentDescription description = self.componentDescription;
AudioComponentDescription appleSynthDescription = [[self class] appleSynthComponentDescription];
if (description.componentManufacturer != appleSynthDescription.componentManufacturer) return NO;
if (description.componentType != appleSynthDescription.componentType) return NO;
if (description.componentSubType != appleSynthDescription.componentSubType) return NO;
return YES;
}
- (BOOL)isUsingAppleDLSSynth
{
AudioComponentDescription appleCD = [[self class] appleSynthComponentDescription];
AudioComponentDescription description = self.componentDescription;
if (description.componentManufacturer != appleCD.componentManufacturer) return NO;
if (description.componentType != appleCD.componentType) return NO;
if (description.componentSubType != appleCD.componentSubType) return NO;
return YES;
}
+ (AudioComponentDescription)appleSynthComponentDescription
{
AudioComponentDescription instrumentcd = (AudioComponentDescription){0};
instrumentcd.componentManufacturer = kAudioUnitManufacturer_Apple;
instrumentcd.componentType = kAudioUnitType_MusicDevice;
#if TARGET_OS_IPHONE
instrumentcd.componentSubType = kAudioUnitSubType_Sampler;
#else
instrumentcd.componentSubType = kAudioUnitSubType_DLSSynth;
#endif
return instrumentcd;
}
#pragma mark - MIKMIDICommandScheduler
- (void)scheduleMIDICommands:(NSArray *)commands
{
dispatch_queue_t queue = _scheduledCommandQueue;
if (!queue) {
NSString *queueLabel = [[[NSBundle mainBundle] bundleIdentifier] stringByAppendingFormat:@".%@.%p", [self class], self];
dispatch_queue_attr_t attr = DISPATCH_QUEUE_SERIAL;
#if defined (__MAC_10_10) || defined (__IPHONE_8_0)
if (&dispatch_queue_attr_make_with_qos_class != NULL) {
// See https://github.com/mixedinkey-opensource/MIKMIDI/issues/204
//attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, DISPATCH_QUEUE_PRIORITY_HIGH);
attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0);
}
#endif
queue = dispatch_queue_create(queueLabel.UTF8String, attr);
_scheduledCommandQueue = queue;
}
for (MIKMIDICommand *command in commands) {
dispatch_sync(queue, ^{
NSUInteger count = commands.count;
if (!_scheduledCommandsByTimeStamp) {
_scheduledCommandsByTimeStamp = CFDictionaryCreateMutable(NULL, count, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
if (!_scheduledCommandTimeStampsSet) {
_scheduledCommandTimeStampsSet = CFSetCreateMutable(NULL, count, &kCFTypeSetCallBacks);
}
if (!_scheduledCommandTimeStampsArray) {
_scheduledCommandTimeStampsArray = CFArrayCreateMutable(NULL, count, &kCFTypeArrayCallBacks);
}
MIDITimeStamp timeStamp = command.midiTimestamp;
void *timeStampNumber = (__bridge void*)@(timeStamp);
CFMutableArrayRef commandsAtTimeStamp = (CFMutableArrayRef)CFDictionaryGetValue(_scheduledCommandsByTimeStamp, timeStampNumber);
if (!commandsAtTimeStamp) {
commandsAtTimeStamp = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
CFDictionarySetValue(_scheduledCommandsByTimeStamp, timeStampNumber, (void *)commandsAtTimeStamp);
CFRelease(commandsAtTimeStamp);
if (!CFSetContainsValue(_scheduledCommandTimeStampsSet, timeStampNumber)) {
CFSetAddValue(_scheduledCommandTimeStampsSet, timeStampNumber);
CFArrayAppendValue(_scheduledCommandTimeStampsArray, timeStampNumber);
}
}
CFArrayAppendValue(commandsAtTimeStamp, (__bridge void *)command);
});
}
}
#pragma mark - Callbacks
OSStatus MIKMIDISynthesizerScheduleUpcomingMIDICommands(MIKMIDISynthesizer *synth, AudioUnit instrumentUnit, UInt32 inNumberFrames, Float64 sampleRate, const AudioTimeStamp *inTimeStamp)
{
dispatch_queue_t queue = synth->_scheduledCommandQueue;
if (!queue) return noErr; // no commands have been scheduled with this synth
static NSTimeInterval lastTimeUntilNextCallback = 0;
static MIDITimeStamp lastMIDITimeStampsUntilNextCallback = 0;
NSTimeInterval timeUntilNextCallback = inNumberFrames / sampleRate;
MIDITimeStamp midiTimeStampsUntilNextCallback = lastMIDITimeStampsUntilNextCallback;
if (lastTimeUntilNextCallback != timeUntilNextCallback) {
midiTimeStampsUntilNextCallback = MIKMIDIClockMIDITimeStampsPerTimeInterval(timeUntilNextCallback);
lastTimeUntilNextCallback = timeUntilNextCallback;
lastMIDITimeStampsUntilNextCallback = midiTimeStampsUntilNextCallback;
}
MIDITimeStamp toTimeStamp = inTimeStamp->mHostTime + midiTimeStampsUntilNextCallback;
CFMutableArrayRef commandsToSend = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);;
dispatch_sync(queue, ^{
CFMutableDictionaryRef commandsByTimeStamp = synth->_scheduledCommandsByTimeStamp;
if (!commandsByTimeStamp || !CFDictionaryGetCount(commandsByTimeStamp)) return;
CFMutableSetRef commandTimeStampsSet = synth->_scheduledCommandTimeStampsSet;
CFMutableArrayRef commandTimeStampsArray = synth->_scheduledCommandTimeStampsArray;
if (!commandTimeStampsSet || !commandTimeStampsArray) return;
CFArrayRef commandTimeStampsArrayCopy = CFArrayCreateCopy(NULL, commandTimeStampsArray);
CFIndex count = CFArrayGetCount(commandTimeStampsArrayCopy);
for (CFIndex i = 0; i < count; i++) {
NSNumber *timeStampNumber = (__bridge NSNumber *)CFArrayGetValueAtIndex(commandTimeStampsArrayCopy, i);
MIDITimeStamp timeStamp = timeStampNumber.unsignedLongLongValue;
if (timeStamp >= toTimeStamp) break;
CFMutableArrayRef commandsAtTimeStamp = (CFMutableArrayRef)CFDictionaryGetValue(commandsByTimeStamp, (__bridge void*)timeStampNumber);
CFArrayAppendArray(commandsToSend, commandsAtTimeStamp, CFRangeMake(0, CFArrayGetCount(commandsAtTimeStamp)));
CFDictionaryRemoveValue(commandsByTimeStamp, (__bridge void *)timeStampNumber);
CFSetRemoveValue(commandTimeStampsSet, (__bridge void*)timeStampNumber);
CFArrayRemoveValueAtIndex(commandTimeStampsArray, 0);
}
CFRelease(commandTimeStampsArrayCopy);
});
NSTimeInterval secondsPerMIDITimeStamp = MIKMIDIClockSecondsPerMIDITimeStamp();
CFIndex commandCount = CFArrayGetCount(commandsToSend);
for (CFIndex i = 0; i < commandCount; i++) {
MIKMIDICommand *command = (__bridge MIKMIDICommand *)CFArrayGetValueAtIndex(commandsToSend, i);
MIDITimeStamp sendTimeStamp = command.midiTimestamp;
if (sendTimeStamp < inTimeStamp->mHostTime) sendTimeStamp = inTimeStamp->mHostTime;
MIDITimeStamp timeStampOffset = sendTimeStamp - inTimeStamp->mHostTime;
Float64 sampleOffset = secondsPerMIDITimeStamp * timeStampOffset * sampleRate;
OSStatus err = synth->_sendMIDICommand(synth, instrumentUnit, command.statusByte, command.dataByte1, command.dataByte2, sampleOffset);
if (err) {
NSLog(@"Unable to schedule MIDI command %@ for instrument unit %p: %@", command, instrumentUnit, @(err));
return err;
}
}
CFRelease(commandsToSend);
return noErr;
}
static OSStatus MIKMIDISynthesizerInstrumentUnitRenderCallback(void * inRefCon,
AudioUnitRenderActionFlags * ioActionFlags,
const AudioTimeStamp * inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData)
{
if (*ioActionFlags & kAudioUnitRenderAction_PreRender) {
if (!(inTimeStamp->mFlags & kAudioTimeStampHostTimeValid)) return noErr;
if (!(inTimeStamp->mFlags & kAudioTimeStampSampleTimeValid)) return noErr;
MIKMIDISynthesizer *synth = (__bridge MIKMIDISynthesizer *)inRefCon;
AudioUnit instrumentUnit = synth.instrumentUnit;
AudioStreamBasicDescription LPCMASBD;
UInt32 sizeOfLPCMASBD = sizeof(LPCMASBD);
OSStatus err = AudioUnitGetProperty(instrumentUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &LPCMASBD, &sizeOfLPCMASBD);
if (err) {
NSLog(@"Unable to get stream description for instrument unit %p: %@", instrumentUnit, @(err));
return err;
}
return MIKMIDISynthesizerScheduleUpcomingMIDICommands(synth, instrumentUnit, inNumberFrames, LPCMASBD.mSampleRate, inTimeStamp);
}
return noErr;
}
#pragma mark - Properties
- (void)setGraph:(AUGraph)graph
{
if (graph != _graph) {
if (_graph) DisposeAUGraph(_graph);
_graph = graph;
}
}
- (void)handleMIDIMessages:(NSArray *)commands
{
for (MIKMIDICommand *command in commands) {
OSStatus err = _sendMIDICommand(self, self.instrumentUnit, command.statusByte, command.dataByte1, command.dataByte2, 0);
if (err) NSLog(@"Unable to send MIDI command to synthesizer %@: %@", command, @(err));
}
}
- (void)setInstrumentUnit:(AudioUnit)instrumentUnit
{
if (_instrumentUnit != instrumentUnit) {
OSStatus err;
if (_instrumentUnit) {
err = AudioUnitRemoveRenderNotify(_instrumentUnit, MIKMIDISynthesizerInstrumentUnitRenderCallback, (__bridge void *)self);
if (err) NSLog(@"Unable to remove render notify from instrument unit %p: %@", _instrumentUnit, @(err));
}
_instrumentUnit = instrumentUnit;
if (_instrumentUnit) {
err = AudioUnitAddRenderNotify(_instrumentUnit, MIKMIDISynthesizerInstrumentUnitRenderCallback, (__bridge void *)self);
if (err) NSLog(@"Unable to add render notify to instrument unit %p: %@", _instrumentUnit, @(err));
}
}
}
#pragma mark - Deprecated
+ (NSSet *)keyPathsForValuesAffectingInstrument { return [NSSet setWithObjects:@"instrumentUnit", nil]; }
- (AudioUnit)instrument { return self.instrumentUnit; }
- (void)setInstrument:(AudioUnit)instrument { self.instrumentUnit = instrument; }
- (BOOL)selectInstrument:(MIKMIDISynthesizerInstrument *)instrument
{
return [self selectInstrument:instrument error:NULL];
}
@end