Handmade Hero » Forums » Code » Sound buffer section.
Carlos Gabriel Hasbun Comandari
32 posts

|· noise ·|

#1048 Sound buffer section.
2 years, 10 months ago

I am having difficulty visualizing the sound buffer in chapter 8 and 9 and how the play cursor and write cursor advance with respect to the ByteToLock and BytesToWrite.

|· We were all born equal... except for all those things that set us apart. ·|
Iker Murga
43 posts
#1051 Sound buffer section.
2 years, 10 months ago

It is not possible to write between the PlayCursor and the WriteCursor, this as an area that if written to would cause audio problems since direct sound is in the middle of playing it.

ByteToLock is a place further down in which the code writes the audio samples to, due to the way it is calculated (using the RunningSampleIndex which keeps track of the sample we are writing) it is ahead of the WriteCursor so there is no problem with this. On day 19 or 20 Casey runs into the problem of writing within that area, so even if for the day you are on you can "ignore" it, you should still try to remember that concept.

BytesToWrite is simply the area that encompasses the samples the code will write into the buffer. It changes from day 8 to day 9, since on day 8 it writes onto the whole buffer (excluding the area between the PlayCursor and ByteToLock), but on day 9 it only writes the area between the ByteToLock and the TargetCursor, which is still ahead of the WriteCursor but is a smaller sample (writing less samples means that if the audio is changed, the change is reflected sooner than otherwise).

You can try seeing the values change by setting a breakpoint within the GetCurrentPosition call within the game loop and watching the values on the watch window, you will see they remain in the same memory "position" to each other (they change due to the ring-style sound buffer, but they are still in the same relative positions within that sound buffer loop).

If you look at the weekly illustrated summaries I made in the Coding Resources section there is a diagram of it on week 2, I made a more "in depth" one after watching day 8 you can see on https://twitter.com/MisterIker/status/537978302640975872 , it doesn't however take day 9's latency into account, but neither did the illustration for week 2. Do let me know if they help.

I am currently trying to redo the first three weeks to a better level of detail / explanation (and fixing a few mistakes that I was not happy to discover), but that meant falling behind on doing the last two weeks. I am now trying to do the streams as they occur and will hopefully have week 6 on time and then will fill the blank weeks (I have fortunately being able to follow the stream up to date until now, if not that would be impossible... or at least pretty difficult).