19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dx5audio.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Define this if you want to use DirectX 6 DirectSoundNotify interface */
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//#define USE_POSITION_NOTIFY
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* DirectX function pointers for audio */
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallHRESULT (WINAPI *DSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DX5_OpenAudio(_THIS, SDL_AudioSpec *spec);
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_ThreadInit(_THIS);
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_WaitAudio_BusyWait(_THIS);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_POSITION_NOTIFY
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX6_WaitAudio_EventWait(_THIS);
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_PlayAudio(_THIS);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DX5_GetAudioBuf(_THIS);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_WaitDone(_THIS);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_CloseAudio(_THIS);
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HINSTANCE DSoundDLL;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int dsound_ok;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Version check DSOUND.DLL (Is DirectX okay?) */
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dsound_ok = 0;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL"));
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( DSoundDLL != NULL ) {
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* We just use basic DirectSound, we're okay */
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Yay! */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Unfortunately, the sound drivers on NT have
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   higher latencies than the audio buffers used
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   by many SDL applications, so there are gaps
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   in the audio - it sounds terrible.  Punt for now.
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		OSVERSIONINFO ver;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GetVersionEx(&ver);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (ver.dwPlatformId) {
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case VER_PLATFORM_WIN32_NT:
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( ver.dwMajorVersion > 4 ) {
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Win2K */
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dsound_ok = 1;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* WinNT */
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dsound_ok = 0;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Win95 or Win98 */
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dsound_ok = 1;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Now check for DirectX 5 or better - otherwise
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * we will fail later in DX5_OpenAudio without a chance
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * to fall back to the DIB driver. */
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (dsound_ok) {
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* DirectSoundCaptureCreate was added in DX5 */
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (!GetProcAddress(DSoundDLL, TEXT("DirectSoundCaptureCreate")))
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dsound_ok = 0;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Clean up.. */
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FreeLibrary(DSoundDLL);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(dsound_ok);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions for loading the DirectX functions dynamically */
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic HINSTANCE DSoundDLL = NULL;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_Unload(void)
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( DSoundDLL != NULL ) {
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FreeLibrary(DSoundDLL);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DSoundCreate = NULL;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DSoundDLL = NULL;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DX5_Load(void)
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int status;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DX5_Unload();
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL"));
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( DSoundDLL != NULL ) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DSoundCreate = (void *)GetProcAddress(DSoundDLL,
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					TEXT("DirectSoundCreate"));
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( DSoundDLL && DSoundCreate ) {
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = 0;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DX5_Unload();
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = -1;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return status;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DX5_Unload();
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Load DirectX */
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( DX5_Load() < 0 ) {
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = DX5_OpenAudio;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->ThreadInit = DX5_ThreadInit;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = DX5_WaitAudio_BusyWait;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = DX5_PlayAudio;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = DX5_GetAudioBuf;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitDone = DX5_WaitDone;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = DX5_CloseAudio;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = Audio_DeleteDevice;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap DSOUND_bootstrap = {
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"dsound", "Win95/98/2000 DirectSound",
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SetDSerror(const char *function, int code)
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static const char *error;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static char  errbuf[1024];
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	errbuf[0] = 0;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (code) {
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case E_NOINTERFACE:
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error =
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		"Unsupported interface\n-- Is DirectX 5.0 or later installed?";
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_ALLOCATED:
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Audio device in use";
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_BADFORMAT:
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Unsupported audio format";
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_BUFFERLOST:
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Mixing buffer was lost";
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_CONTROLUNAVAIL:
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Control requested is not available";
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_INVALIDCALL:
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Invalid call for the current state";
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_INVALIDPARAM:
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Invalid parameter";
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_NODRIVER:
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "No audio device found";
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_OUTOFMEMORY:
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Out of memory";
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_PRIOLEVELNEEDED:
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Caller doesn't have priority";
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case DSERR_UNSUPPORTED:
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			error = "Function not supported";
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_snprintf(errbuf, SDL_arraysize(errbuf),
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			         "%s: Unknown DirectSound error: 0x%x",
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								function, code);
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! errbuf[0] ) {
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error);
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("%s", errbuf);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* DirectSound needs to be associated with a window */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic HWND mainwin = NULL;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* */
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DX5_SoundFocus(HWND hwnd)
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mainwin = hwnd;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_ThreadInit(_THIS)
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_WaitAudio_BusyWait(_THIS)
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD status;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD cursor, junk;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HRESULT result;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Semi-busy wait, since we have no way of getting play notification
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   on a primary mixing buffer located in hardware (DirectX 5.0)
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &junk, &cursor);
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( result == DSERR_BUFFERLOST ) {
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			IDirectSoundBuffer_Restore(mixbuf);
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound GetCurrentPosition", result);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( (cursor/mixlen) == lastchunk ) {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* FIXME: find out how much time is left and sleep that long */
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Delay(1);
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Try to restore a lost sound buffer */
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_GetStatus(mixbuf, &status);
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (status&DSBSTATUS_BUFFERLOST) ) {
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			IDirectSoundBuffer_Restore(mixbuf);
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			IDirectSoundBuffer_GetStatus(mixbuf, &status);
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (status&DSBSTATUS_BUFFERLOST) ) {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! (status&DSBSTATUS_PLAYING) ) {
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING);
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( result == DS_OK ) {
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				continue;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetDSerror("DirectSound Play", result);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Find out where we are playing */
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								&junk, &cursor);
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( result != DS_OK ) {
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetDSerror("DirectSound GetCurrentPosition", result);
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_POSITION_NOTIFY
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX6_WaitAudio_EventWait(_THIS)
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD status;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HRESULT result;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try to restore a lost sound buffer */
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IDirectSoundBuffer_GetStatus(mixbuf, &status);
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (status&DSBSTATUS_BUFFERLOST) ) {
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_Restore(mixbuf);
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_GetStatus(mixbuf, &status);
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (status&DSBSTATUS_BUFFERLOST) ) {
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! (status&DSBSTATUS_PLAYING) ) {
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = IDirectSoundBuffer_Play(mixbuf, 0, 0, DSBPLAY_LOOPING);
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( result != DS_OK ) {
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SetDSerror("DirectSound Play", result);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WaitForSingleObject(audio_event, INFINITE);
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_POSITION_NOTIFY */
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_PlayAudio(_THIS)
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Unlock the buffer, allowing it to play */
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( locked_buf ) {
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_Unlock(mixbuf, locked_buf, mixlen, NULL, 0);
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DX5_GetAudioBuf(_THIS)
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD   cursor, junk;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HRESULT result;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD   rawlen;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Figure out which blocks to fill next */
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	locked_buf = NULL;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSoundBuffer_GetCurrentPosition(mixbuf, &junk, &cursor);
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result == DSERR_BUFFERLOST ) {
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_Restore(mixbuf);
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = IDirectSoundBuffer_GetCurrentPosition(mixbuf,
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								&junk, &cursor);
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound GetCurrentPosition", result);
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cursor /= mixlen;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Detect audio dropouts */
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ DWORD spot = cursor;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  if ( spot < lastchunk ) {
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    spot += NUM_BUFFERS;
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  if ( spot > lastchunk+1 ) {
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    fprintf(stderr, "Audio dropout, missed %d fragments\n",
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	            (spot - (lastchunk+1)));
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	lastchunk = cursor;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cursor = (cursor+1)%NUM_BUFFERS;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cursor *= mixlen;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Lock the audio buffer */
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen,
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(LPVOID *)&locked_buf, &rawlen, NULL, &junk, 0);
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result == DSERR_BUFFERLOST ) {
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_Restore(mixbuf);
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = IDirectSoundBuffer_Lock(mixbuf, cursor, mixlen,
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(LPVOID *)&locked_buf, &rawlen, NULL, &junk, 0);
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound Lock", result);
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(locked_buf);
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_WaitDone(_THIS)
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *stream;
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wait for the playing chunk to finish */
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	stream = this->GetAudioBuf(this);
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( stream != NULL ) {
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(stream, silence, mixlen);
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->PlayAudio(this);
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio(this);
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Stop the looping sound buffer */
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IDirectSoundBuffer_Stop(mixbuf);
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DX5_CloseAudio(_THIS)
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( sound != NULL ) {
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mixbuf != NULL ) {
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Clean up the audio buffer */
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			IDirectSoundBuffer_Release(mixbuf);
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mixbuf = NULL;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( audio_event != NULL ) {
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			CloseHandle(audio_event);
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			audio_event = NULL;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSound_Release(sound);
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sound = NULL;
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_PRIMARY_BUFFER
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function tries to create a primary audio buffer, and returns the
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   number of audio chunks available in the created buffer.
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CreatePrimary(LPDIRECTSOUND sndObj, HWND focus,
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize)
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HRESULT result;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DSBUFFERDESC format;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DSBCAPS caps;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int numchunks;
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try to set primary mixing privileges */
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSound_SetCooperativeLevel(sndObj, focus,
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							DSSCL_WRITEPRIMARY);
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound SetCooperativeLevel", result);
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try to create the primary buffer */
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&format, 0, sizeof(format));
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwSize = sizeof(format);
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwFlags=(DSBCAPS_PRIMARYBUFFER|DSBCAPS_GETCURRENTPOSITION2);
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwFlags |= DSBCAPS_STICKYFOCUS;
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_POSITION_NOTIFY
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound CreateSoundBuffer", result);
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check the size of the fragment buffer */
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&caps, 0, sizeof(caps));
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	caps.dwSize = sizeof(caps);
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSoundBuffer_GetCaps(*sndbuf, &caps);
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound GetCaps", result);
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_Release(*sndbuf);
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (chunksize > caps.dwBufferBytes) ||
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				((caps.dwBufferBytes%chunksize) != 0) ) {
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* The primary buffer size is not a multiple of 'chunksize'
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   -- this hopefully doesn't happen when 'chunksize' is a
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      power of 2.
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_Release(*sndbuf);
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError(
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall"Primary buffer size is: %d, cannot break it into chunks of %d bytes\n",
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					caps.dwBufferBytes, chunksize);
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	numchunks = (caps.dwBufferBytes/chunksize);
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the primary audio format */
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound SetFormat", result);
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_Release(*sndbuf);
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(numchunks);
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_PRIMARY_BUFFER */
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function tries to create a secondary audio buffer, and returns the
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   number of audio chunks available in the created buffer.
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CreateSecondary(LPDIRECTSOUND sndObj, HWND focus,
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LPDIRECTSOUNDBUFFER *sndbuf, WAVEFORMATEX *wavefmt, Uint32 chunksize)
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const int numchunks = 8;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HRESULT result;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DSBUFFERDESC format;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LPVOID pvAudioPtr1, pvAudioPtr2;
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD  dwAudioBytes1, dwAudioBytes2;
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try to set primary mixing privileges */
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( focus ) {
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = IDirectSound_SetCooperativeLevel(sndObj,
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					focus, DSSCL_PRIORITY);
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = IDirectSound_SetCooperativeLevel(sndObj,
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GetDesktopWindow(), DSSCL_NORMAL);
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound SetCooperativeLevel", result);
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try to create the secondary buffer */
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&format, 0, sizeof(format));
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwSize = sizeof(format);
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_POSITION_NOTIFY
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! focus ) {
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.dwFlags |= DSBCAPS_GLOBALFOCUS;
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.dwFlags |= DSBCAPS_STICKYFOCUS;
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwBufferBytes = numchunks*chunksize;
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (format.dwBufferBytes < DSBSIZE_MIN) ||
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (format.dwBufferBytes > DSBSIZE_MAX) ) {
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Sound buffer size must be between %d and %d",
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				DSBSIZE_MIN/numchunks, DSBSIZE_MAX/numchunks);
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.dwReserved = 0;
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format.lpwfxFormat = wavefmt;
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSound CreateSoundBuffer", result);
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IDirectSoundBuffer_SetFormat(*sndbuf, wavefmt);
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Silence the initial audio buffer */
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSoundBuffer_Lock(*sndbuf, 0, format.dwBufferBytes,
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                 (LPVOID *)&pvAudioPtr1, &dwAudioBytes1,
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                 (LPVOID *)&pvAudioPtr2, &dwAudioBytes2,
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                 DSBLOCK_ENTIREBUFFER);
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result == DS_OK ) {
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( wavefmt->wBitsPerSample == 8 ) {
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memset(pvAudioPtr1, 0x80, dwAudioBytes1);
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memset(pvAudioPtr1, 0x00, dwAudioBytes1);
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundBuffer_Unlock(*sndbuf,
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                          (LPVOID)pvAudioPtr1, dwAudioBytes1,
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                          (LPVOID)pvAudioPtr2, dwAudioBytes2);
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're ready to go */
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(numchunks);
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function tries to set position notify events on the mixing buffer */
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_POSITION_NOTIFY
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CreateAudioEvent(_THIS)
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LPDIRECTSOUNDNOTIFY notify;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DSBPOSITIONNOTIFY *notify_positions;
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, retval;
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HRESULT result;
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Default to fail on exit */
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = -1;
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	notify = NULL;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Query for the interface */
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSoundBuffer_QueryInterface(mixbuf,
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			&IID_IDirectSoundNotify, (void *)&notify);
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the notify structures */
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	notify_positions = (DSBPOSITIONNOTIFY *)SDL_malloc(NUM_BUFFERS*
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					sizeof(*notify_positions));
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( notify_positions == NULL ) {
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the notify event */
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_event = CreateEvent(NULL, FALSE, FALSE, NULL);
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( audio_event == NULL ) {
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		goto done;
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the notify structures */
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<NUM_BUFFERS; ++i ) {
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		notify_positions[i].dwOffset = i*mixlen;
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		notify_positions[i].hEventNotify = audio_event;
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IDirectSoundNotify_SetNotificationPositions(notify,
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					NUM_BUFFERS, notify_positions);
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result == DS_OK ) {
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = 0;
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldone:
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( notify != NULL ) {
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		IDirectSoundNotify_Release(notify);
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_POSITION_NOTIFY */
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DX5_OpenAudio(_THIS, SDL_AudioSpec *spec)
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HRESULT      result;
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WAVEFORMATEX waveformat;
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set basic WAVE format parameters */
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&waveformat, 0, sizeof(waveformat));
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.wFormatTag = WAVE_FORMAT_PCM;
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the audio parameters from the AudioSpec */
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch ( spec->format & 0xFF ) {
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 8:
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Unsigned 8 bit audio data */
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			spec->format = AUDIO_U8;
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			silence = 0x80;
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			waveformat.wBitsPerSample = 8;
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 16:
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Signed 16 bit audio data */
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			spec->format = AUDIO_S16;
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			silence = 0x00;
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			waveformat.wBitsPerSample = 16;
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unsupported audio format");
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.nChannels = spec->channels;
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.nSamplesPerSec = spec->freq;
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.nBlockAlign =
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		waveformat.nChannels * (waveformat.wBitsPerSample/8);
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	waveformat.nAvgBytesPerSec =
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		waveformat.nSamplesPerSec * waveformat.nBlockAlign;
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update the fragment size as size in bytes */
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Open the audio device */
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = DSoundCreate(NULL, &sound, NULL);
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( result != DS_OK ) {
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetDSerror("DirectSoundCreate", result);
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the audio buffer to which we write */
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NUM_BUFFERS = -1;
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_PRIMARY_BUFFER
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mainwin ) {
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		NUM_BUFFERS = CreatePrimary(sound, mainwin, &mixbuf,
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						&waveformat, spec->size);
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_PRIMARY_BUFFER */
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( NUM_BUFFERS < 0 ) {
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		NUM_BUFFERS = CreateSecondary(sound, mainwin, &mixbuf,
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						&waveformat, spec->size);
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( NUM_BUFFERS < 0 ) {
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Using secondary audio buffer\n");
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_SOUND
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Using primary audio buffer\n");
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The buffer will auto-start playing in DX5_WaitAudio() */
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	lastchunk = 0;
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixlen = spec->size;
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_POSITION_NOTIFY
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* See if we can use DirectX 6 event notification */
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( CreateAudioEvent(this) == 0 ) {
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->WaitAudio = DX6_WaitAudio_EventWait;
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->WaitAudio = DX5_WaitAudio_BusyWait;
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
706