4

I have a C++ application that receives a timestamped audio stream and attempts to play the audio samples as close as possible to the specified timestamp. To do so I need to know the delay (with reasonable accuracy) from when I place the audio samples in the output buffer until the audio is actually heard.

There are many discussions about audio output latency but everything I have found is about minimizing latency. This is irrelevant to me, all I need is an (at run-time) known latency.

On Linux I solve this with snd_pcm_delay() with very good results, but I'm looking for a decent solution for Windows.

I have looked at the following:

  • With OpenAL I have measured delays at 80ms that are unaccounted for. I assume this isn't a hardcoded value and I haven't found any API to read the latency. There are some extensions to OpenAL that claims to support this but from what I can tell it's only implemented on Linux.
  • Wasapi has GetStreamLatency() which sounds like the real deal but this is apparently only some thread polling interval or something so it's also useless. I still have 30ms unaccounted delay on my machine.
  • DirectSound has no API for getting latency? But can I get close enough by just keeping track of my output buffers?

Edit in response to Brad's comment:

  • My impression of ASIO is that it is primarily targeted for professional audio applications and audio connoiseurs, and that the user might have to install special sound card drivers and I will have to deal with licensing. Feature-wise it seems like a good option though.
jensnielsen
  • 191
  • 1
  • 6
  • I think ASIO has methods for calculating latency, as every program I've used for ASIO seems to display the latency in milliseconds and number of samples. I suppose though that it's always possible that the audio device will have an undetermined amount of its own latency. In addition, you have folks that use S/PDIF speakers and what not which will add latency as well since they have their own DACs. – Brad Mar 16 '14 at 03:18
  • Thanks @Brad, I checked out ASIO and added some comments. Regarding S/PDIF DACs I think that's within the error margin of my clock sync algorithm anyway. I don't need it perfect, just "close enough". – jensnielsen Mar 16 '14 at 09:56
  • Hi @jensnielsen , I know this is a 7 year old question, but I have the same one and am curious if you ever came to a solution. I have noticed that Windows 10 introduced some changes to enable low latency audio that might be relevant: It seems that audio drivers can now report buffer sizes ( https://learn.microsoft.com/en-us/windows-hardware/drivers/audio/low-latency-audio#driver-improvements ), but I am just starting to look into how this may be helpful... – Allen Pestaluky Jun 02 '21 at 21:12
  • 1
    @AllenPestaluky unfortunately not, and that project has been dead for ages now. But I dug up the source code just out of curiosity, I ended up with wasapi and a hardcoded adjustment of 30 ms. – jensnielsen Jun 03 '21 at 20:48
  • Thanks for looking into it! It's too bad to hear, but I really appreciate knowing how things ended up. For my case specifically, I am looking to synchronize audio output between two different audio devices, so I figured calculating output latency would be a good place to start. Maybe a better way would be to write my own driver... https://learn.microsoft.com/en-us/windows-hardware/drivers/audio/synchronizing-two-or-more-streams – Allen Pestaluky Jun 04 '21 at 00:05

0 Answers0