Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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