/
SEMIDIClockSender.m
369 lines (303 loc) · 14 KB
/
SEMIDIClockSender.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
//
// SEMIDIClockSender.m
// The Spectacular Sync Engine
//
// Created by Michael Tyson on 31/12/2014.
// Copyright (c) 2015 A Tasty Pixel. All rights reserved.
//
#import "SEMIDIClockSender.h"
#import "SECommon.h"
static const int kTicksPerSendInterval = 4; // Max MIDI ticks to send per interval
static const NSTimeInterval kFirstBeatSyncThreshold = 1.0e-3; // Wait to send first beat if it's further away than this
static const NSTimeInterval kTickResyncThreshold = 1.0e-6; // If tick is beyond this threshold out of sync, resync
static const double kThreadPriority = 0.8; // Priority of the sender thread
static const int kMaxPendingMessages = 10; // Size of pending message buffer
@interface SEMIDIClockSenderThread : NSThread
@property (nonatomic, weak) SEMIDIClockSender * sender;
@end
@interface SEMIDIClockSender () {
double _positionAtStart;
MIDIPacketList _pendingMessages[kMaxPendingMessages];
}
@property (nonatomic, strong, readwrite) id<SEMIDIClockSenderInterface> senderInterface;
@property (nonatomic, strong) SEMIDIClockSenderThread *thread;
@property (nonatomic, readwrite) BOOL started;
@property (nonatomic) uint64_t nextTickTime;
@property (nonatomic) uint64_t timeBase;
@property (nonatomic) MIDIPacketList * pendingMessages;
@end
@implementation SEMIDIClockSender
@dynamic timelinePosition;
@dynamic pendingMessages;
-(instancetype)initWithInterface:(id<SEMIDIClockSenderInterface>)senderInterface {
if ( !(self = [super init]) ) return nil;
self.senderInterface = senderInterface;
return self;
}
-(void)dealloc {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sendSongPositionDelayed) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startThread) object:nil];
if ( _thread ) {
[_thread cancel];
while ( !_thread.isFinished ) {
[NSThread sleepForTimeInterval:0.01];
}
}
}
-(uint64_t)startAtTime:(uint64_t)startTime {
NSAssert(_tempo != 0, @"You must provide a tempo first");
return [self startOrSeekWithPosition:_positionAtStart atTime:startTime startClock:YES];
}
-(void)stop {
@synchronized ( self ) {
// Send stop message
MIDIPacketList packetList;
MIDIPacket *packet = MIDIPacketListInit(&packetList);
unsigned char message[1] = { SEMIDIMessageClockStop };
MIDIPacketListAdd(&packetList, sizeof(packetList), packet, SECurrentTimeInHostTicks(), sizeof(message), message);
[_senderInterface sendMIDIPacketList:&packetList];
self.started = NO;
}
if ( !_sendClockTicksWhileTimelineStopped && _thread ) {
// Stop the thread
[_thread cancel];
self.thread = nil;
}
}
-(uint64_t)setActiveTimelinePosition:(double)timelinePosition atTime:(uint64_t)applyTime {
return [self startOrSeekWithPosition:timelinePosition atTime:applyTime startClock:NO];
}
-(double)timelinePositionForTime:(uint64_t)timestamp {
return SEMIDIClockSenderGetTimelinePosition(self, timestamp);
}
double SEMIDIClockSenderGetTimelinePosition(__unsafe_unretained SEMIDIClockSender * THIS, uint64_t time) {
if ( !THIS->_started ) {
return THIS->_positionAtStart;
}
if ( !time ) {
time = SECurrentTimeInHostTicks();
}
if ( time < THIS->_timeBase ) {
return 0.0;
}
// Calculate offset from our time base, and convert to beats using current tempo
return SEHostTicksToBeats(time - THIS->_timeBase, THIS->_tempo);
}
BOOL SEMIDIClockSenderIsStarted(__unsafe_unretained SEMIDIClockSender * THIS) {
return THIS->_started;
}
-(void)setTimelinePosition:(double)timelinePosition {
[self setActiveTimelinePosition:timelinePosition atTime:SECurrentTimeInHostTicks()];
}
-(double)timelinePosition {
return [self timelinePositionForTime:0];
}
-(void)setTempo:(double)tempo {
if ( _tempo == tempo ) {
return;
}
if ( tempo == 0.0 && !_started ) {
[self stop];
}
@synchronized ( self ) {
if ( _timeBase ) {
// Scale time base to new tempo, so our relative timeline position remains the same (as it is dependent on tempo)
double ratio = _tempo / tempo;
uint64_t now = SECurrentTimeInHostTicks();
_timeBase = now - ((now - _timeBase) * ratio);
}
_tempo = tempo;
}
if ( _sendClockTicksWhileTimelineStopped ) {
if ( tempo != 0.0 && !_thread ) {
// Start the thread which will send out the ticks - in a moment, in case clock is started next
[self performSelector:@selector(startThread) withObject:nil afterDelay:0.0];
} else if ( tempo == 0.0 && _thread ) {
// Stop the thread
[_thread cancel];
self.thread = nil;
}
}
}
-(uint64_t)startOrSeekWithPosition:(double)timelinePosition atTime:(uint64_t)applyTime startClock:(BOOL)start {
@synchronized ( self ) {
uint64_t tickDuration = SESecondsToHostTicks((60.0 / _tempo) / SEMIDITicksPerBeat);
uint64_t MIDIBeatDuration = tickDuration * SEMIDITicksPerSongPositionBeat;
double beatsToMIDIBeats = (double)SEMIDITicksPerBeat / (double)SEMIDITicksPerSongPositionBeat;
uint64_t beatSyncThreshold = SESecondsToHostTicks(kFirstBeatSyncThreshold);
if ( !_started && !start ) {
// Cue this position for when we start
_positionAtStart = timelinePosition;
// Send song position in next run loop (delayed, in case we're just about to start the clock,
// in which case we want to send the song position at the same timestamp
[self performSelector:@selector(sendSongPositionDelayed) withObject:nil afterDelay:0];
return applyTime;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sendSongPositionDelayed) object:nil];
if ( !applyTime ) {
// We've been left to choose an apply time ourselves: choose the next tick time,
// to give us the best chance of a smooth transition.
applyTime = _nextTickTime ? _nextTickTime : SECurrentTimeInHostTicks();
} else if ( _nextTickTime ) {
// Find the next tick time after the given apply time
uint64_t originalApplyTime = applyTime;
if ( applyTime < _nextTickTime ) {
applyTime = _nextTickTime;
} else {
uint64_t modulus = (applyTime - _nextTickTime) % tickDuration;
if ( modulus > beatSyncThreshold && (tickDuration - modulus) > beatSyncThreshold ) {
applyTime += tickDuration - modulus;
}
}
if ( applyTime > originalApplyTime ) {
// Need to adjust the timeline position accordingly
timelinePosition += SEHostTicksToBeats(applyTime - originalApplyTime, _tempo);
}
}
// Calculate time base, and determine relative position in host ticks
uint64_t timeBase = applyTime - SEBeatsToHostTicks(timelinePosition, _tempo);
if ( _nextTickTime && applyTime <= (_nextTickTime-tickDuration) ) {
// If our apply time is before the last tick we sent, we'll need to move up the timeline.
// Work out when the next MIDI beat is, and use that as our apply time
uint64_t latestPosition = _nextTickTime - timeBase;
uint64_t timeUntilNextMIDIBeat = MIDIBeatDuration - (latestPosition % MIDIBeatDuration);
applyTime = timeBase + latestPosition + timeUntilNextMIDIBeat;
timelinePosition = SEHostTicksToBeats(applyTime - timeBase, _tempo);
}
// Calculate time, in our new timeline, to the closest MIDI Beat (16th note)
uint64_t timeUntilNextMIDIBeat = 0;
uint64_t position = applyTime - timeBase;
uint64_t modulus = position % MIDIBeatDuration;
if ( modulus > beatSyncThreshold && MIDIBeatDuration - modulus > beatSyncThreshold ) {
timeUntilNextMIDIBeat = MIDIBeatDuration - modulus;
}
// Determine number of MIDI Beats to report
int totalBeats = round((timelinePosition + SEHostTicksToBeats(timeUntilNextMIDIBeat, _tempo)) * beatsToMIDIBeats);
if ( _started || totalBeats > 0 ) {
// Send song position
[self enqueueMessage:(unsigned char[3]){SEMIDIMessageSongPosition, totalBeats & 0x7F, (totalBeats >> 7) & 0x7F}
length:3
time:applyTime + timeUntilNextMIDIBeat - 1 /* force ordering before tick */];
}
if ( _started || start) {
// Update the timebase
_timeBase = timeBase;
}
if ( !_started && start ) {
[self enqueueMessage:(unsigned char[1]){ totalBeats > 0.0 ? SEMIDIMessageContinue : SEMIDIMessageClockStart }
length:1
time:applyTime + timeUntilNextMIDIBeat - 1 /* force ordering before tick */];
_positionAtStart = 0;
self.started = YES;
_nextTickTime = applyTime + timeUntilNextMIDIBeat;
if ( !_thread ) {
[self startThread];
}
}
}
return applyTime;
}
-(void)startThread {
if ( !_thread ) {
self.thread = [SEMIDIClockSenderThread new];
_thread.sender = self;
[_thread start];
}
}
-(void)enqueueMessage:(const unsigned char*)message length:(int)length time:(MIDITimeStamp)timestamp {
if ( _thread ) {
// Enqueue message to be sent from sender thread, at the appropriate time
for ( int i=0; i<kMaxPendingMessages; i++ ) {
if ( _pendingMessages[i].numPackets == 0 ) {
MIDIPacket *packet = MIDIPacketListInit(&_pendingMessages[i]);
packet = MIDIPacketListAdd(&_pendingMessages[i], sizeof(_pendingMessages[i]), packet, timestamp, length, message);
break;
}
}
} else {
// Send immediately
MIDIPacketList packetList;
MIDIPacket *packet = MIDIPacketListInit(&packetList);
packet = MIDIPacketListAdd(&packetList, sizeof(packetList), packet, timestamp, length, message);
[_senderInterface sendMIDIPacketList:&packetList];
}
}
-(MIDIPacketList *)pendingMessages {
return _pendingMessages;
}
-(void)sendSongPositionDelayed {
double beatsToMIDIBeats = (double)SEMIDITicksPerBeat / (double)SEMIDITicksPerSongPositionBeat;
int totalBeats = round(_positionAtStart * beatsToMIDIBeats);
[self enqueueMessage:(unsigned char[3]){SEMIDIMessageSongPosition, totalBeats & 0x7F, (totalBeats >> 7) & 0x7F}
length:3
time:SECurrentTimeInHostTicks()];
}
@end
@implementation SEMIDIClockSenderThread
-(void)main {
[NSThread setThreadPriority:kThreadPriority];
while ( !self.isCancelled ) {
uint64_t nextSendTime = 0;
@synchronized ( _sender ) {
uint64_t now = SECurrentTimeInHostTicks();
uint64_t nextTickTime = _sender.nextTickTime;
if ( !nextTickTime ) {
_sender.nextTickTime = nextTickTime = now;
}
// Send the next batch of ticks
uint64_t tickDuration = SESecondsToHostTicks((60.0 / _sender.tempo) / SEMIDITicksPerBeat);
uint64_t endTime = MAX(nextTickTime, now) + (tickDuration * kTicksPerSendInterval);
_sender.nextTickTime = nextTickTime = [self sendFromTime:nextTickTime toTime:endTime];
// Wait half the duration of the ticks we just sent (to avoid running out of time; we'll skip the ticks we've already sent)
nextSendTime = nextTickTime - (tickDuration * kTicksPerSendInterval) / 2;
}
// Sleep
mach_wait_until(nextSendTime);
}
@synchronized ( _sender ) {
_sender.nextTickTime = 0;
}
}
-(uint64_t)sendFromTime:(uint64_t)start toTime:(uint64_t)end {
if ( _sender.tempo == 0 ) {
return start;
}
uint64_t tickDuration = SESecondsToHostTicks((60.0 / _sender.tempo) / SEMIDITicksPerBeat);
uint64_t timeBase = _sender.timeBase;
if ( timeBase ) {
// Calculate distance to next scheduled tick
uint64_t position = start - timeBase;
uint64_t modulus = position % tickDuration;
uint64_t threshold = SESecondsToHostTicks(kTickResyncThreshold);
if ( modulus > threshold && tickDuration - modulus > threshold ) {
// Resync to the closest tick, to make sure it's a multiple of the tick duration away from the time base
start = timeBase + round((double)position / (double)tickDuration) * tickDuration;
}
}
MIDIPacketList * pendingMessages = _sender.pendingMessages;
// Send messages for the time period from 'start', and up to (but not including) 'end'
MIDIPacketList packetList;
uint8_t message = SEMIDIMessageClock;
uint64_t time = start;
int count = 0;
for ( count = 0; time < end; count++, time += tickDuration ) {
// Dispatch pending messages
for ( int i=0; i<kMaxPendingMessages; i++ ) {
if ( pendingMessages[i].numPackets != 0 && pendingMessages[i].packet[0].timeStamp < time ) {
[_sender.senderInterface sendMIDIPacketList:&pendingMessages[i]];
pendingMessages[i].numPackets = 0;
}
}
if ( time < start ) {
// Skip ticks we've already sent
continue;
}
// Send tick
MIDIPacket *packet = MIDIPacketListInit(&packetList);
MIDIPacketListAdd(&packetList, sizeof(packetList), packet, time, 1, &message);
[_sender.senderInterface sendMIDIPacketList:&packetList];
}
// Return the time the next tick should be sent
return time;
}
@end