src/audio/windx5/SDL_dx5audio.c
changeset 0 74212992fb08
child 118 7c47e511459d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/windx5/SDL_dx5audio.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,677 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* Allow access to a raw mixing buffer */
    1.32 +
    1.33 +#include <stdio.h>
    1.34 +
    1.35 +#include "SDL_types.h"
    1.36 +#include "SDL_error.h"
    1.37 +#include "SDL_timer.h"
    1.38 +#include "SDL_audio.h"
    1.39 +#include "SDL_audio_c.h"
    1.40 +#include "SDL_dx5audio.h"
    1.41 +
    1.42 +/* Define this if you want to use DirectX 6 DirectSoundNotify interface */
    1.43 +//#define USE_POSITION_NOTIFY
    1.44 +
    1.45 +/* DirectX function pointers for audio */
    1.46 +HRESULT (WINAPI *DSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
    1.47 +
    1.48 +/* Audio driver functions */
    1.49 +static int DX5_OpenAudio(_THIS, SDL_AudioSpec *spec);
    1.50 +static void DX5_ThreadInit(_THIS);
    1.51 +static void DX5_WaitAudio_BusyWait(_THIS);
    1.52 +#ifdef USE_POSITION_NOTIFY
    1.53 +static void DX6_WaitAudio_EventWait(_THIS);
    1.54 +#endif
    1.55 +static void DX5_PlayAudio(_THIS);
    1.56 +static Uint8 *DX5_GetAudioBuf(_THIS);
    1.57 +static void DX5_WaitDone(_THIS);
    1.58 +static void DX5_CloseAudio(_THIS);
    1.59 +
    1.60 +/* Audio driver bootstrap functions */
    1.61 +
    1.62 +static int Audio_Available(void)
    1.63 +{
    1.64 +	HINSTANCE DSoundDLL;
    1.65 +	int dsound_ok;
    1.66 +
    1.67 +	/* Version check DSOUND.DLL (Is DirectX okay?) */
    1.68 +	dsound_ok = 0;
    1.69 +	DSoundDLL = LoadLibrary("DSOUND.DLL");
    1.70 +	if ( DSoundDLL != NULL ) {
    1.71 +		/* We just use basic DirectSound, we're okay */
    1.72 +		/* Yay! */
    1.73 +		/* Unfortunately, the sound drivers on NT have
    1.74 +		   higher latencies than the audio buffers used
    1.75 +		   by many SDL applications, so there are gaps
    1.76 +		   in the audio - it sounds terrible.  Punt for now.
    1.77 +		 */
    1.78 +		OSVERSIONINFO ver;
    1.79 +		ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
    1.80 +		GetVersionEx(&ver);
    1.81 +		switch (ver.dwPlatformId) {
    1.82 +			case VER_PLATFORM_WIN32_NT:
    1.83 +				if ( ver.dwMajorVersion > 4 ) {
    1.84 +					/* Win2K */
    1.85 +					dsound_ok = 1;
    1.86 +				} else {
    1.87 +					/* WinNT */
    1.88 +					dsound_ok = 0;
    1.89 +				}
    1.90 +				break;
    1.91 +			default:
    1.92 +				/* Win95 or Win98 */
    1.93 +				dsound_ok = 1;
    1.94 +				break;
    1.95 +		}
    1.96 +		/* Now check for DirectX 5 or better - otherwise
    1.97 +		 * we will fail later in DX5_OpenAudio without a chance
    1.98 +		 * to fall back to the DIB driver. */
    1.99 +		if (dsound_ok) {
   1.100 +			/* DirectSoundCaptureCreate was added in DX5 */
   1.101 +			if (!GetProcAddress(DSoundDLL, "DirectSoundCaptureCreate"))
   1.102 +				dsound_ok = 0;
   1.103 +
   1.104 +		}
   1.105 +		/* Clean up.. */
   1.106 +		FreeLibrary(DSoundDLL);
   1.107 +	}
   1.108 +	return(dsound_ok);
   1.109 +}
   1.110 +
   1.111 +/* Functions for loading the DirectX functions dynamically */
   1.112 +static HINSTANCE DSoundDLL = NULL;
   1.113 +
   1.114 +static void DX5_Unload(void)
   1.115 +{
   1.116 +	if ( DSoundDLL != NULL ) {
   1.117 +		FreeLibrary(DSoundDLL);
   1.118 +		DSoundCreate = NULL;
   1.119 +		DSoundDLL = NULL;
   1.120 +	}
   1.121 +}
   1.122 +static int DX5_Load(void)
   1.123 +{
   1.124 +	int status;
   1.125 +
   1.126 +	DX5_Unload();
   1.127 +	DSoundDLL = LoadLibrary("DSOUND.DLL");
   1.128 +	if ( DSoundDLL != NULL ) {
   1.129 +		DSoundCreate = (void *)GetProcAddress(DSoundDLL,
   1.130 +					"DirectSoundCreate");
   1.131 +	}
   1.132 +	if ( DSoundDLL && DSoundCreate ) {
   1.133 +		status = 0;
   1.134 +	} else {
   1.135 +		DX5_Unload();
   1.136 +		status = -1;
   1.137 +	}
   1.138 +	return status;
   1.139 +}
   1.140 +
   1.141 +static void Audio_DeleteDevice(SDL_AudioDevice *device)
   1.142 +{
   1.143 +	DX5_Unload();
   1.144 +	free(device->hidden);
   1.145 +	free(device);
   1.146 +}
   1.147 +
   1.148 +static SDL_AudioDevice *Audio_CreateDevice(int devindex)
   1.149 +{
   1.150 +	SDL_AudioDevice *this;
   1.151 +
   1.152 +	/* Load DirectX */
   1.153 +	if ( DX5_Load() < 0 ) {
   1.154 +		return(NULL);
   1.155 +	}
   1.156 +
   1.157 +	/* Initialize all variables that we clean on shutdown */
   1.158 +	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
   1.159 +	if ( this ) {
   1.160 +		memset(this, 0, (sizeof *this));
   1.161 +		this->hidden = (struct SDL_PrivateAudioData *)
   1.162 +				malloc((sizeof *this->hidden));
   1.163 +	}
   1.164 +	if ( (this == NULL) || (this->hidden == NULL) ) {
   1.165 +		SDL_OutOfMemory();
   1.166 +		if ( this ) {
   1.167 +			free(this);
   1.168 +		}
   1.169 +		return(0);
   1.170 +	}
   1.171 +	memset(this->hidden, 0, (sizeof *this->hidden));
   1.172 +
   1.173 +	/* Set the function pointers */
   1.174 +	this->OpenAudio = DX5_OpenAudio;
   1.175 +	this->ThreadInit = DX5_ThreadInit;
   1.176 +	this->WaitAudio = DX5_WaitAudio_BusyWait;
   1.177 +	this->PlayAudio = DX5_PlayAudio;
   1.178 +	this->GetAudioBuf = DX5_GetAudioBuf;
   1.179 +	this->WaitDone = DX5_WaitDone;
   1.180 +	this->CloseAudio = DX5_CloseAudio;
   1.181 +
   1.182 +	this->free = Audio_DeleteDevice;
   1.183 +
   1.184 +	return this;
   1.185 +}
   1.186 +
   1.187 +AudioBootStrap DSOUND_bootstrap = {
   1.188 +	"dsound", "Win95/98/2000 DirectSound",
   1.189 +	Audio_Available, Audio_CreateDevice
   1.190 +};
   1.191 +
   1.192 +static void SetDSerror(const char *function, int code)
   1.193 +{
   1.194 +	static const char *error;
   1.195 +	static char  errbuf[BUFSIZ];
   1.196 +
   1.197 +	errbuf[0] = 0;
   1.198 +	switch (code) {
   1.199 +		case E_NOINTERFACE:
   1.200 +			error = 
   1.201 +		"Unsupported interface\n-- Is DirectX 5.0 or later installed?";
   1.202 +			break;
   1.203 +		case DSERR_ALLOCATED:
   1.204 +			error = "Audio device in use";
   1.205 +			break;
   1.206 +		case DSERR_BADFORMAT:
   1.207 +			error = "Unsupported audio format";
   1.208 +			break;
   1.209 +		case DSERR_BUFFERLOST:
   1.210 +			error = "Mixing buffer was lost";
   1.211 +			break;
   1.212 +		case DSERR_CONTROLUNAVAIL:
   1.213 +			error = "Control requested is not available";
   1.214 +			break;
   1.215 +		case DSERR_INVALIDCALL:
   1.216 +			error = "Invalid call for the current state";
   1.217 +			break;
   1.218 +		case DSERR_INVALIDPARAM:
   1.219 +			error = "Invalid parameter";
   1.220 +			break;
   1.221 +		case DSERR_NODRIVER:
   1.222 +			error = "No audio device found";
   1.223 +			break;
   1.224 +		case DSERR_OUTOFMEMORY:
   1.225 +			error = "Out of memory";
   1.226 +			break;
   1.227 +		case DSERR_PRIOLEVELNEEDED:
   1.228 +			error = "Caller doesn't have priority";
   1.229 +			break;
   1.230 +		case DSERR_UNSUPPORTED:
   1.231 +			error = "Function not supported";
   1.232 +			break;
   1.233 +		default:
   1.234 +			sprintf(errbuf, "%s: Unknown DirectSound error: 0x%x",
   1.235 +								function, code);
   1.236 +			break;
   1.237 +	}
   1.238 +	if ( ! errbuf[0] ) {
   1.239 +		sprintf(errbuf, "%s: %s", function, error);
   1.240 +	}
   1.241 +	SDL_SetError("%s", errbuf);
   1.242 +	return;
   1.243 +}
   1.244 +
   1.245 +/* DirectSound needs to be associated with a window */
   1.246 +static HWND mainwin = NULL;
   1.247 +/* */
   1.248 +void DX5_SoundFocus(HWND hwnd)
   1.249 +{
   1.250 +	mainwin = hwnd;
   1.251 +}
   1.252 +
   1.253 +static void DX5_ThreadInit(_THIS)
   1.254 +{
   1.255 +	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
   1.256 +}
   1.257 +
   1.258 +static void DX5_WaitAudio_BusyWait(_THIS)
   1.259 +{
   1.260 +	DWORD status;
   1.261 +	DWORD cursor, junk;
   1.262 +	HRESULT result;
   1.263 +
   1.264 +	/* Semi-busy wait, since we have no way of getting play notification
   1.265 +	   on a primary mixing buffer located in hardware (DirectX 5.0)
   1.266 +	*/
   1.267 +	result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &cursor, &junk);
   1.268 +	if ( result != DS_OK ) {
   1.269 +		if ( result == DSERR_BUFFERLOST ) {
   1.270 +			IDirectSoundBuffer_Restore(mixbuf);
   1.271 +		}
   1.272 +#ifdef DEBUG_SOUND
   1.273 +		SetDSerror("DirectSound GetCurrentPosition", result);
   1.274 +#endif
   1.275 +		return;
   1.276 +	}
   1.277 +	cursor /= mixlen;
   1.278 +
   1.279 +	while ( cursor == playing ) {
   1.280 +		/* FIXME: find out how much time is left and sleep that long */
   1.281 +		SDL_Delay(10);
   1.282 +
   1.283 +		/* Try to restore a lost sound buffer */
   1.284 +		IDirectSoundBuffer_GetStatus(mixbuf, &status);
   1.285 +		if ( (status&DSBSTATUS_BUFFERLOST) ) {
   1.286 +			IDirectSoundBuffer_Restore(mixbuf);
   1.287 +			IDirectSoundBuffer_GetStatus(mixbuf, &status);
   1.288 +			if ( (status&DSBSTATUS_BUFFERLOST) ) {
   1.289 +				break;
   1.290 +			}
   1.291 +		}
   1.292 +		if ( ! (status&DSBSTATUS_PLAYING) ) {
   1.293 +			result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING);
   1.294 +			if ( result == DS_OK ) {
   1.295 +				continue;
   1.296 +			}
   1.297 +#ifdef DEBUG_SOUND
   1.298 +			SetDSerror("DirectSound Play", result);
   1.299 +#endif
   1.300 +			return;
   1.301 +		}
   1.302 +
   1.303 +		/* Find out where we are playing */
   1.304 +		result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
   1.305 +								&cursor, &junk);
   1.306 +		if ( result != DS_OK ) {
   1.307 +			SetDSerror("DirectSound GetCurrentPosition", result);
   1.308 +			return;
   1.309 +		}
   1.310 +		cursor /= mixlen;
   1.311 +	}
   1.312 +}
   1.313 +
   1.314 +#ifdef USE_POSITION_NOTIFY
   1.315 +static void DX6_WaitAudio_EventWait(_THIS)
   1.316 +{
   1.317 +	DWORD status;
   1.318 +	HRESULT result;
   1.319 +
   1.320 +	/* Try to restore a lost sound buffer */
   1.321 +	IDirectSoundBuffer_GetStatus(mixbuf, &status);
   1.322 +	if ( (status&DSBSTATUS_BUFFERLOST) ) {
   1.323 +		IDirectSoundBuffer_Restore(mixbuf);
   1.324 +		IDirectSoundBuffer_GetStatus(mixbuf, &status);
   1.325 +		if ( (status&DSBSTATUS_BUFFERLOST) ) {
   1.326 +			return;
   1.327 +		}
   1.328 +	}
   1.329 +	if ( ! (status&DSBSTATUS_PLAYING) ) {
   1.330 +		result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING);
   1.331 +		if ( result != DS_OK ) {
   1.332 +#ifdef DEBUG_SOUND
   1.333 +			SetDSerror("DirectSound Play", result);
   1.334 +#endif
   1.335 +			return;
   1.336 +		}
   1.337 +	}
   1.338 +	WaitForSingleObject(audio_event, INFINITE);
   1.339 +}
   1.340 +#endif /* USE_POSITION_NOTIFY */
   1.341 +
   1.342 +static void DX5_PlayAudio(_THIS)
   1.343 +{
   1.344 +	/* Unlock the buffer, allowing it to play */
   1.345 +	if ( locked_buf ) {
   1.346 +		IDirectSoundBuffer_Unlock(mixbuf, locked_buf, mixlen, NULL, 0);
   1.347 +	}
   1.348 +
   1.349 +}
   1.350 +
   1.351 +static Uint8 *DX5_GetAudioBuf(_THIS)
   1.352 +{
   1.353 +	DWORD   cursor, junk;
   1.354 +	HRESULT result;
   1.355 +	DWORD   rawlen;
   1.356 +
   1.357 +	/* Figure out which blocks to fill next */
   1.358 +	locked_buf = NULL;
   1.359 +	result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &cursor, &junk);
   1.360 +	if ( result == DSERR_BUFFERLOST ) {
   1.361 +		IDirectSoundBuffer_Restore(mixbuf);
   1.362 +		result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
   1.363 +								&cursor, &junk);
   1.364 +	}
   1.365 +	if ( result != DS_OK ) {
   1.366 +		SetDSerror("DirectSound GetCurrentPosition", result);
   1.367 +		return(NULL);
   1.368 +	}
   1.369 +	cursor /= mixlen;
   1.370 +	playing = cursor;
   1.371 +	cursor = (cursor+1)%NUM_BUFFERS;
   1.372 +	cursor *= mixlen;
   1.373 +
   1.374 +	/* Lock the audio buffer */
   1.375 +	result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen,
   1.376 +				(LPVOID *)&locked_buf, &rawlen, NULL, &junk, 0);
   1.377 +	if ( result == DSERR_BUFFERLOST ) {
   1.378 +		IDirectSoundBuffer_Restore(mixbuf);
   1.379 +		result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen,
   1.380 +				(LPVOID *)&locked_buf, &rawlen, NULL, &junk, 0);
   1.381 +	}
   1.382 +	if ( result != DS_OK ) {
   1.383 +		SetDSerror("DirectSound Lock", result);
   1.384 +		return(NULL);
   1.385 +	}
   1.386 +	return(locked_buf);
   1.387 +}
   1.388 +
   1.389 +static void DX5_WaitDone(_THIS)
   1.390 +{
   1.391 +	Uint8 *stream;
   1.392 +
   1.393 +	/* Wait for the playing chunk to finish */
   1.394 +	stream = this->GetAudioBuf(this);
   1.395 +	if ( stream != NULL ) {
   1.396 +		memset(stream, silence, mixlen);
   1.397 +		this->PlayAudio(this);
   1.398 +	}
   1.399 +	this->WaitAudio(this);
   1.400 +
   1.401 +	/* Stop the looping sound buffer */
   1.402 +	IDirectSoundBuffer_Stop(mixbuf);
   1.403 +}
   1.404 +
   1.405 +static void DX5_CloseAudio(_THIS)
   1.406 +{
   1.407 +	if ( sound != NULL ) {
   1.408 +		if ( mixbuf != NULL ) {
   1.409 +			/* Clean up the audio buffer */
   1.410 +			IDirectSoundBuffer_Release(mixbuf);
   1.411 +			mixbuf = NULL;
   1.412 +		}
   1.413 +		if ( audio_event != NULL ) {
   1.414 +			CloseHandle(audio_event);
   1.415 +			audio_event = NULL;
   1.416 +		}
   1.417 +		IDirectSound_Release(sound);
   1.418 +		sound = NULL;
   1.419 +	}
   1.420 +}
   1.421 +
   1.422 +/* This function tries to create a primary audio buffer, and returns the
   1.423 +   number of audio chunks available in the created buffer.
   1.424 +*/
   1.425 +static int CreatePrimary(LPDIRECTSOUND sndObj, HWND focus, 
   1.426 +	LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize)
   1.427 +{
   1.428 +	HRESULT result;
   1.429 +	DSBUFFERDESC format;
   1.430 +	DSBCAPS caps;
   1.431 +	int numchunks;
   1.432 +
   1.433 +	/* Try to set primary mixing privileges */
   1.434 +	result = IDirectSound_SetCooperativeLevel(sndObj, focus,
   1.435 +							DSSCL_WRITEPRIMARY);
   1.436 +	if ( result != DS_OK ) {
   1.437 +#ifdef DEBUG_SOUND
   1.438 +		SetDSerror("DirectSound SetCooperativeLevel", result);
   1.439 +#endif
   1.440 +		return(-1);
   1.441 +	}
   1.442 +
   1.443 +	/* Try to create the primary buffer */
   1.444 +	memset(&format, 0, sizeof(format));
   1.445 +	format.dwSize = sizeof(format);
   1.446 +	format.dwFlags=(DSBCAPS_PRIMARYBUFFER|DSBCAPS_GETCURRENTPOSITION2);
   1.447 +#ifdef USE_POSITION_NOTIFY
   1.448 +	format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
   1.449 +#endif
   1.450 +	result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
   1.451 +	if ( result != DS_OK ) {
   1.452 +#ifdef DEBUG_SOUND
   1.453 +		SetDSerror("DirectSound CreateSoundBuffer", result);
   1.454 +#endif
   1.455 +		return(-1);
   1.456 +	}
   1.457 +
   1.458 +	/* Check the size of the fragment buffer */
   1.459 +	memset(&caps, 0, sizeof(caps));
   1.460 +	caps.dwSize = sizeof(caps);
   1.461 +	result = IDirectSoundBuffer_GetCaps(*sndbuf, &caps);
   1.462 +	if ( result != DS_OK ) {
   1.463 +#ifdef DEBUG_SOUND
   1.464 +		SetDSerror("DirectSound GetCaps", result);
   1.465 +#endif
   1.466 +		IDirectSoundBuffer_Release(*sndbuf);
   1.467 +		return(-1);
   1.468 +	}
   1.469 +	if ( (chunksize > caps.dwBufferBytes) ||
   1.470 +				((caps.dwBufferBytes%chunksize) != 0) ) {
   1.471 +		/* The primary buffer size is not a multiple of 'chunksize'
   1.472 +		   -- this hopefully doesn't happen when 'chunksize' is a 
   1.473 +		      power of 2.
   1.474 +		*/
   1.475 +		IDirectSoundBuffer_Release(*sndbuf);
   1.476 +		SDL_SetError(
   1.477 +"Primary buffer size is: %d, cannot break it into chunks of %d bytes\n",
   1.478 +					caps.dwBufferBytes, chunksize);
   1.479 +		return(-1);
   1.480 +	}
   1.481 +	numchunks = (caps.dwBufferBytes/chunksize);
   1.482 +
   1.483 +	/* Set the primary audio format */
   1.484 +	result = IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);
   1.485 +	if ( result != DS_OK ) {
   1.486 +#ifdef DEBUG_SOUND
   1.487 +		SetDSerror("DirectSound SetFormat", result);
   1.488 +#endif
   1.489 +		IDirectSoundBuffer_Release(*sndbuf);
   1.490 +		return(-1);
   1.491 +	}
   1.492 +	return(numchunks);
   1.493 +}
   1.494 +
   1.495 +/* This function tries to create a secondary audio buffer, and returns the
   1.496 +   number of audio chunks available in the created buffer.
   1.497 +*/
   1.498 +static int CreateSecondary(LPDIRECTSOUND sndObj, HWND focus,
   1.499 +	LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize)
   1.500 +{
   1.501 +	HRESULT result;
   1.502 +	DSBUFFERDESC format;
   1.503 +	const int numchunks = 2;
   1.504 +
   1.505 +	/* Try to set primary mixing privileges */
   1.506 +	if ( focus ) {
   1.507 +		result = IDirectSound_SetCooperativeLevel(sndObj,
   1.508 +					focus, DSSCL_PRIORITY);
   1.509 +	} else {
   1.510 +		result = IDirectSound_SetCooperativeLevel(sndObj,
   1.511 +					GetDesktopWindow(), DSSCL_NORMAL);
   1.512 +	}
   1.513 +	if ( result != DS_OK ) {
   1.514 +#ifdef DEBUG_SOUND
   1.515 +		SetDSerror("DirectSound SetCooperativeLevel", result);
   1.516 +#endif
   1.517 +		return(-1);
   1.518 +	}
   1.519 +
   1.520 +	/* Try to create the secondary buffer */
   1.521 +	memset(&format, 0, sizeof(format));
   1.522 +	format.dwSize = sizeof(format);
   1.523 +	format.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
   1.524 +#ifdef USE_POSITION_NOTIFY
   1.525 +	format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
   1.526 +#endif
   1.527 +	if ( ! focus ) {
   1.528 +		format.dwFlags |= DSBCAPS_GLOBALFOCUS;
   1.529 +	}
   1.530 +	format.dwBufferBytes = numchunks*chunksize;
   1.531 +	if ( (format.dwBufferBytes < DSBSIZE_MIN) ||
   1.532 +	     (format.dwBufferBytes > DSBSIZE_MAX) ) {
   1.533 +		SDL_SetError("Sound buffer size must be between %d and %d",
   1.534 +				DSBSIZE_MIN/numchunks, DSBSIZE_MAX/numchunks);
   1.535 +		return(-1);
   1.536 +	}
   1.537 +	format.dwReserved = 0;
   1.538 +	format.lpwfxFormat = wavefmt;
   1.539 +	result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
   1.540 +	if ( result != DS_OK ) {
   1.541 +		SetDSerror("DirectSound CreateSoundBuffer", result);
   1.542 +		return(-1);
   1.543 +	}
   1.544 +	IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);
   1.545 +
   1.546 +	/* We're ready to go */
   1.547 +	return(numchunks);
   1.548 +}
   1.549 +
   1.550 +/* This function tries to set position notify events on the mixing buffer */
   1.551 +#ifdef USE_POSITION_NOTIFY
   1.552 +static int CreateAudioEvent(_THIS)
   1.553 +{
   1.554 +	LPDIRECTSOUNDNOTIFY notify;
   1.555 +	DSBPOSITIONNOTIFY *notify_positions;
   1.556 +	int i, retval;
   1.557 +	HRESULT result;
   1.558 +
   1.559 +	/* Default to fail on exit */
   1.560 +	retval = -1;
   1.561 +	notify = NULL;
   1.562 +
   1.563 +	/* Query for the interface */
   1.564 +	result = IDirectSoundBuffer_QueryInterface(mixbuf,
   1.565 +			&IID_IDirectSoundNotify, (void *)&notify);
   1.566 +	if ( result != DS_OK ) {
   1.567 +		goto done;
   1.568 +	}
   1.569 +
   1.570 +	/* Allocate the notify structures */
   1.571 +	notify_positions = (DSBPOSITIONNOTIFY *)malloc(NUM_BUFFERS*
   1.572 +					sizeof(*notify_positions));
   1.573 +	if ( notify_positions == NULL ) {
   1.574 +		goto done;
   1.575 +	}
   1.576 +
   1.577 +	/* Create the notify event */
   1.578 +	audio_event = CreateEvent(NULL, FALSE, FALSE, NULL);
   1.579 +	if ( audio_event == NULL ) {
   1.580 +		goto done;
   1.581 +	}
   1.582 +
   1.583 +	/* Set up the notify structures */
   1.584 +	for ( i=0; i<NUM_BUFFERS; ++i ) {
   1.585 +		notify_positions[i].dwOffset = i*mixlen;
   1.586 +		notify_positions[i].hEventNotify = audio_event;
   1.587 +	}
   1.588 +	result = IDirectSoundNotify_SetNotificationPositions(notify,
   1.589 +					NUM_BUFFERS, notify_positions);
   1.590 +	if ( result == DS_OK ) {
   1.591 +		retval = 0;
   1.592 +	}
   1.593 +done:
   1.594 +	if ( notify != NULL ) {
   1.595 +		IDirectSoundNotify_Release(notify);
   1.596 +	}
   1.597 +	return(retval);
   1.598 +}
   1.599 +#endif /* USE_POSITION_NOTIFY */
   1.600 +
   1.601 +static int DX5_OpenAudio(_THIS, SDL_AudioSpec *spec)
   1.602 +{
   1.603 +	HRESULT      result;
   1.604 +	WAVEFORMATEX waveformat;
   1.605 +
   1.606 +	/* Set basic WAVE format parameters */
   1.607 +	memset(&waveformat, 0, sizeof(waveformat));
   1.608 +	waveformat.wFormatTag = WAVE_FORMAT_PCM;
   1.609 +
   1.610 +	/* Determine the audio parameters from the AudioSpec */
   1.611 +	switch ( spec->format & 0xFF ) {
   1.612 +		case 8:
   1.613 +			/* Unsigned 8 bit audio data */
   1.614 +			spec->format = AUDIO_U8;
   1.615 +			silence = 0x80;
   1.616 +			waveformat.wBitsPerSample = 8;
   1.617 +			break;
   1.618 +		case 16:
   1.619 +			/* Signed 16 bit audio data */
   1.620 +			spec->format = AUDIO_S16;
   1.621 +			silence = 0x00;
   1.622 +			waveformat.wBitsPerSample = 16;
   1.623 +			break;
   1.624 +		default:
   1.625 +			SDL_SetError("Unsupported audio format");
   1.626 +			return(-1);
   1.627 +	}
   1.628 +	waveformat.nChannels = spec->channels;
   1.629 +	waveformat.nSamplesPerSec = spec->freq;
   1.630 +	waveformat.nBlockAlign =
   1.631 +		waveformat.nChannels * (waveformat.wBitsPerSample/8);
   1.632 +	waveformat.nAvgBytesPerSec = 
   1.633 +		waveformat.nSamplesPerSec * waveformat.nBlockAlign;
   1.634 +
   1.635 +	/* Update the fragment size as size in bytes */
   1.636 +	SDL_CalculateAudioSpec(spec);
   1.637 +
   1.638 +	/* Open the audio device */
   1.639 +	result = DSoundCreate(NULL, &sound, NULL);
   1.640 +	if ( result != DS_OK ) {
   1.641 +		SetDSerror("DirectSoundCreate", result);
   1.642 +		return(-1);
   1.643 +	}
   1.644 +
   1.645 +	/* Create the audio buffer to which we write */
   1.646 +	NUM_BUFFERS = -1;
   1.647 +	if ( mainwin ) {
   1.648 +		NUM_BUFFERS = CreatePrimary(sound, mainwin, &mixbuf,
   1.649 +						&waveformat, spec->size);
   1.650 +	}
   1.651 +	if ( NUM_BUFFERS < 0 ) {
   1.652 +		NUM_BUFFERS = CreateSecondary(sound, mainwin, &mixbuf,
   1.653 +						&waveformat, spec->size);
   1.654 +		if ( NUM_BUFFERS < 0 ) {
   1.655 +			return(-1);
   1.656 +		}
   1.657 +#ifdef DEBUG_SOUND
   1.658 +		fprintf(stderr, "Using secondary audio buffer\n");
   1.659 +#endif
   1.660 +	}
   1.661 +#ifdef DEBUG_SOUND
   1.662 +	else
   1.663 +		fprintf(stderr, "Using primary audio buffer\n");
   1.664 +#endif
   1.665 +
   1.666 +	/* The buffer will auto-start playing in DX5_WaitAudio() */
   1.667 +	playing = 0;
   1.668 +	mixlen = spec->size;
   1.669 +
   1.670 +#ifdef USE_POSITION_NOTIFY
   1.671 +	/* See if we can use DirectX 6 event notification */
   1.672 +	if ( CreateAudioEvent(this) == 0 ) {
   1.673 +		this->WaitAudio = DX6_WaitAudio_EventWait;
   1.674 +	} else {
   1.675 +		this->WaitAudio = DX5_WaitAudio_BusyWait;
   1.676 +	}
   1.677 +#endif
   1.678 +	return(0);
   1.679 +}
   1.680 +