diff --git a/include/winquake.h b/include/winquake.h index 77eb95a..2166590 100644 --- a/include/winquake.h +++ b/include/winquake.h @@ -103,6 +103,10 @@ void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify); void S_BlockSound (void); void S_UnblockSound (void); +DWORD *DSOUND_LockBuffer(qboolean lockit); +void DSOUND_ClearBuffer(int clear); +void DSOUND_Restore(void); + void VID_SetDefaultMode (void); int (PASCAL FAR *pWSAStartup)(WORD wVersionRequired, LPWSADATA lpWSAData); diff --git a/source/snd_dma.c b/source/snd_dma.c index 846ac24..63caf5b 100644 --- a/source/snd_dma.c +++ b/source/snd_dma.c @@ -38,11 +38,6 @@ #include "sys.h" #include "sound.h" -// fixme: Damn crappy complier doesn't allow me to UNDEF _win32 on command line! -#ifdef WIN32SDL -#undef _WIN32 -#endif - #ifdef _WIN32 #include "winquake.h" #include "in_win.h" @@ -620,35 +615,8 @@ S_ClearBuffer (void) clear = 0; #ifdef _WIN32 - if (pDSBuf) { - DWORD dwSize; - DWORD *pData; - int reps; - HRESULT hresult; - - reps = 0; - - while ((hresult = pDSBuf->lpVtbl->Lock (pDSBuf, 0, gSndBufSize, - (LPVOID *) & pData, &dwSize, - NULL, NULL, 0)) != DS_OK) { - if (hresult != DSERR_BUFFERLOST) { - Con_Printf ("S_ClearBuffer: DS::Lock Sound Buffer Failed\n"); - S_Shutdown (); - return; - } - - if (++reps > 10000) { - Con_Printf ("S_ClearBuffer: DS: couldn't restore buffer\n"); - S_Shutdown (); - return; - } - } - - memset (pData, clear, shm->samples * shm->samplebits / 8); - - pDSBuf->lpVtbl->Unlock (pDSBuf, pData, dwSize, NULL, 0); - - } else + if (pDSBuf) DSOUND_ClearBuffer(clear); + else #endif { memset (shm->buffer, clear, shm->samples * shm->samplebits / 8); @@ -909,21 +877,7 @@ S_Update_ (void) endtime = soundtime + samps; #ifdef _WIN32 -// if the buffer was lost or stopped, restore it and/or restart it - { - DWORD dwStatus; - - if (pDSBuf) { - if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK) - Con_Printf ("Couldn't get sound buffer status\n"); - - if (dwStatus & DSBSTATUS_BUFFERLOST) - pDSBuf->lpVtbl->Restore (pDSBuf); - - if (!(dwStatus & DSBSTATUS_PLAYING)) - pDSBuf->lpVtbl->Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); - } - } + if(pDSBuf) DSOUND_Restore(); #endif S_PaintChannels (endtime); diff --git a/source/snd_mix.c b/source/snd_mix.c index e7090ea..9f9d19a 100644 --- a/source/snd_mix.c +++ b/source/snd_mix.c @@ -36,11 +36,6 @@ #include "console.h" #include "sound.h" -// fixme: Damn crappy complier doesn't allow me to UNDEF _win32 on command line! -#ifdef WIN32SDL -#undef _WIN32 -#endif - #ifdef _WIN32 #include "winquake.h" #else @@ -90,43 +85,15 @@ S_TransferStereo16 (int endtime) int lpaintedtime; DWORD *pbuf; -#ifdef _WIN32 - int reps; - DWORD dwSize, dwSize2; - DWORD *pbuf2; - HRESULT hresult; -#endif - snd_vol = volume->value * 256; snd_p = (int *) paintbuffer; lpaintedtime = paintedtime; #ifdef _WIN32 - if (pDSBuf) { - reps = 0; - - while ((hresult = pDSBuf->lpVtbl->Lock (pDSBuf, 0, gSndBufSize, - (LPVOID *) & pbuf, &dwSize, - (LPVOID *) & pbuf2, &dwSize2, - 0)) != DS_OK) { - if (hresult != DSERR_BUFFERLOST) { - Con_Printf - ("S_TransferStereo16: DS::Lock Sound Buffer Failed\n"); - S_Shutdown (); - S_Startup (); - return; - } - - if (++reps > 10000) { - Con_Printf - ("S_TransferStereo16: DS: couldn't restore buffer\n"); - S_Shutdown (); - S_Startup (); - return; - } - } - } else + // fixme: should check for NULL pbuf value + if (pDSBuf) pbuf=DSOUND_LockBuffer(true); + else #endif { pbuf = (DWORD *) shm->buffer; @@ -152,8 +119,7 @@ S_TransferStereo16 (int endtime) } #ifdef _WIN32 - if (pDSBuf) - pDSBuf->lpVtbl->Unlock (pDSBuf, pbuf, dwSize, NULL, 0); + if (pDSBuf) DSOUND_LockBuffer(false); #endif } @@ -169,13 +135,6 @@ S_TransferPaintBuffer (int endtime) int snd_vol; DWORD *pbuf; -#ifdef _WIN32 - int reps; - DWORD dwSize, dwSize2; - DWORD *pbuf2; - HRESULT hresult; -#endif - if (shm->samplebits == 16 && shm->channels == 2) { S_TransferStereo16 (endtime); return; @@ -189,30 +148,9 @@ S_TransferPaintBuffer (int endtime) snd_vol = volume->value * 256; #ifdef _WIN32 - if (pDSBuf) { - reps = 0; - - while ((hresult = pDSBuf->lpVtbl->Lock (pDSBuf, 0, gSndBufSize, - (LPVOID *) & pbuf, &dwSize, - (LPVOID *) & pbuf2, &dwSize2, - 0)) != DS_OK) { - if (hresult != DSERR_BUFFERLOST) { - Con_Printf - ("S_TransferPaintBuffer: DS::Lock Sound Buffer Failed\n"); - S_Shutdown (); - S_Startup (); - return; - } - - if (++reps > 10000) { - Con_Printf - ("S_TransferPaintBuffer: DS: couldn't restore buffer\n"); - S_Shutdown (); - S_Startup (); - return; - } - } - } else + // fixme: should check for NULL pbuf value + if (pDSBuf) pbuf=DSOUND_LockBuffer(true); + else #endif { pbuf = (DWORD *) shm->buffer; @@ -246,20 +184,7 @@ S_TransferPaintBuffer (int endtime) } } #ifdef _WIN32 - if (pDSBuf) { - DWORD dwNewpos, dwWrite; - int il = paintedtime; - int ir = endtime - paintedtime; - - ir += il; - - pDSBuf->lpVtbl->Unlock (pDSBuf, pbuf, dwSize, NULL, 0); - - pDSBuf->lpVtbl->GetCurrentPosition (pDSBuf, &dwNewpos, &dwWrite); - -// if ((dwNewpos >= il) && (dwNewpos <= ir)) -// Con_Printf("%d-%d p %d c\n", il, ir, dwNewpos); - } + if (pDSBuf) DSOUND_LockBuffer(false); #endif } diff --git a/source/snd_win.c b/source/snd_win.c index c4f8005..1d410d7 100644 --- a/source/snd_win.c +++ b/source/snd_win.c @@ -37,8 +37,7 @@ #define iDirectSoundCreate(a,b,c) pDirectSoundCreate(a,b,c) HRESULT (WINAPI * pDirectSoundCreate) (GUID FAR * lpGUID, - LPDIRECTSOUND FAR * lplpDS, - IUnknown FAR * pUnkOuter); + LPDIRECTSOUND FAR * lplpDS,IUnknown FAR * pUnkOuter); // 64K is > 1 second at 16-bit, 22050 Hz #define WAV_BUFFERS 64 @@ -57,7 +56,6 @@ static qboolean primary_format_set; static int sample16; static int snd_sent, snd_completed; - /* * Global variables. Must be visible to window-procedure function * so it can unlock and free the data block after it has been played. @@ -692,3 +690,77 @@ SNDDMA_Shutdown (void) { FreeSound (); } + +DWORD * +DSOUND_LockBuffer(qboolean lockit) +{ + int reps; + + static DWORD dwSize; + static DWORD dwSize2; + static DWORD *pbuf1; + static DWORD *pbuf2; + HRESULT hresult; + + if (!pDSBuf) return; + + if (lockit) { + reps = 0; + while ((hresult = pDSBuf->lpVtbl->Lock (pDSBuf, 0, gSndBufSize, + (LPVOID *) & pbuf1, &dwSize, + (LPVOID *) & pbuf2, &dwSize2,0)) != DS_OK) { + if (hresult != DSERR_BUFFERLOST) { + Con_Printf + ("S_TransferStereo16: DS::Lock Sound Buffer Failed\n"); + S_Shutdown (); + S_Startup (); + return; + } + + if (++reps > 10000) { + Con_Printf + ("S_TransferStereo16: DS: couldn't restore buffer\n"); + S_Shutdown (); + S_Startup (); + return; + } + } + } else { + pDSBuf->lpVtbl->Unlock (pDSBuf, pbuf1, dwSize, NULL, 0); + pbuf1=NULL; + pbuf2=NULL; + dwSize=0; + dwSize2=0; + } + return(pbuf1); +} + +void DSOUND_ClearBuffer(int clear) +{ + DWORD *pData; + +// fixme: this should be called with 2nd pbuf2 = NULL, dwsize =0 + pData=DSOUND_LockBuffer(true); + memset (pData, clear, shm->samples * shm->samplebits / 8); + DSOUND_LockBuffer(false); +} + +void DSOUND_Restore(void) +{ +// if the buffer was lost or stopped, restore it and/or restart it + DWORD dwStatus; + + if (!pDSBuf) return; + + if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK) + Con_Printf ("Couldn't get sound buffer status\n"); + + if (dwStatus & DSBSTATUS_BUFFERLOST) + pDSBuf->lpVtbl->Restore (pDSBuf); + + if (!(dwStatus & DSBSTATUS_PLAYING)) + pDSBuf->lpVtbl->Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); + + return; +} +