Mercurial > sdl-ios-xcode
annotate src/audio/windx5/SDL_dx5audio.c @ 1365:b70f45aa5d0c
Add missing clause
author | Patrice Mandin <patmandin@gmail.com> |
---|---|
date | Thu, 16 Feb 2006 22:33:34 +0000 |
parents | 19418e4422cb |
children | d910939febfa |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
217
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* Allow access to a raw mixing buffer */ | |
24 | |
25 #include "SDL_timer.h" | |
26 #include "SDL_audio.h" | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
27 #include "../SDL_audio_c.h" |
0 | 28 #include "SDL_dx5audio.h" |
29 | |
30 /* Define this if you want to use DirectX 6 DirectSoundNotify interface */ | |
31 //#define USE_POSITION_NOTIFY | |
32 | |
33 /* DirectX function pointers for audio */ | |
34 HRESULT (WINAPI *DSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN); | |
35 | |
36 /* Audio driver functions */ | |
37 static int DX5_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
38 static void DX5_ThreadInit(_THIS); | |
39 static void DX5_WaitAudio_BusyWait(_THIS); | |
40 #ifdef USE_POSITION_NOTIFY | |
41 static void DX6_WaitAudio_EventWait(_THIS); | |
42 #endif | |
43 static void DX5_PlayAudio(_THIS); | |
44 static Uint8 *DX5_GetAudioBuf(_THIS); | |
45 static void DX5_WaitDone(_THIS); | |
46 static void DX5_CloseAudio(_THIS); | |
47 | |
48 /* Audio driver bootstrap functions */ | |
49 | |
50 static int Audio_Available(void) | |
51 { | |
52 HINSTANCE DSoundDLL; | |
53 int dsound_ok; | |
54 | |
55 /* Version check DSOUND.DLL (Is DirectX okay?) */ | |
56 dsound_ok = 0; | |
453
a6fa62b1be09
Updated for embedded Visual C++ 4.0
Sam Lantinga <slouken@libsdl.org>
parents:
375
diff
changeset
|
57 DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL")); |
0 | 58 if ( DSoundDLL != NULL ) { |
59 /* We just use basic DirectSound, we're okay */ | |
60 /* Yay! */ | |
61 /* Unfortunately, the sound drivers on NT have | |
62 higher latencies than the audio buffers used | |
63 by many SDL applications, so there are gaps | |
64 in the audio - it sounds terrible. Punt for now. | |
65 */ | |
66 OSVERSIONINFO ver; | |
67 ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); | |
68 GetVersionEx(&ver); | |
69 switch (ver.dwPlatformId) { | |
70 case VER_PLATFORM_WIN32_NT: | |
71 if ( ver.dwMajorVersion > 4 ) { | |
72 /* Win2K */ | |
73 dsound_ok = 1; | |
74 } else { | |
75 /* WinNT */ | |
76 dsound_ok = 0; | |
77 } | |
78 break; | |
79 default: | |
80 /* Win95 or Win98 */ | |
81 dsound_ok = 1; | |
82 break; | |
83 } | |
84 /* Now check for DirectX 5 or better - otherwise | |
85 * we will fail later in DX5_OpenAudio without a chance | |
86 * to fall back to the DIB driver. */ | |
87 if (dsound_ok) { | |
88 /* DirectSoundCaptureCreate was added in DX5 */ | |
453
a6fa62b1be09
Updated for embedded Visual C++ 4.0
Sam Lantinga <slouken@libsdl.org>
parents:
375
diff
changeset
|
89 if (!GetProcAddress(DSoundDLL, TEXT("DirectSoundCaptureCreate"))) |
0 | 90 dsound_ok = 0; |
91 | |
92 } | |
93 /* Clean up.. */ | |
94 FreeLibrary(DSoundDLL); | |
95 } | |
96 return(dsound_ok); | |
97 } | |
98 | |
99 /* Functions for loading the DirectX functions dynamically */ | |
100 static HINSTANCE DSoundDLL = NULL; | |
101 | |
102 static void DX5_Unload(void) | |
103 { | |
104 if ( DSoundDLL != NULL ) { | |
105 FreeLibrary(DSoundDLL); | |
106 DSoundCreate = NULL; | |
107 DSoundDLL = NULL; | |
108 } | |
109 } | |
110 static int DX5_Load(void) | |
111 { | |
112 int status; | |
113 | |
114 DX5_Unload(); | |
453
a6fa62b1be09
Updated for embedded Visual C++ 4.0
Sam Lantinga <slouken@libsdl.org>
parents:
375
diff
changeset
|
115 DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL")); |
0 | 116 if ( DSoundDLL != NULL ) { |
117 DSoundCreate = (void *)GetProcAddress(DSoundDLL, | |
453
a6fa62b1be09
Updated for embedded Visual C++ 4.0
Sam Lantinga <slouken@libsdl.org>
parents:
375
diff
changeset
|
118 TEXT("DirectSoundCreate")); |
0 | 119 } |
120 if ( DSoundDLL && DSoundCreate ) { | |
121 status = 0; | |
122 } else { | |
123 DX5_Unload(); | |
124 status = -1; | |
125 } | |
126 return status; | |
127 } | |
128 | |
129 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
130 { | |
131 DX5_Unload(); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
132 SDL_free(device->hidden); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
133 SDL_free(device); |
0 | 134 } |
135 | |
136 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
137 { | |
138 SDL_AudioDevice *this; | |
139 | |
140 /* Load DirectX */ | |
141 if ( DX5_Load() < 0 ) { | |
142 return(NULL); | |
143 } | |
144 | |
145 /* Initialize all variables that we clean on shutdown */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
146 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); |
0 | 147 if ( this ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
148 SDL_memset(this, 0, (sizeof *this)); |
0 | 149 this->hidden = (struct SDL_PrivateAudioData *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
150 SDL_malloc((sizeof *this->hidden)); |
0 | 151 } |
152 if ( (this == NULL) || (this->hidden == NULL) ) { | |
153 SDL_OutOfMemory(); | |
154 if ( this ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
155 SDL_free(this); |
0 | 156 } |
157 return(0); | |
158 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
159 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); |
0 | 160 |
161 /* Set the function pointers */ | |
162 this->OpenAudio = DX5_OpenAudio; | |
163 this->ThreadInit = DX5_ThreadInit; | |
164 this->WaitAudio = DX5_WaitAudio_BusyWait; | |
165 this->PlayAudio = DX5_PlayAudio; | |
166 this->GetAudioBuf = DX5_GetAudioBuf; | |
167 this->WaitDone = DX5_WaitDone; | |
168 this->CloseAudio = DX5_CloseAudio; | |
169 | |
170 this->free = Audio_DeleteDevice; | |
171 | |
172 return this; | |
173 } | |
174 | |
175 AudioBootStrap DSOUND_bootstrap = { | |
176 "dsound", "Win95/98/2000 DirectSound", | |
177 Audio_Available, Audio_CreateDevice | |
178 }; | |
179 | |
180 static void SetDSerror(const char *function, int code) | |
181 { | |
182 static const char *error; | |
453
a6fa62b1be09
Updated for embedded Visual C++ 4.0
Sam Lantinga <slouken@libsdl.org>
parents:
375
diff
changeset
|
183 static char errbuf[1024]; |
0 | 184 |
185 errbuf[0] = 0; | |
186 switch (code) { | |
187 case E_NOINTERFACE: | |
188 error = | |
189 "Unsupported interface\n-- Is DirectX 5.0 or later installed?"; | |
190 break; | |
191 case DSERR_ALLOCATED: | |
192 error = "Audio device in use"; | |
193 break; | |
194 case DSERR_BADFORMAT: | |
195 error = "Unsupported audio format"; | |
196 break; | |
197 case DSERR_BUFFERLOST: | |
198 error = "Mixing buffer was lost"; | |
199 break; | |
200 case DSERR_CONTROLUNAVAIL: | |
201 error = "Control requested is not available"; | |
202 break; | |
203 case DSERR_INVALIDCALL: | |
204 error = "Invalid call for the current state"; | |
205 break; | |
206 case DSERR_INVALIDPARAM: | |
207 error = "Invalid parameter"; | |
208 break; | |
209 case DSERR_NODRIVER: | |
210 error = "No audio device found"; | |
211 break; | |
212 case DSERR_OUTOFMEMORY: | |
213 error = "Out of memory"; | |
214 break; | |
215 case DSERR_PRIOLEVELNEEDED: | |
216 error = "Caller doesn't have priority"; | |
217 break; | |
218 case DSERR_UNSUPPORTED: | |
219 error = "Function not supported"; | |
220 break; | |
221 default: | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
222 SDL_snprintf(errbuf, SDL_arraysize(errbuf), |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
223 "%s: Unknown DirectSound error: 0x%x", |
0 | 224 function, code); |
225 break; | |
226 } | |
227 if ( ! errbuf[0] ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
228 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error); |
0 | 229 } |
230 SDL_SetError("%s", errbuf); | |
231 return; | |
232 } | |
233 | |
234 /* DirectSound needs to be associated with a window */ | |
235 static HWND mainwin = NULL; | |
236 /* */ | |
237 void DX5_SoundFocus(HWND hwnd) | |
238 { | |
239 mainwin = hwnd; | |
240 } | |
241 | |
242 static void DX5_ThreadInit(_THIS) | |
243 { | |
244 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); | |
245 } | |
246 | |
247 static void DX5_WaitAudio_BusyWait(_THIS) | |
248 { | |
249 DWORD status; | |
250 DWORD cursor, junk; | |
251 HRESULT result; | |
252 | |
253 /* Semi-busy wait, since we have no way of getting play notification | |
254 on a primary mixing buffer located in hardware (DirectX 5.0) | |
255 */ | |
256 result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &cursor, &junk); | |
257 if ( result != DS_OK ) { | |
258 if ( result == DSERR_BUFFERLOST ) { | |
259 IDirectSoundBuffer_Restore(mixbuf); | |
260 } | |
261 #ifdef DEBUG_SOUND | |
262 SetDSerror("DirectSound GetCurrentPosition", result); | |
263 #endif | |
264 return; | |
265 } | |
266 cursor /= mixlen; | |
267 | |
268 while ( cursor == playing ) { | |
269 /* FIXME: find out how much time is left and sleep that long */ | |
270 SDL_Delay(10); | |
271 | |
272 /* Try to restore a lost sound buffer */ | |
273 IDirectSoundBuffer_GetStatus(mixbuf, &status); | |
274 if ( (status&DSBSTATUS_BUFFERLOST) ) { | |
275 IDirectSoundBuffer_Restore(mixbuf); | |
276 IDirectSoundBuffer_GetStatus(mixbuf, &status); | |
277 if ( (status&DSBSTATUS_BUFFERLOST) ) { | |
278 break; | |
279 } | |
280 } | |
281 if ( ! (status&DSBSTATUS_PLAYING) ) { | |
282 result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING); | |
283 if ( result == DS_OK ) { | |
284 continue; | |
285 } | |
286 #ifdef DEBUG_SOUND | |
287 SetDSerror("DirectSound Play", result); | |
288 #endif | |
289 return; | |
290 } | |
291 | |
292 /* Find out where we are playing */ | |
293 result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, | |
294 &cursor, &junk); | |
295 if ( result != DS_OK ) { | |
296 SetDSerror("DirectSound GetCurrentPosition", result); | |
297 return; | |
298 } | |
299 cursor /= mixlen; | |
300 } | |
301 } | |
302 | |
303 #ifdef USE_POSITION_NOTIFY | |
304 static void DX6_WaitAudio_EventWait(_THIS) | |
305 { | |
306 DWORD status; | |
307 HRESULT result; | |
308 | |
309 /* Try to restore a lost sound buffer */ | |
310 IDirectSoundBuffer_GetStatus(mixbuf, &status); | |
311 if ( (status&DSBSTATUS_BUFFERLOST) ) { | |
312 IDirectSoundBuffer_Restore(mixbuf); | |
313 IDirectSoundBuffer_GetStatus(mixbuf, &status); | |
314 if ( (status&DSBSTATUS_BUFFERLOST) ) { | |
315 return; | |
316 } | |
317 } | |
318 if ( ! (status&DSBSTATUS_PLAYING) ) { | |
319 result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING); | |
320 if ( result != DS_OK ) { | |
321 #ifdef DEBUG_SOUND | |
322 SetDSerror("DirectSound Play", result); | |
323 #endif | |
324 return; | |
325 } | |
326 } | |
327 WaitForSingleObject(audio_event, INFINITE); | |
328 } | |
329 #endif /* USE_POSITION_NOTIFY */ | |
330 | |
331 static void DX5_PlayAudio(_THIS) | |
332 { | |
333 /* Unlock the buffer, allowing it to play */ | |
334 if ( locked_buf ) { | |
335 IDirectSoundBuffer_Unlock(mixbuf, locked_buf, mixlen, NULL, 0); | |
336 } | |
337 | |
338 } | |
339 | |
340 static Uint8 *DX5_GetAudioBuf(_THIS) | |
341 { | |
342 DWORD cursor, junk; | |
343 HRESULT result; | |
344 DWORD rawlen; | |
345 | |
346 /* Figure out which blocks to fill next */ | |
347 locked_buf = NULL; | |
348 result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &cursor, &junk); | |
349 if ( result == DSERR_BUFFERLOST ) { | |
350 IDirectSoundBuffer_Restore(mixbuf); | |
351 result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, | |
352 &cursor, &junk); | |
353 } | |
354 if ( result != DS_OK ) { | |
355 SetDSerror("DirectSound GetCurrentPosition", result); | |
356 return(NULL); | |
357 } | |
358 cursor /= mixlen; | |
359 playing = cursor; | |
360 cursor = (cursor+1)%NUM_BUFFERS; | |
361 cursor *= mixlen; | |
362 | |
363 /* Lock the audio buffer */ | |
364 result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen, | |
365 (LPVOID *)&locked_buf, &rawlen, NULL, &junk, 0); | |
366 if ( result == DSERR_BUFFERLOST ) { | |
367 IDirectSoundBuffer_Restore(mixbuf); | |
368 result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen, | |
369 (LPVOID *)&locked_buf, &rawlen, NULL, &junk, 0); | |
370 } | |
371 if ( result != DS_OK ) { | |
372 SetDSerror("DirectSound Lock", result); | |
373 return(NULL); | |
374 } | |
375 return(locked_buf); | |
376 } | |
377 | |
378 static void DX5_WaitDone(_THIS) | |
379 { | |
380 Uint8 *stream; | |
381 | |
382 /* Wait for the playing chunk to finish */ | |
383 stream = this->GetAudioBuf(this); | |
384 if ( stream != NULL ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
385 SDL_memset(stream, silence, mixlen); |
0 | 386 this->PlayAudio(this); |
387 } | |
388 this->WaitAudio(this); | |
389 | |
390 /* Stop the looping sound buffer */ | |
391 IDirectSoundBuffer_Stop(mixbuf); | |
392 } | |
393 | |
394 static void DX5_CloseAudio(_THIS) | |
395 { | |
396 if ( sound != NULL ) { | |
397 if ( mixbuf != NULL ) { | |
398 /* Clean up the audio buffer */ | |
399 IDirectSoundBuffer_Release(mixbuf); | |
400 mixbuf = NULL; | |
401 } | |
402 if ( audio_event != NULL ) { | |
403 CloseHandle(audio_event); | |
404 audio_event = NULL; | |
405 } | |
406 IDirectSound_Release(sound); | |
407 sound = NULL; | |
408 } | |
409 } | |
410 | |
469 | 411 #ifdef USE_PRIMARY_BUFFER |
0 | 412 /* This function tries to create a primary audio buffer, and returns the |
413 number of audio chunks available in the created buffer. | |
414 */ | |
415 static int CreatePrimary(LPDIRECTSOUND sndObj, HWND focus, | |
416 LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize) | |
417 { | |
418 HRESULT result; | |
419 DSBUFFERDESC format; | |
420 DSBCAPS caps; | |
421 int numchunks; | |
422 | |
423 /* Try to set primary mixing privileges */ | |
424 result = IDirectSound_SetCooperativeLevel(sndObj, focus, | |
425 DSSCL_WRITEPRIMARY); | |
426 if ( result != DS_OK ) { | |
427 #ifdef DEBUG_SOUND | |
428 SetDSerror("DirectSound SetCooperativeLevel", result); | |
429 #endif | |
430 return(-1); | |
431 } | |
432 | |
433 /* Try to create the primary buffer */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
434 SDL_memset(&format, 0, sizeof(format)); |
0 | 435 format.dwSize = sizeof(format); |
436 format.dwFlags=(DSBCAPS_PRIMARYBUFFER|DSBCAPS_GETCURRENTPOSITION2); | |
217
add626b825bb
Use the sticky focus flag so audio isn't muted when application is switched.
Sam Lantinga <slouken@libsdl.org>
parents:
118
diff
changeset
|
437 format.dwFlags |= DSBCAPS_STICKYFOCUS; |
0 | 438 #ifdef USE_POSITION_NOTIFY |
439 format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY; | |
440 #endif | |
441 result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL); | |
442 if ( result != DS_OK ) { | |
443 #ifdef DEBUG_SOUND | |
444 SetDSerror("DirectSound CreateSoundBuffer", result); | |
445 #endif | |
446 return(-1); | |
447 } | |
448 | |
449 /* Check the size of the fragment buffer */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
450 SDL_memset(&caps, 0, sizeof(caps)); |
0 | 451 caps.dwSize = sizeof(caps); |
452 result = IDirectSoundBuffer_GetCaps(*sndbuf, &caps); | |
453 if ( result != DS_OK ) { | |
454 #ifdef DEBUG_SOUND | |
455 SetDSerror("DirectSound GetCaps", result); | |
456 #endif | |
457 IDirectSoundBuffer_Release(*sndbuf); | |
458 return(-1); | |
459 } | |
460 if ( (chunksize > caps.dwBufferBytes) || | |
461 ((caps.dwBufferBytes%chunksize) != 0) ) { | |
462 /* The primary buffer size is not a multiple of 'chunksize' | |
463 -- this hopefully doesn't happen when 'chunksize' is a | |
464 power of 2. | |
465 */ | |
466 IDirectSoundBuffer_Release(*sndbuf); | |
467 SDL_SetError( | |
468 "Primary buffer size is: %d, cannot break it into chunks of %d bytes\n", | |
469 caps.dwBufferBytes, chunksize); | |
470 return(-1); | |
471 } | |
472 numchunks = (caps.dwBufferBytes/chunksize); | |
473 | |
474 /* Set the primary audio format */ | |
475 result = IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt); | |
476 if ( result != DS_OK ) { | |
477 #ifdef DEBUG_SOUND | |
478 SetDSerror("DirectSound SetFormat", result); | |
479 #endif | |
480 IDirectSoundBuffer_Release(*sndbuf); | |
481 return(-1); | |
482 } | |
483 return(numchunks); | |
484 } | |
469 | 485 #endif /* USE_PRIMARY_BUFFER */ |
0 | 486 |
487 /* This function tries to create a secondary audio buffer, and returns the | |
488 number of audio chunks available in the created buffer. | |
489 */ | |
490 static int CreateSecondary(LPDIRECTSOUND sndObj, HWND focus, | |
491 LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize) | |
492 { | |
118
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
493 const int numchunks = 2; |
0 | 494 HRESULT result; |
495 DSBUFFERDESC format; | |
118
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
496 LPVOID pvAudioPtr1, pvAudioPtr2; |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
497 DWORD dwAudioBytes1, dwAudioBytes2; |
0 | 498 |
499 /* Try to set primary mixing privileges */ | |
500 if ( focus ) { | |
501 result = IDirectSound_SetCooperativeLevel(sndObj, | |
502 focus, DSSCL_PRIORITY); | |
503 } else { | |
504 result = IDirectSound_SetCooperativeLevel(sndObj, | |
505 GetDesktopWindow(), DSSCL_NORMAL); | |
506 } | |
507 if ( result != DS_OK ) { | |
508 #ifdef DEBUG_SOUND | |
509 SetDSerror("DirectSound SetCooperativeLevel", result); | |
510 #endif | |
511 return(-1); | |
512 } | |
513 | |
514 /* Try to create the secondary buffer */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
515 SDL_memset(&format, 0, sizeof(format)); |
0 | 516 format.dwSize = sizeof(format); |
517 format.dwFlags = DSBCAPS_GETCURRENTPOSITION2; | |
518 #ifdef USE_POSITION_NOTIFY | |
519 format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY; | |
520 #endif | |
521 if ( ! focus ) { | |
522 format.dwFlags |= DSBCAPS_GLOBALFOCUS; | |
217
add626b825bb
Use the sticky focus flag so audio isn't muted when application is switched.
Sam Lantinga <slouken@libsdl.org>
parents:
118
diff
changeset
|
523 } else { |
add626b825bb
Use the sticky focus flag so audio isn't muted when application is switched.
Sam Lantinga <slouken@libsdl.org>
parents:
118
diff
changeset
|
524 format.dwFlags |= DSBCAPS_STICKYFOCUS; |
0 | 525 } |
526 format.dwBufferBytes = numchunks*chunksize; | |
527 if ( (format.dwBufferBytes < DSBSIZE_MIN) || | |
528 (format.dwBufferBytes > DSBSIZE_MAX) ) { | |
529 SDL_SetError("Sound buffer size must be between %d and %d", | |
530 DSBSIZE_MIN/numchunks, DSBSIZE_MAX/numchunks); | |
531 return(-1); | |
532 } | |
533 format.dwReserved = 0; | |
534 format.lpwfxFormat = wavefmt; | |
535 result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL); | |
536 if ( result != DS_OK ) { | |
537 SetDSerror("DirectSound CreateSoundBuffer", result); | |
538 return(-1); | |
539 } | |
540 IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt); | |
541 | |
118
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
542 /* Silence the initial audio buffer */ |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
543 result = IDirectSoundBuffer_Lock(*sndbuf, 0, format.dwBufferBytes, |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
544 (LPVOID *)&pvAudioPtr1, &dwAudioBytes1, |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
545 (LPVOID *)&pvAudioPtr2, &dwAudioBytes2, |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
546 DSBLOCK_ENTIREBUFFER); |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
547 if ( result == DS_OK ) { |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
548 if ( wavefmt->wBitsPerSample == 8 ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
549 SDL_memset(pvAudioPtr1, 0x80, dwAudioBytes1); |
118
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
550 } else { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
551 SDL_memset(pvAudioPtr1, 0x00, dwAudioBytes1); |
118
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
552 } |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
553 IDirectSoundBuffer_Unlock(*sndbuf, |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
554 (LPVOID)pvAudioPtr1, dwAudioBytes1, |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
555 (LPVOID)pvAudioPtr2, dwAudioBytes2); |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
556 } |
7c47e511459d
Fix noise when starting audio under DX5 (thanks Jesse!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
557 |
0 | 558 /* We're ready to go */ |
559 return(numchunks); | |
560 } | |
561 | |
562 /* This function tries to set position notify events on the mixing buffer */ | |
563 #ifdef USE_POSITION_NOTIFY | |
564 static int CreateAudioEvent(_THIS) | |
565 { | |
566 LPDIRECTSOUNDNOTIFY notify; | |
567 DSBPOSITIONNOTIFY *notify_positions; | |
568 int i, retval; | |
569 HRESULT result; | |
570 | |
571 /* Default to fail on exit */ | |
572 retval = -1; | |
573 notify = NULL; | |
574 | |
575 /* Query for the interface */ | |
576 result = IDirectSoundBuffer_QueryInterface(mixbuf, | |
577 &IID_IDirectSoundNotify, (void *)¬ify); | |
578 if ( result != DS_OK ) { | |
579 goto done; | |
580 } | |
581 | |
582 /* Allocate the notify structures */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
583 notify_positions = (DSBPOSITIONNOTIFY *)SDL_malloc(NUM_BUFFERS* |
0 | 584 sizeof(*notify_positions)); |
585 if ( notify_positions == NULL ) { | |
586 goto done; | |
587 } | |
588 | |
589 /* Create the notify event */ | |
590 audio_event = CreateEvent(NULL, FALSE, FALSE, NULL); | |
591 if ( audio_event == NULL ) { | |
592 goto done; | |
593 } | |
594 | |
595 /* Set up the notify structures */ | |
596 for ( i=0; i<NUM_BUFFERS; ++i ) { | |
597 notify_positions[i].dwOffset = i*mixlen; | |
598 notify_positions[i].hEventNotify = audio_event; | |
599 } | |
600 result = IDirectSoundNotify_SetNotificationPositions(notify, | |
601 NUM_BUFFERS, notify_positions); | |
602 if ( result == DS_OK ) { | |
603 retval = 0; | |
604 } | |
605 done: | |
606 if ( notify != NULL ) { | |
607 IDirectSoundNotify_Release(notify); | |
608 } | |
609 return(retval); | |
610 } | |
611 #endif /* USE_POSITION_NOTIFY */ | |
612 | |
613 static int DX5_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
614 { | |
615 HRESULT result; | |
616 WAVEFORMATEX waveformat; | |
617 | |
618 /* Set basic WAVE format parameters */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
619 SDL_memset(&waveformat, 0, sizeof(waveformat)); |
0 | 620 waveformat.wFormatTag = WAVE_FORMAT_PCM; |
621 | |
622 /* Determine the audio parameters from the AudioSpec */ | |
623 switch ( spec->format & 0xFF ) { | |
624 case 8: | |
625 /* Unsigned 8 bit audio data */ | |
626 spec->format = AUDIO_U8; | |
627 silence = 0x80; | |
628 waveformat.wBitsPerSample = 8; | |
629 break; | |
630 case 16: | |
631 /* Signed 16 bit audio data */ | |
632 spec->format = AUDIO_S16; | |
633 silence = 0x00; | |
634 waveformat.wBitsPerSample = 16; | |
635 break; | |
636 default: | |
637 SDL_SetError("Unsupported audio format"); | |
638 return(-1); | |
639 } | |
640 waveformat.nChannels = spec->channels; | |
641 waveformat.nSamplesPerSec = spec->freq; | |
642 waveformat.nBlockAlign = | |
643 waveformat.nChannels * (waveformat.wBitsPerSample/8); | |
644 waveformat.nAvgBytesPerSec = | |
645 waveformat.nSamplesPerSec * waveformat.nBlockAlign; | |
646 | |
647 /* Update the fragment size as size in bytes */ | |
648 SDL_CalculateAudioSpec(spec); | |
649 | |
650 /* Open the audio device */ | |
651 result = DSoundCreate(NULL, &sound, NULL); | |
652 if ( result != DS_OK ) { | |
653 SetDSerror("DirectSoundCreate", result); | |
654 return(-1); | |
655 } | |
656 | |
657 /* Create the audio buffer to which we write */ | |
658 NUM_BUFFERS = -1; | |
375
17976f0f503f
Use secondary audio buffers to avoid problems on various soundcards
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
659 #ifdef USE_PRIMARY_BUFFER |
0 | 660 if ( mainwin ) { |
661 NUM_BUFFERS = CreatePrimary(sound, mainwin, &mixbuf, | |
662 &waveformat, spec->size); | |
663 } | |
375
17976f0f503f
Use secondary audio buffers to avoid problems on various soundcards
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
664 #endif /* USE_PRIMARY_BUFFER */ |
0 | 665 if ( NUM_BUFFERS < 0 ) { |
666 NUM_BUFFERS = CreateSecondary(sound, mainwin, &mixbuf, | |
667 &waveformat, spec->size); | |
668 if ( NUM_BUFFERS < 0 ) { | |
669 return(-1); | |
670 } | |
671 #ifdef DEBUG_SOUND | |
672 fprintf(stderr, "Using secondary audio buffer\n"); | |
673 #endif | |
674 } | |
675 #ifdef DEBUG_SOUND | |
676 else | |
677 fprintf(stderr, "Using primary audio buffer\n"); | |
678 #endif | |
679 | |
680 /* The buffer will auto-start playing in DX5_WaitAudio() */ | |
681 playing = 0; | |
682 mixlen = spec->size; | |
683 | |
684 #ifdef USE_POSITION_NOTIFY | |
685 /* See if we can use DirectX 6 event notification */ | |
686 if ( CreateAudioEvent(this) == 0 ) { | |
687 this->WaitAudio = DX6_WaitAudio_EventWait; | |
688 } else { | |
689 this->WaitAudio = DX5_WaitAudio_BusyWait; | |
690 } | |
691 #endif | |
692 return(0); | |
693 } | |
694 |