# SOUNDS like a problem

## Recommended Posts

Yes, I've been playing more, and decided I wanted to add sound. However, "Focus on SDL" has proven to be a waste of my time for this (it suggests using SDL_Mixer, but that's overkill for just playing 1 wav file [razz]), as the author glossed over it, only mentioning it briefly and saying he'd cover mixer in a later chapter. Nothing fancy, just need to play a sound, with the following properties:
Bit Rate    : 64kbps
Sample Size : 8 bits
Channels    : 1
Sample Rate : 8KHz
Audio Format: PCM

So can anyone point me to a tutorial or something that I can use to convert the info I have into the info I need to populate an SDL_AudioSpec structure to pass to SDL_OpenAudio? Who'd have thought just playing a 5-second wav file would be such a pain? [razz]

##### Share on other sites
SDL's built in audio stuff is a horrible pain to use. It would almost certainly be worth it to use SDL_Mixer.

Alternatively, If you are on Windows and want a quick solution, you could use PlaySound.

##### Share on other sites
I was going for cross-platform compatibility, so, while it is on Windows, I want to avoid any Windoze-centric solutions. Probly should have mentioned that.

I did actually grab the SDL_Mixer, but there seems to be no actual file structure to it (headers aren't in 'include' etc.) Best solution I can see if I can figure out what all the files are is to move them into the folder where SDL resides. Then I'd just need to start an SDL project in Code::Blocks instead of fighting with it to get it to find the pieces of the mixer [razz]

While not needed for this experiment, how does SDL_Mixer fare for games that require higher-end sound? RPGs for example with switching music and such.

You know, I keep going at this rate and I'll be in the "Question a day club", if anyone ever makes one [razz]

##### Share on other sites
Quote:
 Original post by CharabisI was going for cross-platform compatibility, so, while it is on Windows, I want to avoid any Windoze-centric solutions. Probly should have mentioned that.I did actually grab the SDL_Mixer, but there seems to be no actual file structure to it (headers aren't in 'include' etc.) Best solution I can see if I can figure out what all the files are is to move them into the folder where SDL resides. Then I'd just need to start an SDL project in Code::Blocks instead of fighting with it to get it to find the pieces of the mixer [razz]While not needed for this experiment, how does SDL_Mixer fare for games that require higher-end sound? RPGs for example with switching music and such.You know, I keep going at this rate and I'll be in the "Question a day club", if anyone ever makes one [razz]
I would recommend sticking with SDL_mixer. If you're having problems getting it set up, I'm sure you can get some help here or in Alternative Game Libraries.

SDL_mixer is perfectly adequate for most purposes. You mentioned RPGs; the game 'Battle for Wesnoth' uses SDL_mixer to manage sound and music. The game is open source, so you can download the code and check it out if you wish (look at sound.*).

As for setting up SDL_mixer, make sure that you grab the binaries instead of (or in addition to) the source code. It's intended to be used much like SDL: you include the header file(s) and link to the library.

You can also build SDL_mixer yourself if needed. The currently available binary has a bug which causes a crash if SDL_mixer is started up and shut down more than once in a given run of your application (as a result, Battle for Wesnoth will actually crash if you try to change the audio prefs while the game is running). If this is an issue, you can rebuild SDL_mixer with Timidity support disabled to fix this error. This was easy in OS X (the source code package comes with an Xcode project file), but I haven't tried it yet on Windows or Linux.

Again, I suggest you stick with SDL_mixer (and post back if you find you're having trouble using it or setting it up).

##### Share on other sites
Gonna have to switch forums and get more info, since the binary file contains the dlls. Not a lib file in sight.

##### Share on other sites
Grab the SDL_mixer-devel-1.2.7-VC6.zip package (devel stands for development). You were probably trying to download just the runtime [wink]

##### Share on other sites
Ah. The VC threw me. Thought it was a specific compiler :P

I should be able to get it working now...I hope [grin]

EDIT: Took out my last edit. I did something monumentally stupid is why it wouldn't work [lol]

However, now this still doesn't work:

    if (Mix_OpenAudio (MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 4096) == -1)    {        cout << "Failed to initialize Mixer!\n";        return 1;    }    Mix_Chunk* wav = Mix_LoadWAV("sounds\\howl.wav");    Mix_AllocateChannels(1);    Mix_VolumeChunk (wav, MIX_MAX_VOLUME);    Mix_PlayChannel(1, wav,0);

So what noob mistake did I make? Every time I run it, it exits with status 3 unless I remove the Mix_VolumeChunk, at which point I hear nothing. Interestingly enough, I've also tried setting the 'volume' member of wav directly, and it has the same effect (Exit status 3).

[Edited by - Charabis on March 7, 2007 6:15:03 PM]

##### Share on other sites
Quote:
Be sure to check the value of wav after this call to ensure that it is not null. If it is null (which means that loading failed for some reason or another), use Mix_GetError() to find out the cause of the failure.

##### Share on other sites
Sure enough, it wasn't loading the right file (slight diff between 'sounds' and 'sound', although I still don't seem to be getting the sound to play, even though everything does run. Weird.

##### Share on other sites
Quote:
 Original post by CharabisSure enough, it wasn't loading the right file (slight diff between 'sounds' and 'sound', although I still don't seem to be getting the sound to play, even though everything does run. Weird.
A couple more notes:

1. The return value of Mix_PlayChannel() tells you what channel the sound was actually played on, and whether it was played, so you might double check this as well.

2. Be sure to have the SDL_mixer online documentation handy, so that you can refer to the function definitions and see what (if any) information they make available in the case of failure.

3. If your project isn't too long, you might post it in its entirety (use [ source ] tags for large blocks of code), along with information about your development environment and a detailed description of the problem(s) you've encountered. If the error is obvious (and there's not too much code to dig through), someone here will probably be able to spot it.

##### Share on other sites
Just found the problem, thanks :) Forgot the channels started at 0, so playing to channel 1 when I set channels TO 1 meant it was playing on a closed channel [grin]

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627674
• Total Posts
2978558

• 11
• 11
• 10
• 12
• 22