I'm having troubles with midi synchronization. Following posts in the syntheticbits.com, I use a PGMidi class. Sync works, but is constantly shifting to 1-2 bpm.
Here is the code that I use:
- (void)sendMidiClockInMidiThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[lock lock];
const int64_t kOneMillion = 1000 * 1000;
const UInt8 tick[] = { 0xF8 };
// Calculate tick time
// returns sample for sixteen note (5512 if current bpm is 120)
SInt32 sixteen = AGGetSamples_SixteensWithoutSwing(_grid, 1.0);
UInt64 tickTime = (sixteen / 6) * kOneMillion;
int tickLatency = 0;
// Send ticks messages
for (int i = 0; i < 6; ++i) {
int beginTime = clock();
hostTime = hostTime + (tickTime - tickLatency);
[self.midi sendBytes:tick size:sizeof(tick) atTime:hostTime];
int endTime = clock();
tickLatency = endTime - beginTime;
}
[lock unlock];
[pool drain];
}
Please tell me how to get a clear synchronization.