]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_win.c
win32 fixes and some MSVC warnings fixed
[xonotic/darkplaces.git] / snd_win.c
index 2aa42d61ed315f37013f37ce76e7ec7658659587..2fed37be8279b8dda4ac6dcdf30e56f7e9a7f2ae 100644 (file)
--- a/snd_win.c
+++ b/snd_win.c
@@ -66,9 +66,8 @@ LPDIRECTSOUNDBUFFER pDSBuf, pDSPBuf;
 
 HINSTANCE hInstDS;
 
-qboolean SNDDMA_InitDirect (void);
 qboolean SNDDMA_InitWav (void);
-
+sndinitstat SNDDMA_InitDirect (void);
 
 /*
 ==================
@@ -189,6 +188,7 @@ sndinitstat SNDDMA_InitDirect (void)
        WAVEFORMATEX    format, pformat; 
        HRESULT                 hresult;
        int                             reps;
+       int i;
 
        memset ((void *)&sn, 0, sizeof (sn));
 
@@ -196,18 +196,20 @@ sndinitstat SNDDMA_InitDirect (void)
 
        shm->channels = 2;
        shm->samplebits = 16;
-       shm->speed = 11025;
+       i = COM_CheckParm ("-sndspeed"); // LordHavoc: -sndspeed option
+       if (i && i != (com_argc - 1))
+               shm->speed = atoi(com_argv[i+1]);
+       else
+               shm->speed = 11025;
 
        memset (&format, 0, sizeof(format));
        format.wFormatTag = WAVE_FORMAT_PCM;
     format.nChannels = shm->channels;
     format.wBitsPerSample = shm->samplebits;
     format.nSamplesPerSec = shm->speed;
-    format.nBlockAlign = format.nChannels
-               *format.wBitsPerSample / 8;
+    format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
     format.cbSize = 0;
-    format.nAvgBytesPerSec = format.nSamplesPerSec
-               *format.nBlockAlign; 
+    format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign; 
 
        if (!hInstDS)
        {
@@ -360,7 +362,7 @@ sndinitstat SNDDMA_InitDirect (void)
        if (snd_firsttime)
                Con_SafePrintf("   %d channel(s)\n"
                               "   %d bits/sample\n"
-                                          "   %d bytes/sec\n",
+                                          "   %d samples/sec\n",
                                           shm->channels, shm->samplebits, shm->speed);
        
        gSndBufSize = dsbcaps.dwBufferBytes;
@@ -387,7 +389,6 @@ sndinitstat SNDDMA_InitDirect (void)
        }
 
        memset(lpData, 0, dwSize);
-//             lpData[4] = lpData[5] = 0x7f;   // force a pop for debugging
 
        pDSBuf->lpVtbl->Unlock(pDSBuf, lpData, dwSize, NULL, 0);
 
@@ -432,7 +433,11 @@ qboolean SNDDMA_InitWav (void)
 
        shm->channels = 2;
        shm->samplebits = 16;
-       shm->speed = 11025;
+       i = COM_CheckParm ("-sndspeed"); // LordHavoc: -sndspeed option
+       if (i && i != (com_argc - 1))
+               shm->speed = atoi(com_argv[i+1]);
+       else
+               shm->speed = 11025;
 
        memset (&format, 0, sizeof(format));
        format.wFormatTag = WAVE_FORMAT_PCM;
@@ -554,7 +559,7 @@ Returns false if nothing is found.
 ==================
 */
 
-int SNDDMA_Init(void)
+qboolean SNDDMA_Init(void)
 {
        sndinitstat     stat;
 
@@ -613,12 +618,7 @@ int SNDDMA_Init(void)
        snd_firsttime = false;
 
        if (!dsound_init && !wav_init)
-       {
-//             if (snd_firsttime)
-//                     Con_SafePrintf ("No sound device initialized\n");
-
                return 0;
-       }
 
        return 1;
 }
@@ -648,6 +648,8 @@ int SNDDMA_GetDMAPos(void)
        {
                s = snd_sent * WAV_BUFFER_SIZE;
        }
+       else
+               s = 0;
 
 
        s >>= sample16;
@@ -699,19 +701,19 @@ void SNDDMA_Submit(void)
                h = lpWaveHdr + ( snd_sent&WAV_MASK );
 
                snd_sent++;
-               /* 
-                * Now the data block can be sent to the output device. The 
-                * waveOutWrite function returns immediately and waveform 
-                * data is sent to the output device in the background. 
-                */ 
-               wResult = waveOutWrite(hWaveOut, h, sizeof(WAVEHDR)); 
+               /*
+                * Now the data block can be sent to the output device. The
+                * waveOutWrite function returns immediately and waveform
+                * data is sent to the output device in the background.
+                */
+               wResult = waveOutWrite(hWaveOut, h, sizeof(WAVEHDR));
 
                if (wResult != MMSYSERR_NOERROR)
-               { 
+               {
                        Con_SafePrintf ("Failed to write block to device\n");
                        FreeSound ();
-                       return; 
-               } 
+                       return;
+               }
        }
 }
 
@@ -727,3 +729,46 @@ void SNDDMA_Shutdown(void)
        FreeSound ();
 }
 
+DWORD dsound_dwSize;
+DWORD dsound_dwSize2;
+DWORD *dsound_pbuf;
+DWORD *dsound_pbuf2;
+void *S_LockBuffer(void)
+{
+       int reps;
+       HRESULT hresult;
+
+       if (pDSBuf)
+       {
+               reps = 0;
+
+               while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &dsound_pbuf, &dsound_dwSize, &dsound_pbuf2, &dsound_dwSize2, 0)) != DS_OK)
+               {
+                       if (hresult != DSERR_BUFFERLOST)
+                       {
+                               Con_Printf ("S_LockBuffer: DS::Lock Sound Buffer Failed\n");
+                               S_Shutdown ();
+                               S_Startup ();
+                               return NULL;
+                       }
+
+                       if (++reps > 10000)
+                       {
+                               Con_Printf ("S_LockBuffer: DS: couldn't restore buffer\n");
+                               S_Shutdown ();
+                               S_Startup ();
+                               return NULL;
+                       }
+               }
+               return dsound_pbuf;
+       }
+       else
+               return shm->buffer;
+}
+
+void S_UnlockBuffer()
+{
+       if (pDSBuf)
+               pDSBuf->lpVtbl->Unlock(pDSBuf, dsound_pbuf, dsound_dwSize, dsound_pbuf2, dsound_dwSize2);
+}
+