snd_win.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <float.h>
  16. #include "../client/client.h"
  17. #include "../client/snd_loc.h"
  18. #include "winquake.h"
  19. #define iDirectSoundCreate(a,b,c) pDirectSoundCreate(a,b,c)
  20. HRESULT (WINAPI *pDirectSoundCreate)(GUID FAR *lpGUID, LPDIRECTSOUND FAR *lplpDS, IUnknown FAR *pUnkOuter);
  21. // 64K is > 1 second at 16-bit, 22050 Hz
  22. #define WAV_BUFFERS 64
  23. #define WAV_MASK 0x3F
  24. #define WAV_BUFFER_SIZE 0x0400
  25. #define SECONDARY_BUFFER_SIZE 0x10000
  26. typedef enum {SIS_SUCCESS, SIS_FAILURE, SIS_NOTAVAIL} sndinitstat;
  27. cvar_t *s_wavonly;
  28. static qboolean dsound_init;
  29. static qboolean wav_init;
  30. static qboolean snd_firsttime = true, snd_isdirect, snd_iswave;
  31. static qboolean primary_format_set;
  32. // starts at 0 for disabled
  33. static int snd_buffer_count = 0;
  34. static int sample16;
  35. static int snd_sent, snd_completed;
  36. /*
  37. * Global variables. Must be visible to window-procedure function
  38. * so it can unlock and free the data block after it has been played.
  39. */
  40. HANDLE hData;
  41. HPSTR lpData, lpData2;
  42. HGLOBAL hWaveHdr;
  43. LPWAVEHDR lpWaveHdr;
  44. HWAVEOUT hWaveOut;
  45. WAVEOUTCAPS wavecaps;
  46. DWORD gSndBufSize;
  47. MMTIME mmstarttime;
  48. LPDIRECTSOUND pDS;
  49. LPDIRECTSOUNDBUFFER pDSBuf, pDSPBuf;
  50. HINSTANCE hInstDS;
  51. qboolean SNDDMA_InitDirect (void);
  52. qboolean SNDDMA_InitWav (void);
  53. void FreeSound( void );
  54. static const char *DSoundError( int error )
  55. {
  56. switch ( error )
  57. {
  58. case DSERR_BUFFERLOST:
  59. return "DSERR_BUFFERLOST";
  60. case DSERR_INVALIDCALL:
  61. return "DSERR_INVALIDCALLS";
  62. case DSERR_INVALIDPARAM:
  63. return "DSERR_INVALIDPARAM";
  64. case DSERR_PRIOLEVELNEEDED:
  65. return "DSERR_PRIOLEVELNEEDED";
  66. }
  67. return "unknown";
  68. }
  69. /*
  70. ** DS_CreateBuffers
  71. */
  72. static qboolean DS_CreateBuffers( void )
  73. {
  74. DSBUFFERDESC dsbuf;
  75. DSBCAPS dsbcaps;
  76. WAVEFORMATEX pformat, format;
  77. DWORD dwWrite;
  78. memset (&format, 0, sizeof(format));
  79. format.wFormatTag = WAVE_FORMAT_PCM;
  80. format.nChannels = dma.channels;
  81. format.wBitsPerSample = dma.samplebits;
  82. format.nSamplesPerSec = dma.speed;
  83. format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
  84. format.cbSize = 0;
  85. format.nAvgBytesPerSec = format.nSamplesPerSec*format.nBlockAlign;
  86. Com_Printf( "Creating DS buffers\n" );
  87. Com_DPrintf("...setting EXCLUSIVE coop level: " );
  88. if ( DS_OK != pDS->lpVtbl->SetCooperativeLevel( pDS, cl_hwnd, DSSCL_EXCLUSIVE ) )
  89. {
  90. Com_Printf ("failed\n");
  91. FreeSound ();
  92. return false;
  93. }
  94. Com_DPrintf("ok\n" );
  95. // get access to the primary buffer, if possible, so we can set the
  96. // sound hardware format
  97. memset (&dsbuf, 0, sizeof(dsbuf));
  98. dsbuf.dwSize = sizeof(DSBUFFERDESC);
  99. dsbuf.dwFlags = DSBCAPS_PRIMARYBUFFER;
  100. dsbuf.dwBufferBytes = 0;
  101. dsbuf.lpwfxFormat = NULL;
  102. memset(&dsbcaps, 0, sizeof(dsbcaps));
  103. dsbcaps.dwSize = sizeof(dsbcaps);
  104. primary_format_set = false;
  105. Com_DPrintf( "...creating primary buffer: " );
  106. if (DS_OK == pDS->lpVtbl->CreateSoundBuffer(pDS, &dsbuf, &pDSPBuf, NULL))
  107. {
  108. pformat = format;
  109. Com_DPrintf( "ok\n" );
  110. if (DS_OK != pDSPBuf->lpVtbl->SetFormat (pDSPBuf, &pformat))
  111. {
  112. if (snd_firsttime)
  113. Com_DPrintf ("...setting primary sound format: failed\n");
  114. }
  115. else
  116. {
  117. if (snd_firsttime)
  118. Com_DPrintf ("...setting primary sound format: ok\n");
  119. primary_format_set = true;
  120. }
  121. }
  122. else
  123. Com_Printf( "failed\n" );
  124. if ( !primary_format_set || !s_primary->value)
  125. {
  126. // create the secondary buffer we'll actually work with
  127. memset (&dsbuf, 0, sizeof(dsbuf));
  128. dsbuf.dwSize = sizeof(DSBUFFERDESC);
  129. dsbuf.dwFlags = DSBCAPS_CTRLFREQUENCY | DSBCAPS_LOCSOFTWARE;
  130. dsbuf.dwBufferBytes = SECONDARY_BUFFER_SIZE;
  131. dsbuf.lpwfxFormat = &format;
  132. memset(&dsbcaps, 0, sizeof(dsbcaps));
  133. dsbcaps.dwSize = sizeof(dsbcaps);
  134. Com_DPrintf( "...creating secondary buffer: " );
  135. if (DS_OK != pDS->lpVtbl->CreateSoundBuffer(pDS, &dsbuf, &pDSBuf, NULL))
  136. {
  137. Com_Printf( "failed\n" );
  138. FreeSound ();
  139. return false;
  140. }
  141. Com_DPrintf( "ok\n" );
  142. dma.channels = format.nChannels;
  143. dma.samplebits = format.wBitsPerSample;
  144. dma.speed = format.nSamplesPerSec;
  145. if (DS_OK != pDSBuf->lpVtbl->GetCaps (pDSBuf, &dsbcaps))
  146. {
  147. Com_Printf ("*** GetCaps failed ***\n");
  148. FreeSound ();
  149. return false;
  150. }
  151. Com_Printf ("...using secondary sound buffer\n");
  152. }
  153. else
  154. {
  155. Com_Printf( "...using primary buffer\n" );
  156. Com_DPrintf( "...setting WRITEPRIMARY coop level: " );
  157. if (DS_OK != pDS->lpVtbl->SetCooperativeLevel (pDS, cl_hwnd, DSSCL_WRITEPRIMARY))
  158. {
  159. Com_Printf( "failed\n" );
  160. FreeSound ();
  161. return false;
  162. }
  163. Com_DPrintf( "ok\n" );
  164. if (DS_OK != pDSPBuf->lpVtbl->GetCaps (pDSPBuf, &dsbcaps))
  165. {
  166. Com_Printf ("*** GetCaps failed ***\n");
  167. return false;
  168. }
  169. pDSBuf = pDSPBuf;
  170. }
  171. // Make sure mixer is active
  172. pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
  173. if (snd_firsttime)
  174. Com_Printf(" %d channel(s)\n"
  175. " %d bits/sample\n"
  176. " %d bytes/sec\n",
  177. dma.channels, dma.samplebits, dma.speed);
  178. gSndBufSize = dsbcaps.dwBufferBytes;
  179. /* we don't want anyone to access the buffer directly w/o locking it first. */
  180. lpData = NULL;
  181. pDSBuf->lpVtbl->Stop(pDSBuf);
  182. pDSBuf->lpVtbl->GetCurrentPosition(pDSBuf, &mmstarttime.u.sample, &dwWrite);
  183. pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
  184. dma.samples = gSndBufSize/(dma.samplebits/8);
  185. dma.samplepos = 0;
  186. dma.submission_chunk = 1;
  187. dma.buffer = (unsigned char *) lpData;
  188. sample16 = (dma.samplebits/8) - 1;
  189. return true;
  190. }
  191. /*
  192. ** DS_DestroyBuffers
  193. */
  194. static void DS_DestroyBuffers( void )
  195. {
  196. Com_DPrintf( "Destroying DS buffers\n" );
  197. if ( pDS )
  198. {
  199. Com_DPrintf( "...setting NORMAL coop level\n" );
  200. pDS->lpVtbl->SetCooperativeLevel( pDS, cl_hwnd, DSSCL_NORMAL );
  201. }
  202. if ( pDSBuf )
  203. {
  204. Com_DPrintf( "...stopping and releasing sound buffer\n" );
  205. pDSBuf->lpVtbl->Stop( pDSBuf );
  206. pDSBuf->lpVtbl->Release( pDSBuf );
  207. }
  208. // only release primary buffer if it's not also the mixing buffer we just released
  209. if ( pDSPBuf && ( pDSBuf != pDSPBuf ) )
  210. {
  211. Com_DPrintf( "...releasing primary buffer\n" );
  212. pDSPBuf->lpVtbl->Release( pDSPBuf );
  213. }
  214. pDSBuf = NULL;
  215. pDSPBuf = NULL;
  216. dma.buffer = NULL;
  217. }
  218. /*
  219. ==================
  220. FreeSound
  221. ==================
  222. */
  223. void FreeSound (void)
  224. {
  225. int i;
  226. Com_DPrintf( "Shutting down sound system\n" );
  227. if ( pDS )
  228. DS_DestroyBuffers();
  229. if ( hWaveOut )
  230. {
  231. Com_DPrintf( "...resetting waveOut\n" );
  232. waveOutReset (hWaveOut);
  233. if (lpWaveHdr)
  234. {
  235. Com_DPrintf( "...unpreparing headers\n" );
  236. for (i=0 ; i< WAV_BUFFERS ; i++)
  237. waveOutUnprepareHeader (hWaveOut, lpWaveHdr+i, sizeof(WAVEHDR));
  238. }
  239. Com_DPrintf( "...closing waveOut\n" );
  240. waveOutClose (hWaveOut);
  241. if (hWaveHdr)
  242. {
  243. Com_DPrintf( "...freeing WAV header\n" );
  244. GlobalUnlock(hWaveHdr);
  245. GlobalFree(hWaveHdr);
  246. }
  247. if (hData)
  248. {
  249. Com_DPrintf( "...freeing WAV buffer\n" );
  250. GlobalUnlock(hData);
  251. GlobalFree(hData);
  252. }
  253. }
  254. if ( pDS )
  255. {
  256. Com_DPrintf( "...releasing DS object\n" );
  257. pDS->lpVtbl->Release( pDS );
  258. }
  259. if ( hInstDS )
  260. {
  261. Com_DPrintf( "...freeing DSOUND.DLL\n" );
  262. FreeLibrary( hInstDS );
  263. hInstDS = NULL;
  264. }
  265. pDS = NULL;
  266. pDSBuf = NULL;
  267. pDSPBuf = NULL;
  268. hWaveOut = 0;
  269. hData = 0;
  270. hWaveHdr = 0;
  271. lpData = NULL;
  272. lpWaveHdr = NULL;
  273. dsound_init = false;
  274. wav_init = false;
  275. }
  276. /*
  277. ==================
  278. SNDDMA_InitDirect
  279. Direct-Sound support
  280. ==================
  281. */
  282. sndinitstat SNDDMA_InitDirect (void)
  283. {
  284. DSCAPS dscaps;
  285. HRESULT hresult;
  286. dma.channels = 2;
  287. dma.samplebits = 16;
  288. if (s_khz->value == 44)
  289. dma.speed = 44100;
  290. if (s_khz->value == 22)
  291. dma.speed = 22050;
  292. else
  293. dma.speed = 11025;
  294. Com_Printf( "Initializing DirectSound\n");
  295. if ( !hInstDS )
  296. {
  297. Com_DPrintf( "...loading dsound.dll: " );
  298. hInstDS = LoadLibrary("dsound.dll");
  299. if (hInstDS == NULL)
  300. {
  301. Com_Printf ("failed\n");
  302. return SIS_FAILURE;
  303. }
  304. Com_DPrintf ("ok\n");
  305. pDirectSoundCreate = (void *)GetProcAddress(hInstDS,"DirectSoundCreate");
  306. if (!pDirectSoundCreate)
  307. {
  308. Com_Printf ("*** couldn't get DS proc addr ***\n");
  309. return SIS_FAILURE;
  310. }
  311. }
  312. Com_DPrintf( "...creating DS object: " );
  313. while ( ( hresult = iDirectSoundCreate( NULL, &pDS, NULL ) ) != DS_OK )
  314. {
  315. if (hresult != DSERR_ALLOCATED)
  316. {
  317. Com_Printf( "failed\n" );
  318. return SIS_FAILURE;
  319. }
  320. if (MessageBox (NULL,
  321. "The sound hardware is in use by another app.\n\n"
  322. "Select Retry to try to start sound again or Cancel to run Quake with no sound.",
  323. "Sound not available",
  324. MB_RETRYCANCEL | MB_SETFOREGROUND | MB_ICONEXCLAMATION) != IDRETRY)
  325. {
  326. Com_Printf ("failed, hardware already in use\n" );
  327. return SIS_NOTAVAIL;
  328. }
  329. }
  330. Com_DPrintf( "ok\n" );
  331. dscaps.dwSize = sizeof(dscaps);
  332. if ( DS_OK != pDS->lpVtbl->GetCaps( pDS, &dscaps ) )
  333. {
  334. Com_Printf ("*** couldn't get DS caps ***\n");
  335. }
  336. if ( dscaps.dwFlags & DSCAPS_EMULDRIVER )
  337. {
  338. Com_DPrintf ("...no DSound driver found\n" );
  339. FreeSound();
  340. return SIS_FAILURE;
  341. }
  342. if ( !DS_CreateBuffers() )
  343. return SIS_FAILURE;
  344. dsound_init = true;
  345. Com_DPrintf("...completed successfully\n" );
  346. return SIS_SUCCESS;
  347. }
  348. /*
  349. ==================
  350. SNDDM_InitWav
  351. Crappy windows multimedia base
  352. ==================
  353. */
  354. qboolean SNDDMA_InitWav (void)
  355. {
  356. WAVEFORMATEX format;
  357. int i;
  358. HRESULT hr;
  359. Com_Printf( "Initializing wave sound\n" );
  360. snd_sent = 0;
  361. snd_completed = 0;
  362. dma.channels = 2;
  363. dma.samplebits = 16;
  364. if (s_khz->value == 44)
  365. dma.speed = 44100;
  366. if (s_khz->value == 22)
  367. dma.speed = 22050;
  368. else
  369. dma.speed = 11025;
  370. memset (&format, 0, sizeof(format));
  371. format.wFormatTag = WAVE_FORMAT_PCM;
  372. format.nChannels = dma.channels;
  373. format.wBitsPerSample = dma.samplebits;
  374. format.nSamplesPerSec = dma.speed;
  375. format.nBlockAlign = format.nChannels
  376. *format.wBitsPerSample / 8;
  377. format.cbSize = 0;
  378. format.nAvgBytesPerSec = format.nSamplesPerSec
  379. *format.nBlockAlign;
  380. /* Open a waveform device for output using window callback. */
  381. Com_DPrintf ("...opening waveform device: ");
  382. while ((hr = waveOutOpen((LPHWAVEOUT)&hWaveOut, WAVE_MAPPER,
  383. &format,
  384. 0, 0L, CALLBACK_NULL)) != MMSYSERR_NOERROR)
  385. {
  386. if (hr != MMSYSERR_ALLOCATED)
  387. {
  388. Com_Printf ("failed\n");
  389. return false;
  390. }
  391. if (MessageBox (NULL,
  392. "The sound hardware is in use by another app.\n\n"
  393. "Select Retry to try to start sound again or Cancel to run Quake 2 with no sound.",
  394. "Sound not available",
  395. MB_RETRYCANCEL | MB_SETFOREGROUND | MB_ICONEXCLAMATION) != IDRETRY)
  396. {
  397. Com_Printf ("hw in use\n" );
  398. return false;
  399. }
  400. }
  401. Com_DPrintf( "ok\n" );
  402. /*
  403. * Allocate and lock memory for the waveform data. The memory
  404. * for waveform data must be globally allocated with
  405. * GMEM_MOVEABLE and GMEM_SHARE flags.
  406. */
  407. Com_DPrintf ("...allocating waveform buffer: ");
  408. gSndBufSize = WAV_BUFFERS*WAV_BUFFER_SIZE;
  409. hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, gSndBufSize);
  410. if (!hData)
  411. {
  412. Com_Printf( " failed\n" );
  413. FreeSound ();
  414. return false;
  415. }
  416. Com_DPrintf( "ok\n" );
  417. Com_DPrintf ("...locking waveform buffer: ");
  418. lpData = GlobalLock(hData);
  419. if (!lpData)
  420. {
  421. Com_Printf( " failed\n" );
  422. FreeSound ();
  423. return false;
  424. }
  425. memset (lpData, 0, gSndBufSize);
  426. Com_DPrintf( "ok\n" );
  427. /*
  428. * Allocate and lock memory for the header. This memory must
  429. * also be globally allocated with GMEM_MOVEABLE and
  430. * GMEM_SHARE flags.
  431. */
  432. Com_DPrintf ("...allocating waveform header: ");
  433. hWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
  434. (DWORD) sizeof(WAVEHDR) * WAV_BUFFERS);
  435. if (hWaveHdr == NULL)
  436. {
  437. Com_Printf( "failed\n" );
  438. FreeSound ();
  439. return false;
  440. }
  441. Com_DPrintf( "ok\n" );
  442. Com_DPrintf ("...locking waveform header: ");
  443. lpWaveHdr = (LPWAVEHDR) GlobalLock(hWaveHdr);
  444. if (lpWaveHdr == NULL)
  445. {
  446. Com_Printf( "failed\n" );
  447. FreeSound ();
  448. return false;
  449. }
  450. memset (lpWaveHdr, 0, sizeof(WAVEHDR) * WAV_BUFFERS);
  451. Com_DPrintf( "ok\n" );
  452. /* After allocation, set up and prepare headers. */
  453. Com_DPrintf ("...preparing headers: ");
  454. for (i=0 ; i<WAV_BUFFERS ; i++)
  455. {
  456. lpWaveHdr[i].dwBufferLength = WAV_BUFFER_SIZE;
  457. lpWaveHdr[i].lpData = lpData + i*WAV_BUFFER_SIZE;
  458. if (waveOutPrepareHeader(hWaveOut, lpWaveHdr+i, sizeof(WAVEHDR)) !=
  459. MMSYSERR_NOERROR)
  460. {
  461. Com_Printf ("failed\n");
  462. FreeSound ();
  463. return false;
  464. }
  465. }
  466. Com_DPrintf ("ok\n");
  467. dma.samples = gSndBufSize/(dma.samplebits/8);
  468. dma.samplepos = 0;
  469. dma.submission_chunk = 512;
  470. dma.buffer = (unsigned char *) lpData;
  471. sample16 = (dma.samplebits/8) - 1;
  472. wav_init = true;
  473. return true;
  474. }
  475. /*
  476. ==================
  477. SNDDMA_Init
  478. Try to find a sound device to mix for.
  479. Returns false if nothing is found.
  480. ==================
  481. */
  482. int SNDDMA_Init(void)
  483. {
  484. sndinitstat stat;
  485. memset ((void *)&dma, 0, sizeof (dma));
  486. s_wavonly = Cvar_Get ("s_wavonly", "0", 0);
  487. dsound_init = wav_init = 0;
  488. stat = SIS_FAILURE; // assume DirectSound won't initialize
  489. /* Init DirectSound */
  490. if (!s_wavonly->value)
  491. {
  492. if (snd_firsttime || snd_isdirect)
  493. {
  494. stat = SNDDMA_InitDirect ();
  495. if (stat == SIS_SUCCESS)
  496. {
  497. snd_isdirect = true;
  498. if (snd_firsttime)
  499. Com_Printf ("dsound init succeeded\n" );
  500. }
  501. else
  502. {
  503. snd_isdirect = false;
  504. Com_Printf ("*** dsound init failed ***\n");
  505. }
  506. }
  507. }
  508. // if DirectSound didn't succeed in initializing, try to initialize
  509. // waveOut sound, unless DirectSound failed because the hardware is
  510. // already allocated (in which case the user has already chosen not
  511. // to have sound)
  512. if (!dsound_init && (stat != SIS_NOTAVAIL))
  513. {
  514. if (snd_firsttime || snd_iswave)
  515. {
  516. snd_iswave = SNDDMA_InitWav ();
  517. if (snd_iswave)
  518. {
  519. if (snd_firsttime)
  520. Com_Printf ("Wave sound init succeeded\n");
  521. }
  522. else
  523. {
  524. Com_Printf ("Wave sound init failed\n");
  525. }
  526. }
  527. }
  528. snd_firsttime = false;
  529. snd_buffer_count = 1;
  530. if (!dsound_init && !wav_init)
  531. {
  532. if (snd_firsttime)
  533. Com_Printf ("*** No sound device initialized ***\n");
  534. return 0;
  535. }
  536. return 1;
  537. }
  538. /*
  539. ==============
  540. SNDDMA_GetDMAPos
  541. return the current sample position (in mono samples read)
  542. inside the recirculating dma buffer, so the mixing code will know
  543. how many sample are required to fill it up.
  544. ===============
  545. */
  546. int SNDDMA_GetDMAPos(void)
  547. {
  548. MMTIME mmtime;
  549. int s;
  550. DWORD dwWrite;
  551. if (dsound_init)
  552. {
  553. mmtime.wType = TIME_SAMPLES;
  554. pDSBuf->lpVtbl->GetCurrentPosition(pDSBuf, &mmtime.u.sample, &dwWrite);
  555. s = mmtime.u.sample - mmstarttime.u.sample;
  556. }
  557. else if (wav_init)
  558. {
  559. s = snd_sent * WAV_BUFFER_SIZE;
  560. }
  561. s >>= sample16;
  562. s &= (dma.samples-1);
  563. return s;
  564. }
  565. /*
  566. ==============
  567. SNDDMA_BeginPainting
  568. Makes sure dma.buffer is valid
  569. ===============
  570. */
  571. DWORD locksize;
  572. void SNDDMA_BeginPainting (void)
  573. {
  574. int reps;
  575. DWORD dwSize2;
  576. DWORD *pbuf, *pbuf2;
  577. HRESULT hresult;
  578. DWORD dwStatus;
  579. if (!pDSBuf)
  580. return;
  581. // if the buffer was lost or stopped, restore it and/or restart it
  582. if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DS_OK)
  583. Com_Printf ("Couldn't get sound buffer status\n");
  584. if (dwStatus & DSBSTATUS_BUFFERLOST)
  585. pDSBuf->lpVtbl->Restore (pDSBuf);
  586. if (!(dwStatus & DSBSTATUS_PLAYING))
  587. pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
  588. // lock the dsound buffer
  589. reps = 0;
  590. dma.buffer = NULL;
  591. while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &locksize,
  592. &pbuf2, &dwSize2, 0)) != DS_OK)
  593. {
  594. if (hresult != DSERR_BUFFERLOST)
  595. {
  596. Com_Printf( "S_TransferStereo16: Lock failed with error '%s'\n", DSoundError( hresult ) );
  597. S_Shutdown ();
  598. return;
  599. }
  600. else
  601. {
  602. pDSBuf->lpVtbl->Restore( pDSBuf );
  603. }
  604. if (++reps > 2)
  605. return;
  606. }
  607. dma.buffer = (unsigned char *)pbuf;
  608. }
  609. /*
  610. ==============
  611. SNDDMA_Submit
  612. Send sound to device if buffer isn't really the dma buffer
  613. Also unlocks the dsound buffer
  614. ===============
  615. */
  616. void SNDDMA_Submit(void)
  617. {
  618. LPWAVEHDR h;
  619. int wResult;
  620. if (!dma.buffer)
  621. return;
  622. // unlock the dsound buffer
  623. if (pDSBuf)
  624. pDSBuf->lpVtbl->Unlock(pDSBuf, dma.buffer, locksize, NULL, 0);
  625. if (!wav_init)
  626. return;
  627. //
  628. // find which sound blocks have completed
  629. //
  630. while (1)
  631. {
  632. if ( snd_completed == snd_sent )
  633. {
  634. Com_DPrintf ("Sound overrun\n");
  635. break;
  636. }
  637. if ( ! (lpWaveHdr[ snd_completed & WAV_MASK].dwFlags & WHDR_DONE) )
  638. {
  639. break;
  640. }
  641. snd_completed++; // this buffer has been played
  642. }
  643. //Com_Printf ("completed %i\n", snd_completed);
  644. //
  645. // submit a few new sound blocks
  646. //
  647. while (((snd_sent - snd_completed) >> sample16) < 8)
  648. {
  649. h = lpWaveHdr + ( snd_sent&WAV_MASK );
  650. if (paintedtime/256 <= snd_sent)
  651. break; // Com_Printf ("submit overrun\n");
  652. //Com_Printf ("send %i\n", snd_sent);
  653. snd_sent++;
  654. /*
  655. * Now the data block can be sent to the output device. The
  656. * waveOutWrite function returns immediately and waveform
  657. * data is sent to the output device in the background.
  658. */
  659. wResult = waveOutWrite(hWaveOut, h, sizeof(WAVEHDR));
  660. if (wResult != MMSYSERR_NOERROR)
  661. {
  662. Com_Printf ("Failed to write block to device\n");
  663. FreeSound ();
  664. return;
  665. }
  666. }
  667. }
  668. /*
  669. ==============
  670. SNDDMA_Shutdown
  671. Reset the sound device for exiting
  672. ===============
  673. */
  674. void SNDDMA_Shutdown(void)
  675. {
  676. FreeSound ();
  677. }
  678. /*
  679. ===========
  680. S_Activate
  681. Called when the main window gains or loses focus.
  682. The window have been destroyed and recreated
  683. between a deactivate and an activate.
  684. ===========
  685. */
  686. void S_Activate (qboolean active)
  687. {
  688. if ( active )
  689. {
  690. if ( pDS && cl_hwnd && snd_isdirect )
  691. {
  692. DS_CreateBuffers();
  693. }
  694. }
  695. else
  696. {
  697. if ( pDS && cl_hwnd && snd_isdirect )
  698. {
  699. DS_DestroyBuffers();
  700. }
  701. }
  702. }