40 posts
Help with loading wav files (Day 138)
Edited by BernFeth on Reason: Initial post
Hey guys,

I am in day 138, implementing the loading of wave files but I am getting extremely bad sound artifacts.

The file is being loaded correctly, all the Asserts in the DEBUGLoadWAV function are ok so I believe the data is also being loaded correctly, but somehow it sounds like some extremely high pitch and loud noise.

I am using SDL on Mac and I have the same setup for the audio as the handmade hero SDL branch on github, also I can play a sine wave and it sounds just fine so I'm guessing that side is not at fault.

The only other thing I have that is different from handmade hero is that my DEBUGReadEntireFile is using an arena with PushSizes, I am making the PushSizes that have anything to do with audio and/or assets to be aligned by 1 so the code is the same as Casey but that didn't help.

Any ideas of what I'm doing wrong? Should I post some specific part of the code here?

Thanks!
Simon Anciaux
1241 posts
Help with loading wav files (Day 138)
You should post the whole code, so we could build it and try to figure it out. Or better make the smallest reproduction code possible, as it will make you review your code and often figure out the problem.

I don't have a mac, so I won't be able to compile it, but I would suggest you create a wav file that only contains a sine wave at a known frequency and amplitude, and when you read the file, make sure the samples make sens (should start at zero, go up to maximum amplitude, than back to zero). It's tedious to do, and alternatively you can try to graph each samples to visually see it you get a sine wave.

Before doing that, make sure your wave file has the correct bits per sample, frequency and number of channels (but I assume that's what the asserts are testing).
40 posts
Help with loading wav files (Day 138)
Hi Simon,

mrmixer
create a wav file that only contains a sine wave at a known frequency and amplitude, and when you read the file, make sure the samples make sens (should start at zero, go up to maximum amplitude, than back to zero).

I have done that with audacity (the audio tool),

and the results are confusing:

In the watch window I can see the following
 1 2 3 4 5 6 7 8 9 GameState->TestSound.Samples[0][0]: 1 GameState->TestSound.Samples[0][1]: 1884 GameState->TestSound.Samples[0][2]: 3770 GameState->TestSound.Samples[0][3]: 7484 ... GameState->TestSound.Samples[0][33]: 31001 GameState->TestSound.Samples[0][34]: 30339 GameState->TestSound.Samples[0][35]: 29576 ... 

Which makes sense for the sine wave I'm assuming, however, at the same very watch window, at the very same time, I get the following:

 1 SampleValue: -31945 

SampleValue will have -31945 in it always even after I just seen the correct numbers in TestSound.Samples right before reading it into SampleValue:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 GAME_GET_SOUND_SAMPLES(GameGetSoundSamples) { game_state *GameState = (game_state *)Platform->Memory->PermanentStorage; if (GameState->TestSound.SampleCount) { i16 *SampleOut = Platform->SoundBuffer->Samples; i32 SampleIndex; for (SampleIndex = 0; SampleIndex < Platform->SoundBuffer->SampleCount; ++SampleIndex) { u32 SampleIndex = (GameState->TestSampleIndex + SampleIndex) % GameState->TestSound.SampleCount; i16 SampleValue = GameState->TestSound.Samples[0][SampleIndex]; *SampleOut++ = SampleValue; *SampleOut++ = SampleValue; } GameState->TestSampleIndex += Platform->SoundBuffer->SampleCount; } } 

So, I'm confused... The values are there and they seem correct, but they wont go into "SampleValue" variable correctly... They all turn into -31945.

About posting the entire code, I can't do that unfortunately since my code isn't exactly handmade hero's code, and it's quite big.

I can post my loadwav function but that is exactly as Day 138 from handmade hero.
40 posts
Help with loading wav files (Day 138)
Oh boy,

was this an owl of shame! Haha

So, turns out I was both mistyping SampleIndex but not only, even overwriting itself.

That has fixed the issue.

Your debugging suggestion has helped me figure this out, thanks a lot Simon!
Mārtiņš Možeiko
2405 posts / 2 projects
Help with loading wav files (Day 138)
You should really enable warnings as errors for your compiler (and increase warning level). These kind of mistakes is what compiler is good at catching - you just need to enable this.

In your case you would get "uninitialized variable used" and/or "declaration of X hides previous local declaration" warnings.

For MSVC use "-W4 -WX" compiler arguments and then disable annoying warnings with "-wdXXX"
40 posts
Help with loading wav files (Day 138)
Edited by BernFeth on
mmozeiko
You should really enable warnings as errors for your compiler (and increase warning level). These kind of mistakes is what compiler is good at catching - you just need to enable this.

Very well, it's true I have neglected that in this project.

Thanks for reminding me of it. Since I'm using gcc I have googled around and it seems I should use these:

 1 -Wall -Werror 

There is also a -Wextra but that seems to be about c++ only but I am using c 89 at the moment.

Now that I got sound working, an idea/question came up on my mind:

How to take into account the beat of a song, in other words, is it possible, easy or extremely hard to have entities of the game hop around on the beat of the music that is playing? I can think of a few ways to try that out, but my concern is that if the initial setup isn't perfect they will get incrementally out of sync with time. Also, inconsistent frame rate worries me about this sync issue.

Any ideas?