Handmade Hero»Forums»Code
2 posts
[Day 146] - Audio gets super loud when window is minimized
Edited by deatheater on Reason: this and thats
Hey folks,

After day 146 I have noticed the audio playing gets super loud and clipped if the window is minimized. Even if DSBCAPS_GLOBALFOCUS is not present it is still possible to get your ears blown for a brief second before windows take over and make the sound silent.

Anyone that knows what is causing that mind explaining it here?

Thanks a lot in advance and also I know that is probably fixed later by Casey but I'm curious about what is causing it at the current state of the code (day 146).
Simon Anciaux
1109 posts
[Day 146] - Audio gets super loud when window is minimized
Before reading my answer you might want to try to figure it out for yourself as it's a good exercise and it's not that hard to figure out.

When you minimize the window, the volume goes up. So the first thing I did was to check the code that outputs sounds by setting a breakpoint in OutputPlayingSounds and check the requested volume for the music (it's the only sound when you launch the game). When the window is minimized, the value are high: it was 15 for the left channel and 17 for the right channel and I believe the volume should never be outside the 0 to 1 range.

My next step was to set a data breakpoint on &PlayingSound->CurrentVolume to see who was modifying it. It didn't work, I'm not sure if it's an issue with the debugger or me not understanding something. The idea for the data breakpoint was that some unrelated code was writing to that memory location by error and so I wanted to see who was doing that.

Since the data breakpoint didn't work, I did a text search to find out who modifies the volume variable. That gave me the ChangeVolume function. So I searched for who's calling the ChangeVolume function. There are only 5 calls and only one doesn't hardcode the volume value.

In handmade.cpp starting at line 980, the game is computing the left/right value for the music volume based on the mouse position in the window. When the window is minimized, the Input->MouseX value is big (~33000 in my case). So I checked where this value comes from. In win32_handmade.cpp starting at line 1493, the game is getting the mouse position and converting it to client space position. GetCursorPos returns a correct value, but ScreenToClient doesn't return a correct value when the window is minimized.
2 posts
[Day 146] - Audio gets super loud when window is minimized
That is cool, thanks for the walkthrough of your debug process!
1 posts
[Day 146] - Audio gets super loud when window is minimized
Edited by OliviaEllison on
Ohh, very detailed! Big thx you! Saved!!