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