Learning to use SDL_Mixer

For the past few months I’ve been learning SDL 1 from this book I bought. I’m still pretty new, but I have managed to build a few cool little games. I just got to the chapter in my book about “SDL_Mixer” and I set up the mixer framework so I could start adding sounds to my games. I added some really simple code to one of my games, just so I’d get a sound when the user scores a point. But now when I try to run it, the SDL window opens for a half a second, and then I get this message in the Xcode console:

SDL_Template(10031,0x7fff706d2cc0) malloc: *** error for object 0x101f4ebf0: pointer being freed was not allocated

I know the framework is set up right, because the example program that comes with my book runs fine. So I guess I must have messed up the code. I was hoping that someone could take a look at this – this is all the SDL_Mixer code that I added to my game, and since my game was running fine before I added this code, I’m assuming the problem must be here:

At the top:

Code:
#include “sdl_mixer/sdl_mixer.h”

Then, after the headers:

Code:
Mix_Chunk *succeedSnd = NULL;

Then, in my init() function:

Code:
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096) == -1)
{
return false;
}

if (Mix_AllocateChannels(2) == -1)
{
	return false;
}

Then, in my load_files() function:

Code:
succeedSnd = Mix_LoadWAV(“succeed.wav”);
if (succeedSnd == NULL)
{
return false;
}

Then, in my cleanup() function:

Code:
Mix_AllocateChannels(0);
Mix_FreeChunk(succeedSnd);
Mix_CloseAudio();

And finally, in my increaseDiff() function, which gets called whenever the user scores a point:

Code:
Mix_PlayChannel(-1, succeedSnd, 0);

Anyone see any problems there? From the error message I got, I’m guessing there must be something wrong with my chunk pointer, but I don’t see it. Up until now, when I got errors, they were usually compile errors, and Xcode would highlight the problematic code, and I could usually spot the mistake easily enough. But I really don’t know how to handle this kind of error. The Xcode console is telling me “*** set a breakpoint in malloc_error_break to debug” but I really don’t know how to set up breakpoints, or even what they are, really. I tried reading the Xcode documentation, but it assumes a level of competency that I don’t yet have. Sigh

Thanks for your help.

Okay, I just figured out that the problem isn’t in my code; it’s apparently the audio file I’m using. I just changed this line

Code:
succeedSnd = Mix_LoadWAV(“succeed.wav”);

to this

Code:
succeedSnd = Mix_LoadWAV(“Bomb.wav”);

So it uses the audio file that came with my SDL book, and now my game works fine. This seems weird, because the “Bomb.wav” file is an 8 bit, 11025 Hz file, and I thought that MIX_DEFAULT_FREQUENCY and MIX_DEFAULT_FORMAT were supposed to set it to 16 bit, 22050 Hz, which is the format of my “succeed.wav” file. Obviously there’s a lot I don’t know about the mixer, so I’m going to look for some more info on properly initializing it. Does anyone know where I can find a list of all the constants for the Mix_OpenAudio function?

Thanks.

Maybe this?
http://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC11

Jonny DOn Mon, Jul 7, 2014 at 5:30 PM, Explodey wrote:

Okay, I just figured out that the problem isn’t in my code; it’s
apparently the audio file I’m using. I just changed this line

Code:

succeedSnd = Mix_LoadWAV(“succeed.wav”);

to this

Code:

succeedSnd = Mix_LoadWAV(“Bomb.wav”);

So it uses the audio file that came with my SDL book, and now my game
works fine. This seems weird, because the “Bomb.wav” file is an 8 bit,
11025 Hz file, and I thought that MIX_DEFAULT_FREQUENCY and
MIX_DEFAULT_FORMAT were supposed to set it to 16 bit, 22050 Hz, which is
the format of my “succeed.wav” file. Obviously there’s a lot I don’t know
about the mixer, so I’m going to look for some more info on properly
initializing it. Does anyone know where I can find a list of all the
constants for the Mix_OpenAudio function?

Thanks.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well, I am stumped. My audio file is 16 bit little-endian signed integer, 2 channels, 22050 Hz. So I called the OpenAudio function like this

Code:

Mix_OpenAudio(MIX_DEFAULT_FREQUENCY,AUDIO_S16LSB,2,4096);

