1SDL_OpenAudio(3)               SDL API Reference              SDL_OpenAudio(3)
2
3
4

NAME

6       SDL_OpenAudio - Opens the audio device with the desired parameters.
7

SYNOPSIS

9       #include "SDL.h"
10
11       int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained);
12

DESCRIPTION

14       This  function  opens the audio device with the desired parameters, and
15       returns 0 if successful, placing the actual hardware parameters in  the
16       structure  pointed  to by obtained. If obtained is NULL, the audio data
17       passed to the callback  function  will  be  guaranteed  to  be  in  the
18       requested  format,  and will be automatically converted to the hardware
19       audio format if necessary. This function returns -1  if  it  failed  to
20       open the audio device, or couldn't set up the audio thread.
21
22       To open the audio device a desired SDL_AudioSpec must be created.
23
24       SDL_AudioSpec *desired;
25       .
26       .
27       desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
28
29        You  must  then fill this structure with your desired audio specifica‐
30       tions.
31
32       desired->freq
33
34       desired->format
35
36       desired->samples
37
38       desired->callback
39
40       void callback(void *userdata, Uint8 *stream, int len);
41
42        userdata is the pointer stored in userdata field of the SDL_AudioSpec.
43       stream  is a pointer to the audio buffer you want to fill with informa‐
44       tion and len is the length of the audio buffer in bytes.
45
46       desired->userdata
47
48       SDL_OpenAudio reads these fields from the desired SDL_AudioSpec  struc‐
49       ture  pass  to the function and attempts to find an audio configuration
50       matching your desired. As mentioned above, if the obtained parameter is
51       NULL  then  SDL  with  convert  from your desired audio settings to the
52       hardware settings as it plays.
53
54       If obtained is NULL then the  desired  SDL_AudioSpec  is  your  working
55       specification, otherwise the obtained SDL_AudioSpec becomes the working
56       specification and the desirec specification can be deleted. The data in
57       the working specification is used when building SDL_AudioCVT's for con‐
58       verting loaded data to the hardware format.
59
60       SDL_OpenAudio calculates the size  and  silence  fields  for  both  the
61       desired  and  obtained  specifications. The size field stores the total
62       size of the audio buffer in bytes, while the silence stores  the  value
63       used to represent silence in the audio buffer
64
65       The  audio  device  starts  out  playing  silence when it's opened, and
66       should be enabled for playing by calling SDL_PauseAudio(0) when you are
67       ready  for  your  audio callback function to be called. Since the audio
68       driver may modify the requested size of the audio  buffer,  you  should
69       allocate any local mixing buffers after you open the audio device.
70

EXAMPLES

72       /* Prototype of our callback function */
73       void my_audio_callback(void *userdata, Uint8 *stream, int len);
74
75       /* Open the audio device */
76       SDL_AudioSpec *desired, *obtained;
77       SDL_AudioSpec *hardware_spec;
78
79       /* Allocate a desired SDL_AudioSpec */
80       desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
81
82       /* Allocate space for the obtained SDL_AudioSpec */
83       obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
84
85       /* 22050Hz - FM Radio quality */
86       desired->freq=22050;
87
88       /* 16-bit signed audio */
89       desired->format=AUDIO_S16LSB;
90
91       /* Mono */
92       desired->channels=0;
93
94       /* Large audio buffer reduces risk of dropouts but increases response time */
95       desired->samples=8192;
96
97       /* Our callback function */
98       desired->callback=my_audio_callback;
99
100       desired->userdata=NULL;
101
102       /* Open the audio device */
103       if ( SDL_OpenAudio(desired, obtained) < 0 ){
104         fprintf(stderr, "Couldn't open audio: %s
105       ", SDL_GetError());
106         exit(-1);
107       }
108       /* desired spec is no longer needed */
109       free(desired);
110       hardware_spec=obtained;
111       .
112       .
113       /* Prepare callback for playing */
114       .
115       .
116       .
117       /* Start playing */
118       SDL_PauseAudio(0);
119

SEE ALSO

121       SDL_AudioSpec, SDL_LockAudio, SDL_UnlockAudio, SDL_PauseAudio
122
123
124
125SDL                         Tue 11 Sep 2001, 22:58            SDL_OpenAudio(3)
Impressum