j0h
j0h

Reputation: 1784

SDL MIX_LOADWAV() crashes on loading some wav files

I have a C++ Program that loads wave files. and accepts keystrokes to function as a piano. Recently, I tried changing the wav files from notes from the internet, to live recording notes. The only difference in the files might be that the live recordings are in stereo. in any event, when I give Mix_LoadWAV(a.wav) it returns null [For example, all the live recordings load null].

Is there any limitions to the sdl 1.2 MIX_LOADWAV() I should konw about?

bool LoadFiles()
{
    Background = LoadImage("graphics/background.bmp");

    cNote = Mix_LoadWAV("notes/c.wav" );
    csNote = Mix_LoadWAV("notes/cs.wav" );
    dNote = Mix_LoadWAV("notes/d.wav" );
    dsNote = Mix_LoadWAV("notes/ds.wav" );
    eNote = Mix_LoadWAV("notes/e.wav" );
    fNote = Mix_LoadWAV("notes/f.wav" );
    fsNote = Mix_LoadWAV("notes/fs.wav" );
    gNote = Mix_LoadWAV("notes/g.wav" );
    gsNote = Mix_LoadWAV("notes/gs.wav" );
    aNote = Mix_LoadWAV("notes/a.wav" );
    asNote = Mix_LoadWAV("notes/as.wav" );
    bNote = Mix_LoadWAV("notes/b.wav" );
    highCNote = Mix_LoadWAV("notes/highC.wav" );

    if(Background == NULL || cNote == NULL || csNote == NULL || dNote == NULL || dsNote == NULL || eNote == NULL || fNote == NULL || fsNote == NULL || gNote == NULL || gsNote == NULL || aNote == NULL || asNote == NULL || bNote == NULL || highCNote == NULL)
    {
        printf("File load error\n");
        return false;
    }

    return true;
}

Upvotes: 1

Views: 453

Answers (1)

Sciborg
Sciborg

Reputation: 526

I think I figured out the problem with SDL Mixer's WAV loading function, after much hardship today. I re-recorded some sound effects, and once I replaced them, the game I was working on crashed completely. After some error tracing, I deduced it was Mixer refusing to load the new WAV files, but I could not figure out why.

Then it occurred to me that I had recorded the new sound effects in Audacity, and had saved them as 32-bit signed PCM, instead of 16-bit PCM.

enter image description here

It turns out that Load_WAV does not support 32-bit signed PCM WAV files, and will crash if you try to load them. Instead, you have to convert them to 16-bit signed PCM WAV files, i.e. using a program like Audacity. Once you do that, it works perfectly.

Hopefully this helps someone out who was as confused as me and OP! As far as I know the documentation never says this limitation anywhere, so I'm sure it has tripped up a lot of people.

(Other place this was noted: https://gamedev.stackexchange.com/questions/136817/how-to-get-sdl2-to-play-32bit-wav-files)

Upvotes: 1

Related Questions