But but the same thing still happens: My wav file doesn’t work, and the “Bomb.wav” file works fine. Then I tried experimenting with some other constants, but still no dice. I guess I could just change my audio so it’s the same format as the Bomb.wav file, which is 8 bit little-endian unsigned integer, 1 channel, 11025 Hz. But I really don’t want to use 8 bit audio, plus I wouldn’t even know how to change it to an unsigned integer. So frustrating!

This sounds like a bug. Can you post a bug report with a link to the
complete example and data files?On Mon, Jul 7, 2014 at 12:18 AM, Explodey wrote:

For the past few months I’ve been learning SDL 1 from this book I
bought. I’m still pretty new, but I have managed to build a few cool little
games. I just got to the chapter in my book about “SDL_Mixer” and I set up
the mixer framework so I could start adding sounds to my games. I added
some really simple code to one of my games, just so I’d get a sound when
the user scores a point. But now when I try to run it, the SDL window opens
for a half a second, and then I get this message in the Xcode console:

SDL_Template(10031,0x7fff706d2cc0) malloc: *** error for object
0x101f4ebf0: pointer being freed was not allocated

I know the framework is set up right, because the example program that
comes with my book runs fine. So I guess I must have messed up the code. I
was hoping that someone could take a look at this – this is all the
SDL_Mixer code that I added to my game, and since my game was running fine
before I added this code, I’m assuming the problem must be here:

At the top:

Code:

#include “sdl_mixer/sdl_mixer.h”

Then, after the headers:

Code:

Mix_Chunk *succeedSnd = NULL;

Then, in my init() function:

Code:

if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT,

MIX_DEFAULT_CHANNELS, 4096) == -1)
{
return false;
}

if (Mix_AllocateChannels(2) == -1)
{
return false;
}

Then, in my load_files() function:

Code:

succeedSnd = Mix_LoadWAV(“succeed.wav”);
if (succeedSnd == NULL)
{
return false;
}

Then, in my cleanup() function:

Code:

Mix_AllocateChannels(0);
Mix_FreeChunk(succeedSnd);
Mix_CloseAudio();

And finally, in my increaseDiff() function, which gets called whenever the
user scores a point:

Code:

Mix_PlayChannel(-1, succeedSnd, 0);

Anyone see any problems there? From the error message I got, I’m guessing
there must be something wrong with my chunk pointer, but I don’t see it. Up
until now, when I got errors, they were usually compile errors, and Xcode
would highlight the problematic code, and I could usually spot the mistake
easily enough. But I really don’t know how to handle this kind of error.
The Xcode console is telling me “*** set a breakpoint in malloc_error_break
to debug” but I really don’t know how to set up breakpoints, or even what
they are, really. I tried reading the Xcode documentation, but it assumes a
level of competency that I don’t yet have. Sigh

Thanks for your help.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I’ve never posted a bug report before, so I don’t really know the protocol. Here’s a link to a zip archive of the complete Xcode project:

http://jeremyseanbell.com/FOSDL11_1.zip

And this is the error message I get:

SDL_Template(10031,0x7fff706d2cc0) malloc: *** error for object 0x101f4ebf0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

The link to the Xcode project above is just the SDL_Mixer example program that came with my book, except I changed this line

Code:

g_pChunk[1]=Mix_LoadWAV(“Bomb.wav”);

To this:

Code:

g_pChunk[1]=Mix_LoadWAV(“succeed.wav”);

And that change makes it not work. Change it back and it works fine.

And btw, I converted my “succeed.wav” file to 8 bit little-endian unsigned integer, 1 channel, 11025 Hz, which is exactly the same format as the audio file that does work, “Bomb.wav”. And it still doesn’t work! How does it know?? The only difference between those two files is their name, and I even tried changing the name of my audio file to “Bomb.wav” and putting it in the same directory, but it still doesn’t work. I’ve tried other wav files too, and the only ones that work are the ones that came with my SDL book. How is that possible?

Bomb.wav contains a normal wav header:

00000000 52 49 46 46 58 6d 00 00 57 41 56 45 66 6d 74 20 |RIFFXm…WAVEfmt |

succeed.wav is exported by Pro Tools and there is a JUNK chunk:

00000000 52 49 46 46 b2 27 00 00 57 41 56 45 4a 55 4e 4b |RIFF??’…WAVEJUNK|
00000010 5c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |…|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |…|On Tue, Jul 08, 2014 at 12:29:50AM +0000, Explodey wrote:

