how to schedule a timer in sync with audio
how to schedule a timer in sync with audio
- Subject: how to schedule a timer in sync with audio
- From: jhno <email@hidden>
- Date: Wed, 5 Jan 2011 19:14:07 -0500
This is a response to my own inquiry about correlating hostTime to CFAbsoluteTime:
My objective was to send UDP messages (OSC) in synchronization with audio events generated during a RemoteIO callback.
The UDP sends occur in a separate thread, and are triggered by a CFRunLoopTimerRef.
So, I just need to set the timer's fire date to synchronize with the audio event.
The timer needs to be scheduled in CFAbsoluteTime.
I know the exact sample of the audio event, so how do I calculate the fire date for the timer?
In the RemoteIO callback, let's say the audio event occurs at
int eventSample
...which is a sample offset into the ioData audio vector. First I calculate the delay from the start of the audio vector to eventSample:
NSTimeInterval delayInSeconds = eventSample / hwSampleRate;
Then I need to add the difference in time between NOW and the start of the audio vector:
UInt64 hostTimeDiff = inTimeStamp->mHostTime - mach_absolute_time();
The inTimeStamp is always greater than the current time, so I assume that this is when the output will start!
However I have to convert from HostTime to seconds:
mach_timebase_info_data_t tinfo;
mach_timebase_info(&tinfo);
double hTime2nsFactor = (double)tinfo.numer / tinfo.denom;
Float64 extraTimeOffset = hostTimeDiff * hTime2nsFactor * 0.000000001;
That should do it. Now I can add this offset to the vector delay calculated above, and schedule away:
delaySeconds += extraTimeOffset;
CFRunLoopTimerSetNextFireDate (myUDPtimer, CFAbsoluteTimeGetCurrent () + delaySeconds);
This technique results in an event stream with low jitter, which was my main objective.
However, I have not yet measured the correlation between the network events and the audio output of the iDevice or Simulator - !
Comments &c. welcome,
jhno
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden