Audio Bug Fixed!!!


Today I finally got audio working! Check out the Twitter thread before reading further please: https://twitter.com/AndrewNReinke/status/1457417298470313987

I marked this as a tech post to best describe what I needed to do and where my problems arose.

First and foremost, you've gotta output some amount of audio samples every single frame of your game.
So in my case, I needed to output at max, 800 audio frames per game frame as i'm blitting the video frames 60 times a second and also updating all game state 60 times a second.

Technically I guess you could output 1 audio frame for every 1 game frame to be equivalent, but I don't think you'll be able to make any cool audio until your second frame as you need the wave to go up and down to actually move the speaker in and out to produce a sound so yea, not really viable. The thing that got me most on this was the fact that it's some larger number of samples per frame that's being explicitly outputted to the player which was hard for me to wrap my head around until now.

But, one thing I have learned is that the number of audio frames you output actually does depend on how much time your frame took up to the point of playing audio in so far as I can tell. So since i'm trying to output each frame within 16.6 milliseconds, if I were to get to outputting audio at 10.0 milliseconds leftover, I should only output 6.6 milliseconds or so of audio for that frame. That or i'm still misunderstanding why i'm outputting a reduced amount of audio frames when submitting audio to Windows via DirectSound (which I swapped to after having a hard time with WASAPI (recommended viewing if you're interested in using DirectSound)). So this bug might just be Windows specific so maybe take what i'm saying here with a grain of salt.

Anyways, the problem I was having for about 1-2 days after switching to DirectSound was that I was actually telling my game side of things that all 800 audio samples would be played even though like 100 were being played. Due to this, my audio skipped a ton so after making it so you don't actually advance any audio clips until the audio gets outputted to Windows, I now don't have this bug.

As for the latency reduction, I think it has to do with the fact that I A: didn't really know what I was doing with WASAPI and B: DirectSound being much easier to use / having a good resource on how to use that API (it's also really simple when you get down to using it in the way that HMH does with the whole just ensuring you wrap properly in the audio circular buffer that continually reads while you write into it).

Either way, I have learned more about the world of audio programming that i'm still quite newbish at but better than before so that's always a positive I suppose!

Thank you for reading and have a nice day! Also, let me know your thoughts on the current build with the no input lag audio playing (sorry for audio still being scratchy as I need to figure out why i'm not actually hitting 60 frames a second on the game so yea, that's the next fix).

Files

Funmi_Zip.7z 4.1 MB
Nov 07, 2021

Get Funmi

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.