| Games and SDL SDL Installation SDL for Embedded SDL API SDL Events | SDL Graphics SDL Threads Thread Example SDL Animation SDL Sound | Raw Video Player Video Formats Video Compression | Game Trees About The Author |
| 1 | 2 |
SDL_mixer is a sound mixing library that is used with the SDL library. It allows a programmer to use multiple samples along with music; it simplifies the handling of loading and playing samples and music from different kinds of file formats. It supports the following formats.
| File Format | Comments |
|---|---|
| WAVE/RIFF | ( .wav ) |
| AIFF | ( .aiff ) |
| VOC | ( .voc ) |
| MOD | (.mod .xm .s3m .669 .it .med and more) using including mikmod |
| MIDI | (.mid) using timidity or native midi hardware |
| OggVorbis | (.ogg) requiring ogg/vorbis libraries on system |
| MP3 | (.mp3) requiring SMPEG library on system |
| also any command-line player, which is not mixed by SDL_mixer... | |
To use the mixer library, you have to include the header file "SDL_mixer.h" in your program:
| AUDIO_U8 | Unsigned 8-bit samples |
| AUDIO_S8 | Signed 8-bit samples |
| AUDIO_U16LSB &nssp; | Unsigned 16-bit samples, in little-endian byte order |
| AUDIO_S16LSB | Signed 16-bit samples, in little-endian byte order |
| AUDIO_U16MSB | Unsigned 16-bit samples, in big-endian byte order |
| AUDIO_S16MSB | Signed 16-bit samples, in big-endian byte order |
| AUDIO_U16 | same as AUDIO_U16LSB (for backwards compatibility probably) |
| AUDIO_S16 | same as AUDIO_S16LSB (for backwards compatability probably) |
| AUDIO_U16SYS | Unsigned 16-bit samples, in system byte order |
| AUDIO_S16SYS | Signed 16-bit samples, in system byte order |
The following are some of the more commonly used mixer functions.
| Synopsis |
#include "SDL_mixer.h"
int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize); |
|||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Description |
This function must be called before using other mixer functions.
It initializes the mixer API where
|
|||||||||
| Returns | 0 on success, -1 on failure |
| Synopsis |
#include "SDL_mixer.h"
Mix_Music *Mix_LoadMUS ( const char *file ) |
|
|---|---|---|
| Description | Loads the music file with filename specified by file. It returns a pointer to a Mix_Music struct which will be used as a handle for future reference of the data of the file. | |
| Returns | A pointer to a Mix_Music struct on success, NULL on errors |
| Synopsis |
#include "SDL_mixer.h"
int Mix_PlayMusic ( Mix_Music *music, int loops ) |
|
|---|---|---|
| Description | Plays the loaded music pointed by music. Parameter loops specify the number of times to play through the music, 0 for playing once and -1 for playing an infinite number of times. | |
| Returns | A pointer to a Mix_Music struct on success, NULL on errors |
| Synopsis |
#include "SDL_mixer.h"
void Mix_HookMusic ( void (*mix_func)(void *udata, Uint8 *stream, int len), void *arg ) |
|
|---|---|---|
| Description | Sets up a custom music player function. The function will be called with arg passed into the udata parameter when the mix_func is called. The stream parameter passes in the audio stream buffer to be filled with len bytes of music. The music player will then be called automatically when the mixer needs it. Music playing will start as soon as this is called. All the music playing and stopping functions have no effect on music after this. Pause and resume will work. Using a custom music player and the internal music player is not feasible as the custom music player takes priority. You can stop the custom music player by calling Mix_HookMusic(NULL, NULL). | |
| Returns | none |
The following piece of code, music.cpp presents the usage of SDL_mixer functions to load an ogg file and plays the music. The ogg format is open-source sound data compression standard which achieves better compression than the popular MP3. Detailed information about it can be found at http://www.vorbis.com/
The code is simple and straight forward. The playing parameters are hard-coded with the playback frequency set to 22050 Hz and audio sample size set to 16-bit. The function Mix_OpenAudio() initializes the SDL_mixer with the chosen audio settings and Mix_LoadMUS() loads and uncompresses the data from the music file "music.ogg" into memory. The music will be played repeatedly as the loops parameter in Mix_PlayMusic() is set to -1. When the key 'p' is pressed, the function Mix_Pause() is called to pause playing, and playing can be resumed by pressing the key 'r', which calls Mix_ResumeMusic() to resume playing the music.
/* music.cpp -- demo program for producing music and sound Compile: g++ -o music music.cpp -I/usr/include -L/usr/local/lib -lSDL -lSDL_mixer Execute: ./music ( need the file music.ogg to play ) See http://www.webkinesia.com/games/sdl-sdl1.php or http://www.libsdl.org/projects/docs/SDL_mixer/SDL_mixer.html for tutorials of using SDL_mixer */ #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_mixer.h" #include <string> #include <iostream> #include <stdlib.h> using namespace std; bool musicPlaying = true; void musicFinished() { //Music is done! musicPlaying = false; } int main () { SDL_Surface *screen; SDL_Event event; int status, key; bool quit = false; // Fire up SDL, init VIDEO as well as AUDIO if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0) { printf("Error initializing SDL: %s\n", SDL_GetError()); return 1; } atexit(SDL_Quit); // Set a video mode. Force 16-bit hicolor. if ((screen = SDL_SetVideoMode(640, 480, 16, 0)) == NULL) { printf("Error setting a video, 16-bit video mode: %s\n", SDL_GetError()); return 1; } Mix_Music *music; //Pointer to our music, in memory int audio_rate = 22050; //Frequency of audio playback Uint16 audio_format = AUDIO_S16SYS; //Format of the audio we're playing int audio_channels = 2; //2 channels = stereo int audio_buffers = 4096; //Size of the audio buffers in memory //Initialize SDL_mixer with our chosen audio settings if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) { printf("Unable to initialize audio: %s\n", Mix_GetError()); return 1; } //Load our OGG file from disk music = Mix_LoadMUS("music.ogg"); if(music == NULL) { printf("Unable to load OGG file: %s\n", Mix_GetError()); return 1; } //Play that funky music! // -1 in argument means play music again and again, if 0, means play once if(Mix_PlayMusic(music, -1) == -1) { printf("Unable to play OGG file: %s\n", Mix_GetError()); return 1; } //Make sure that the musicFinished() function is called when the music stops playing Mix_HookMusicFinished(musicFinished); while ( !quit ) { status = SDL_WaitEvent(&event); //wait indefinitely for an event to occur //event will be removed from event queue if ( !status ) { //Error has occured while waiting printf("SDL_WaitEvent error: %s\n", SDL_GetError()); quit = true; break; } switch(event.type) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: key = event.key.keysym.sym; if ( key == SDLK_p ) { //key 'p' pressed to pause music Mix_PauseMusic(); //pause music musicPlaying = false; } else if ( key == SDLK_r ) { //key 'r' pressed to resume music if ( !musicPlaying ) { Mix_ResumeMusic(); //resume playing music musicPlaying = true; } } else if ( key == SDLK_ESCAPE ) //quit if 'ESC' pressed quit = true; break; } //switch SDL_Delay ( 100 ); //give up some cpu time for others } //while ( !quit ) //quit may be set to true when 'ESC' is pressed //Release the memory allocated to our music Mix_FadeOutMusic( 1000 ); //fade out music in 1 sec Mix_HaltMusic(); //stop the music Mix_FreeMusic(music); //free up the allocated resources //Need to make sure that SDL_mixer and SDL have a chance to clean up Mix_CloseAudio(); return 1; } |