forked from mixedinkey-opensource/MIKMIDI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIKMIDIClock.m
468 lines (376 loc) · 15.4 KB
/
MIKMIDIClock.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
//
// MIKMIDIClock.m
// MIKMIDI
//
// Created by Chris Flesner on 11/26/14.
// Copyright (c) 2014 Mixed In Key. All rights reserved.
//
#import "MIKMIDIClock.h"
#import "MIKMIDIUtilities.h"
#import <mach/mach_time.h>
#if !__has_feature(objc_arc)
#error MIKMIDIClock.m must be compiled with ARC. Either turn on ARC for the project or set the -fobjc-arc flag for MIKMIDIClock.m in the Build Phases for this target
#endif
#define kDurationToKeepHistoricalClocks 1.0
#pragma mark -
@interface MIKMIDISyncedClockProxy : NSProxy
+ (instancetype)syncedClockWithClock:(MIKMIDIClock *)masterClock;
@property (readonly, nonatomic) MIKMIDIClock *masterClock;
@end
#pragma mark -
@interface MIKMIDIClock ()
{
Float64 _currentTempo;
MIDITimeStamp _timeStampZero;
MIDITimeStamp _lastSyncedMIDITimeStamp;
MusicTimeStamp _lastSyncedMusicTimeStamp;
Float64 _musicTimeStampsPerMIDITimeStamp;
Float64 _midiTimeStampsPerMusicTimeStamp;
CFMutableDictionaryRef _historicalClocks;
CFMutableSetRef _historicalClockMIDITimeStampsSet;
CFMutableArrayRef _historicalClockMIDITimeStampsArray;
dispatch_queue_t _clockQueue;
}
@property (nonatomic, getter=isReady) BOOL ready;
@end
#pragma mark -
@implementation MIKMIDIClock
#pragma mark - Lifecycle
+ (instancetype)clock
{
return [[self alloc] init];
}
- (instancetype)init
{
return [self initWithQueue:YES];
}
- (instancetype)initWithQueue:(BOOL)createQueue
{
if (self = [super init]) {
if (createQueue) {
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
_clockQueue = dispatch_queue_create(queueLabel.UTF8String, attr);
}
}
return self;
}
- (void)dealloc
{
releaseHistoricalClocks(self);
}
#pragma mark - Queue
static void dispatchToClockQueue(MIKMIDIClock *self, void(^block)())
{
if (!block) return;
dispatch_queue_t queue = self->_clockQueue;
if (queue) {
dispatch_sync(queue, block);
} else {
block();
}
}
#pragma mark - Time Stamps
- (void)syncMusicTimeStamp:(MusicTimeStamp)musicTimeStamp withMIDITimeStamp:(MIDITimeStamp)midiTimeStamp tempo:(Float64)tempo
{
[self willChangeValueForKey:@"ready"];
dispatchToClockQueue(self, ^{
if (_lastSyncedMIDITimeStamp != 0) {
// Add a clock to the historical clocks
NSNumber *midiTimeStampNumber = @(midiTimeStamp);
if (!_historicalClocks) {
_historicalClocks = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
_historicalClockMIDITimeStampsSet = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
_historicalClockMIDITimeStampsArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
} else {
// Remove clocks old enough to not be needed anymore
MIDITimeStamp oldTimeStamp = MIKMIDIGetCurrentTimeStamp() - MIKMIDIClockMIDITimeStampsPerTimeInterval(kDurationToKeepHistoricalClocks);
CFIndex count = CFArrayGetCount(_historicalClockMIDITimeStampsArray);
CFMutableArrayRef timeStampsToRemove = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
CFMutableSetRef indexesToRemoveSet = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
CFMutableArrayRef indexesToRemoveArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
for (CFIndex i = 0; i < count; i++) {
NSNumber *timeStampNumber = (__bridge NSNumber *)CFArrayGetValueAtIndex(_historicalClockMIDITimeStampsArray, i);
MIDITimeStamp timeStamp = timeStampNumber.unsignedLongLongValue;
if (timeStamp <= oldTimeStamp) {
void *timeStampValue = (__bridge void *)timeStampNumber;
CFArrayAppendValue(timeStampsToRemove, timeStampValue);
if (!CFSetContainsValue(indexesToRemoveSet, timeStampValue)) {
CFSetAddValue(indexesToRemoveSet, timeStampValue);
CFArrayAppendValue(indexesToRemoveArray, timeStampValue);
}
} else {
break;
}
}
CFIndex timeStampsToRemoveCount = CFArrayGetCount(timeStampsToRemove);
for (CFIndex i = (timeStampsToRemoveCount - 1); i >= 0; i--) {
const void *timeStampValue = CFArrayGetValueAtIndex(timeStampsToRemove, i);
CFDictionaryRemoveValue(_historicalClocks, timeStampValue);
CFSetRemoveValue(_historicalClockMIDITimeStampsSet, timeStampValue);
CFArrayRemoveValueAtIndex(_historicalClockMIDITimeStampsArray, i);
}
CFRelease(timeStampsToRemove);
CFRelease(indexesToRemoveSet);
CFRelease(indexesToRemoveArray);
}
// Add clock to history
MIKMIDIClock *historicalClock = [[MIKMIDIClock alloc] initWithQueue:NO];
historicalClock->_currentTempo = _currentTempo;
historicalClock->_timeStampZero = _timeStampZero;
historicalClock->_lastSyncedMIDITimeStamp = _lastSyncedMIDITimeStamp;
historicalClock->_musicTimeStampsPerMIDITimeStamp = _musicTimeStampsPerMIDITimeStamp;
historicalClock->_midiTimeStampsPerMusicTimeStamp = _midiTimeStampsPerMusicTimeStamp;
void *midiTimeStampValue = (__bridge void *)midiTimeStampNumber;
CFDictionaryAddValue(_historicalClocks, midiTimeStampValue, (__bridge void *)historicalClock);
if (!CFSetContainsValue(_historicalClockMIDITimeStampsSet, midiTimeStampValue)) {
CFSetAddValue(_historicalClockMIDITimeStampsSet, midiTimeStampValue);
CFArrayAppendValue(_historicalClockMIDITimeStampsArray, midiTimeStampValue);
}
}
// Update new tempo and timing information
Float64 secondsPerMIDITimeStamp = MIKMIDIClockSecondsPerMIDITimeStamp();
Float64 secondsPerMusicTimeStamp = 60.0 / tempo;
Float64 midiTimeStampsPerMusicTimeStamp = secondsPerMusicTimeStamp / secondsPerMIDITimeStamp;
_currentTempo = tempo;
_lastSyncedMIDITimeStamp = midiTimeStamp;
_lastSyncedMusicTimeStamp = musicTimeStamp;
_timeStampZero = midiTimeStamp - (musicTimeStamp * midiTimeStampsPerMusicTimeStamp);
_midiTimeStampsPerMusicTimeStamp = midiTimeStampsPerMusicTimeStamp;
_musicTimeStampsPerMIDITimeStamp = secondsPerMIDITimeStamp / secondsPerMusicTimeStamp;
_ready = YES;
});
[self didChangeValueForKey:@"ready"];
}
- (void)unsyncMusicTimeStampsAndTemposFromMIDITimeStamps
{
[self willChangeValueForKey:@"ready"];
dispatchToClockQueue(self, ^{
_ready = NO;
_currentTempo = 0;
_lastSyncedMIDITimeStamp = 0;
releaseHistoricalClocks(self);
});
[self didChangeValueForKey:@"ready"];
}
static MusicTimeStamp musicTimeStampForMIDITimeStamp(MIKMIDIClock *self, MIDITimeStamp midiTimeStamp)
{
__block MusicTimeStamp musicTimeStamp = 0;
dispatchToClockQueue(self, ^{
if (!self->_ready) return;
MIDITimeStamp lastSyncedMIDITimeStamp = self->_lastSyncedMIDITimeStamp;
if (midiTimeStamp >= lastSyncedMIDITimeStamp) {
musicTimeStamp = musicTimeStampForMIDITimeStampWithHistoricalClock(midiTimeStamp, self);
} else {
musicTimeStamp = musicTimeStampForMIDITimeStampWithHistoricalClock(midiTimeStamp, clockForMIDITimeStamp(self, midiTimeStamp));
}
});
return musicTimeStamp;
}
- (MusicTimeStamp)musicTimeStampForMIDITimeStamp:(MIDITimeStamp)midiTimeStamp
{
return musicTimeStampForMIDITimeStamp(self, midiTimeStamp);
}
static MusicTimeStamp musicTimeStampForMIDITimeStampWithHistoricalClock(MIDITimeStamp midiTimeStamp, MIKMIDIClock *clock)
{
if (midiTimeStamp == clock->_lastSyncedMIDITimeStamp) return clock->_lastSyncedMusicTimeStamp;
MIDITimeStamp timeStampZero = clock->_timeStampZero;
return (midiTimeStamp >= timeStampZero) ? ((midiTimeStamp - timeStampZero) * clock->_musicTimeStampsPerMIDITimeStamp) : -((timeStampZero - midiTimeStamp) * clock->_musicTimeStampsPerMIDITimeStamp);
}
static MIDITimeStamp midiTimeStampForMusicTimeStamp(MIKMIDIClock *self, MusicTimeStamp musicTimeStamp)
{
__block MIDITimeStamp midiTimeStamp = 0;
dispatchToClockQueue(self, ^{
if (!self->_ready) return;
if (musicTimeStamp == self->_lastSyncedMusicTimeStamp) { midiTimeStamp = self->_lastSyncedMIDITimeStamp; return; }
midiTimeStamp = round(musicTimeStamp * self->_midiTimeStampsPerMusicTimeStamp) + self->_timeStampZero;
if (midiTimeStamp < self->_lastSyncedMIDITimeStamp && self->_historicalClockMIDITimeStampsArray) {
CFIndex historicalClockMIDITimeStampsCount = CFArrayGetCount(self->_historicalClockMIDITimeStampsArray);
for (CFIndex i = (historicalClockMIDITimeStampsCount - 1); i >= 0; i--) {
const void *midiTimeStampValue = CFArrayGetValueAtIndex(self->_historicalClockMIDITimeStampsArray, i);
MIKMIDIClock *clock = (__bridge MIKMIDIClock *)CFDictionaryGetValue(self->_historicalClocks, midiTimeStampValue);
MIDITimeStamp historicalMIDITimeStamp = round(musicTimeStamp * clock->_midiTimeStampsPerMusicTimeStamp) + clock->_timeStampZero;
if (historicalMIDITimeStamp >= clock->_lastSyncedMIDITimeStamp) {
midiTimeStamp = historicalMIDITimeStamp;
break;
}
}
}
});
return midiTimeStamp;
}
- (MIDITimeStamp)midiTimeStampForMusicTimeStamp:(MusicTimeStamp)musicTimeStamp
{
return midiTimeStampForMusicTimeStamp(self, musicTimeStamp);
}
static MIDITimeStamp midiTimeStampsPerMusicTimeStamp(MIKMIDIClock *self, MusicTimeStamp musicTimeStamp)
{
__block MIDITimeStamp midiTimeStamps = 0;
dispatchToClockQueue(self, ^{
if (self->_ready) midiTimeStamps = musicTimeStamp * self->_midiTimeStampsPerMusicTimeStamp;
});
return midiTimeStamps;
}
- (MIDITimeStamp)midiTimeStampsPerMusicTimeStamp:(MusicTimeStamp)musicTimeStamp
{
return midiTimeStampsPerMusicTimeStamp(self, musicTimeStamp);
}
#pragma mark - Tempo
static Float64 tempoAtMIDITimeStamp(MIKMIDIClock *self, MIDITimeStamp midiTimeStamp)
{
__block Float64 tempo = 0;
dispatchToClockQueue(self, ^{
if (self->_ready) {
if (midiTimeStamp >= self->_lastSyncedMIDITimeStamp) {
tempo = self->_currentTempo;
} else {
tempo = [clockForMIDITimeStamp(self, midiTimeStamp) currentTempo];
}
}
});
return tempo;
}
- (Float64)tempoAtMIDITimeStamp:(MIDITimeStamp)midiTimeStamp
{
return tempoAtMIDITimeStamp(self, midiTimeStamp);
}
Float64 tempoAtMusicTimeStamp(MIKMIDIClock *self, MusicTimeStamp musicTimeStamp)
{
return tempoAtMIDITimeStamp(self, midiTimeStampForMusicTimeStamp(self, musicTimeStamp));
}
- (Float64)tempoAtMusicTimeStamp:(MusicTimeStamp)musicTimeStamp
{
return tempoAtMusicTimeStamp(self, musicTimeStamp);
}
#pragma mark - Historical Clocks
static MIKMIDIClock *clockForMIDITimeStamp(MIKMIDIClock *self, MIDITimeStamp midiTimeStamp)
{
MIKMIDIClock *clock = self;
if (self->_historicalClockMIDITimeStampsArray) {
CFIndex count = CFArrayGetCount(self->_historicalClockMIDITimeStampsArray);
for (CFIndex i = (count - 1); i >= 0; i--) {
NSNumber *historicalClockTimeStamp = (__bridge NSNumber *)CFArrayGetValueAtIndex(self->_historicalClockMIDITimeStampsArray, i);
if ([historicalClockTimeStamp unsignedLongLongValue] > midiTimeStamp) {
clock = (__bridge MIKMIDIClock *)CFDictionaryGetValue(self->_historicalClocks, (__bridge void *)historicalClockTimeStamp);
} else {
break;
}
}
}
return clock;
}
static void releaseHistoricalClocks(MIKMIDIClock *self)
{
if (self->_historicalClocks) {
CFRelease(self->_historicalClocks);
self->_historicalClocks = NULL;
}
if (self->_historicalClockMIDITimeStampsSet) {
CFRelease(self->_historicalClockMIDITimeStampsSet);
self->_historicalClockMIDITimeStampsSet = NULL;
}
if (self->_historicalClockMIDITimeStampsArray) {
CFRelease(self->_historicalClockMIDITimeStampsArray);
self->_historicalClockMIDITimeStampsArray = NULL;
}
}
#pragma mark - Synced Clock
- (MIKMIDIClock *)syncedClock
{
return (MIKMIDIClock *)[MIKMIDISyncedClockProxy syncedClockWithClock:self];
}
#pragma mark - Functions
Float64 MIKMIDIClockSecondsPerMIDITimeStamp()
{
static Float64 secondsPerMIDITimeStamp;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
mach_timebase_info_data_t timeBaseInfoData;
mach_timebase_info(&timeBaseInfoData);
secondsPerMIDITimeStamp = ((Float64)timeBaseInfoData.numer / timeBaseInfoData.denom) / 1.0e9;
});
return secondsPerMIDITimeStamp;
}
Float64 MIKMIDIClockMIDITimeStampsPerTimeInterval(NSTimeInterval timeInterval)
{
Float64 midiTimeStampsPerSecond = 0;
if (!midiTimeStampsPerSecond) {
Float64 clockSecondsPerMIDITimeStamp = MIKMIDIClockSecondsPerMIDITimeStamp();
midiTimeStampsPerSecond = (1.0 / clockSecondsPerMIDITimeStamp);
}
return midiTimeStampsPerSecond * timeInterval;
}
#pragma mark - Deprecated Methods
- (void)setMusicTimeStamp:(MusicTimeStamp)musicTimeStamp withTempo:(Float64)tempo atMIDITimeStamp:(MIDITimeStamp)midiTimeStamp
{
[self syncMusicTimeStamp:musicTimeStamp withMIDITimeStamp:midiTimeStamp tempo:tempo];
}
+ (Float64)secondsPerMIDITimeStamp
{
return MIKMIDIClockSecondsPerMIDITimeStamp();
}
+ (Float64)midiTimeStampsPerTimeInterval:(NSTimeInterval)timeInterval
{
return MIKMIDIClockMIDITimeStampsPerTimeInterval(timeInterval);
}
@end
#pragma mark -
@implementation MIKMIDISyncedClockProxy
+ (instancetype)syncedClockWithClock:(MIKMIDIClock *)masterClock
{
MIKMIDISyncedClockProxy *proxy = [self alloc];
proxy->_masterClock = masterClock;
return proxy;
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
SEL selector = invocation.selector;
// Optimizations
if (selector == @selector(midiTimeStampForMusicTimeStamp:)) {
MusicTimeStamp musicTimeStamp;
[invocation getArgument:&musicTimeStamp atIndex:2];
MIDITimeStamp midiTimeStamp = midiTimeStampForMusicTimeStamp(_masterClock, musicTimeStamp);
return [invocation setReturnValue:&midiTimeStamp];
} else if (selector == @selector(musicTimeStampForMIDITimeStamp:)) {
MIDITimeStamp midiTimeStamp;
[invocation getArgument:&midiTimeStamp atIndex:2];
MusicTimeStamp musicTimeStamp = musicTimeStampForMIDITimeStamp(_masterClock, midiTimeStamp);
return [invocation setReturnValue:&musicTimeStamp];
} else if (selector == @selector(tempoAtMIDITimeStamp:)) {
MIDITimeStamp midiTimeStamp;
[invocation getArgument:&midiTimeStamp atIndex:2];
Float64 tempo = tempoAtMIDITimeStamp(_masterClock, midiTimeStamp);
return [invocation setReturnValue:&tempo];
} else if (selector == @selector(tempoAtMusicTimeStamp:)) {
MusicTimeStamp musicTimeStamp;
[invocation getArgument:&musicTimeStamp atIndex:2];
Float64 tempo = tempoAtMusicTimeStamp(_masterClock, musicTimeStamp);
return [invocation setReturnValue:&tempo];
} else if (selector == @selector(midiTimeStampsPerMusicTimeStamp:)) {
MusicTimeStamp musicTimeStamp;
[invocation getArgument:&musicTimeStamp atIndex:2];
MIDITimeStamp midiTimeStamps = midiTimeStampsPerMusicTimeStamp(_masterClock, musicTimeStamp);
return [invocation setReturnValue:&midiTimeStamps];
} else if (selector == @selector(syncedClock)) {
MIKMIDISyncedClockProxy *syncedClock = self;
return [invocation setReturnValue:&syncedClock];
}
// Ignored selectors
if (selector == @selector(syncMusicTimeStamp:withMIDITimeStamp:tempo:)) return;
if (selector == @selector(unsyncMusicTimeStampsAndTemposFromMIDITimeStamps)) return;
if (selector == @selector(setMusicTimeStamp:withTempo:atMIDITimeStamp:)) return; // deprecated
// Pass through remaining selectors
[invocation invokeWithTarget:_masterClock];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [_masterClock methodSignatureForSelector:sel];
}
@end