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