Skip to content

Commit

Permalink
Tweaks to SEMIDIClockSender timeline query interface
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltyson committed Apr 21, 2015
1 parent ebae13d commit e2c327f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
19 changes: 18 additions & 1 deletion TheSpectacularSyncEngine/SEMIDIClockSender.h
Expand Up @@ -126,7 +126,24 @@ extern "C" {
* in host ticks. See mach_absolute_time, or SECurrentTimeInHostTicks
* @return The position in your app's timeline, in beats, for the provided global timestamp
*/
-(double)activeTimelinePositionForTime:(uint64_t)timestamp;
-(double)timelinePositionForTime:(uint64_t)timestamp;

/*!
* Get the current timeline position, in beats
*
* Use this C function from the realtime audio thread to determine the timeline
* position, in beats (that is, quarter notes - use SEBeatsToSeconds to convert to
* seconds, if necessary), for the given global timestamp.
*
* You may use this method to keep track of the timeline while rendering, if you
* do not already have your own internal clock system, and you are not currently
* receiving clock messages from an external source.
*
* @param sender The sender
* @param time The global timestamp to retrieve the corresponding timeline position for
* @return The position in the remote timeline for the provided global timestamp, in beats
*/
double SEMIDIClockSenderGetTimelinePosition(__unsafe_unretained SEMIDIClockSender * sender, uint64_t time);

/*!
* The current position in the timeline (in beats)
Expand Down
20 changes: 12 additions & 8 deletions TheSpectacularSyncEngine/SEMIDIClockSender.m
Expand Up @@ -82,29 +82,33 @@ -(uint64_t)setActiveTimelinePosition:(double)timelinePosition atTime:(uint64_t)a
return [self startOrSeekWithPosition:timelinePosition atTime:applyTime startClock:NO];
}

-(double)activeTimelinePositionForTime:(uint64_t)timestamp {
if ( !_started ) {
return _positionAtStart;
-(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 ( !timestamp ) {
timestamp = SECurrentTimeInHostTicks();
if ( !time ) {
time = SECurrentTimeInHostTicks();
}

if ( timestamp < _timeBase ) {
if ( time < THIS->_timeBase ) {
return 0.0;
}

// Calculate offset from our time base, and convert to beats using current tempo
return SEHostTicksToBeats(timestamp - _timeBase, _tempo);
return SEHostTicksToBeats(time - THIS->_timeBase, THIS->_tempo);
}

-(void)setTimelinePosition:(double)timelinePosition {
[self setActiveTimelinePosition:timelinePosition atTime:SECurrentTimeInHostTicks()];
}

-(double)timelinePosition {
return [self activeTimelinePositionForTime:0];
return [self timelinePositionForTime:0];
}

-(void)setTempo:(double)tempo {
Expand Down

0 comments on commit e2c327f

Please sign in to comment.