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