2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #define iDirectSoundCreate(a,b,c) pDirectSoundCreate(a,b,c)
25 HRESULT (WINAPI *pDirectSoundCreate)(GUID FAR *lpGUID, LPDIRECTSOUND FAR *lplpDS, IUnknown FAR *pUnkOuter);
27 // 64K is > 1 second at 16-bit, 22050 Hz
28 #define WAV_BUFFERS 64
30 #define WAV_BUFFER_SIZE 0x0400
31 #define SECONDARY_BUFFER_SIZE 0x10000
33 typedef enum {SIS_SUCCESS, SIS_FAILURE, SIS_NOTAVAIL} sndinitstat;
35 static qboolean wavonly;
36 static qboolean dsound_init;
37 static qboolean wav_init;
38 static qboolean snd_firsttime = true, snd_isdirect, snd_iswave;
39 static qboolean primary_format_set;
42 static int snd_sent, snd_completed;
46 * Global variables. Must be visible to window-procedure function
47 * so it can unlock and free the data block after it has been played.
51 HPSTR lpData, lpData2;
65 LPDIRECTSOUNDBUFFER pDSBuf, pDSPBuf;
69 qboolean SNDDMA_InitWav (void);
70 sndinitstat SNDDMA_InitDirect (void);
77 void S_BlockSound (void)
80 // DirectSound takes care of blocking itself
87 waveOutReset (hWaveOut);
98 void S_UnblockSound (void)
101 // DirectSound takes care of blocking itself
114 void FreeSound (void)
120 pDSBuf->lpVtbl->Stop(pDSBuf);
121 pDSBuf->lpVtbl->Release(pDSBuf);
124 // only release primary buffer if it's not also the mixing buffer we just released
125 if (pDSPBuf && (pDSBuf != pDSPBuf))
127 pDSPBuf->lpVtbl->Release(pDSPBuf);
132 pDS->lpVtbl->SetCooperativeLevel (pDS, mainwindow, DSSCL_NORMAL);
133 pDS->lpVtbl->Release(pDS);
138 waveOutReset (hWaveOut);
142 for (i=0 ; i< WAV_BUFFERS ; i++)
143 waveOutUnprepareHeader (hWaveOut, lpWaveHdr+i, sizeof(WAVEHDR));
146 waveOutClose (hWaveOut);
150 GlobalUnlock(hWaveHdr);
151 GlobalFree(hWaveHdr);
182 sndinitstat SNDDMA_InitDirect (void)
186 DWORD dwSize, dwWrite;
188 WAVEFORMATEX format, pformat;
192 memset ((void *)&sn, 0, sizeof (sn));
197 shm->samplebits = 16;
200 memset (&format, 0, sizeof(format));
201 format.wFormatTag = WAVE_FORMAT_PCM;
202 format.nChannels = shm->channels;
203 format.wBitsPerSample = shm->samplebits;
204 format.nSamplesPerSec = shm->speed;
205 format.nBlockAlign = format.nChannels
206 *format.wBitsPerSample / 8;
208 format.nAvgBytesPerSec = format.nSamplesPerSec
213 hInstDS = LoadLibrary("dsound.dll");
217 Con_SafePrintf ("Couldn't load dsound.dll\n");
221 pDirectSoundCreate = (void *)GetProcAddress(hInstDS,"DirectSoundCreate");
223 if (!pDirectSoundCreate)
225 Con_SafePrintf ("Couldn't get DS proc addr\n");
230 while ((hresult = iDirectSoundCreate(NULL, &pDS, NULL)) != DS_OK)
232 if (hresult != DSERR_ALLOCATED)
234 Con_SafePrintf ("DirectSound create failed\n");
238 if (MessageBox (NULL,
239 "The sound hardware is in use by another app.\n\n"
240 "Select Retry to try to start sound again or Cancel to run Quake with no sound.",
241 "Sound not available",
242 MB_RETRYCANCEL | MB_SETFOREGROUND | MB_ICONEXCLAMATION) != IDRETRY)
244 Con_SafePrintf ("DirectSoundCreate failure\n"
245 " hardware already in use\n");
250 dscaps.dwSize = sizeof(dscaps);
252 if (DS_OK != pDS->lpVtbl->GetCaps (pDS, &dscaps))
254 Con_SafePrintf ("Couldn't get DS caps\n");
257 if (dscaps.dwFlags & DSCAPS_EMULDRIVER)
259 Con_SafePrintf ("No DirectSound driver installed\n");
264 if (DS_OK != pDS->lpVtbl->SetCooperativeLevel (pDS, mainwindow, DSSCL_EXCLUSIVE))
266 Con_SafePrintf ("Set coop level failed\n");
271 // get access to the primary buffer, if possible, so we can set the
272 // sound hardware format
273 memset (&dsbuf, 0, sizeof(dsbuf));
274 dsbuf.dwSize = sizeof(DSBUFFERDESC);
275 dsbuf.dwFlags = DSBCAPS_PRIMARYBUFFER;
276 dsbuf.dwBufferBytes = 0;
277 dsbuf.lpwfxFormat = NULL;
279 memset(&dsbcaps, 0, sizeof(dsbcaps));
280 dsbcaps.dwSize = sizeof(dsbcaps);
281 primary_format_set = false;
283 if (!COM_CheckParm ("-snoforceformat"))
285 if (DS_OK == pDS->lpVtbl->CreateSoundBuffer(pDS, &dsbuf, &pDSPBuf, NULL))
289 if (DS_OK != pDSPBuf->lpVtbl->SetFormat (pDSPBuf, &pformat))
292 Con_SafePrintf ("Set primary sound buffer format: no\n");
297 Con_SafePrintf ("Set primary sound buffer format: yes\n");
299 primary_format_set = true;
304 if (!primary_format_set || !COM_CheckParm ("-primarysound"))
306 // create the secondary buffer we'll actually work with
307 memset (&dsbuf, 0, sizeof(dsbuf));
308 dsbuf.dwSize = sizeof(DSBUFFERDESC);
309 dsbuf.dwFlags = DSBCAPS_CTRLFREQUENCY | DSBCAPS_LOCSOFTWARE;
310 dsbuf.dwBufferBytes = SECONDARY_BUFFER_SIZE;
311 dsbuf.lpwfxFormat = &format;
313 memset(&dsbcaps, 0, sizeof(dsbcaps));
314 dsbcaps.dwSize = sizeof(dsbcaps);
316 if (DS_OK != pDS->lpVtbl->CreateSoundBuffer(pDS, &dsbuf, &pDSBuf, NULL))
318 Con_SafePrintf ("DS:CreateSoundBuffer Failed");
323 shm->channels = format.nChannels;
324 shm->samplebits = format.wBitsPerSample;
325 shm->speed = format.nSamplesPerSec;
327 if (DS_OK != pDSBuf->lpVtbl->GetCaps (pDSBuf, &dsbcaps))
329 Con_SafePrintf ("DS:GetCaps failed\n");
335 Con_SafePrintf ("Using secondary sound buffer\n");
339 if (DS_OK != pDS->lpVtbl->SetCooperativeLevel (pDS, mainwindow, DSSCL_WRITEPRIMARY))
341 Con_SafePrintf ("Set coop level failed\n");
346 if (DS_OK != pDSPBuf->lpVtbl->GetCaps (pDSPBuf, &dsbcaps))
348 Con_Printf ("DS:GetCaps failed\n");
353 Con_SafePrintf ("Using primary sound buffer\n");
356 // Make sure mixer is active
357 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
360 Con_SafePrintf(" %d channel(s)\n"
363 shm->channels, shm->samplebits, shm->speed);
365 gSndBufSize = dsbcaps.dwBufferBytes;
367 // initialize the buffer
370 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &lpData, &dwSize, NULL, NULL, 0)) != DS_OK)
372 if (hresult != DSERR_BUFFERLOST)
374 Con_SafePrintf ("SNDDMA_InitDirect: DS::Lock Sound Buffer Failed\n");
381 Con_SafePrintf ("SNDDMA_InitDirect: DS: couldn't restore buffer\n");
388 memset(lpData, 0, dwSize);
389 // lpData[4] = lpData[5] = 0x7f; // force a pop for debugging
391 pDSBuf->lpVtbl->Unlock(pDSBuf, lpData, dwSize, NULL, 0);
393 /* we don't want anyone to access the buffer directly w/o locking it first. */
396 pDSBuf->lpVtbl->Stop(pDSBuf);
397 pDSBuf->lpVtbl->GetCurrentPosition(pDSBuf, &mmstarttime.u.sample, &dwWrite);
398 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
400 shm->soundalive = true;
401 shm->splitbuffer = false;
402 shm->samples = gSndBufSize/(shm->samplebits/8);
404 shm->submission_chunk = 1;
405 shm->buffer = (unsigned char *) lpData;
406 sample16 = (shm->samplebits/8) - 1;
418 Crappy windows multimedia base
421 qboolean SNDDMA_InitWav (void)
433 shm->samplebits = 16;
436 memset (&format, 0, sizeof(format));
437 format.wFormatTag = WAVE_FORMAT_PCM;
438 format.nChannels = shm->channels;
439 format.wBitsPerSample = shm->samplebits;
440 format.nSamplesPerSec = shm->speed;
441 format.nBlockAlign = format.nChannels
442 *format.wBitsPerSample / 8;
444 format.nAvgBytesPerSec = format.nSamplesPerSec
447 /* Open a waveform device for output using window callback. */
448 while ((hr = waveOutOpen((LPHWAVEOUT)&hWaveOut, WAVE_MAPPER,
450 0, 0L, CALLBACK_NULL)) != MMSYSERR_NOERROR)
452 if (hr != MMSYSERR_ALLOCATED)
454 Con_SafePrintf ("waveOutOpen failed\n");
458 if (MessageBox (NULL,
459 "The sound hardware is in use by another app.\n\n"
460 "Select Retry to try to start sound again or Cancel to run Quake with no sound.",
461 "Sound not available",
462 MB_RETRYCANCEL | MB_SETFOREGROUND | MB_ICONEXCLAMATION) != IDRETRY)
464 Con_SafePrintf ("waveOutOpen failure;\n"
465 " hardware already in use\n");
471 * Allocate and lock memory for the waveform data. The memory
472 * for waveform data must be globally allocated with
473 * GMEM_MOVEABLE and GMEM_SHARE flags.
476 gSndBufSize = WAV_BUFFERS*WAV_BUFFER_SIZE;
477 hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, gSndBufSize);
480 Con_SafePrintf ("Sound: Out of memory.\n");
484 lpData = GlobalLock(hData);
487 Con_SafePrintf ("Sound: Failed to lock.\n");
491 memset (lpData, 0, gSndBufSize);
494 * Allocate and lock memory for the header. This memory must
495 * also be globally allocated with GMEM_MOVEABLE and
498 hWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
499 (DWORD) sizeof(WAVEHDR) * WAV_BUFFERS);
501 if (hWaveHdr == NULL)
503 Con_SafePrintf ("Sound: Failed to Alloc header.\n");
508 lpWaveHdr = (LPWAVEHDR) GlobalLock(hWaveHdr);
510 if (lpWaveHdr == NULL)
512 Con_SafePrintf ("Sound: Failed to lock header.\n");
517 memset (lpWaveHdr, 0, sizeof(WAVEHDR) * WAV_BUFFERS);
519 /* After allocation, set up and prepare headers. */
520 for (i=0 ; i<WAV_BUFFERS ; i++)
522 lpWaveHdr[i].dwBufferLength = WAV_BUFFER_SIZE;
523 lpWaveHdr[i].lpData = lpData + i*WAV_BUFFER_SIZE;
525 if (waveOutPrepareHeader(hWaveOut, lpWaveHdr+i, sizeof(WAVEHDR)) !=
528 Con_SafePrintf ("Sound: failed to prepare wave headers\n");
534 shm->soundalive = true;
535 shm->splitbuffer = false;
536 shm->samples = gSndBufSize/(shm->samplebits/8);
538 shm->submission_chunk = 1;
539 shm->buffer = (unsigned char *) lpData;
540 sample16 = (shm->samplebits/8) - 1;
551 Try to find a sound device to mix for.
552 Returns false if nothing is found.
556 qboolean SNDDMA_Init(void)
560 if (COM_CheckParm ("-wavonly"))
563 dsound_init = wav_init = 0;
565 stat = SIS_FAILURE; // assume DirectSound won't initialize
567 /* Init DirectSound */
570 if (snd_firsttime || snd_isdirect)
572 stat = SNDDMA_InitDirect ();;
574 if (stat == SIS_SUCCESS)
579 Con_SafePrintf ("DirectSound initialized\n");
583 snd_isdirect = false;
584 Con_SafePrintf ("DirectSound failed to init\n");
589 // if DirectSound didn't succeed in initializing, try to initialize
590 // waveOut sound, unless DirectSound failed because the hardware is
591 // already allocated (in which case the user has already chosen not
593 if (!dsound_init && (stat != SIS_NOTAVAIL))
595 if (snd_firsttime || snd_iswave)
598 snd_iswave = SNDDMA_InitWav ();
603 Con_SafePrintf ("Wave sound initialized\n");
607 Con_SafePrintf ("Wave sound failed to init\n");
612 snd_firsttime = false;
614 if (!dsound_init && !wav_init)
616 // if (snd_firsttime)
617 // Con_SafePrintf ("No sound device initialized\n");
629 return the current sample position (in mono samples read)
630 inside the recirculating dma buffer, so the mixing code will know
631 how many sample are required to fill it up.
634 int SNDDMA_GetDMAPos(void)
642 mmtime.wType = TIME_SAMPLES;
643 pDSBuf->lpVtbl->GetCurrentPosition(pDSBuf, &mmtime.u.sample, &dwWrite);
644 s = mmtime.u.sample - mmstarttime.u.sample;
648 s = snd_sent * WAV_BUFFER_SIZE;
654 s &= (shm->samples-1);
663 Send sound to device if buffer isn't really the dma buffer
666 void SNDDMA_Submit(void)
675 // find which sound blocks have completed
679 if ( snd_completed == snd_sent )
681 Con_DPrintf ("Sound overrun\n");
685 if ( ! (lpWaveHdr[ snd_completed & WAV_MASK].dwFlags & WHDR_DONE) )
690 snd_completed++; // this buffer has been played
694 // submit two new sound blocks
696 while (((snd_sent - snd_completed) >> sample16) < 4)
698 h = lpWaveHdr + ( snd_sent&WAV_MASK );
702 * Now the data block can be sent to the output device. The
703 * waveOutWrite function returns immediately and waveform
704 * data is sent to the output device in the background.
706 wResult = waveOutWrite(hWaveOut, h, sizeof(WAVEHDR));
708 if (wResult != MMSYSERR_NOERROR)
710 Con_SafePrintf ("Failed to write block to device\n");
721 Reset the sound device for exiting
724 void SNDDMA_Shutdown(void)