comparison src/audio/mme/SDL_mmeaudio.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
30 #include "SDL_mmeaudio.h" 30 #include "SDL_mmeaudio.h"
31 31
32 static BOOL inUse[NUM_BUFFERS]; 32 static BOOL inUse[NUM_BUFFERS];
33 33
34 /* Audio driver functions */ 34 /* Audio driver functions */
35 static int MME_OpenAudio (_THIS, SDL_AudioSpec * spec); 35 static int MME_OpenAudio(_THIS, SDL_AudioSpec * spec);
36 static void MME_WaitAudio (_THIS); 36 static void MME_WaitAudio(_THIS);
37 static Uint8 *MME_GetAudioBuf (_THIS); 37 static Uint8 *MME_GetAudioBuf(_THIS);
38 static void MME_PlayAudio (_THIS); 38 static void MME_PlayAudio(_THIS);
39 static void MME_WaitDone (_THIS); 39 static void MME_WaitDone(_THIS);
40 static void MME_CloseAudio (_THIS); 40 static void MME_CloseAudio(_THIS);
41 41
42 /* Audio driver bootstrap functions */ 42 /* Audio driver bootstrap functions */
43 static int 43 static int
44 Audio_Available (void) 44 Audio_Available(void)
45 { 45 {
46 return (1); 46 return (1);
47 } 47 }
48 48
49 static void 49 static void
50 Audio_DeleteDevice (SDL_AudioDevice * device) 50 Audio_DeleteDevice(SDL_AudioDevice * device)
51 { 51 {
52 if (device) { 52 if (device) {
53 if (device->hidden) { 53 if (device->hidden) {
54 SDL_free (device->hidden); 54 SDL_free(device->hidden);
55 device->hidden = NULL; 55 device->hidden = NULL;
56 } 56 }
57 SDL_free (device); 57 SDL_free(device);
58 device = NULL; 58 device = NULL;
59 } 59 }
60 } 60 }
61 61
62 static SDL_AudioDevice * 62 static SDL_AudioDevice *
63 Audio_CreateDevice (int devindex) 63 Audio_CreateDevice(int devindex)
64 { 64 {
65 SDL_AudioDevice *this; 65 SDL_AudioDevice *this;
66 66
67 /* Initialize all variables that we clean on shutdown */ 67 /* Initialize all variables that we clean on shutdown */
68 this = SDL_malloc (sizeof (SDL_AudioDevice)); 68 this = SDL_malloc(sizeof(SDL_AudioDevice));
69 if (this) { 69 if (this) {
70 SDL_memset (this, 0, (sizeof *this)); 70 SDL_memset(this, 0, (sizeof *this));
71 this->hidden = SDL_malloc ((sizeof *this->hidden)); 71 this->hidden = SDL_malloc((sizeof *this->hidden));
72 } 72 }
73 if ((this == NULL) || (this->hidden == NULL)) { 73 if ((this == NULL) || (this->hidden == NULL)) {
74 SDL_OutOfMemory (); 74 SDL_OutOfMemory();
75 if (this) { 75 if (this) {
76 SDL_free (this); 76 SDL_free(this);
77 } 77 }
78 return (0); 78 return (0);
79 } 79 }
80 SDL_memset (this->hidden, 0, (sizeof *this->hidden)); 80 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
81 /* Set the function pointers */ 81 /* Set the function pointers */
82 this->OpenAudio = MME_OpenAudio; 82 this->OpenAudio = MME_OpenAudio;
83 this->WaitAudio = MME_WaitAudio; 83 this->WaitAudio = MME_WaitAudio;
84 this->PlayAudio = MME_PlayAudio; 84 this->PlayAudio = MME_PlayAudio;
85 this->GetAudioBuf = MME_GetAudioBuf; 85 this->GetAudioBuf = MME_GetAudioBuf;
94 "waveout", "Tru64 MME WaveOut", 94 "waveout", "Tru64 MME WaveOut",
95 Audio_Available, Audio_CreateDevice 95 Audio_Available, Audio_CreateDevice
96 }; 96 };
97 97
98 static void 98 static void
99 SetMMerror (char *function, MMRESULT code) 99 SetMMerror(char *function, MMRESULT code)
100 { 100 {
101 int len; 101 int len;
102 char errbuf[MAXERRORLENGTH]; 102 char errbuf[MAXERRORLENGTH];
103 103
104 SDL_snprintf (errbuf, SDL_arraysize (errbuf), "%s: ", function); 104 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
105 len = SDL_strlen (errbuf); 105 len = SDL_strlen(errbuf);
106 waveOutGetErrorText (code, errbuf + len, MAXERRORLENGTH - len); 106 waveOutGetErrorText(code, errbuf + len, MAXERRORLENGTH - len);
107 SDL_SetError ("%s", errbuf); 107 SDL_SetError("%s", errbuf);
108 } 108 }
109 109
110 static void CALLBACK 110 static void CALLBACK
111 MME_CALLBACK (HWAVEOUT hwo, 111 MME_CALLBACK(HWAVEOUT hwo,
112 UINT uMsg, DWORD dwInstance, LPARAM dwParam1, LPARAM dwParam2) 112 UINT uMsg, DWORD dwInstance, LPARAM dwParam1, LPARAM dwParam2)
113 { 113 {
114 WAVEHDR *wp = (WAVEHDR *) dwParam1; 114 WAVEHDR *wp = (WAVEHDR *) dwParam1;
115 115
116 if (uMsg == WOM_DONE) 116 if (uMsg == WOM_DONE)
117 inUse[wp->dwUser] = FALSE; 117 inUse[wp->dwUser] = FALSE;
118 } 118 }
119 119
120 static int 120 static int
121 MME_OpenAudio (_THIS, SDL_AudioSpec * spec) 121 MME_OpenAudio(_THIS, SDL_AudioSpec * spec)
122 { 122 {
123 MMRESULT result; 123 MMRESULT result;
124 int i; 124 int i;
125 125
126 mixbuf = NULL; 126 mixbuf = NULL;
127 127
128 /* Set basic WAVE format parameters */ 128 /* Set basic WAVE format parameters */
129 shm = mmeAllocMem (sizeof (*shm)); 129 shm = mmeAllocMem(sizeof(*shm));
130 if (shm == NULL) { 130 if (shm == NULL) {
131 SDL_SetError ("Out of memory: shm"); 131 SDL_SetError("Out of memory: shm");
132 return (-1); 132 return (-1);
133 } 133 }
134 shm->sound = 0; 134 shm->sound = 0;
135 shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM; 135 shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM;
136 136
145 /* Signed 16 bit audio data */ 145 /* Signed 16 bit audio data */
146 spec->format = AUDIO_S16; 146 spec->format = AUDIO_S16;
147 shm->wFmt.wBitsPerSample = 16; 147 shm->wFmt.wBitsPerSample = 16;
148 break; 148 break;
149 default: 149 default:
150 SDL_SetError ("Unsupported audio format"); 150 SDL_SetError("Unsupported audio format");
151 return (-1); 151 return (-1);
152 } 152 }
153 153
154 shm->wFmt.wf.nChannels = spec->channels; 154 shm->wFmt.wf.nChannels = spec->channels;
155 shm->wFmt.wf.nSamplesPerSec = spec->freq; 155 shm->wFmt.wf.nSamplesPerSec = spec->freq;
161 /* Check the buffer size -- minimum of 1/4 second (word aligned) */ 161 /* Check the buffer size -- minimum of 1/4 second (word aligned) */
162 if (spec->samples < (spec->freq / 4)) 162 if (spec->samples < (spec->freq / 4))
163 spec->samples = ((spec->freq / 4) + 3) & ~3; 163 spec->samples = ((spec->freq / 4) + 3) & ~3;
164 164
165 /* Update the fragment size as size in bytes */ 165 /* Update the fragment size as size in bytes */
166 SDL_CalculateAudioSpec (spec); 166 SDL_CalculateAudioSpec(spec);
167 167
168 /* Open the audio device */ 168 /* Open the audio device */
169 result = waveOutOpen (&(shm->sound), 169 result = waveOutOpen(&(shm->sound),
170 WAVE_MAPPER, 170 WAVE_MAPPER,
171 &(shm->wFmt.wf), 171 &(shm->wFmt.wf),
172 MME_CALLBACK, 172 MME_CALLBACK,
173 NULL, (CALLBACK_FUNCTION | WAVE_OPEN_SHAREABLE)); 173 NULL, (CALLBACK_FUNCTION | WAVE_OPEN_SHAREABLE));
174 if (result != MMSYSERR_NOERROR) { 174 if (result != MMSYSERR_NOERROR) {
175 SetMMerror ("waveOutOpen()", result); 175 SetMMerror("waveOutOpen()", result);
176 return (-1); 176 return (-1);
177 } 177 }
178 178
179 /* Create the sound buffers */ 179 /* Create the sound buffers */
180 mixbuf = (Uint8 *) mmeAllocBuffer (NUM_BUFFERS * (spec->size)); 180 mixbuf = (Uint8 *) mmeAllocBuffer(NUM_BUFFERS * (spec->size));
181 if (mixbuf == NULL) { 181 if (mixbuf == NULL) {
182 SDL_SetError ("Out of memory: mixbuf"); 182 SDL_SetError("Out of memory: mixbuf");
183 return (-1); 183 return (-1);
184 } 184 }
185 185
186 for (i = 0; i < NUM_BUFFERS; i++) { 186 for (i = 0; i < NUM_BUFFERS; i++) {
187 shm->wHdr[i].lpData = &mixbuf[i * (spec->size)]; 187 shm->wHdr[i].lpData = &mixbuf[i * (spec->size)];
196 next_buffer = 0; 196 next_buffer = 0;
197 return 0; 197 return 0;
198 } 198 }
199 199
200 static void 200 static void
201 MME_WaitAudio (_THIS) 201 MME_WaitAudio(_THIS)
202 { 202 {
203 while (inUse[next_buffer]) { 203 while (inUse[next_buffer]) {
204 mmeWaitForCallbacks (); 204 mmeWaitForCallbacks();
205 mmeProcessCallbacks (); 205 mmeProcessCallbacks();
206 } 206 }
207 } 207 }
208 208
209 static Uint8 * 209 static Uint8 *
210 MME_GetAudioBuf (_THIS) 210 MME_GetAudioBuf(_THIS)
211 { 211 {
212 Uint8 *retval; 212 Uint8 *retval;
213 213
214 inUse[next_buffer] = TRUE; 214 inUse[next_buffer] = TRUE;
215 retval = (Uint8 *) (shm->wHdr[next_buffer].lpData); 215 retval = (Uint8 *) (shm->wHdr[next_buffer].lpData);
216 return retval; 216 return retval;
217 } 217 }
218 218
219 static void 219 static void
220 MME_PlayAudio (_THIS) 220 MME_PlayAudio(_THIS)
221 { 221 {
222 /* Queue it up */ 222 /* Queue it up */
223 waveOutWrite (shm->sound, &(shm->wHdr[next_buffer]), sizeof (WAVEHDR)); 223 waveOutWrite(shm->sound, &(shm->wHdr[next_buffer]), sizeof(WAVEHDR));
224 next_buffer = (next_buffer + 1) % NUM_BUFFERS; 224 next_buffer = (next_buffer + 1) % NUM_BUFFERS;
225 } 225 }
226 226
227 static void 227 static void
228 MME_WaitDone (_THIS) 228 MME_WaitDone(_THIS)
229 { 229 {
230 MMRESULT result; 230 MMRESULT result;
231 int i; 231 int i;
232 232
233 if (shm->sound) { 233 if (shm->sound) {
234 for (i = 0; i < NUM_BUFFERS; i++) 234 for (i = 0; i < NUM_BUFFERS; i++)
235 while (inUse[i]) { 235 while (inUse[i]) {
236 mmeWaitForCallbacks (); 236 mmeWaitForCallbacks();
237 mmeProcessCallbacks (); 237 mmeProcessCallbacks();
238 } 238 }
239 result = waveOutReset (shm->sound); 239 result = waveOutReset(shm->sound);
240 if (result != MMSYSERR_NOERROR) 240 if (result != MMSYSERR_NOERROR)
241 SetMMerror ("waveOutReset()", result); 241 SetMMerror("waveOutReset()", result);
242 mmeProcessCallbacks (); 242 mmeProcessCallbacks();
243 } 243 }
244 } 244 }
245 245
246 static void 246 static void
247 MME_CloseAudio (_THIS) 247 MME_CloseAudio(_THIS)
248 { 248 {
249 MMRESULT result; 249 MMRESULT result;
250 250
251 if (mixbuf) { 251 if (mixbuf) {
252 result = mmeFreeBuffer (mixbuf); 252 result = mmeFreeBuffer(mixbuf);
253 if (result != MMSYSERR_NOERROR) 253 if (result != MMSYSERR_NOERROR)
254 SetMMerror ("mmeFreeBuffer", result); 254 SetMMerror("mmeFreeBuffer", result);
255 mixbuf = NULL; 255 mixbuf = NULL;
256 } 256 }
257 257
258 if (shm) { 258 if (shm) {
259 if (shm->sound) { 259 if (shm->sound) {
260 result = waveOutClose (shm->sound); 260 result = waveOutClose(shm->sound);
261 if (result != MMSYSERR_NOERROR) 261 if (result != MMSYSERR_NOERROR)
262 SetMMerror ("waveOutClose()", result); 262 SetMMerror("waveOutClose()", result);
263 mmeProcessCallbacks (); 263 mmeProcessCallbacks();
264 } 264 }
265 result = mmeFreeMem (shm); 265 result = mmeFreeMem(shm);
266 if (result != MMSYSERR_NOERROR) 266 if (result != MMSYSERR_NOERROR)
267 SetMMerror ("mmeFreeMem()", result); 267 SetMMerror("mmeFreeMem()", result);
268 shm = NULL; 268 shm = NULL;
269 } 269 }
270 } 270 }
271 271
272 /* vi: set ts=4 sw=4 expandtab: */ 272 /* vi: set ts=4 sw=4 expandtab: */