Frequency drift in Core Audio on OSX -
i have skeleton audio app uses kaudiounitsubtype_haloutput play audio via aurendercallback. i'm generating simple pure tone test things out, tone changes pitch noticeably time time; drifting or down, , changing rapidly. can couple of tones out @ ~500hz. here's callback:
static osstatus outputcallback(void *inrefcon, audiounitrenderactionflags *ioactionflags, const audiotimestamp *intimestamp, uint32 inoutputbusnumber, uint32 innumberframes, audiobufferlist *iodata) { static const float frequency = 1000; static const float rate = 48000; static float phase = 0; sint16 *buffer = (sint16 *)iodata->mbuffers[0].mdata; (int s = 0; s < innumberframes; s++) { buffer[s] = (sint16)(sinf(phase) * int16_max); phase += 2.0 * m_pi * frequency / rate; } return noerr; } i understand audio devices drift on time (especially cheap ones built-in io), a lot of drift — it's unusable music. ideas?
recording http://files.danhalliday.com/stackoverflow/audio.png
you're never resetting phase, value increase indefinitely. since it's stored in floating-point type, precision of stored value degraded value increases. cause of frequency variations you're describing.
adding following lines body of for() loop should mitigate problem:
if (phase > 2.0 * m_pi) phase -= 2.0 * m_pi; changing type of phase float double significantly.
Comments
Post a Comment