Wiki Page Content

Revision 4 as of 2009-11-19 06:52:40

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 parameters; see Remarks for more information

obtained

a pointer to an SDL_AudioSpec structure to be filled in

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

/* Prototype of our callback function */
void my_audio_callback(void *userdata, Uint8 *stream, int len);

/* Open the audio device */
SDL_AudioSpec *desired, *obtained;
SDL_AudioSpec *hardware_spec;

/* Allocate a desired SDL_AudioSpec */
desired = malloc(sizeof(SDL_AudioSpec));

/* Allocate space for the obtained SDL_AudioSpec */
obtained = malloc(sizeof(SDL_AudioSpec));

/* 22050Hz - FM Radio quality */
desired->freq=22050;

/* 16-bit signed audio */
desired->format=AUDIO_S16SYS;

/* Mono */
desired->channels=1;

/* Large audio buffer reduces risk of dropouts but increases response time */
desired->samples=8192;

/* Our callback function */
desired->callback=my_audio_callback;

desired->userdata=NULL;

/* Open the audio device */
if ( SDL_OpenAudio(desired, obtained) < 0 ){
  fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
  exit(-1);
}
/* desired spec is no longer needed */
free(desired);
hardware_spec=obtained;
.
.
/* Prepare callback for playing */
.
.
.
/* Start playing */
SDL_PauseAudio(0);

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: ms = (samples*1000)/freq

  • 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.

SDL_OpenAudio() reads these fields from desired and attempts to find a matching audio configuration. As mentioned above, if obtained is NULL then SDL with convert from your desired audio settings to the hardware settings as it plays.

If obtained is NULL then desired is your working specification, otherwise obtained is filled in with the working specification. The data in the working specification is used when building SDL_AudioCVT's for converting loaded data to the hardware format.

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