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