Using WASAPI for sound

Does anybody here have experience using WASAPI for audio? I've run into a problem with it:

So in WASAPI, you grab the audio buffer with
1
audioRenderClient.GetBuffer(numAudioSamplesToGrab, &buffer)

then you can write the audio data into that buffer, and queue those frames for playing with
1
audioRenderClient.ReleaseBuffer(numAudioSamplesWritten)

And as far as I can tell, the next call to GetBuffer will give you a buffer starting after the frames you wrote last. This is where my problem is. Consider this game loop:
1
2
3
4
5
6
7
8
9
while(running)
{
    updateAndRenderGame();

    //write next 1/60 of a second of audio
    writeAudio();

    waitUntilNextFrame();
}

If the game is running at 60fps this is fine, but as soon as the game slows down the audio will cut out and probably sound horrible.

Ideally I would write the next second or so of audio every frame, so that if the game's framerate stutters at all the audio will not cut out, and then on the next frame this excess audio will get overwritten. But with my current understanding of WASAPI, this would not be possible because each write would have to start after the last write. If I wanted to write the next second of audio, I would also be introducing a second of latency :ohmy:

So what's the solution to this? Do audio on a separate thread from the game? Or am I just fundamentally misunderstanding how to use WASAPI?

I feel like it's probably the latter, since I don't think this is a problem with DirectSound which is implemented on top of WASAPI (though I'll admit I haven't watched all of Casey's audio videos yet).

Edited by Hugo Burd on Reason: Additional information
Yes, you need to introduce some latency. Not whole second though. But something like 1/2 or maybe 1/4 of next frame. Request buffer for 1 frame + max latency, get current playback position and write enough data so audio can have uninterrupted playback till next frame flip + latency.

The good thing is that you can control what latency will be instead of DirectSound providing whatever latency it wants.
Ah, thanks. I was confused because there was no way to tell how much audio to write until the next frame. But I guess in 99% of cases the framerate won't differ by more than half a frame.

Cheers!