comparison src/audio/windib/SDL_dibaudio.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
35 #include "win_ce_semaphore.h" 35 #include "win_ce_semaphore.h"
36 #endif 36 #endif
37 37
38 38
39 /* Audio driver functions */ 39 /* Audio driver functions */
40 static int DIB_OpenAudio (_THIS, SDL_AudioSpec * spec); 40 static int DIB_OpenAudio(_THIS, SDL_AudioSpec * spec);
41 static void DIB_ThreadInit (_THIS); 41 static void DIB_ThreadInit(_THIS);
42 static void DIB_WaitAudio (_THIS); 42 static void DIB_WaitAudio(_THIS);
43 static Uint8 *DIB_GetAudioBuf (_THIS); 43 static Uint8 *DIB_GetAudioBuf(_THIS);
44 static void DIB_PlayAudio (_THIS); 44 static void DIB_PlayAudio(_THIS);
45 static void DIB_WaitDone (_THIS); 45 static void DIB_WaitDone(_THIS);
46 static void DIB_CloseAudio (_THIS); 46 static void DIB_CloseAudio(_THIS);
47 47
48 /* Audio driver bootstrap functions */ 48 /* Audio driver bootstrap functions */
49 49
50 static int 50 static int
51 Audio_Available (void) 51 Audio_Available(void)
52 { 52 {
53 return (1); 53 return (1);
54 } 54 }
55 55
56 static void 56 static void
57 Audio_DeleteDevice (SDL_AudioDevice * device) 57 Audio_DeleteDevice(SDL_AudioDevice * device)
58 { 58 {
59 SDL_free (device->hidden); 59 SDL_free(device->hidden);
60 SDL_free (device); 60 SDL_free(device);
61 } 61 }
62 62
63 static SDL_AudioDevice * 63 static SDL_AudioDevice *
64 Audio_CreateDevice (int devindex) 64 Audio_CreateDevice(int devindex)
65 { 65 {
66 SDL_AudioDevice *this; 66 SDL_AudioDevice *this;
67 67
68 /* Initialize all variables that we clean on shutdown */ 68 /* Initialize all variables that we clean on shutdown */
69 this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice)); 69 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
70 if (this) { 70 if (this) {
71 SDL_memset (this, 0, (sizeof *this)); 71 SDL_memset(this, 0, (sizeof *this));
72 this->hidden = (struct SDL_PrivateAudioData *) 72 this->hidden = (struct SDL_PrivateAudioData *)
73 SDL_malloc ((sizeof *this->hidden)); 73 SDL_malloc((sizeof *this->hidden));
74 } 74 }
75 if ((this == NULL) || (this->hidden == NULL)) { 75 if ((this == NULL) || (this->hidden == NULL)) {
76 SDL_OutOfMemory (); 76 SDL_OutOfMemory();
77 if (this) { 77 if (this) {
78 SDL_free (this); 78 SDL_free(this);
79 } 79 }
80 return (0); 80 return (0);
81 } 81 }
82 SDL_memset (this->hidden, 0, (sizeof *this->hidden)); 82 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
83 83
84 /* Set the function pointers */ 84 /* Set the function pointers */
85 this->OpenAudio = DIB_OpenAudio; 85 this->OpenAudio = DIB_OpenAudio;
86 this->ThreadInit = DIB_ThreadInit; 86 this->ThreadInit = DIB_ThreadInit;
87 this->WaitAudio = DIB_WaitAudio; 87 this->WaitAudio = DIB_WaitAudio;
101 }; 101 };
102 102
103 103
104 /* The Win32 callback for filling the WAVE device */ 104 /* The Win32 callback for filling the WAVE device */
105 static void CALLBACK 105 static void CALLBACK
106 FillSound (HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance, 106 FillSound(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
107 DWORD dwParam1, DWORD dwParam2) 107 DWORD dwParam1, DWORD dwParam2)
108 { 108 {
109 SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance; 109 SDL_AudioDevice *this = (SDL_AudioDevice *) dwInstance;
110 110
111 /* Only service "buffer done playing" messages */ 111 /* Only service "buffer done playing" messages */
112 if (uMsg != WOM_DONE) 112 if (uMsg != WOM_DONE)
113 return; 113 return;
114 114
115 /* Signal that we are done playing a buffer */ 115 /* Signal that we are done playing a buffer */
116 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 116 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
117 ReleaseSemaphoreCE (audio_sem, 1, NULL); 117 ReleaseSemaphoreCE(audio_sem, 1, NULL);
118 #else 118 #else
119 ReleaseSemaphore (audio_sem, 1, NULL); 119 ReleaseSemaphore(audio_sem, 1, NULL);
120 #endif 120 #endif
121 } 121 }
122 122
123 static void 123 static void
124 SetMMerror (char *function, MMRESULT code) 124 SetMMerror(char *function, MMRESULT code)
125 { 125 {
126 size_t len; 126 size_t len;
127 char errbuf[MAXERRORLENGTH]; 127 char errbuf[MAXERRORLENGTH];
128 #ifdef _WIN32_WCE 128 #ifdef _WIN32_WCE
129 wchar_t werrbuf[MAXERRORLENGTH]; 129 wchar_t werrbuf[MAXERRORLENGTH];
130 #endif 130 #endif
131 131
132 SDL_snprintf (errbuf, SDL_arraysize (errbuf), "%s: ", function); 132 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function);
133 len = SDL_strlen (errbuf); 133 len = SDL_strlen(errbuf);
134 134
135 #ifdef _WIN32_WCE 135 #ifdef _WIN32_WCE
136 /* UNICODE version */ 136 /* UNICODE version */
137 waveOutGetErrorText (code, werrbuf, MAXERRORLENGTH - len); 137 waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH - len);
138 WideCharToMultiByte (CP_ACP, 0, werrbuf, -1, errbuf + len, 138 WideCharToMultiByte(CP_ACP, 0, werrbuf, -1, errbuf + len,
139 MAXERRORLENGTH - len, NULL, NULL); 139 MAXERRORLENGTH - len, NULL, NULL);
140 #else 140 #else
141 waveOutGetErrorText (code, errbuf + len, (UINT) (MAXERRORLENGTH - len)); 141 waveOutGetErrorText(code, errbuf + len, (UINT) (MAXERRORLENGTH - len));
142 #endif 142 #endif
143 143
144 SDL_SetError ("%s", errbuf); 144 SDL_SetError("%s", errbuf);
145 } 145 }
146 146
147 /* Set high priority for the audio thread */ 147 /* Set high priority for the audio thread */
148 static void 148 static void
149 DIB_ThreadInit (_THIS) 149 DIB_ThreadInit(_THIS)
150 { 150 {
151 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST); 151 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
152 } 152 }
153 153
154 void 154 void
155 DIB_WaitAudio (_THIS) 155 DIB_WaitAudio(_THIS)
156 { 156 {
157 /* Wait for an audio chunk to finish */ 157 /* Wait for an audio chunk to finish */
158 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 158 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
159 WaitForSemaphoreCE (audio_sem, INFINITE); 159 WaitForSemaphoreCE(audio_sem, INFINITE);
160 #else 160 #else
161 WaitForSingleObject (audio_sem, INFINITE); 161 WaitForSingleObject(audio_sem, INFINITE);
162 #endif 162 #endif
163 } 163 }
164 164
165 Uint8 * 165 Uint8 *
166 DIB_GetAudioBuf (_THIS) 166 DIB_GetAudioBuf(_THIS)
167 { 167 {
168 Uint8 *retval; 168 Uint8 *retval;
169 169
170 retval = (Uint8 *) (wavebuf[next_buffer].lpData); 170 retval = (Uint8 *) (wavebuf[next_buffer].lpData);
171 return retval; 171 return retval;
172 } 172 }
173 173
174 void 174 void
175 DIB_PlayAudio (_THIS) 175 DIB_PlayAudio(_THIS)
176 { 176 {
177 /* Queue it up */ 177 /* Queue it up */
178 waveOutWrite (sound, &wavebuf[next_buffer], sizeof (wavebuf[0])); 178 waveOutWrite(sound, &wavebuf[next_buffer], sizeof(wavebuf[0]));
179 next_buffer = (next_buffer + 1) % NUM_BUFFERS; 179 next_buffer = (next_buffer + 1) % NUM_BUFFERS;
180 } 180 }
181 181
182 void 182 void
183 DIB_WaitDone (_THIS) 183 DIB_WaitDone(_THIS)
184 { 184 {
185 int i, left; 185 int i, left;
186 186
187 do { 187 do {
188 left = NUM_BUFFERS; 188 left = NUM_BUFFERS;
190 if (wavebuf[i].dwFlags & WHDR_DONE) { 190 if (wavebuf[i].dwFlags & WHDR_DONE) {
191 --left; 191 --left;
192 } 192 }
193 } 193 }
194 if (left > 0) { 194 if (left > 0) {
195 SDL_Delay (100); 195 SDL_Delay(100);
196 } 196 }
197 } 197 }
198 while (left > 0); 198 while (left > 0);
199 } 199 }
200 200
201 void 201 void
202 DIB_CloseAudio (_THIS) 202 DIB_CloseAudio(_THIS)
203 { 203 {
204 int i; 204 int i;
205 205
206 /* Close up audio */ 206 /* Close up audio */
207 if (audio_sem) { 207 if (audio_sem) {
208 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 208 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
209 CloseSynchHandle (audio_sem); 209 CloseSynchHandle(audio_sem);
210 #else 210 #else
211 CloseHandle (audio_sem); 211 CloseHandle(audio_sem);
212 #endif 212 #endif
213 } 213 }
214 if (sound) { 214 if (sound) {
215 waveOutClose (sound); 215 waveOutClose(sound);
216 } 216 }
217 217
218 /* Clean up mixing buffers */ 218 /* Clean up mixing buffers */
219 for (i = 0; i < NUM_BUFFERS; ++i) { 219 for (i = 0; i < NUM_BUFFERS; ++i) {
220 if (wavebuf[i].dwUser != 0xFFFF) { 220 if (wavebuf[i].dwUser != 0xFFFF) {
221 waveOutUnprepareHeader (sound, &wavebuf[i], sizeof (wavebuf[i])); 221 waveOutUnprepareHeader(sound, &wavebuf[i], sizeof(wavebuf[i]));
222 wavebuf[i].dwUser = 0xFFFF; 222 wavebuf[i].dwUser = 0xFFFF;
223 } 223 }
224 } 224 }
225 /* Free raw mixing buffer */ 225 /* Free raw mixing buffer */
226 if (mixbuf != NULL) { 226 if (mixbuf != NULL) {
227 SDL_free (mixbuf); 227 SDL_free(mixbuf);
228 mixbuf = NULL; 228 mixbuf = NULL;
229 } 229 }
230 } 230 }
231 231
232 int 232 int
233 DIB_OpenAudio (_THIS, SDL_AudioSpec * spec) 233 DIB_OpenAudio(_THIS, SDL_AudioSpec * spec)
234 { 234 {
235 MMRESULT result; 235 MMRESULT result;
236 int i; 236 int i;
237 WAVEFORMATEX waveformat; 237 WAVEFORMATEX waveformat;
238 238
242 for (i = 0; i < NUM_BUFFERS; ++i) 242 for (i = 0; i < NUM_BUFFERS; ++i)
243 wavebuf[i].dwUser = 0xFFFF; 243 wavebuf[i].dwUser = 0xFFFF;
244 mixbuf = NULL; 244 mixbuf = NULL;
245 245
246 /* Set basic WAVE format parameters */ 246 /* Set basic WAVE format parameters */
247 SDL_memset (&waveformat, 0, sizeof (waveformat)); 247 SDL_memset(&waveformat, 0, sizeof(waveformat));
248 waveformat.wFormatTag = WAVE_FORMAT_PCM; 248 waveformat.wFormatTag = WAVE_FORMAT_PCM;
249 249
250 /* Determine the audio parameters from the AudioSpec */ 250 /* Determine the audio parameters from the AudioSpec */
251 switch (spec->format & 0xFF) { 251 switch (spec->format & 0xFF) {
252 case 8: 252 case 8:
258 /* Signed 16 bit audio data */ 258 /* Signed 16 bit audio data */
259 spec->format = AUDIO_S16; 259 spec->format = AUDIO_S16;
260 waveformat.wBitsPerSample = 16; 260 waveformat.wBitsPerSample = 16;
261 break; 261 break;
262 default: 262 default:
263 SDL_SetError ("Unsupported audio format"); 263 SDL_SetError("Unsupported audio format");
264 return (-1); 264 return (-1);
265 } 265 }
266 waveformat.nChannels = spec->channels; 266 waveformat.nChannels = spec->channels;
267 waveformat.nSamplesPerSec = spec->freq; 267 waveformat.nSamplesPerSec = spec->freq;
268 waveformat.nBlockAlign = 268 waveformat.nBlockAlign =
273 /* Check the buffer size -- minimum of 1/4 second (word aligned) */ 273 /* Check the buffer size -- minimum of 1/4 second (word aligned) */
274 if (spec->samples < (spec->freq / 4)) 274 if (spec->samples < (spec->freq / 4))
275 spec->samples = ((spec->freq / 4) + 3) & ~3; 275 spec->samples = ((spec->freq / 4) + 3) & ~3;
276 276
277 /* Update the fragment size as size in bytes */ 277 /* Update the fragment size as size in bytes */
278 SDL_CalculateAudioSpec (spec); 278 SDL_CalculateAudioSpec(spec);
279 279
280 /* Open the audio device */ 280 /* Open the audio device */
281 result = waveOutOpen (&sound, WAVE_MAPPER, &waveformat, 281 result = waveOutOpen(&sound, WAVE_MAPPER, &waveformat,
282 (DWORD_PTR) FillSound, (DWORD_PTR) this, 282 (DWORD_PTR) FillSound, (DWORD_PTR) this,
283 CALLBACK_FUNCTION); 283 CALLBACK_FUNCTION);
284 if (result != MMSYSERR_NOERROR) { 284 if (result != MMSYSERR_NOERROR) {
285 SetMMerror ("waveOutOpen()", result); 285 SetMMerror("waveOutOpen()", result);
286 return (-1); 286 return (-1);
287 } 287 }
288 #ifdef SOUND_DEBUG 288 #ifdef SOUND_DEBUG
289 /* Check the sound device we retrieved */ 289 /* Check the sound device we retrieved */
290 { 290 {
291 WAVEOUTCAPS caps; 291 WAVEOUTCAPS caps;
292 292
293 result = waveOutGetDevCaps ((UINT) sound, &caps, sizeof (caps)); 293 result = waveOutGetDevCaps((UINT) sound, &caps, sizeof(caps));
294 if (result != MMSYSERR_NOERROR) { 294 if (result != MMSYSERR_NOERROR) {
295 SetMMerror ("waveOutGetDevCaps()", result); 295 SetMMerror("waveOutGetDevCaps()", result);
296 return (-1); 296 return (-1);
297 } 297 }
298 printf ("Audio device: %s\n", caps.szPname); 298 printf("Audio device: %s\n", caps.szPname);
299 } 299 }
300 #endif 300 #endif
301 301
302 /* Create the audio buffer semaphore */ 302 /* Create the audio buffer semaphore */
303 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) 303 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
304 audio_sem = CreateSemaphoreCE (NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL); 304 audio_sem = CreateSemaphoreCE(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
305 #else 305 #else
306 audio_sem = CreateSemaphore (NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL); 306 audio_sem = CreateSemaphore(NULL, NUM_BUFFERS - 1, NUM_BUFFERS, NULL);
307 #endif 307 #endif
308 if (audio_sem == NULL) { 308 if (audio_sem == NULL) {
309 SDL_SetError ("Couldn't create semaphore"); 309 SDL_SetError("Couldn't create semaphore");
310 return (-1); 310 return (-1);
311 } 311 }
312 312
313 /* Create the sound buffers */ 313 /* Create the sound buffers */
314 mixbuf = (Uint8 *) SDL_malloc (NUM_BUFFERS * spec->size); 314 mixbuf = (Uint8 *) SDL_malloc(NUM_BUFFERS * spec->size);
315 if (mixbuf == NULL) { 315 if (mixbuf == NULL) {
316 SDL_SetError ("Out of memory"); 316 SDL_SetError("Out of memory");
317 return (-1); 317 return (-1);
318 } 318 }
319 for (i = 0; i < NUM_BUFFERS; ++i) { 319 for (i = 0; i < NUM_BUFFERS; ++i) {
320 SDL_memset (&wavebuf[i], 0, sizeof (wavebuf[i])); 320 SDL_memset(&wavebuf[i], 0, sizeof(wavebuf[i]));
321 wavebuf[i].lpData = (LPSTR) & mixbuf[i * spec->size]; 321 wavebuf[i].lpData = (LPSTR) & mixbuf[i * spec->size];
322 wavebuf[i].dwBufferLength = spec->size; 322 wavebuf[i].dwBufferLength = spec->size;
323 wavebuf[i].dwFlags = WHDR_DONE; 323 wavebuf[i].dwFlags = WHDR_DONE;
324 result = waveOutPrepareHeader (sound, &wavebuf[i], 324 result = waveOutPrepareHeader(sound, &wavebuf[i], sizeof(wavebuf[i]));
325 sizeof (wavebuf[i]));
326 if (result != MMSYSERR_NOERROR) { 325 if (result != MMSYSERR_NOERROR) {
327 SetMMerror ("waveOutPrepareHeader()", result); 326 SetMMerror("waveOutPrepareHeader()", result);
328 return (-1); 327 return (-1);
329 } 328 }
330 } 329 }
331 330
332 /* Ready to go! */ 331 /* Ready to go! */