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