Skip to content

Instantly share code, notes, and snippets.

@armadsen
Created July 1, 2016 16:17
Show Gist options
  • Save armadsen/0164408942aaa30e5cf2599bdcbe8b88 to your computer and use it in GitHub Desktop.
Save armadsen/0164408942aaa30e5cf2599bdcbe8b88 to your computer and use it in GitHub Desktop.
Quick (untested) example of getting bar-beat time for a particular timestamp in an MIKMIDISequence.
@interface MIKMIDISequence (BarBeatTime)
- (CABarBeatTime)barBeatTimeForTimeStamp:(MusicTimeStamp)timeStamp error:(NSError **)error;
@end
@implementation MIKMIDISequence (BarBeatTime)
- (CABarBeatTime)barBeatTimeForTimeStamp:(MusicTimeStamp)timeStamp error:(NSError **)error
{
error = error ?: &(NSError *__autoreleasing){ nil };
UInt32 timeResolution = 0;
UInt32 propertyLength = 0;
OSStatus err = MusicTrackGetProperty(self.tempoTrack.musicTrack,
kSequenceTrackProperty_TimeResolution,
NULL,
&propertyLength);
if (err != noErr) {
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
return (CABarBeatTime){0};
}
err = MusicTrackGetProperty(self.tempoTrack.musicTrack,
kSequenceTrackProperty_TimeResolution,
&timeResolution,
&propertyLength);
if (err != noErr) {
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
return (CABarBeatTime){0};
}
CABarBeatTime barBeatTime = {0};
err = MusicSequenceBeatsToBarBeatTime(self.musicSequence, timeStamp, timeResolution, &barBeatTime);
if (err != noErr) {
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
return (CABarBeatTime){0};
}
return barBeatTime;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment