[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.4 Mix_OpenAudio

int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize)

frequency
Output sampling frequency in samples per second (Hz).
you might use MIX_DEFAULT_FREQUENCY(22050) since that is a good value for most games.
format
Output sample format.
channels
Number of sound channels in output.
Set to 2 for stereo, 1 for mono. This has nothing to do with mixing channels.
chunksize
Bytes used per output sample.

Initialize the mixer API.
This must be called before using other functions in this library.
SDL must be initialized with SDL_INIT_AUDIO before this call. frequency would be 44100 for 44.1KHz, which is CD audio rate. Most games use 22050, because 44100 requires too much CPU power on older computers. chunksize is the size of each mixed sample. The smaller this is the more your hooks will be called. If make this too small on a slow system, sound may skip. If made to large, sound effects will lag behind the action more. You want a happy medium for your target computer. You also may make this 4096, or larger, if you are just playing music. MIX_CHANNELS(8) mixing channels will be allocated by default. You may call this function multiple times, however you will have to call Mix_CloseAudio just as many times for the device to actually close. The format will not changed on subsequent calls until fully closed. So you will have to close all the way before trying to open with different format parameters.

format is based on SDL audio support, see SDL_audio.h. Here are the values listed there:

AUDIO_U8
Unsigned 8-bit samples
AUDIO_S8
Signed 8-bit samples
AUDIO_U16LSB
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 compatability 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

MIX_DEFAULT_FORMAT is the same as AUDIO_S16SYS.

Returns: 0 on success, -1 on errors

 
// start SDL with audio support
if(SDL_Init(SDL_INIT_AUDIO)==-1) {
    printf("SDL_Init: %s\n", SDL_GetError());
    exit(1);
}
// open 44.1KHz, signed 16bit, system byte order,
//      stereo audio, using 1024 byte chunks
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1) {
    printf("Mix_OpenAudio: %s\n", Mix_GetError());
    exit(2);
}

See Also:
4.1.5 Mix_CloseAudio, 4.1.8 Mix_QuerySpec, 4.3.1 Mix_AllocateChannels


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on November, 13 2009 using texi2html