How accurately (in terms of time) does Windows play audio?

Let's say I play a stereo WAV file with 317,520,000 samples, which theoretically is 1 hour. Assuming that interruptions in playback will not be interrupted, will there be a file playback in exactly one hour or will there be any slight variations in playback speed, so that it will be slightly larger or slightly less (by several milliseconds) than by one hour?

I am trying to synchronize the animation with audio, and I am using System.Diagnostics.Stopwatch to save frames corresponding to the sound. But if the playback speed of WAV audio in Windows may vary slightly over time, the sound will go out of sync with the animation controlled by the stopwatch.

This leads to a second question: it seems that a Stopwatch - while very narrow and precise for short durations - works a little fast. On my laptop, a Stopwatch run for exactly 24 hours (measured by the time of a computer system and a real stopwatch) shows the elapsed time of 24 hours plus about 5 seconds (not milliseconds).

Is this a known issue with Stopwatch ? (A related question will be β€œAm I crazy?”, But you can try it for yourself.) Given its use as a diagnostic tool, I see where such a mismatch will only be displayed when measuring long durations for which most people will use what something other than Stopwatch .

If I'm really lucky, then Stopwatch and sound playback are controlled by the same basic mechanism and, thus, will remain in sync with each other for several days in a row. Is any chance right?

Update . I just did the math, and if Stopwatch drifts for 5 seconds in 24 hours, it means that it will drift for 10 milliseconds in 172 seconds. Thus, after 3 minutes, the animation will noticeably not synchronize.

I experiment periodically (every 10 seconds or so) starts the timer again from the waveOutWrite callback, but this does not work, because then the whole next set of timer events is compensated by whatever callback error happened. Sucks for me.

+6
windows audio
source share
2 answers

Larry Osterman answered in a comment:

FWIW, a Windows video clock driven by a sound clock. If you are rendering audio, you get an audio clock by calling waveOutGetPosition (since the audio is isochronous, the position is directly related to time). All other sound effects API have a similar API "GetPosition" which can be used to determine the rendering position of the audio.

0
source share

No clock will measure time β€œaccurately,” since all physical devices must have some variation and measurement error. This means that ALL watches will be a little too fast or too slow (although the number of errors can vary greatly depending on the clock).

In your case, the audio output is controlled by the clock on the sound card that controls the DAC. I do not know the .NET platform, but I assume that the stopwatch is some kind of system timer, which means that it is controlled by different clocks (for example, on your motherboard).

Now, in general, two different physical measures will NEVER work at the same speed - for the reasons stated above. This is where the discrepancy came from. The same thing will happen with your animation - you can absolutely never count the system clock, and the DAC clocks of the sound card are the same - they will be different!

This means that if you want to synchronize two streams (video and audio), they must be controlled by the same clock. Since you cannot change the clock that controls your sound card, this is a good bet to sync everything with the sound card.

+2
source share

All Articles