I’ve never posted a bug report before, so I don’t really know the protocol. Here’s a link to a zip archive of the complete Xcode project:

http://jeremyseanbell.com/FOSDL11_1.zip

And this is the error message I get:

SDL_Template(10031,0x7fff706d2cc0) malloc: *** error for object 0x101f4ebf0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

The link to the Xcode project above is just the SDL_Mixer example program that came with my book, except I changed this line

Code:

g_pChunk[1]=Mix_LoadWAV(“Bomb.wav”);

To this:

Code:

g_pChunk[1]=Mix_LoadWAV(“succeed.wav”);

And that change makes it not work. Change it back and it works fine.

And btw, I converted my “succeed.wav” file to 8 bit little-endian unsigned integer, 1 channel, 11025 Hz, which is exactly the same format as the audio file that does work, “Bomb.wav”. And it still doesn’t work! How does it know?? The only difference between those two files is their name, and I even tried changing the name of my audio file to “Bomb.wav” and putting it in the same directory, but it still doesn’t work. I’ve tried other wav files too, and the only ones that work are the ones that came with my SDL book. How is that possible?

00000070 62 65 78 74 5a 02 00 00 00 00 00 00 00 00 00 00 |bextZ…|
00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |…|
*
00000170 00 00 00 00 00 00 00 00 50 72 6f 20 54 6f 6f 6c |…Pro Tool|
00000180 73 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |s…|

where

‘All fields except “JUNK data”, “format data” and “wave data” are 32-bit fields’

See pag 10 here: http://tech.ebu.ch/docs/tech/tech3306-2009.pdf

You could change or fix the header of succeed.wav

This is fixed in the latest commit:

Thanks!On Mon, Jul 7, 2014 at 11:32 PM, Tito Latini <tito.01beta at gmail.com> wrote:

On Tue, Jul 08, 2014 at 12:29:50AM +0000, Explodey wrote:

I’ve never posted a bug report before, so I don’t really know the
protocol. Here’s a link to a zip archive of the complete Xcode project:

http://jeremyseanbell.com/FOSDL11_1.zip

And this is the error message I get:

SDL_Template(10031,0x7fff706d2cc0) malloc: *** error for object
0x101f4ebf0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

The link to the Xcode project above is just the SDL_Mixer example
program that came with my book, except I changed this line

Code:

g_pChunk[1]=Mix_LoadWAV(“Bomb.wav”);

To this:

Code:

g_pChunk[1]=Mix_LoadWAV(“succeed.wav”);

And that change makes it not work. Change it back and it works fine.

And btw, I converted my “succeed.wav” file to 8 bit little-endian
unsigned integer, 1 channel, 11025 Hz, which is exactly the same format as
the audio file that does work, “Bomb.wav”. And it still doesn’t work!
How does it know?? The only difference between those two files is their
name, and I even tried changing the name of my audio file to “Bomb.wav” and
putting it in the same directory, but it still doesn’t work. I’ve tried
other wav files too, and the only ones that work are the ones that came
with my SDL book. How is that possible?

Bomb.wav contains a normal wav header:

00000000 52 49 46 46 58 6d 00 00 57 41 56 45 66 6d 74 20
|RIFFXm…WAVEfmt |

succeed.wav is exported by Pro Tools and there is a JUNK chunk:

00000000 52 49 46 46 b2 27 00 00 57 41 56 45 4a 55 4e 4b
|RIFF??’…WAVEJUNK|
00000010 5c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|…|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|…|
*
00000070 62 65 78 74 5a 02 00 00 00 00 00 00 00 00 00 00
|bextZ…|
00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|…|
*
00000170 00 00 00 00 00 00 00 00 50 72 6f 20 54 6f 6f 6c |…Pro
Tool|
00000180 73 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|s…|

where

‘All fields except “JUNK data”, “format data” and “wave data” are 32-bit
fields’

See pag 10 here: http://tech.ebu.ch/docs/tech/tech3306-2009.pdf

You could change or fix the header of succeed.wav


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks Tito, I would never have thought of that on my own. I probably would have given up on coding altogether if it weren’t for all the helpful people on the forums.

And thanks for the update Sam. Forgive my ignorance, but I’m not entirely sure how to update my project. Should I just paste the code into SDL_Mixer.h ? I’m assuming the fix will be a lot easier in the long run than trying to hex edit all my audio files, which is something else I don’t really know how to do!

