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