snd_dma.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. Copyright (C) 1996-1997 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. // snd_dma.c -- main control for any streaming sound output device
  16. #include "quakedef.h"
  17. #ifdef _WIN32
  18. #include "winquake.h"
  19. #endif
  20. void S_Play(void);
  21. void S_PlayVol(void);
  22. void S_SoundList(void);
  23. void S_Update_();
  24. void S_StopAllSounds(qboolean clear);
  25. void S_StopAllSoundsC(void);
  26. // QuakeWorld hack...
  27. #define viewentity playernum+1
  28. // =======================================================================
  29. // Internal sound data & structures
  30. // =======================================================================
  31. channel_t channels[MAX_CHANNELS];
  32. int total_channels;
  33. int snd_blocked = 0;
  34. static qboolean snd_ambient = 1;
  35. qboolean snd_initialized = false;
  36. // pointer should go away
  37. volatile dma_t *shm = 0;
  38. volatile dma_t sn;
  39. vec3_t listener_origin;
  40. vec3_t listener_forward;
  41. vec3_t listener_right;
  42. vec3_t listener_up;
  43. vec_t sound_nominal_clip_dist=1000.0;
  44. int soundtime; // sample PAIRS
  45. int paintedtime; // sample PAIRS
  46. #define MAX_SFX 512
  47. sfx_t *known_sfx; // hunk allocated [MAX_SFX]
  48. int num_sfx;
  49. sfx_t *ambient_sfx[NUM_AMBIENTS];
  50. int desired_speed = 11025;
  51. int desired_bits = 16;
  52. int sound_started=0;
  53. cvar_t bgmvolume = {"bgmvolume", "1", true};
  54. cvar_t volume = {"volume", "0.7", true};
  55. cvar_t nosound = {"nosound", "0"};
  56. cvar_t precache = {"precache", "1"};
  57. cvar_t loadas8bit = {"loadas8bit", "0"};
  58. cvar_t bgmbuffer = {"bgmbuffer", "4096"};
  59. cvar_t ambient_level = {"ambient_level", "0.3"};
  60. cvar_t ambient_fade = {"ambient_fade", "100"};
  61. cvar_t snd_noextraupdate = {"snd_noextraupdate", "0"};
  62. cvar_t snd_show = {"snd_show", "0"};
  63. cvar_t _snd_mixahead = {"_snd_mixahead", "0.1", true};
  64. // ====================================================================
  65. // User-setable variables
  66. // ====================================================================
  67. //
  68. // Fake dma is a synchronous faking of the DMA progress used for
  69. // isolating performance in the renderer. The fakedma_updates is
  70. // number of times S_Update() is called per second.
  71. //
  72. qboolean fakedma = false;
  73. int fakedma_updates = 15;
  74. void S_AmbientOff (void)
  75. {
  76. snd_ambient = false;
  77. }
  78. void S_AmbientOn (void)
  79. {
  80. snd_ambient = true;
  81. }
  82. void S_SoundInfo_f(void)
  83. {
  84. if (!sound_started || !shm)
  85. {
  86. Con_Printf ("sound system not started\n");
  87. return;
  88. }
  89. Con_Printf("%5d stereo\n", shm->channels - 1);
  90. Con_Printf("%5d samples\n", shm->samples);
  91. Con_Printf("%5d samplepos\n", shm->samplepos);
  92. Con_Printf("%5d samplebits\n", shm->samplebits);
  93. Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
  94. Con_Printf("%5d speed\n", shm->speed);
  95. Con_Printf("0x%x dma buffer\n", shm->buffer);
  96. Con_Printf("%5d total_channels\n", total_channels);
  97. }
  98. /*
  99. ================
  100. S_Startup
  101. ================
  102. */
  103. void S_Startup (void)
  104. {
  105. int rc;
  106. if (!snd_initialized)
  107. return;
  108. if (!fakedma)
  109. {
  110. rc = SNDDMA_Init();
  111. if (!rc)
  112. {
  113. #ifndef _WIN32
  114. Con_Printf("S_Startup: SNDDMA_Init failed.\n");
  115. #endif
  116. sound_started = 0;
  117. return;
  118. }
  119. }
  120. sound_started = 1;
  121. }
  122. /*
  123. ================
  124. S_Init
  125. ================
  126. */
  127. void S_Init (void)
  128. {
  129. // Con_Printf("\nSound Initialization\n");
  130. if (COM_CheckParm("-nosound"))
  131. return;
  132. if (COM_CheckParm("-simsound"))
  133. fakedma = true;
  134. Cmd_AddCommand("play", S_Play);
  135. Cmd_AddCommand("playvol", S_PlayVol);
  136. Cmd_AddCommand("stopsound", S_StopAllSoundsC);
  137. Cmd_AddCommand("soundlist", S_SoundList);
  138. Cmd_AddCommand("soundinfo", S_SoundInfo_f);
  139. Cvar_RegisterVariable(&nosound);
  140. Cvar_RegisterVariable(&volume);
  141. Cvar_RegisterVariable(&precache);
  142. Cvar_RegisterVariable(&loadas8bit);
  143. Cvar_RegisterVariable(&bgmvolume);
  144. Cvar_RegisterVariable(&bgmbuffer);
  145. Cvar_RegisterVariable(&ambient_level);
  146. Cvar_RegisterVariable(&ambient_fade);
  147. Cvar_RegisterVariable(&snd_noextraupdate);
  148. Cvar_RegisterVariable(&snd_show);
  149. Cvar_RegisterVariable(&_snd_mixahead);
  150. if (host_parms.memsize < 0x800000)
  151. {
  152. Cvar_Set ("loadas8bit", "1");
  153. Con_Printf ("loading all sounds as 8bit\n");
  154. }
  155. snd_initialized = true;
  156. S_Startup ();
  157. SND_InitScaletable ();
  158. known_sfx = Hunk_AllocName (MAX_SFX*sizeof(sfx_t), "sfx_t");
  159. num_sfx = 0;
  160. // create a piece of DMA memory
  161. if (fakedma)
  162. {
  163. shm = (void *) Hunk_AllocName(sizeof(*shm), "shm");
  164. shm->splitbuffer = 0;
  165. shm->samplebits = 16;
  166. shm->speed = 22050;
  167. shm->channels = 2;
  168. shm->samples = 32768;
  169. shm->samplepos = 0;
  170. shm->soundalive = true;
  171. shm->gamealive = true;
  172. shm->submission_chunk = 1;
  173. shm->buffer = Hunk_AllocName(1<<16, "shmbuf");
  174. }
  175. // Con_Printf ("Sound sampling rate: %i\n", shm->speed);
  176. // provides a tick sound until washed clean
  177. // if (shm->buffer)
  178. // shm->buffer[4] = shm->buffer[5] = 0x7f; // force a pop for debugging
  179. ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav");
  180. ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav");
  181. S_StopAllSounds (true);
  182. }
  183. // =======================================================================
  184. // Shutdown sound engine
  185. // =======================================================================
  186. void S_Shutdown(void)
  187. {
  188. if (!sound_started)
  189. return;
  190. if (shm)
  191. shm->gamealive = 0;
  192. shm = 0;
  193. sound_started = 0;
  194. if (!fakedma)
  195. {
  196. SNDDMA_Shutdown();
  197. }
  198. }
  199. // =======================================================================
  200. // Load a sound
  201. // =======================================================================
  202. /*
  203. ==================
  204. S_FindName
  205. ==================
  206. */
  207. sfx_t *S_FindName (char *name)
  208. {
  209. int i;
  210. sfx_t *sfx;
  211. if (!name)
  212. Sys_Error ("S_FindName: NULL\n");
  213. if (Q_strlen(name) >= MAX_QPATH)
  214. Sys_Error ("Sound name too long: %s", name);
  215. // see if already loaded
  216. for (i=0 ; i < num_sfx ; i++)
  217. if (!Q_strcmp(known_sfx[i].name, name))
  218. {
  219. return &known_sfx[i];
  220. }
  221. if (num_sfx == MAX_SFX)
  222. Sys_Error ("S_FindName: out of sfx_t");
  223. sfx = &known_sfx[i];
  224. strcpy (sfx->name, name);
  225. num_sfx++;
  226. return sfx;
  227. }
  228. /*
  229. ==================
  230. S_TouchSound
  231. ==================
  232. */
  233. void S_TouchSound (char *name)
  234. {
  235. sfx_t *sfx;
  236. if (!sound_started)
  237. return;
  238. sfx = S_FindName (name);
  239. Cache_Check (&sfx->cache);
  240. }
  241. /*
  242. ==================
  243. S_PrecacheSound
  244. ==================
  245. */
  246. sfx_t *S_PrecacheSound (char *name)
  247. {
  248. sfx_t *sfx;
  249. if (!sound_started || nosound.value)
  250. return NULL;
  251. sfx = S_FindName (name);
  252. // cache it in
  253. if (precache.value)
  254. S_LoadSound (sfx);
  255. return sfx;
  256. }
  257. //=============================================================================
  258. /*
  259. =================
  260. SND_PickChannel
  261. =================
  262. */
  263. channel_t *SND_PickChannel(int entnum, int entchannel)
  264. {
  265. int ch_idx;
  266. int first_to_die;
  267. int life_left;
  268. // Check for replacement sound, or find the best one to replace
  269. first_to_die = -1;
  270. life_left = 0x7fffffff;
  271. for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
  272. {
  273. if (entchannel != 0 // channel 0 never overrides
  274. && channels[ch_idx].entnum == entnum
  275. && (channels[ch_idx].entchannel == entchannel || entchannel == -1) )
  276. { // allways override sound from same entity
  277. first_to_die = ch_idx;
  278. break;
  279. }
  280. // don't let monster sounds override player sounds
  281. if (channels[ch_idx].entnum == cl.viewentity && entnum != cl.viewentity && channels[ch_idx].sfx)
  282. continue;
  283. if (channels[ch_idx].end - paintedtime < life_left)
  284. {
  285. life_left = channels[ch_idx].end - paintedtime;
  286. first_to_die = ch_idx;
  287. }
  288. }
  289. if (first_to_die == -1)
  290. return NULL;
  291. if (channels[first_to_die].sfx)
  292. channels[first_to_die].sfx = NULL;
  293. return &channels[first_to_die];
  294. }
  295. /*
  296. =================
  297. SND_Spatialize
  298. =================
  299. */
  300. void SND_Spatialize(channel_t *ch)
  301. {
  302. vec_t dot;
  303. vec_t dist;
  304. vec_t lscale, rscale, scale;
  305. vec3_t source_vec;
  306. sfx_t *snd;
  307. // anything coming from the view entity will allways be full volume
  308. if (ch->entnum == cl.viewentity)
  309. {
  310. ch->leftvol = ch->master_vol;
  311. ch->rightvol = ch->master_vol;
  312. return;
  313. }
  314. // calculate stereo seperation and distance attenuation
  315. snd = ch->sfx;
  316. VectorSubtract(ch->origin, listener_origin, source_vec);
  317. dist = VectorNormalize(source_vec) * ch->dist_mult;
  318. dot = DotProduct(listener_right, source_vec);
  319. if (shm->channels == 1)
  320. {
  321. rscale = 1.0;
  322. lscale = 1.0;
  323. }
  324. else
  325. {
  326. rscale = 1.0 + dot;
  327. lscale = 1.0 - dot;
  328. }
  329. // add in distance effect
  330. scale = (1.0 - dist) * rscale;
  331. ch->rightvol = (int) (ch->master_vol * scale);
  332. if (ch->rightvol < 0)
  333. ch->rightvol = 0;
  334. scale = (1.0 - dist) * lscale;
  335. ch->leftvol = (int) (ch->master_vol * scale);
  336. if (ch->leftvol < 0)
  337. ch->leftvol = 0;
  338. }
  339. // =======================================================================
  340. // Start a sound effect
  341. // =======================================================================
  342. void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
  343. {
  344. channel_t *target_chan, *check;
  345. sfxcache_t *sc;
  346. int vol;
  347. int ch_idx;
  348. int skip;
  349. if (!sound_started)
  350. return;
  351. if (!sfx)
  352. return;
  353. if (nosound.value)
  354. return;
  355. vol = fvol*255;
  356. // pick a channel to play on
  357. target_chan = SND_PickChannel(entnum, entchannel);
  358. if (!target_chan)
  359. return;
  360. // spatialize
  361. memset (target_chan, 0, sizeof(*target_chan));
  362. VectorCopy(origin, target_chan->origin);
  363. target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
  364. target_chan->master_vol = vol;
  365. target_chan->entnum = entnum;
  366. target_chan->entchannel = entchannel;
  367. SND_Spatialize(target_chan);
  368. if (!target_chan->leftvol && !target_chan->rightvol)
  369. return; // not audible at all
  370. // new channel
  371. sc = S_LoadSound (sfx);
  372. if (!sc)
  373. {
  374. target_chan->sfx = NULL;
  375. return; // couldn't load the sound's data
  376. }
  377. target_chan->sfx = sfx;
  378. target_chan->pos = 0.0;
  379. target_chan->end = paintedtime + sc->length;
  380. // if an identical sound has also been started this frame, offset the pos
  381. // a bit to keep it from just making the first one louder
  382. check = &channels[NUM_AMBIENTS];
  383. for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
  384. {
  385. if (check == target_chan)
  386. continue;
  387. if (check->sfx == sfx && !check->pos)
  388. {
  389. skip = rand () % (int)(0.1*shm->speed);
  390. if (skip >= target_chan->end)
  391. skip = target_chan->end - 1;
  392. target_chan->pos += skip;
  393. target_chan->end -= skip;
  394. break;
  395. }
  396. }
  397. }
  398. void S_StopSound(int entnum, int entchannel)
  399. {
  400. int i;
  401. for (i=0 ; i<MAX_DYNAMIC_CHANNELS ; i++)
  402. {
  403. if (channels[i].entnum == entnum
  404. && channels[i].entchannel == entchannel)
  405. {
  406. channels[i].end = 0;
  407. channels[i].sfx = NULL;
  408. return;
  409. }
  410. }
  411. }
  412. void S_StopAllSounds(qboolean clear)
  413. {
  414. int i;
  415. if (!sound_started)
  416. return;
  417. total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
  418. for (i=0 ; i<MAX_CHANNELS ; i++)
  419. if (channels[i].sfx)
  420. channels[i].sfx = NULL;
  421. Q_memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
  422. if (clear)
  423. S_ClearBuffer ();
  424. }
  425. void S_StopAllSoundsC (void)
  426. {
  427. S_StopAllSounds (true);
  428. }
  429. void S_ClearBuffer (void)
  430. {
  431. int clear;
  432. #ifdef _WIN32
  433. if (!sound_started || !shm || (!shm->buffer && !pDSBuf))
  434. #else
  435. if (!sound_started || !shm || !shm->buffer)
  436. #endif
  437. return;
  438. if (shm->samplebits == 8)
  439. clear = 0x80;
  440. else
  441. clear = 0;
  442. #ifdef _WIN32
  443. if (pDSBuf)
  444. {
  445. DWORD dwSize;
  446. DWORD *pData;
  447. int reps;
  448. HRESULT hresult;
  449. reps = 0;
  450. while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pData, &dwSize, NULL, NULL, 0)) != DS_OK)
  451. {
  452. if (hresult != DSERR_BUFFERLOST)
  453. {
  454. Con_Printf ("S_ClearBuffer: DS::Lock Sound Buffer Failed\n");
  455. S_Shutdown ();
  456. return;
  457. }
  458. if (++reps > 10000)
  459. {
  460. Con_Printf ("S_ClearBuffer: DS: couldn't restore buffer\n");
  461. S_Shutdown ();
  462. return;
  463. }
  464. }
  465. Q_memset(pData, clear, shm->samples * shm->samplebits/8);
  466. pDSBuf->lpVtbl->Unlock(pDSBuf, pData, dwSize, NULL, 0);
  467. }
  468. else
  469. #endif
  470. {
  471. Q_memset(shm->buffer, clear, shm->samples * shm->samplebits/8);
  472. }
  473. }
  474. /*
  475. =================
  476. S_StaticSound
  477. =================
  478. */
  479. void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation)
  480. {
  481. channel_t *ss;
  482. sfxcache_t *sc;
  483. if (!sfx)
  484. return;
  485. if (total_channels == MAX_CHANNELS)
  486. {
  487. Con_Printf ("total_channels == MAX_CHANNELS\n");
  488. return;
  489. }
  490. ss = &channels[total_channels];
  491. total_channels++;
  492. sc = S_LoadSound (sfx);
  493. if (!sc)
  494. return;
  495. if (sc->loopstart == -1)
  496. {
  497. Con_Printf ("Sound %s not looped\n", sfx->name);
  498. return;
  499. }
  500. ss->sfx = sfx;
  501. VectorCopy (origin, ss->origin);
  502. ss->master_vol = vol;
  503. ss->dist_mult = (attenuation/64) / sound_nominal_clip_dist;
  504. ss->end = paintedtime + sc->length;
  505. SND_Spatialize (ss);
  506. }
  507. //=============================================================================
  508. /*
  509. ===================
  510. S_UpdateAmbientSounds
  511. ===================
  512. */
  513. void S_UpdateAmbientSounds (void)
  514. {
  515. mleaf_t *l;
  516. float vol;
  517. int ambient_channel;
  518. channel_t *chan;
  519. if (!snd_ambient)
  520. return;
  521. // calc ambient sound levels
  522. if (!cl.worldmodel)
  523. return;
  524. l = Mod_PointInLeaf (listener_origin, cl.worldmodel);
  525. if (!l || !ambient_level.value)
  526. {
  527. for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
  528. channels[ambient_channel].sfx = NULL;
  529. return;
  530. }
  531. for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
  532. {
  533. chan = &channels[ambient_channel];
  534. chan->sfx = ambient_sfx[ambient_channel];
  535. vol = ambient_level.value * l->ambient_sound_level[ambient_channel];
  536. if (vol < 8)
  537. vol = 0;
  538. // don't adjust volume too fast
  539. if (chan->master_vol < vol)
  540. {
  541. chan->master_vol += host_frametime * ambient_fade.value;
  542. if (chan->master_vol > vol)
  543. chan->master_vol = vol;
  544. }
  545. else if (chan->master_vol > vol)
  546. {
  547. chan->master_vol -= host_frametime * ambient_fade.value;
  548. if (chan->master_vol < vol)
  549. chan->master_vol = vol;
  550. }
  551. chan->leftvol = chan->rightvol = chan->master_vol;
  552. }
  553. }
  554. /*
  555. ============
  556. S_Update
  557. Called once each time through the main loop
  558. ============
  559. */
  560. void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
  561. {
  562. int i, j;
  563. int total;
  564. channel_t *ch;
  565. channel_t *combine;
  566. if (!sound_started || (snd_blocked > 0))
  567. return;
  568. VectorCopy(origin, listener_origin);
  569. VectorCopy(forward, listener_forward);
  570. VectorCopy(right, listener_right);
  571. VectorCopy(up, listener_up);
  572. // update general area ambient sound sources
  573. S_UpdateAmbientSounds ();
  574. combine = NULL;
  575. // update spatialization for static and dynamic sounds
  576. ch = channels+NUM_AMBIENTS;
  577. for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
  578. {
  579. if (!ch->sfx)
  580. continue;
  581. SND_Spatialize(ch); // respatialize channel
  582. if (!ch->leftvol && !ch->rightvol)
  583. continue;
  584. // try to combine static sounds with a previous channel of the same
  585. // sound effect so we don't mix five torches every frame
  586. if (i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
  587. {
  588. // see if it can just use the last one
  589. if (combine && combine->sfx == ch->sfx)
  590. {
  591. combine->leftvol += ch->leftvol;
  592. combine->rightvol += ch->rightvol;
  593. ch->leftvol = ch->rightvol = 0;
  594. continue;
  595. }
  596. // search for one
  597. combine = channels+MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
  598. for (j=MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS ; j<i; j++, combine++)
  599. if (combine->sfx == ch->sfx)
  600. break;
  601. if (j == total_channels)
  602. {
  603. combine = NULL;
  604. }
  605. else
  606. {
  607. if (combine != ch)
  608. {
  609. combine->leftvol += ch->leftvol;
  610. combine->rightvol += ch->rightvol;
  611. ch->leftvol = ch->rightvol = 0;
  612. }
  613. continue;
  614. }
  615. }
  616. }
  617. //
  618. // debugging output
  619. //
  620. if (snd_show.value)
  621. {
  622. total = 0;
  623. ch = channels;
  624. for (i=0 ; i<total_channels; i++, ch++)
  625. if (ch->sfx && (ch->leftvol || ch->rightvol) )
  626. {
  627. //Con_Printf ("%3i %3i %s\n", ch->leftvol, ch->rightvol, ch->sfx->name);
  628. total++;
  629. }
  630. Con_Printf ("----(%i)----\n", total);
  631. }
  632. // mix some sound
  633. S_Update_();
  634. }
  635. void GetSoundtime(void)
  636. {
  637. int samplepos;
  638. static int buffers;
  639. static int oldsamplepos;
  640. int fullsamples;
  641. fullsamples = shm->samples / shm->channels;
  642. // it is possible to miscount buffers if it has wrapped twice between
  643. // calls to S_Update. Oh well.
  644. samplepos = SNDDMA_GetDMAPos();
  645. if (samplepos < oldsamplepos)
  646. {
  647. buffers++; // buffer wrapped
  648. if (paintedtime > 0x40000000)
  649. { // time to chop things off to avoid 32 bit limits
  650. buffers = 0;
  651. paintedtime = fullsamples;
  652. S_StopAllSounds (true);
  653. }
  654. }
  655. oldsamplepos = samplepos;
  656. soundtime = buffers*fullsamples + samplepos/shm->channels;
  657. }
  658. void S_ExtraUpdate (void)
  659. {
  660. #ifdef _WIN32
  661. IN_Accumulate ();
  662. #endif
  663. if (snd_noextraupdate.value)
  664. return; // don't pollute timings
  665. S_Update_();
  666. }
  667. void S_Update_(void)
  668. {
  669. unsigned endtime;
  670. int samps;
  671. if (!sound_started || (snd_blocked > 0))
  672. return;
  673. // Updates DMA time
  674. GetSoundtime();
  675. // check to make sure that we haven't overshot
  676. if (paintedtime < soundtime)
  677. {
  678. //Con_Printf ("S_Update_ : overflow\n");
  679. paintedtime = soundtime;
  680. }
  681. // mix ahead of current position
  682. endtime = soundtime + _snd_mixahead.value * shm->speed;
  683. samps = shm->samples >> (shm->channels-1);
  684. if (endtime - soundtime > samps)
  685. endtime = soundtime + samps;
  686. #ifdef _WIN32
  687. // if the buffer was lost or stopped, restore it and/or restart it
  688. {
  689. DWORD dwStatus;
  690. if (pDSBuf)
  691. {
  692. if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK)
  693. Con_Printf ("Couldn't get sound buffer status\n");
  694. if (dwStatus & DSBSTATUS_BUFFERLOST)
  695. pDSBuf->lpVtbl->Restore (pDSBuf);
  696. if (!(dwStatus & DSBSTATUS_PLAYING))
  697. pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
  698. }
  699. }
  700. #endif
  701. S_PaintChannels (endtime);
  702. SNDDMA_Submit ();
  703. }
  704. /*
  705. ===============================================================================
  706. console functions
  707. ===============================================================================
  708. */
  709. void S_Play(void)
  710. {
  711. static int hash=345;
  712. int i;
  713. char name[256];
  714. sfx_t *sfx;
  715. i = 1;
  716. while (i<Cmd_Argc())
  717. {
  718. if (!Q_strrchr(Cmd_Argv(i), '.'))
  719. {
  720. Q_strcpy(name, Cmd_Argv(i));
  721. Q_strcat(name, ".wav");
  722. }
  723. else
  724. Q_strcpy(name, Cmd_Argv(i));
  725. sfx = S_PrecacheSound(name);
  726. S_StartSound(hash++, 0, sfx, listener_origin, 1.0, 1.0);
  727. i++;
  728. }
  729. }
  730. void S_PlayVol(void)
  731. {
  732. static int hash=543;
  733. int i;
  734. float vol;
  735. char name[256];
  736. sfx_t *sfx;
  737. i = 1;
  738. while (i<Cmd_Argc())
  739. {
  740. if (!Q_strrchr(Cmd_Argv(i), '.'))
  741. {
  742. Q_strcpy(name, Cmd_Argv(i));
  743. Q_strcat(name, ".wav");
  744. }
  745. else
  746. Q_strcpy(name, Cmd_Argv(i));
  747. sfx = S_PrecacheSound(name);
  748. vol = Q_atof(Cmd_Argv(i+1));
  749. S_StartSound(hash++, 0, sfx, listener_origin, vol, 1.0);
  750. i+=2;
  751. }
  752. }
  753. void S_SoundList(void)
  754. {
  755. int i;
  756. sfx_t *sfx;
  757. sfxcache_t *sc;
  758. int size, total;
  759. total = 0;
  760. for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
  761. {
  762. sc = Cache_Check (&sfx->cache);
  763. if (!sc)
  764. continue;
  765. size = sc->length*sc->width*(sc->stereo+1);
  766. total += size;
  767. if (sc->loopstart >= 0)
  768. Con_Printf ("L");
  769. else
  770. Con_Printf (" ");
  771. Con_Printf("(%2db) %6i : %s\n",sc->width*8, size, sfx->name);
  772. }
  773. Con_Printf ("Total resident: %i\n", total);
  774. }
  775. void S_LocalSound (char *sound)
  776. {
  777. sfx_t *sfx;
  778. if (nosound.value)
  779. return;
  780. if (!sound_started)
  781. return;
  782. sfx = S_PrecacheSound (sound);
  783. if (!sfx)
  784. {
  785. Con_Printf ("S_LocalSound: can't cache %s\n", sound);
  786. return;
  787. }
  788. S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
  789. }
  790. void S_ClearPrecache (void)
  791. {
  792. }
  793. void S_BeginPrecaching (void)
  794. {
  795. }
  796. void S_EndPrecaching (void)
  797. {
  798. }