You need to build SDL from the mercurial branch if you want the recent
updates.On Jul 8, 2014 3:04 AM, “Explodey” wrote:

Thanks Tito, I would never have thought of that on my own. I probably
would have given up on coding altogether if it weren’t for all the helpful
people on the forums.

And thanks for the update Sam. Forgive my ignorance, but I’m not entirely
sure how to update my project. Should I just paste the code into
SDL_Mixer.h ? I’m assuming the fix will be a lot easier in the long run
than trying to hex edit all my audio files, which is something else I don’t
really know how to do!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

If you cannot use the updated code, the follow command removes all the
chunks before the fmt chunk, so you can get wav soundfiles also usable
with 2.0.3.

cc sdl2-config --cflags --libs -o reduce_chunks reduce_chunks.c

./reduce_chunks succeed.wav new_succeed.wav

and the header of new_succeed.wav starts with:

52 49 46 46 ec 24 00 00 57 41 56 45 66 6d 74 20 |RIFF.$…WAVEfmt |

/* reduce_chunks.c */
#include <SDL2/SDL.h>
#include <stdio.h>

#define BUFSIZE (1024)

#define RIFF 0x46464952
#define WAVE 0x45564157
#define FMT 0x20746D66

int main(int argc, char **argv)
{
SDL_RWops *in, *out;
Uint32 size;
char buf[BUFSIZE];

    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_ERROR);

    if (argc != 3) {
            printf("Usage: %s infile outfile\n", argv[0]);
            return 1;
    }
    if ((in = SDL_RWFromFile(argv[1], "r")) == NULL)
            return 2;

    if (SDL_ReadLE32(in) != RIFF || (size = SDL_ReadLE32(in)) == 0 ||
        SDL_ReadLE32(in) != WAVE) {
            SDL_RWclose(in);
            return 1;
    }
    while (size > 0 && SDL_ReadLE32(in) != FMT) {
            Uint32 length = SDL_ReadLE32(in);

            size -= length + 2 * sizeof(Uint32);
            SDL_RWseek(in, length, RW_SEEK_CUR);
    }
    if (size > 0)
            SDL_RWseek(in, -4, RW_SEEK_CUR); /* fmt */

    if ((out = SDL_RWFromFile(argv[2], "w")) == NULL) {
            SDL_RWclose(in);
            return 1;
    }
    SDL_WriteLE32(out, RIFF);
    SDL_WriteLE32(out, size);
    SDL_WriteLE32(out, WAVE);

    while ((size = SDL_RWread(in, buf, 1, BUFSIZE)) > 0)
            SDL_RWwrite(out, buf, size, 1);

    SDL_RWclose(in);
    SDL_RWclose(out);
    return 0;

}On Tue, Jul 08, 2014 at 09:04:14AM +0000, Explodey wrote:

Thanks Tito, I would never have thought of that on my own. I probably would have given up on coding altogether if it weren’t for all the helpful people on the forums.

And thanks for the update Sam. Forgive my ignorance, but I’m not entirely sure how to update my project. Should I just paste the code into SDL_Mixer.h ? I’m assuming the fix will be a lot easier in the long run than trying to hex edit all my audio files, which is something else I don’t really know how to do!

I’m sorry but I really don’t know what to do with this code. I tried typing

Code:
cc sdl2-config --cflags --libs -o reduce_chunks reduce_chunks.c./reduce_chunks /Users/jeremybell/Documents/succeed.wav /Users/jeremybell/Documents/new_succeed.wav

into Terminal, but I just got a bunch of error messages. And I pasted your program into Xcode, and it did something, but I don’t know what. sigh… It must be frustrating trying to help noobs like me.

I really appreciate everyone’s help, but I’m starting to think I’m hopelessly in over my head. I couldn’t figure out how to build SDL from the mercurial branch either. Oh well… maybe I can find an app somewhere that can fix the headers in my wav’s.

Hooray!! I managed to come up with a jury-rigged solution. I exported my sounds from ProTools as aiff’s, then I downloaded a simple app to convert them to wav’s. This will at least tide me over until I figure out how to do it the right way.

Thanks again for all your help, everyone. The whole reason I started learning to code in the first place was because I was interested in doing sound/music for video games, and I thought some coding skills might help get me there. So not being able to add sound to my games was really frustrating! I’m so glad my programs aren’t silent anymore.