]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_dma.c
4f760acc917c4083434abc34b49b37f96e1155e8
[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 = {"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         if (!snd_ambient)
684                 return;
685
686 // calc ambient sound levels
687         if (!cl.worldmodel)
688                 return;
689
690         l = Mod_PointInLeaf (listener_origin, cl.worldmodel);
691         if (!l || !ambient_level.value)
692         {
693                 for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
694                         channels[ambient_channel].sfx = NULL;
695                 return;
696         }
697
698         for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
699         {
700                 chan = &channels[ambient_channel];      
701                 chan->sfx = ambient_sfx[ambient_channel];
702         
703                 vol = ambient_level.value * l->ambient_sound_level[ambient_channel];
704                 if (vol < 8)
705                         vol = 0;
706
707         // don't adjust volume too fast
708                 if (chan->master_vol < vol)
709                 {
710                         chan->master_vol += host_realframetime * ambient_fade.value;
711                         if (chan->master_vol > vol)
712                                 chan->master_vol = vol;
713                 }
714                 else if (chan->master_vol > vol)
715                 {
716                         chan->master_vol -= host_realframetime * ambient_fade.value;
717                         if (chan->master_vol < vol)
718                                 chan->master_vol = vol;
719                 }
720                 
721                 chan->leftvol = chan->rightvol = chan->master_vol;
722         }
723 }
724
725
726 /*
727 ============
728 S_Update
729
730 Called once each time through the main loop
731 ============
732 */
733 void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
734 {
735         int                     i, j;
736         int                     total;
737         channel_t       *ch;
738         channel_t       *combine;
739
740         if (!sound_started || (snd_blocked > 0))
741                 return;
742
743         VectorCopy(origin, listener_origin);
744         VectorCopy(forward, listener_forward);
745         VectorCopy(right, listener_right);
746         VectorCopy(up, listener_up);
747         
748 // update general area ambient sound sources
749         S_UpdateAmbientSounds ();
750
751         combine = NULL;
752
753 // update spatialization for static and dynamic sounds  
754         ch = channels+NUM_AMBIENTS;
755         for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
756         {
757                 if (!ch->sfx)
758                         continue;
759                 SND_Spatialize(ch);         // respatialize channel
760                 if (!ch->leftvol && !ch->rightvol)
761                         continue;
762
763         // try to combine static sounds with a previous channel of the same
764         // sound effect so we don't mix five torches every frame
765         
766                 if (i > MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
767                 {
768                 // see if it can just use the last one
769                         if (combine && combine->sfx == ch->sfx)
770                         {
771                                 combine->leftvol += ch->leftvol;
772                                 combine->rightvol += ch->rightvol;
773                                 ch->leftvol = ch->rightvol = 0;
774                                 continue;
775                         }
776                 // search for one
777                         combine = channels+MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
778                         for (j=MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS ; j<i; j++, combine++)
779                                 if (combine->sfx == ch->sfx)
780                                         break;
781                                         
782                         if (j == total_channels)
783                         {
784                                 combine = NULL;
785                         }
786                         else
787                         {
788                                 if (combine != ch)
789                                 {
790                                         combine->leftvol += ch->leftvol;
791                                         combine->rightvol += ch->rightvol;
792                                         ch->leftvol = ch->rightvol = 0;
793                                 }
794                                 continue;
795                         }
796                 }
797                 
798                 
799         }
800
801 //
802 // debugging output
803 //
804         if (snd_show.value)
805         {
806                 total = 0;
807                 ch = channels;
808                 for (i=0 ; i<total_channels; i++, ch++)
809                         if (ch->sfx && (ch->leftvol || ch->rightvol) )
810                         {
811                                 //Con_Printf ("%3i %3i %s\n", ch->leftvol, ch->rightvol, ch->sfx->name);
812                                 total++;
813                         }
814                 
815                 Con_Printf ("----(%i)----\n", total);
816         }
817
818 // mix some sound
819         S_Update_();
820 }
821
822 void GetSoundtime(void)
823 {
824         int             samplepos;
825         static  int             buffers;
826         static  int             oldsamplepos;
827         int             fullsamples;
828         
829         fullsamples = shm->samples / shm->channels;
830
831 // it is possible to miscount buffers if it has wrapped twice between
832 // calls to S_Update.  Oh well.
833 #ifdef __sun__
834         soundtime = SNDDMA_GetSamples();
835 #else
836         samplepos = SNDDMA_GetDMAPos();
837
838
839         if (samplepos < oldsamplepos)
840         {
841                 buffers++;                                      // buffer wrapped
842                 
843                 if (paintedtime > 0x40000000)
844                 {       // time to chop things off to avoid 32 bit limits
845                         buffers = 0;
846                         paintedtime = fullsamples;
847                         S_StopAllSounds (true);
848                 }
849         }
850         oldsamplepos = samplepos;
851
852         soundtime = buffers*fullsamples + samplepos/shm->channels;
853 #endif
854 }
855
856 void IN_Accumulate (void);
857
858 void S_ExtraUpdate (void)
859 {
860
861 #ifdef _WIN32
862         IN_Accumulate ();
863 #endif
864
865         if (snd_noextraupdate.value)
866                 return;         // don't pollute timings
867         S_Update_();
868 }
869
870 void S_Update_(void)
871 {
872         unsigned        endtime;
873         int                             samps;
874         
875         if (!sound_started || (snd_blocked > 0))
876                 return;
877
878 // Updates DMA time
879         GetSoundtime();
880
881 // check to make sure that we haven't overshot
882         if (paintedtime < soundtime)
883         {
884                 //Con_Printf ("S_Update_ : overflow\n");
885                 paintedtime = soundtime;
886         }
887
888 // mix ahead of current position
889         endtime = soundtime + _snd_mixahead.value * shm->speed;
890         samps = shm->samples >> (shm->channels-1);
891         if (endtime - soundtime > samps)
892                 endtime = soundtime + samps;
893
894 #ifdef _WIN32
895 // if the buffer was lost or stopped, restore it and/or restart it
896         {
897                 DWORD   dwStatus;
898
899                 if (pDSBuf)
900                 {
901                         if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK)
902                                 Con_Printf ("Couldn't get sound buffer status\n");
903                         
904                         if (dwStatus & DSBSTATUS_BUFFERLOST)
905                                 pDSBuf->lpVtbl->Restore (pDSBuf);
906                         
907                         if (!(dwStatus & DSBSTATUS_PLAYING))
908                                 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
909                 }
910         }
911 #endif
912
913         S_PaintChannels (endtime);
914
915         SNDDMA_Submit ();
916 }
917
918 /*
919 ===============================================================================
920
921 console functions
922
923 ===============================================================================
924 */
925
926 void S_Play(void)
927 {
928         static int hash=345;
929         int     i;
930         char name[256];
931         sfx_t   *sfx;
932         
933         i = 1;
934         while (i<Cmd_Argc())
935         {
936                 if (!strrchr(Cmd_Argv(i), '.'))
937                 {
938                         strcpy(name, Cmd_Argv(i));
939                         strcat(name, ".wav");
940                 }
941                 else
942                         strcpy(name, Cmd_Argv(i));
943                 sfx = S_PrecacheSound(name);
944                 S_StartSound(hash++, 0, sfx, listener_origin, 1.0, 1.0);
945                 i++;
946         }
947 }
948
949 void S_Play2(void)
950 {
951         static int hash=345;
952         int     i;
953         char name[256];
954         sfx_t   *sfx;
955         
956         i = 1;
957         while (i<Cmd_Argc())
958         {
959                 if (!strrchr(Cmd_Argv(i), '.'))
960                 {
961                         strcpy(name, Cmd_Argv(i));
962                         strcat(name, ".wav");
963                 }
964                 else
965                         strcpy(name, Cmd_Argv(i));
966                 sfx = S_PrecacheSound(name);
967                 S_StartSound(hash++, 0, sfx, listener_origin, 1.0, 0.0);
968                 i++;
969         }
970 }
971
972 void S_PlayVol(void)
973 {
974         static int hash=543;
975         int i;
976         float vol;
977         char name[256];
978         sfx_t   *sfx;
979         
980         i = 1;
981         while (i<Cmd_Argc())
982         {
983                 if (!strrchr(Cmd_Argv(i), '.'))
984                 {
985                         strcpy(name, Cmd_Argv(i));
986                         strcat(name, ".wav");
987                 }
988                 else
989                         strcpy(name, Cmd_Argv(i));
990                 sfx = S_PrecacheSound(name);
991                 vol = atof(Cmd_Argv(i+1));
992                 S_StartSound(hash++, 0, sfx, listener_origin, vol, 1.0);
993                 i+=2;
994         }
995 }
996
997 void S_SoundList(void)
998 {
999         int             i;
1000         sfx_t   *sfx;
1001         sfxcache_t      *sc;
1002         int             size, total;
1003
1004         total = 0;
1005         for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
1006         {
1007                 sc = Cache_Check (&sfx->cache);
1008                 if (!sc)
1009                         continue;
1010                 size = sc->length*sc->width*(sc->stereo+1);
1011                 total += size;
1012                 if (sc->loopstart >= 0)
1013                         Con_Printf ("L");
1014                 else
1015                         Con_Printf (" ");
1016                 Con_Printf("(%2db) %6i : %s\n",sc->width*8,  size, sfx->name);
1017         }
1018         Con_Printf ("Total resident: %i\n", total);
1019 }
1020
1021
1022 void S_LocalSound (char *sound)
1023 {
1024         sfx_t   *sfx;
1025
1026         if (nosound.value)
1027                 return;
1028         if (!sound_started)
1029                 return;
1030                 
1031         sfx = S_PrecacheSound (sound);
1032         if (!sfx)
1033         {
1034                 Con_Printf ("S_LocalSound: can't cache %s\n", sound);
1035                 return;
1036         }
1037         S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
1038 }
1039
1040
1041 void S_ClearPrecache (void)
1042 {
1043 }
1044
1045
1046 void S_BeginPrecaching (void)
1047 {
1048 }
1049
1050
1051 void S_EndPrecaching (void)
1052 {
1053 }
1054