Wiki Page Content

Revision 1 as of 2009-11-17 21:59:32

Clear message

SDL_OpenAudio

Use this function to open the audio device with the desired parameters.

Syntax

int SDL_OpenAudio(SDL_AudioSpec* desired,
                  SDL_AudioSpec* obtained)

Function Parameters

desired

the desired audio device; see Remarks

obtained

the SDL_AudioSpec structure to receive the data

Return Value

This function opens the audio device with the desired parameters, and returns 0 if successful, placing the actual hardware parameters in the structure pointed to by obtained.

If obtained is NULL, the audio data passed to the callback function will be guaranteed to be in the requested format, and will be automatically converted to the hardware audio format if necessary.

This function returns a negative error code on failure to open the audio device or failure to set up the audio thread; call SDL_GetError() for more information.

Code Examples

You can add your code example here

Remarks

To open the audio device a desired SDL_AudioSpec must be created.

SDL_AudioSpec *desired;
.
.
desired = malloc(sizeof(SDL_AudioSpec));

When filling in the desired audio spec structure,

  • desired->freq should be the desired audio frequency in samples-per-second

  • desired->format should be the desired audio format

  • desired->samples is the desired size of the audio buffer, in samples. This number should be a power of two, and may be adjusted by the audio driver to a value more suitable for the hardware. Good values seem to range between 512 and 8096 inclusive, depending on the application and CPU speed. Smaller values yield faster response time, but can lead to underflow if the application is doing heavy processing and cannot fill the audio buffer in time. A stereo sample consists of both right and left channels in LR ordering. Note that the number of samples is directly related to time by the following formula: \code ms = (samples*1000)/freq \endcode

  • desired->size is the size in bytes of the audio buffer, and is calculated by SDL_OpenAudio()

  • desired->silence is the value used to set the buffer to silence, and is calculated by SDL_OpenAudio()

  • desired->callback should be set to a function that will be called when the audio device is ready for more data. It is passed a pointer to the audio buffer, and the length in bytes of the audio buffer. This function usually runs in a separate thread, so you should protect data structures that it accesses by calling SDL_LockAudio() and SDL_UnlockAudio() in your code.

  • desired->userdata is passed as the first parameter to your callback function.

The audio device starts out playing silence when it is opened, and should be enabled for playing by calling SDL_PauseAudio(0) when you are ready for your audio callback function to be called. Since the audio driver may modify the requested size of the audio buffer, you should allocate any local mixing buffers after you open the audio device.

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit