2009-07-27 05:47:50 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2009 Jonathon Fowler <jf@jonof.id.au>
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
See the GNU General Public License for more details.
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2014-07-20 08:55:56 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DirectSound output driver for MultiVoc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2012-12-29 05:23:28 +00:00
|
|
|
#define DIRECTSOUND_VERSION 0x0800
|
2012-11-05 02:49:08 +00:00
|
|
|
#define CINTERFACE
|
2012-12-29 05:22:39 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h>
|
2014-02-10 10:59:42 +00:00
|
|
|
#include "dx/dsound.h"
|
2009-07-27 05:47:50 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "driver_directsound.h"
|
2010-06-22 21:50:01 +00:00
|
|
|
#include "multivoc.h"
|
2009-07-27 05:47:50 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
DSErr_Warning = -2,
|
|
|
|
DSErr_Error = -1,
|
|
|
|
DSErr_Ok = 0,
|
|
|
|
DSErr_Uninitialised,
|
|
|
|
DSErr_DirectSoundCreate,
|
|
|
|
DSErr_SetCooperativeLevel,
|
|
|
|
DSErr_CreateSoundBuffer,
|
|
|
|
DSErr_CreateSoundBufferSecondary,
|
|
|
|
DSErr_SetFormat,
|
|
|
|
DSErr_SetFormatSecondary,
|
|
|
|
DSErr_Notify,
|
|
|
|
DSErr_NotifyEvents,
|
|
|
|
DSErr_SetNotificationPositions,
|
|
|
|
DSErr_Play,
|
|
|
|
DSErr_PlaySecondary,
|
|
|
|
DSErr_CreateThread,
|
|
|
|
DSErr_CreateMutex
|
|
|
|
};
|
|
|
|
|
|
|
|
static int32_t ErrorCode = DSErr_Ok;
|
|
|
|
static int32_t Initialised = 0;
|
|
|
|
static int32_t Playing = 0;
|
|
|
|
|
2012-12-29 05:22:39 +00:00
|
|
|
static char *MixBuffer = NULL;
|
2009-07-27 05:47:50 +00:00
|
|
|
static int32_t MixBufferSize = 0;
|
|
|
|
static int32_t MixBufferCount = 0;
|
|
|
|
static int32_t MixBufferCurrent = 0;
|
|
|
|
static int32_t MixBufferUsed = 0;
|
2012-12-29 05:22:39 +00:00
|
|
|
static void ( *MixCallBack )( void ) = NULL;
|
2009-07-27 05:47:50 +00:00
|
|
|
|
2012-12-29 05:22:39 +00:00
|
|
|
static LPDIRECTSOUND lpds = NULL;
|
|
|
|
static LPDIRECTSOUNDBUFFER lpdsbprimary = NULL, lpdsbsec = NULL;
|
|
|
|
static LPDIRECTSOUNDNOTIFY lpdsnotify = NULL;
|
2009-07-27 05:47:50 +00:00
|
|
|
static DSBPOSITIONNOTIFY notifyPositions[3] = { { 0,0 }, { 0,0 }, { 0,0 } };
|
2012-12-29 05:22:39 +00:00
|
|
|
static HANDLE mixThread = NULL;
|
|
|
|
static HANDLE mutex = NULL;
|
2009-07-27 05:47:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void FillBufferPortion(char * ptr, int32_t remaining)
|
|
|
|
{
|
2010-06-22 21:50:01 +00:00
|
|
|
int32_t len = 0;
|
2009-07-27 05:47:50 +00:00
|
|
|
char *sptr;
|
|
|
|
|
2010-06-22 21:50:01 +00:00
|
|
|
while (remaining >= len) {
|
2009-07-27 05:47:50 +00:00
|
|
|
if (MixBufferUsed == MixBufferSize) {
|
|
|
|
MixCallBack();
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
MixBufferUsed = 0;
|
|
|
|
MixBufferCurrent++;
|
|
|
|
if (MixBufferCurrent >= MixBufferCount) {
|
|
|
|
MixBufferCurrent -= MixBufferCount;
|
|
|
|
}
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2010-06-22 21:50:01 +00:00
|
|
|
while (remaining >= len && MixBufferUsed < MixBufferSize) {
|
2009-07-27 05:47:50 +00:00
|
|
|
sptr = MixBuffer + (MixBufferCurrent * MixBufferSize) + MixBufferUsed;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
len = MixBufferSize - MixBufferUsed;
|
|
|
|
if (remaining < len) {
|
|
|
|
len = remaining;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
memcpy(ptr, sptr, len);
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
ptr += len;
|
|
|
|
MixBufferUsed += len;
|
|
|
|
remaining -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FillBuffer(int32_t bufnum)
|
|
|
|
{
|
|
|
|
HRESULT err;
|
|
|
|
LPVOID ptr, ptr2;
|
|
|
|
DWORD remaining, remaining2;
|
|
|
|
int32_t retries = 1;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2010-06-22 21:50:01 +00:00
|
|
|
//initprintf( "DirectSound FillBuffer: filling %d\n", bufnum);
|
2009-07-27 05:47:50 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
err = IDirectSoundBuffer_Lock(lpdsbsec,
|
|
|
|
notifyPositions[bufnum].dwOffset,
|
|
|
|
notifyPositions[1].dwOffset,
|
|
|
|
&ptr, &remaining,
|
|
|
|
&ptr2, &remaining2,
|
|
|
|
0);
|
|
|
|
if (FAILED(err)) {
|
|
|
|
if (err == DSERR_BUFFERLOST) {
|
|
|
|
err = IDirectSoundBuffer_Restore(lpdsbsec);
|
|
|
|
if (FAILED(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retries-- > 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-06-22 21:50:01 +00:00
|
|
|
if (MV_Printf)
|
|
|
|
MV_Printf("DirectSound FillBuffer: err %x\n", (uint32_t) err);
|
2009-07-27 05:47:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} while (1);
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
if (ptr) {
|
|
|
|
FillBufferPortion((char *) ptr, remaining);
|
|
|
|
}
|
|
|
|
if (ptr2) {
|
|
|
|
FillBufferPortion((char *) ptr2, remaining2);
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
IDirectSoundBuffer_Unlock(lpdsbsec, ptr, remaining, ptr2, remaining2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI fillDataThread(LPVOID lpParameter)
|
|
|
|
{
|
|
|
|
DWORD waitret, waitret2;
|
2016-06-05 04:46:28 +00:00
|
|
|
HANDLE handles[] = { handles[0] = notifyPositions[0].hEventNotify,
|
2010-06-22 21:50:01 +00:00
|
|
|
handles[1] = notifyPositions[1].hEventNotify,
|
|
|
|
handles[2] = notifyPositions[2].hEventNotify };
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2010-06-22 21:50:01 +00:00
|
|
|
UNREFERENCED_PARAMETER(lpParameter);
|
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
do {
|
2016-06-05 04:46:28 +00:00
|
|
|
waitret = WaitForMultipleObjects(3, handles, FALSE, INFINITE);
|
2009-07-27 05:47:50 +00:00
|
|
|
switch (waitret) {
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
case WAIT_OBJECT_0+1:
|
|
|
|
waitret2 = WaitForSingleObject(mutex, INFINITE);
|
|
|
|
if (waitret2 == WAIT_OBJECT_0) {
|
|
|
|
FillBuffer(WAIT_OBJECT_0 + 1 - waitret);
|
|
|
|
ReleaseMutex(mutex);
|
|
|
|
} else {
|
2010-06-22 21:50:01 +00:00
|
|
|
if (MV_Printf)
|
|
|
|
MV_Printf( "DirectSound fillDataThread: wfso err %d\n", (int32_t) waitret2);
|
2009-07-27 05:47:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0+2:
|
2010-06-22 21:50:01 +00:00
|
|
|
// initprintf( "DirectSound fillDataThread: exiting\n");
|
2009-07-27 05:47:50 +00:00
|
|
|
ExitThread(0);
|
|
|
|
break;
|
|
|
|
default:
|
2010-06-22 21:50:01 +00:00
|
|
|
if (MV_Printf)
|
|
|
|
MV_Printf( "DirectSound fillDataThread: wfmo err %d\n", (int32_t) waitret);
|
2009-07-27 05:47:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t DirectSoundDrv_GetError(void)
|
|
|
|
{
|
|
|
|
return ErrorCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *DirectSoundDrv_ErrorString( int32_t ErrorNumber )
|
|
|
|
{
|
|
|
|
const char *ErrorString;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
switch( ErrorNumber )
|
|
|
|
{
|
|
|
|
case DSErr_Warning :
|
|
|
|
case DSErr_Error :
|
|
|
|
ErrorString = DirectSoundDrv_ErrorString( ErrorCode );
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_Ok :
|
|
|
|
ErrorString = "DirectSound ok.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_Uninitialised:
|
|
|
|
ErrorString = "DirectSound uninitialised.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_DirectSoundCreate:
|
|
|
|
ErrorString = "DirectSound error: DirectSoundCreate failed.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_SetCooperativeLevel:
|
|
|
|
ErrorString = "DirectSound error: SetCooperativeLevel failed.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_CreateSoundBuffer:
|
|
|
|
ErrorString = "DirectSound error: primary CreateSoundBuffer failed.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_CreateSoundBufferSecondary:
|
|
|
|
ErrorString = "DirectSound error: secondary CreateSoundBuffer failed.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_SetFormat:
|
|
|
|
ErrorString = "DirectSound error: primary buffer SetFormat failed.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_SetFormatSecondary:
|
|
|
|
ErrorString = "DirectSound error: secondary buffer SetFormat failed.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_Notify:
|
|
|
|
ErrorString = "DirectSound error: failed querying secondary buffer for notify interface.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_NotifyEvents:
|
|
|
|
ErrorString = "DirectSound error: failed creating notify events.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_SetNotificationPositions:
|
|
|
|
ErrorString = "DirectSound error: failed setting notification positions.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_Play:
|
|
|
|
ErrorString = "DirectSound error: primary buffer Play failed.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_PlaySecondary:
|
|
|
|
ErrorString = "DirectSound error: secondary buffer Play failed.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_CreateThread:
|
|
|
|
ErrorString = "DirectSound error: failed creating mix thread.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
case DSErr_CreateMutex:
|
|
|
|
ErrorString = "DirectSound error: failed creating mix mutex.";
|
|
|
|
break;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
default:
|
|
|
|
ErrorString = "Unknown DirectSound error code.";
|
|
|
|
break;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
return ErrorString;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void TeardownDSound(HRESULT err)
|
|
|
|
{
|
|
|
|
if (FAILED(err)) {
|
2010-06-22 21:50:01 +00:00
|
|
|
if (MV_Printf)
|
|
|
|
MV_Printf( "Dying error: %x\n", (uint32_t) err);
|
2009-07-27 05:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lpdsnotify) IDirectSoundNotify_Release(lpdsnotify);
|
|
|
|
if (notifyPositions[0].hEventNotify) CloseHandle(notifyPositions[0].hEventNotify);
|
|
|
|
if (notifyPositions[1].hEventNotify) CloseHandle(notifyPositions[1].hEventNotify);
|
|
|
|
if (notifyPositions[2].hEventNotify) CloseHandle(notifyPositions[2].hEventNotify);
|
|
|
|
if (mutex) CloseHandle(mutex);
|
|
|
|
if (lpdsbsec) IDirectSoundBuffer_Release(lpdsbsec);
|
|
|
|
if (lpdsbprimary) IDirectSoundBuffer_Release(lpdsbprimary);
|
|
|
|
if (lpds) IDirectSound_Release(lpds);
|
|
|
|
notifyPositions[0].hEventNotify =
|
|
|
|
notifyPositions[1].hEventNotify =
|
|
|
|
notifyPositions[2].hEventNotify = 0;
|
2012-12-29 05:22:39 +00:00
|
|
|
mutex = NULL;
|
|
|
|
lpdsnotify = NULL;
|
|
|
|
lpdsbsec = NULL;
|
|
|
|
lpdsbprimary = NULL;
|
|
|
|
lpds = NULL;
|
2009-07-27 05:47:50 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 03:34:09 +00:00
|
|
|
int32_t DirectSoundDrv_PCM_Init(int32_t *mixrate, int32_t *numchannels, void * initdata)
|
2009-07-27 05:47:50 +00:00
|
|
|
{
|
|
|
|
HRESULT err;
|
|
|
|
DSBUFFERDESC bufdesc;
|
|
|
|
WAVEFORMATEX wfex;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
if (Initialised) {
|
2009-07-31 11:45:29 +00:00
|
|
|
DirectSoundDrv_PCM_Shutdown();
|
2009-07-27 05:47:50 +00:00
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = DirectSoundCreate(0, &lpds, 0);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
ErrorCode = DSErr_DirectSoundCreate;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = IDirectSound_SetCooperativeLevel(lpds, (HWND) initdata, DSSCL_PRIORITY);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
TeardownDSound(err);
|
|
|
|
ErrorCode = DSErr_SetCooperativeLevel;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
memset(&bufdesc, 0, sizeof(DSBUFFERDESC));
|
|
|
|
bufdesc.dwSize = sizeof(DSBUFFERDESC);
|
2016-06-05 04:46:28 +00:00
|
|
|
bufdesc.dwFlags = DSBCAPS_LOCSOFTWARE |
|
|
|
|
DSBCAPS_PRIMARYBUFFER |
|
2009-12-05 09:22:43 +00:00
|
|
|
DSBCAPS_GETCURRENTPOSITION2 |
|
|
|
|
DSBCAPS_STICKYFOCUS ;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = IDirectSound_CreateSoundBuffer(lpds, &bufdesc, &lpdsbprimary, 0);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
TeardownDSound(err);
|
|
|
|
ErrorCode = DSErr_CreateSoundBuffer;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
memset(&wfex, 0, sizeof(WAVEFORMATEX));
|
|
|
|
wfex.wFormatTag = WAVE_FORMAT_PCM;
|
2009-07-31 11:45:29 +00:00
|
|
|
wfex.nChannels = *numchannels;
|
|
|
|
wfex.nSamplesPerSec = *mixrate;
|
2015-07-08 03:34:09 +00:00
|
|
|
wfex.wBitsPerSample = 16;
|
2009-07-27 05:47:50 +00:00
|
|
|
wfex.nBlockAlign = wfex.nChannels * wfex.wBitsPerSample / 8;
|
|
|
|
wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = IDirectSoundBuffer_SetFormat(lpdsbprimary, &wfex);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
TeardownDSound(err);
|
|
|
|
ErrorCode = DSErr_SetFormat;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-10-01 05:05:19 +00:00
|
|
|
bufdesc.dwFlags = DSBCAPS_LOCSOFTWARE |
|
|
|
|
DSBCAPS_CTRLPOSITIONNOTIFY |
|
2009-12-05 09:22:43 +00:00
|
|
|
DSBCAPS_GETCURRENTPOSITION2 |
|
|
|
|
DSBCAPS_STICKYFOCUS ;
|
|
|
|
bufdesc.dwBufferBytes = wfex.nBlockAlign * 2560 * 2;
|
2009-07-27 05:47:50 +00:00
|
|
|
bufdesc.lpwfxFormat = &wfex;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = IDirectSound_CreateSoundBuffer(lpds, &bufdesc, &lpdsbsec, 0);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
TeardownDSound(err);
|
|
|
|
ErrorCode = DSErr_SetFormatSecondary;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = IDirectSoundBuffer_QueryInterface(lpdsbsec, &IID_IDirectSoundNotify,
|
|
|
|
(LPVOID *) &lpdsnotify);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
TeardownDSound(err);
|
|
|
|
ErrorCode = DSErr_Notify;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
notifyPositions[0].dwOffset = 0;
|
|
|
|
notifyPositions[0].hEventNotify = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
notifyPositions[1].dwOffset = bufdesc.dwBufferBytes / 2;
|
|
|
|
notifyPositions[1].hEventNotify = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
notifyPositions[2].dwOffset = DSBPN_OFFSETSTOP;
|
|
|
|
notifyPositions[2].hEventNotify = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!notifyPositions[0].hEventNotify ||
|
|
|
|
!notifyPositions[1].hEventNotify ||
|
|
|
|
!notifyPositions[2].hEventNotify) {
|
|
|
|
TeardownDSound(DS_OK);
|
|
|
|
ErrorCode = DSErr_NotifyEvents;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = IDirectSoundNotify_SetNotificationPositions(lpdsnotify, 3, notifyPositions);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
TeardownDSound(err);
|
|
|
|
ErrorCode = DSErr_SetNotificationPositions;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = IDirectSoundBuffer_Play(lpdsbprimary, 0, 0, DSBPLAY_LOOPING);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
TeardownDSound(err);
|
|
|
|
ErrorCode = DSErr_Play;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
mutex = CreateMutex(0, FALSE, 0);
|
|
|
|
if (!mutex) {
|
|
|
|
TeardownDSound(DS_OK);
|
|
|
|
ErrorCode = DSErr_CreateMutex;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
Initialised = 1;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2010-06-22 21:50:01 +00:00
|
|
|
// initprintf("DirectSound Init: yay\n");
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
return DSErr_Ok;
|
|
|
|
}
|
|
|
|
|
2009-07-31 11:45:29 +00:00
|
|
|
void DirectSoundDrv_PCM_Shutdown(void)
|
2009-07-27 05:47:50 +00:00
|
|
|
{
|
|
|
|
if (!Initialised) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-31 11:45:29 +00:00
|
|
|
DirectSoundDrv_PCM_StopPlayback();
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
TeardownDSound(DS_OK);
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
Initialised = 0;
|
|
|
|
}
|
|
|
|
|
2009-07-31 11:45:29 +00:00
|
|
|
int32_t DirectSoundDrv_PCM_BeginPlayback(char *BufferStart, int32_t BufferSize,
|
2009-07-27 05:47:50 +00:00
|
|
|
int32_t NumDivisions, void ( *CallBackFunc )( void ) )
|
|
|
|
{
|
|
|
|
HRESULT err;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
if (!Initialised) {
|
|
|
|
ErrorCode = DSErr_Uninitialised;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-31 11:45:29 +00:00
|
|
|
DirectSoundDrv_PCM_StopPlayback();
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
MixBuffer = BufferStart;
|
|
|
|
MixBufferSize = BufferSize;
|
|
|
|
MixBufferCount = NumDivisions;
|
|
|
|
MixBufferCurrent = 0;
|
|
|
|
MixBufferUsed = 0;
|
|
|
|
MixCallBack = CallBackFunc;
|
|
|
|
|
|
|
|
// prime the buffer
|
|
|
|
FillBuffer(0);
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
mixThread = CreateThread(NULL, 0, fillDataThread, 0, 0, 0);
|
|
|
|
if (!mixThread) {
|
|
|
|
ErrorCode = DSErr_CreateThread;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
|
|
|
|
2010-06-22 21:50:01 +00:00
|
|
|
SetThreadPriority(mixThread, THREAD_PRIORITY_ABOVE_NORMAL);
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = IDirectSoundBuffer_Play(lpdsbsec, 0, 0, DSBPLAY_LOOPING);
|
|
|
|
if (FAILED( err )) {
|
|
|
|
ErrorCode = DSErr_PlaySecondary;
|
|
|
|
return DSErr_Error;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
Playing = 1;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
return DSErr_Ok;
|
|
|
|
}
|
|
|
|
|
2009-07-31 11:45:29 +00:00
|
|
|
void DirectSoundDrv_PCM_StopPlayback(void)
|
2009-07-27 05:47:50 +00:00
|
|
|
{
|
|
|
|
if (!Playing) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
IDirectSoundBuffer_Stop(lpdsbsec);
|
|
|
|
IDirectSoundBuffer_SetCurrentPosition(lpdsbsec, 0);
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
Playing = 0;
|
|
|
|
}
|
|
|
|
|
2009-07-31 11:45:29 +00:00
|
|
|
void DirectSoundDrv_PCM_Lock(void)
|
2009-07-27 05:47:50 +00:00
|
|
|
{
|
|
|
|
DWORD err;
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2009-07-27 05:47:50 +00:00
|
|
|
err = WaitForSingleObject(mutex, INFINITE);
|
|
|
|
if (err != WAIT_OBJECT_0) {
|
2010-06-22 21:50:01 +00:00
|
|
|
if (MV_Printf)
|
|
|
|
MV_Printf( "DirectSound lock: wfso %d\n", (int32_t) err);
|
2009-07-27 05:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-31 11:45:29 +00:00
|
|
|
void DirectSoundDrv_PCM_Unlock(void)
|
2009-07-27 05:47:50 +00:00
|
|
|
{
|
|
|
|
ReleaseMutex(mutex);
|
|
|
|
}
|