Day 19: Latency Clarification

Hi, so I am watching Day 7, and I'm confused by what exactly Casey is referring to when he says latency. Broadly, I understand that it is the difference between when the sound is written and when it plays, but when specifically talking about Direct Sound why does this occur? Apparently, DSound has minimum 3 Frames of Latency (According to Day 19). The 3 Frames of latency, to my understanding, occur because we can't write any faster than the minimum distance between the Write and Play Cursors (this is about 1 frame or 33ms). But, within the 2nd frame, since the Play Cursor could be anywhere once we actually write to it, we have to write 1 frame even ahead of that, thus we have 3 frames of latency. so to clarify for DSound to have lower latency, this would mean that the play and write cursors would need to be closer together. Is this the correct understanding?

Edited by GameMader on
GameMader
so to clarify for DSound to have lower latency, this would mean that the play and write cursors would need to be closer together.

That's correct. The latency issue is discussed in [Day 019] - Possible solution to the 3-frame latency, and there are other threads in the forums that discuss direct sound and latency issues.
awesome. so i have 1 more thing if you've got the time to answer. i was reading over the flags in the DSound DBSCAPS structure, and I just wanted to ask about these 2 flags:

DSBCAPS_LOCHARDWARE - the buffer uses hardware mixing.
DSBCAPS_LOCSOFTWARE - the buffer uses software mixing.

I have 2 Questions:

1. my assumption is that 'audio mixing' refers to the process of turning our buffer's data into analog audio output on the various channels, but i may be wrong on this, so i would like to clarify my understanding if I am incorrect.

2. i assume hardware mixing is faster than software mixing? if that's the case I guess our sound would come out of the speakers with less latency due to this mixing? if so is there a reason we did not use it on HMH; seems like it would be ideal (although I could totally not understand what mixing is or about my assumptions about HW mixing being faster)

Edited by GameMader on
Mixing here refers to process which takes multiple buffers as input and turns that into one output. Think taking different audio sources in a game, or from multiple applications, and calculating array of samples that is final audio buffer which is later passed to digital-to-analog converter on sound card.

Hardware mixing in DirectSound is obsolete on modern Windows version. It does not work starting with Windows Vista. On Vista and higher DirectSound is just an user space library that uses WASAPI (Windows Audio Session API) to actually talk to hardware. This is the same with XAudio2 and WinMM API - they all are wrappers around WASAPI.

Here is more information in previous forum topics:
https://hero.handmade.network/for..._dsbcaps_getcurrentposition2_flag
https://hero.handmade.network/for...sion/t/102-day_19_-_audio_latency

Edited by Mārtiņš Možeiko on
GameMader
1. my assumption is that 'audio mixing' refers to the process of turning our buffer's data into analog audio output on the various channels[...]


Audio mixing is the process of combining several audio source. In handmade hero we mix to a single track with two channels (left and right). For example, in a game, it's the process of combining the music and sound effects into a single track. It doesn't include the digital to analog conversion which
is done by a digital to analog converter circuit or chip.

For your second question: keep in mind that Direct Sound is emulated since Windows Vista so even if you choose hardware mixing it would not change anything in our case.

But even before that it wouldn't change in the case of Handmade Hero. Casey always does the mixing himself, meaning he only have 1 Direct Sound buffer, and he combines the different sounds he want in software and write them in that buffer. So Direct Sound only has one buffer and no need to mix anything.

To my knowledge, the Direct Sound software and hardware mixer mix several Direct Sound buffer (you would need to create several of them with CreateSoundBuffer) and possibly (I'm not sure) apply effects on them (e.g. reverb). In that case the hardware mixer would possibly have been faster. In this case, you would create a buffer for the music, and several buffer for sound effects. You would fill them up directly with the complete track or sounds (or maybe in big chunks but you would not be chasing the cursors) and start playing them. The Direct Sound mixer would combine them itself.
got it. thanks guys. looking forward to continuing HMH!