src/audio/windx5/SDL_dx5audio.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 23 Jun 2006 08:15:47 +0000
changeset 1877 9b02a5b97f79
parent 1402 d910939febfa
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Fixed bug #69

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