2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Allow access to a raw mixing buffer */
26 #include "SDL_timer.h"
27 #include "SDL_audio.h"
28 #include "../SDL_audio_c.h"
29 #include "SDL_dx5audio.h"
31 /* Define this if you want to use DirectX 6 DirectSoundNotify interface */
32 //#define USE_POSITION_NOTIFY
34 /* DirectX function pointers for audio */
35 HRESULT (WINAPI *DSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
37 /* Audio driver functions */
38 static int DX5_OpenAudio(_THIS, SDL_AudioSpec *spec);
39 static void DX5_ThreadInit(_THIS);
40 static void DX5_WaitAudio_BusyWait(_THIS);
41 #ifdef USE_POSITION_NOTIFY
42 static void DX6_WaitAudio_EventWait(_THIS);
44 static void DX5_PlayAudio(_THIS);
45 static Uint8 *DX5_GetAudioBuf(_THIS);
46 static void DX5_WaitDone(_THIS);
47 static void DX5_CloseAudio(_THIS);
49 /* Audio driver bootstrap functions */
51 static int Audio_Available(void)
56 /* Version check DSOUND.DLL (Is DirectX okay?) */
58 DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL"));
59 if ( DSoundDLL != NULL ) {
60 /* We just use basic DirectSound, we're okay */
62 /* Unfortunately, the sound drivers on NT have
63 higher latencies than the audio buffers used
64 by many SDL applications, so there are gaps
65 in the audio - it sounds terrible. Punt for now.
68 ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
70 switch (ver.dwPlatformId) {
71 case VER_PLATFORM_WIN32_NT:
72 if ( ver.dwMajorVersion > 4 ) {
85 /* Now check for DirectX 5 or better - otherwise
86 * we will fail later in DX5_OpenAudio without a chance
87 * to fall back to the DIB driver. */
89 /* DirectSoundCaptureCreate was added in DX5 */
90 if (!GetProcAddress(DSoundDLL, TEXT("DirectSoundCaptureCreate")))
95 FreeLibrary(DSoundDLL);
100 /* Functions for loading the DirectX functions dynamically */
101 static HINSTANCE DSoundDLL = NULL;
103 static void DX5_Unload(void)
105 if ( DSoundDLL != NULL ) {
106 FreeLibrary(DSoundDLL);
111 static int DX5_Load(void)
116 DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL"));
117 if ( DSoundDLL != NULL ) {
118 DSoundCreate = (void *)GetProcAddress(DSoundDLL,
119 TEXT("DirectSoundCreate"));
121 if ( DSoundDLL && DSoundCreate ) {
130 static void Audio_DeleteDevice(SDL_AudioDevice *device)
133 SDL_free(device->hidden);
137 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
139 SDL_AudioDevice *this;
142 if ( DX5_Load() < 0 ) {
146 /* Initialize all variables that we clean on shutdown */
147 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
149 SDL_memset(this, 0, (sizeof *this));
150 this->hidden = (struct SDL_PrivateAudioData *)
151 SDL_malloc((sizeof *this->hidden));
153 if ( (this == NULL) || (this->hidden == NULL) ) {
160 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
162 /* Set the function pointers */
163 this->OpenAudio = DX5_OpenAudio;
164 this->ThreadInit = DX5_ThreadInit;
165 this->WaitAudio = DX5_WaitAudio_BusyWait;
166 this->PlayAudio = DX5_PlayAudio;
167 this->GetAudioBuf = DX5_GetAudioBuf;
168 this->WaitDone = DX5_WaitDone;
169 this->CloseAudio = DX5_CloseAudio;
171 this->free = Audio_DeleteDevice;
176 AudioBootStrap DSOUND_bootstrap = {
177 "dsound", "Win95/98/2000 DirectSound",
178 Audio_Available, Audio_CreateDevice
181 static void SetDSerror(const char *function, int code)
183 static const char *error;
184 static char errbuf[1024];
190 "Unsupported interface\n-- Is DirectX 5.0 or later installed?";
192 case DSERR_ALLOCATED:
193 error = "Audio device in use";
195 case DSERR_BADFORMAT:
196 error = "Unsupported audio format";
198 case DSERR_BUFFERLOST:
199 error = "Mixing buffer was lost";
201 case DSERR_CONTROLUNAVAIL:
202 error = "Control requested is not available";
204 case DSERR_INVALIDCALL:
205 error = "Invalid call for the current state";
207 case DSERR_INVALIDPARAM:
208 error = "Invalid parameter";
211 error = "No audio device found";
213 case DSERR_OUTOFMEMORY:
214 error = "Out of memory";
216 case DSERR_PRIOLEVELNEEDED:
217 error = "Caller doesn't have priority";
219 case DSERR_UNSUPPORTED:
220 error = "Function not supported";
223 SDL_snprintf(errbuf, SDL_arraysize(errbuf),
224 "%s: Unknown DirectSound error: 0x%x",
229 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error);
231 SDL_SetError("%s", errbuf);
235 /* DirectSound needs to be associated with a window */
236 static HWND mainwin = NULL;
238 void DX5_SoundFocus(HWND hwnd)
243 static void DX5_ThreadInit(_THIS)
245 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
248 static void DX5_WaitAudio_BusyWait(_THIS)
254 /* Semi-busy wait, since we have no way of getting play notification
255 on a primary mixing buffer located in hardware (DirectX 5.0)
257 result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &junk, &cursor);
258 if ( result != DS_OK ) {
259 if ( result == DSERR_BUFFERLOST ) {
260 IDirectSoundBuffer_Restore(mixbuf);
263 SetDSerror("DirectSound GetCurrentPosition", result);
268 while ( (cursor/mixlen) == lastchunk ) {
269 /* FIXME: find out how much time is left and sleep that long */
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) ) {
281 if ( ! (status&DSBSTATUS_PLAYING) ) {
282 result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING);
283 if ( result == DS_OK ) {
287 SetDSerror("DirectSound Play", result);
292 /* Find out where we are playing */
293 result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
295 if ( result != DS_OK ) {
296 SetDSerror("DirectSound GetCurrentPosition", result);
302 #ifdef USE_POSITION_NOTIFY
303 static void DX6_WaitAudio_EventWait(_THIS)
308 /* Try to restore a lost sound buffer */
309 IDirectSoundBuffer_GetStatus(mixbuf, &status);
310 if ( (status&DSBSTATUS_BUFFERLOST) ) {
311 IDirectSoundBuffer_Restore(mixbuf);
312 IDirectSoundBuffer_GetStatus(mixbuf, &status);
313 if ( (status&DSBSTATUS_BUFFERLOST) ) {
317 if ( ! (status&DSBSTATUS_PLAYING) ) {
318 result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING);
319 if ( result != DS_OK ) {
321 SetDSerror("DirectSound Play", result);
326 WaitForSingleObject(audio_event, INFINITE);
328 #endif /* USE_POSITION_NOTIFY */
330 static void DX5_PlayAudio(_THIS)
332 /* Unlock the buffer, allowing it to play */
334 IDirectSoundBuffer_Unlock(mixbuf, locked_buf, mixlen, NULL, 0);
339 static Uint8 *DX5_GetAudioBuf(_THIS)
345 /* Figure out which blocks to fill next */
347 result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &junk, &cursor);
348 if ( result == DSERR_BUFFERLOST ) {
349 IDirectSoundBuffer_Restore(mixbuf);
350 result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
353 if ( result != DS_OK ) {
354 SetDSerror("DirectSound GetCurrentPosition", result);
359 /* Detect audio dropouts */
360 { DWORD spot = cursor;
361 if ( spot < lastchunk ) {
364 if ( spot > lastchunk+1 ) {
365 fprintf(stderr, "Audio dropout, missed %d fragments\n",
366 (spot - (lastchunk+1)));
371 cursor = (cursor+1)%NUM_BUFFERS;
374 /* Lock the audio buffer */
375 result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen,
376 (LPVOID *)&locked_buf, &rawlen, NULL, &junk, 0);
377 if ( result == DSERR_BUFFERLOST ) {
378 IDirectSoundBuffer_Restore(mixbuf);
379 result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen,
380 (LPVOID *)&locked_buf, &rawlen, NULL, &junk, 0);
382 if ( result != DS_OK ) {
383 SetDSerror("DirectSound Lock", result);
389 static void DX5_WaitDone(_THIS)
393 /* Wait for the playing chunk to finish */
394 stream = this->GetAudioBuf(this);
395 if ( stream != NULL ) {
396 SDL_memset(stream, silence, mixlen);
397 this->PlayAudio(this);
399 this->WaitAudio(this);
401 /* Stop the looping sound buffer */
402 IDirectSoundBuffer_Stop(mixbuf);
405 static void DX5_CloseAudio(_THIS)
407 if ( sound != NULL ) {
408 if ( mixbuf != NULL ) {
409 /* Clean up the audio buffer */
410 IDirectSoundBuffer_Release(mixbuf);
413 if ( audio_event != NULL ) {
414 CloseHandle(audio_event);
417 IDirectSound_Release(sound);
422 #ifdef USE_PRIMARY_BUFFER
423 /* This function tries to create a primary audio buffer, and returns the
424 number of audio chunks available in the created buffer.
426 static int CreatePrimary(LPDIRECTSOUND sndObj, HWND focus,
427 LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize)
434 /* Try to set primary mixing privileges */
435 result = IDirectSound_SetCooperativeLevel(sndObj, focus,
437 if ( result != DS_OK ) {
439 SetDSerror("DirectSound SetCooperativeLevel", result);
444 /* Try to create the primary buffer */
445 SDL_memset(&format, 0, sizeof(format));
446 format.dwSize = sizeof(format);
447 format.dwFlags=(DSBCAPS_PRIMARYBUFFER|DSBCAPS_GETCURRENTPOSITION2);
448 format.dwFlags |= DSBCAPS_STICKYFOCUS;
449 #ifdef USE_POSITION_NOTIFY
450 format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
452 result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
453 if ( result != DS_OK ) {
455 SetDSerror("DirectSound CreateSoundBuffer", result);
460 /* Check the size of the fragment buffer */
461 SDL_memset(&caps, 0, sizeof(caps));
462 caps.dwSize = sizeof(caps);
463 result = IDirectSoundBuffer_GetCaps(*sndbuf, &caps);
464 if ( result != DS_OK ) {
466 SetDSerror("DirectSound GetCaps", result);
468 IDirectSoundBuffer_Release(*sndbuf);
471 if ( (chunksize > caps.dwBufferBytes) ||
472 ((caps.dwBufferBytes%chunksize) != 0) ) {
473 /* The primary buffer size is not a multiple of 'chunksize'
474 -- this hopefully doesn't happen when 'chunksize' is a
477 IDirectSoundBuffer_Release(*sndbuf);
479 "Primary buffer size is: %d, cannot break it into chunks of %d bytes\n",
480 caps.dwBufferBytes, chunksize);
483 numchunks = (caps.dwBufferBytes/chunksize);
485 /* Set the primary audio format */
486 result = IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);
487 if ( result != DS_OK ) {
489 SetDSerror("DirectSound SetFormat", result);
491 IDirectSoundBuffer_Release(*sndbuf);
496 #endif /* USE_PRIMARY_BUFFER */
498 /* This function tries to create a secondary audio buffer, and returns the
499 number of audio chunks available in the created buffer.
501 static int CreateSecondary(LPDIRECTSOUND sndObj, HWND focus,
502 LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize)
504 const int numchunks = 8;
507 LPVOID pvAudioPtr1, pvAudioPtr2;
508 DWORD dwAudioBytes1, dwAudioBytes2;
510 /* Try to set primary mixing privileges */
512 result = IDirectSound_SetCooperativeLevel(sndObj,
513 focus, DSSCL_PRIORITY);
515 result = IDirectSound_SetCooperativeLevel(sndObj,
516 GetDesktopWindow(), DSSCL_NORMAL);
518 if ( result != DS_OK ) {
520 SetDSerror("DirectSound SetCooperativeLevel", result);
525 /* Try to create the secondary buffer */
526 SDL_memset(&format, 0, sizeof(format));
527 format.dwSize = sizeof(format);
528 format.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
529 #ifdef USE_POSITION_NOTIFY
530 format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
533 format.dwFlags |= DSBCAPS_GLOBALFOCUS;
535 format.dwFlags |= DSBCAPS_STICKYFOCUS;
537 format.dwBufferBytes = numchunks*chunksize;
538 if ( (format.dwBufferBytes < DSBSIZE_MIN) ||
539 (format.dwBufferBytes > DSBSIZE_MAX) ) {
540 SDL_SetError("Sound buffer size must be between %d and %d",
541 DSBSIZE_MIN/numchunks, DSBSIZE_MAX/numchunks);
544 format.dwReserved = 0;
545 format.lpwfxFormat = wavefmt;
546 result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
547 if ( result != DS_OK ) {
548 SetDSerror("DirectSound CreateSoundBuffer", result);
551 IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);
553 /* Silence the initial audio buffer */
554 result = IDirectSoundBuffer_Lock(*sndbuf, 0, format.dwBufferBytes,
555 (LPVOID *)&pvAudioPtr1, &dwAudioBytes1,
556 (LPVOID *)&pvAudioPtr2, &dwAudioBytes2,
557 DSBLOCK_ENTIREBUFFER);
558 if ( result == DS_OK ) {
559 if ( wavefmt->wBitsPerSample == 8 ) {
560 SDL_memset(pvAudioPtr1, 0x80, dwAudioBytes1);
562 SDL_memset(pvAudioPtr1, 0x00, dwAudioBytes1);
564 IDirectSoundBuffer_Unlock(*sndbuf,
565 (LPVOID)pvAudioPtr1, dwAudioBytes1,
566 (LPVOID)pvAudioPtr2, dwAudioBytes2);
569 /* We're ready to go */
573 /* This function tries to set position notify events on the mixing buffer */
574 #ifdef USE_POSITION_NOTIFY
575 static int CreateAudioEvent(_THIS)
577 LPDIRECTSOUNDNOTIFY notify;
578 DSBPOSITIONNOTIFY *notify_positions;
582 /* Default to fail on exit */
586 /* Query for the interface */
587 result = IDirectSoundBuffer_QueryInterface(mixbuf,
588 &IID_IDirectSoundNotify, (void *)¬ify);
589 if ( result != DS_OK ) {
593 /* Allocate the notify structures */
594 notify_positions = (DSBPOSITIONNOTIFY *)SDL_malloc(NUM_BUFFERS*
595 sizeof(*notify_positions));
596 if ( notify_positions == NULL ) {
600 /* Create the notify event */
601 audio_event = CreateEvent(NULL, FALSE, FALSE, NULL);
602 if ( audio_event == NULL ) {
606 /* Set up the notify structures */
607 for ( i=0; i<NUM_BUFFERS; ++i ) {
608 notify_positions[i].dwOffset = i*mixlen;
609 notify_positions[i].hEventNotify = audio_event;
611 result = IDirectSoundNotify_SetNotificationPositions(notify,
612 NUM_BUFFERS, notify_positions);
613 if ( result == DS_OK ) {
617 if ( notify != NULL ) {
618 IDirectSoundNotify_Release(notify);
622 #endif /* USE_POSITION_NOTIFY */
624 static int DX5_OpenAudio(_THIS, SDL_AudioSpec *spec)
627 WAVEFORMATEX waveformat;
629 /* Set basic WAVE format parameters */
630 SDL_memset(&waveformat, 0, sizeof(waveformat));
631 waveformat.wFormatTag = WAVE_FORMAT_PCM;
633 /* Determine the audio parameters from the AudioSpec */
634 switch ( spec->format & 0xFF ) {
636 /* Unsigned 8 bit audio data */
637 spec->format = AUDIO_U8;
639 waveformat.wBitsPerSample = 8;
642 /* Signed 16 bit audio data */
643 spec->format = AUDIO_S16;
645 waveformat.wBitsPerSample = 16;
648 SDL_SetError("Unsupported audio format");
651 waveformat.nChannels = spec->channels;
652 waveformat.nSamplesPerSec = spec->freq;
653 waveformat.nBlockAlign =
654 waveformat.nChannels * (waveformat.wBitsPerSample/8);
655 waveformat.nAvgBytesPerSec =
656 waveformat.nSamplesPerSec * waveformat.nBlockAlign;
658 /* Update the fragment size as size in bytes */
659 SDL_CalculateAudioSpec(spec);
661 /* Open the audio device */
662 result = DSoundCreate(NULL, &sound, NULL);
663 if ( result != DS_OK ) {
664 SetDSerror("DirectSoundCreate", result);
668 /* Create the audio buffer to which we write */
670 #ifdef USE_PRIMARY_BUFFER
672 NUM_BUFFERS = CreatePrimary(sound, mainwin, &mixbuf,
673 &waveformat, spec->size);
675 #endif /* USE_PRIMARY_BUFFER */
676 if ( NUM_BUFFERS < 0 ) {
677 NUM_BUFFERS = CreateSecondary(sound, mainwin, &mixbuf,
678 &waveformat, spec->size);
679 if ( NUM_BUFFERS < 0 ) {
683 fprintf(stderr, "Using secondary audio buffer\n");
688 fprintf(stderr, "Using primary audio buffer\n");
691 /* The buffer will auto-start playing in DX5_WaitAudio() */
695 #ifdef USE_POSITION_NOTIFY
696 /* See if we can use DirectX 6 event notification */
697 if ( CreateAudioEvent(this) == 0 ) {
698 this->WaitAudio = DX6_WaitAudio_EventWait;
700 this->WaitAudio = DX5_WaitAudio_BusyWait;