-
Notifications
You must be signed in to change notification settings - Fork 139
/
BXExternalMIDIDevice.m
422 lines (347 loc) · 12.3 KB
/
BXExternalMIDIDevice.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
/*
Boxer is copyright 2011 Alun Bestor and contributors.
Boxer is released under the GNU General Public License 2.0. A full copy of this license can be
found in this XCode project at Resources/English.lproj/BoxerHelp/pages/legalese.html, or read
online at [http://www.gnu.org/licenses/gpl-2.0.txt].
*/
#import "BXExternalMIDIDevice.h"
#import "BXExternalMIDIDevice+BXGeneralMIDISysexes.h"
#pragma mark -
#pragma mark Private method declarations
@interface BXExternalMIDIDevice ()
- (BOOL) _connectToDestination: (MIDIEndpointRef)destination
error: (NSError **)outError;
- (BOOL) _connectToDestinationAtIndex: (ItemCount)index
error: (NSError **)outError;
- (BOOL) _connectToDestinationAtUniqueID: (MIDIUniqueID)uniqueID
error: (NSError **)outError;
//The callback for our volume synchronization timer.
//Calls syncVolume and invalidates the timer.
- (void) _performVolumeSync: (NSTimer *)timer;
@end
#pragma mark -
#pragma mark Implementation
@implementation BXExternalMIDIDevice
@synthesize dateWhenReady = _dateWhenReady;
@synthesize destination = _destination;
@synthesize volume = _volume;
@synthesize requestedVolume = _requestedVolume;
#pragma mark -
#pragma mark Class helper methods
+ (NSString *) defaultClientName
{
return NSLocalizedString(@"Boxer",
@"Descriptive name for Boxer’s MIDI client when communicating with external MIDI devices.");
}
+ (NSString *) defaultPortName
{
return NSLocalizedString(@"Boxer MIDI Out",
@"Descriptive name for Boxer’s MIDI output port when communicating with external MIDI devices.");
}
#pragma mark -
#pragma mark Initialization and cleanup
- (id <BXMIDIDevice>) init
{
if ((self = [super init]))
{
//Don't use setVolume:, as it will try to send a message.
_volume = 1.0f;
_requestedVolume = 1.0f;
self.dateWhenReady = [NSDate distantPast];
_secondsPerByte = BXExternalMIDIDeviceDefaultSysexRate;
}
return self;
}
- (id <BXMIDIDevice>) initWithDestination: (MIDIEndpointRef)destination
error: (NSError **)outError
{
if ((self = [self init]))
{
BOOL succeeded = [self _connectToDestination: destination error: outError];
if (!succeeded)
{
[self release];
self = nil;
}
}
return self;
}
- (id <BXMIDIDevice>) initWithDestinationAtIndex: (ItemCount)destIndex
error: (NSError **)outError
{
if ((self = [self init]))
{
BOOL succeeded = [self _connectToDestinationAtIndex: destIndex
error: outError];
if (!succeeded)
{
[self release];
self = nil;
}
}
return self;
}
- (id <BXMIDIDevice>) initWithDestinationAtUniqueID: (MIDIUniqueID)uniqueID
error: (NSError **)outError
{
if ((self = [self init]))
{
BOOL succeeded = [self _connectToDestinationAtUniqueID: uniqueID
error: outError];
if (!succeeded)
{
[self release];
self = nil;
}
}
return self;
}
- (void) dealloc
{
[self close];
self.dateWhenReady = nil;
[super dealloc];
}
- (void) close
{
if (_port)
{
//Ensure the device stops playing notes when closing
[self pause];
MIDIPortDispose(_port);
_port = NULL;
}
if (_client)
{
MIDIClientDispose(_client);
_client = NULL;
}
//This does not need disposing, because we did not create it ourselves
_destination = NULL;
}
- (BOOL) _connectToDestination: (MIDIEndpointRef)destination
error: (NSError **)outError
{
//Create a MIDI client and port
OSStatus errCode = MIDIClientCreate((CFStringRef)[self.class defaultClientName], NULL, NULL, &_client);
if (errCode == noErr)
{
errCode = MIDIOutputPortCreate(_client, (CFStringRef)[self.class defaultPortName], &_port);
}
if (errCode != noErr)
{
if (outError)
{
*outError = [NSError errorWithDomain: NSOSStatusErrorDomain
code: errCode
userInfo: nil];
}
if (_client)
{
MIDIClientDispose(_client);
_client = NULL;
}
return NO;
}
_destination = destination;
//Determine the speed at which we should send to this destination
SInt32 maxSysexSpeed = 0;
errCode = MIDIObjectGetIntegerProperty(destination, kMIDIPropertyMaxSysExSpeed, &maxSysexSpeed);
if (errCode == noErr)
{
_secondsPerByte = 1.0f / (NSTimeInterval)maxSysexSpeed;
}
return YES;
}
- (BOOL) _connectToDestinationAtIndex: (ItemCount)destIndex
error: (NSError **)outError
{
ItemCount numDestinations = MIDIGetNumberOfDestinations();
MIDIEndpointRef destination = NULL;
if (destIndex < numDestinations)
destination = MIDIGetDestination(destIndex);
if (!destination)
{
if (outError)
{
//Paraphrasing a little, but this error code seems appropriate.
*outError = [NSError errorWithDomain: NSOSStatusErrorDomain
code: kMIDIObjectNotFound
userInfo: nil];
}
return NO;
}
else return [self _connectToDestination: destination error: outError];
}
- (BOOL) _connectToDestinationAtUniqueID: (MIDIUniqueID)uniqueID
error: (NSError **)outError
{
MIDIEndpointRef destination;
MIDIObjectType type;
OSStatus errCode = MIDIObjectFindByUniqueID(uniqueID, (MIDIObjectRef)&destination, &type);
if (errCode == noErr && type != kMIDIObjectType_Destination)
errCode = kMIDIObjectNotFound;
if (errCode != noErr)
{
if (outError)
{
*outError = [NSError errorWithDomain: NSOSStatusErrorDomain
code: errCode
userInfo: nil];
}
return NO;
}
else return [self _connectToDestination: destination error: outError];
}
#pragma mark -
#pragma mark MIDI processing and status
- (NSTimeInterval) processingDelayForSysex: (NSData *)sysex
{
return _secondsPerByte * sysex.length;
}
- (BOOL) supportsMT32Music
{
//Technically we don't know, so this is a 'maybe'.
return YES;
}
- (BOOL) supportsGeneralMIDIMusic
{
//Technically we don't know, so this is a 'maybe'.
return YES;
}
- (BOOL) isProcessing
{
//We're still processing if our readiness date is in the future
return self.dateWhenReady.timeIntervalSinceNow > 0;
}
- (void) handleMessage: (NSData *)message
{
NSAssert(_port && _destination, @"handleMessage: called before successful initialization.");
NSAssert(message.length > 0, @"0-length message received by handleMessage:");
UInt8 buffer[sizeof(MIDIPacketList)];
MIDIPacketList *packetList = (MIDIPacketList *)buffer;
MIDIPacket *currentPacket = MIDIPacketListInit(packetList);
MIDIPacketListAdd(packetList, sizeof(buffer), currentPacket, (MIDITimeStamp)0, message.length, (UInt8 *)message.bytes);
MIDISend(_port, _destination, packetList);
}
- (void) handleSysex: (NSData *)message
{
//Sniff the sysex to see if it's a request to set the master volume.
//If so, capture the volume and substitute our own sysex containing our scaled volume.
float requestedVolume;
if ([[self class] isMasterVolumeSysex: message withVolume: &requestedVolume])
{
#if BOXER_DEBUG
NSLog(@"Program attempted to set master volume of %f", requestedVolume);
#endif
self.requestedVolume = requestedVolume;
[self syncVolume];
}
else
{
[self dispatchSysex: message];
//Sniff the sysex to see if it's a message that would reset the device's master volume.
//If so, reapply our own volume after it.
if ([[self class] sysexResetsMasterVolume: message])
{
#if BOXER_DEBUG
NSLog(@"Program attempted to reset master volume with sysex: %@", message);
#endif
self.requestedVolume = 1.0f;
[self scheduleVolumeSync];
}
}
}
- (void) dispatchSysex: (NSData *)message
{
//The same length as DOSBox's MIDI message buffer, plus padding for extra data used by the packet list.
//(Technically a sysex message could be much longer than 1024 bytes, but it would be truncated by DOSBox
//before it ever reaches us.)
#define MAX_SYSEX_PACKET_SIZE 1024 * 4
NSAssert(_port && _destination, @"handleMessage: called before successful initialization.");
NSAssert(message.length > 0, @"0-length message received by handleMessage:");
UInt8 buffer[MAX_SYSEX_PACKET_SIZE];
MIDIPacketList *packetList = (MIDIPacketList *)buffer;
MIDIPacket *currentPacket = MIDIPacketListInit(packetList);
MIDIPacketListAdd(packetList, sizeof(buffer), currentPacket, (MIDITimeStamp)0, message.length, (UInt8 *)message.bytes);
MIDISend(_port, _destination, packetList);
//Now, calculate how long it should take the device to process all that
NSTimeInterval processingDelay = [self processingDelayForSysex: message];
if (processingDelay > 0)
self.dateWhenReady = [NSDate dateWithTimeIntervalSinceNow: processingDelay];
}
- (void) pause
{
//Send All Notes Off signals to channels 0-15 to kill any lingering notes.
//This has been tested on an MT-32 and is valid according to the MIDI spec:
//http://www.midi.org/techspecs/midimessages.php
UInt8 message[3] = {BXChannelModeChangePrefix, BXAllNotesOffMessage, 0};
NSUInteger i, numChannels = 16;
for (i = 0; i < numChannels; i++)
{
//Add the channel number to the mode-change prefix
//to get the proper mode-change message for that channel.
message[0] = BXChannelModeChangePrefix + i;
[self handleMessage: [NSData dataWithBytes: message length: 3]];
}
}
- (void) resume
{
//No resume message is needed, instead we'll resume playing once new MIDI data comes in.
}
- (void) setVolume: (float)volume
{
NSAssert(_port && _destination, @"setVolume: called before successful initialization.");
volume = MIN(1.0f, volume);
volume = MAX(0.0f, volume);
if (self.volume != volume)
{
_volume = volume;
[self scheduleVolumeSync];
}
}
- (void) setRequestedVolume: (float)volume
{
volume = MIN(1.0f, volume);
volume = MAX(0.0f, volume);
_requestedVolume = volume;
}
- (void) scheduleVolumeSync
{
//If we already have a timer in progress, don't reschedule.
if (!_volumeSyncTimer)
{
NSTimeInterval timeUntilReady = MAX(0.0, self.dateWhenReady.timeIntervalSinceNow);
NSTimeInterval syncDelay = timeUntilReady + BXVolumeSyncDelay;
//No need to retain it, since it'll be retained by the runloop until it fires
_volumeSyncTimer = [NSTimer scheduledTimerWithTimeInterval: syncDelay
target: self
selector: @selector(_performVolumeSync:)
userInfo: nil
repeats: NO];
}
}
- (void) _performVolumeSync: (NSTimer *)timer
{
//Invalidate and clear our sync timer.
[_volumeSyncTimer invalidate];
_volumeSyncTimer = nil;
//Only try to sync the volume if we're still connected.
if (_port && _destination)
{
//If we're still busy processing, then defer the sync until after another delay.
if (self.isProcessing)
[self scheduleVolumeSync];
else
[self syncVolume];
}
}
- (void) syncVolume
{
//If this method gets called manually, cancel the timer and clear it.
[_volumeSyncTimer invalidate];
_volumeSyncTimer = nil;
NSData *volumeMessage = [[self class] sysexWithMasterVolume: self.volume * self.requestedVolume];
[self dispatchSysex: volumeMessage];
}
@end