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