]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - snd_dma.c
build 78
[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             desired_speed = 44100; //11025; // LordHavoc: 44.1khz sound
68 int             desired_bits = 16;
69
70 int sound_started=0;
71
72 cvar_t bgmvolume = {"bgmvolume", "1", true};
73 cvar_t volume = {"volume", "0.7", true};
74
75 cvar_t nosound = {"nosound", "0"};
76 cvar_t precache = {"precache", "1"};
77 //cvar_t loadas8bit = {"loadas8bit", "0"};
78 cvar_t bgmbuffer = {"bgmbuffer", "4096"};
79 cvar_t ambient_level = {"ambient_level", "0.3"};
80 cvar_t ambient_fade = {"ambient_fade", "100"};
81 cvar_t snd_noextraupdate = {"snd_noextraupdate", "0"};
82 cvar_t snd_show = {"snd_show", "0"};
83 cvar_t _snd_mixahead = {"_snd_mixahead", "0.1", true};
84 cvar_t snd_swapstereo = {"snd_swapstereo", "0", true};
85
86
87 // ====================================================================
88 // User-setable variables
89 // ====================================================================
90
91
92 //
93 // Fake dma is a synchronous faking of the DMA progress used for
94 // isolating performance in the renderer.  The fakedma_updates is
95 // number of times S_Update() is called per second.
96 //
97
98 qboolean fakedma = false;
99 int fakedma_updates = 15;
100
101
102 void S_AmbientOff (void)
103 {
104         snd_ambient = false;
105 }
106
107
108 void S_AmbientOn (void)
109 {
110         snd_ambient = true;
111 }
112
113
114 void S_SoundInfo_f(void)
115 {
116         if (!sound_started || !shm)
117         {
118                 Con_Printf ("sound system not started\n");
119                 return;
120         }
121         
122     Con_Printf("%5d stereo\n", shm->channels - 1);
123     Con_Printf("%5d samples\n", shm->samples);
124     Con_Printf("%5d samplepos\n", shm->samplepos);
125     Con_Printf("%5d samplebits\n", shm->samplebits);
126     Con_Printf("%5d submission_chunk\n", shm->submission_chunk);
127     Con_Printf("%5d speed\n", shm->speed);
128     Con_Printf("0x%x dma buffer\n", shm->buffer);
129         Con_Printf("%5d total_channels\n", total_channels);
130 }
131
132
133 /*
134 ================
135 S_Startup
136 ================
137 */
138
139 void S_Startup (void)
140 {
141         int             rc;
142
143         if (!snd_initialized)
144                 return;
145
146         if (!fakedma)
147         {
148                 rc = SNDDMA_Init();
149
150                 if (!rc)
151                 {
152 #ifndef _WIN32
153                         Con_Printf("S_Startup: SNDDMA_Init failed.\n");
154 #endif
155                         sound_started = 0;
156                         return;
157                 }
158         }
159
160         sound_started = 1;
161 }
162
163
164 void S_Play2(void);
165
166 /*
167 ================
168 S_Init
169 ================
170 */
171 void S_Init (void)
172 {
173
174         Con_Printf("\nSound Initialization\n");
175
176         if (COM_CheckParm("-nosound"))
177                 return;
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(&volume);
191         Cvar_RegisterVariable(&precache);
192 //      Cvar_RegisterVariable(&loadas8bit);
193         Cvar_RegisterVariable(&bgmvolume);
194         Cvar_RegisterVariable(&bgmbuffer);
195         Cvar_RegisterVariable(&ambient_level);
196         Cvar_RegisterVariable(&ambient_fade);
197         Cvar_RegisterVariable(&snd_noextraupdate);
198         Cvar_RegisterVariable(&snd_show);
199         Cvar_RegisterVariable(&_snd_mixahead);
200         Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
201
202         /*
203         if (host_parms.memsize < 0x800000)
204         {
205                 Cvar_Set ("loadas8bit", "1");
206                 Con_Printf ("loading all sounds as 8bit\n");
207         }
208         */
209
210
211
212         snd_initialized = true;
213
214         S_Startup ();
215
216         SND_InitScaletable ();
217
218         known_sfx = Hunk_AllocName (MAX_SFX*sizeof(sfx_t), "sfx_t");
219         num_sfx = 0;
220
221 // create a piece of DMA memory
222
223         if (fakedma)
224         {
225                 shm = (void *) Hunk_AllocName(sizeof(*shm), "shm");
226                 shm->splitbuffer = 0;
227                 shm->samplebits = 16;
228                 shm->speed = 22050;
229                 shm->channels = 2;
230                 shm->samples = 32768;
231                 shm->samplepos = 0;
232                 shm->soundalive = true;
233                 shm->gamealive = true;
234                 shm->submission_chunk = 1;
235                 shm->buffer = Hunk_AllocName(1<<16, "shmbuf");
236         }
237
238         if (!sound_started)
239                 return;
240
241         Con_Printf ("Sound sampling rate: %i\n", shm->speed);
242
243         // provides a tick sound until washed clean
244
245 //      if (shm->buffer)
246 //              shm->buffer[4] = shm->buffer[5] = 0x7f; // force a pop for debugging
247
248         ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav");
249         ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav");
250
251         S_StopAllSounds (true);
252 }
253
254
255 // =======================================================================
256 // Shutdown sound engine
257 // =======================================================================
258
259 void S_Shutdown(void)
260 {
261
262         if (!sound_started)
263                 return;
264
265         if (shm)
266                 shm->gamealive = 0;
267
268         shm = 0;
269         sound_started = 0;
270
271         if (!fakedma)
272         {
273                 SNDDMA_Shutdown();
274         }
275 }
276
277
278 // =======================================================================
279 // Load a sound
280 // =======================================================================
281
282 /*
283 ==================
284 S_FindName
285
286 ==================
287 */
288 sfx_t *S_FindName (char *name)
289 {
290         int             i;
291         sfx_t   *sfx;
292
293         if (!name)
294                 Host_Error ("S_FindName: NULL\n");
295
296         if (strlen(name) >= MAX_QPATH)
297                 Host_Error ("Sound name too long: %s", name);
298
299 // see if already loaded
300         for (i=0 ; i < num_sfx ; i++)
301                 if (!strcmp(known_sfx[i].name, name))
302                 {
303                         return &known_sfx[i];
304                 }
305
306         if (num_sfx == MAX_SFX)
307                 Sys_Error ("S_FindName: out of sfx_t");
308         
309         sfx = &known_sfx[i];
310         strcpy (sfx->name, name);
311
312         num_sfx++;
313         
314         return sfx;
315 }
316
317
318 /*
319 ==================
320 S_TouchSound
321
322 ==================
323 */
324 void S_TouchSound (char *name)
325 {
326         sfx_t   *sfx;
327         
328         if (!sound_started)
329                 return;
330
331         sfx = S_FindName (name);
332         Cache_Check (&sfx->cache);
333 }
334
335 /*
336 ==================
337 S_PrecacheSound
338
339 ==================
340 */
341 sfx_t *S_PrecacheSound (char *name)
342 {
343         sfx_t   *sfx;
344
345         if (!sound_started || nosound.value)
346                 return NULL;
347
348         sfx = S_FindName (name);
349         
350 // cache it in
351         if (precache.value)
352                 S_LoadSound (sfx);
353         
354         return sfx;
355 }
356
357
358 //=============================================================================
359
360 /*
361 =================
362 SND_PickChannel
363 =================
364 */
365 channel_t *SND_PickChannel(int entnum, int entchannel)
366 {
367     int ch_idx;
368     int first_to_die;
369     int life_left;
370
371 // Check for replacement sound, or find the best one to replace
372     first_to_die = -1;
373     life_left = 0x7fffffff;
374     for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
375     {
376                 if (entchannel != 0             // channel 0 never overrides
377                 && channels[ch_idx].entnum == entnum
378                 && (channels[ch_idx].entchannel == entchannel || entchannel == -1) )
379                 {       // allways override sound from same entity
380                         first_to_die = ch_idx;
381                         break;
382                 }
383
384                 // don't let monster sounds override player sounds
385                 if (channels[ch_idx].entnum == cl.viewentity && entnum != cl.viewentity && channels[ch_idx].sfx)
386                         continue;
387
388                 if (channels[ch_idx].end - paintedtime < life_left)
389                 {
390                         life_left = channels[ch_idx].end - paintedtime;
391                         first_to_die = ch_idx;
392                 }
393    }
394
395         if (first_to_die == -1)
396                 return NULL;
397
398         if (channels[first_to_die].sfx)
399                 channels[first_to_die].sfx = NULL;
400
401     return &channels[first_to_die];    
402 }       
403
404 /*
405 =================
406 SND_Spatialize
407 =================
408 */
409 void SND_Spatialize(channel_t *ch)
410 {
411     vec_t dot;
412     vec_t dist;
413     vec_t lscale, rscale, scale;
414     vec3_t source_vec;
415         sfx_t *snd;
416
417 // anything coming from the view entity will allways be full volume
418 // LordHavoc: make sounds with ATTN_NONE have no spatialization
419         if (ch->entnum == cl.viewentity || ch->dist_mult == 0)
420         {
421                 ch->leftvol = ch->master_vol;
422                 ch->rightvol = ch->master_vol;
423                 return;
424         }
425
426 // calculate stereo seperation and distance attenuation
427
428         snd = ch->sfx;
429         VectorSubtract(ch->origin, listener_origin, source_vec);
430         
431         dist = VectorNormalizeLength(source_vec) * ch->dist_mult;
432         
433         dot = DotProduct(listener_right, source_vec);
434
435         if (shm->channels == 1)
436         {
437                 rscale = 1.0;
438                 lscale = 1.0;
439         }
440         else
441         {
442                 rscale = 1.0 + dot;
443                 lscale = 1.0 - dot;
444         }
445
446 // add in distance effect
447         scale = (1.0 - dist) * rscale;
448         ch->rightvol = (int) (ch->master_vol * scale);
449         if (ch->rightvol < 0)
450                 ch->rightvol = 0;
451
452         scale = (1.0 - dist) * lscale;
453         ch->leftvol = (int) (ch->master_vol * scale);
454         if (ch->leftvol < 0)
455                 ch->leftvol = 0;
456 }           
457
458
459 // =======================================================================
460 // Start a sound effect
461 // =======================================================================
462
463 void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
464 {
465         channel_t *target_chan, *check;
466         sfxcache_t      *sc;
467         int             vol;
468         int             ch_idx;
469         int             skip;
470
471         if (!sound_started)
472                 return;
473
474         if (!sfx)
475                 return;
476
477         if (nosound.value)
478                 return;
479
480         vol = fvol*255;
481
482 // pick a channel to play on
483         target_chan = SND_PickChannel(entnum, entchannel);
484         if (!target_chan)
485                 return;
486                 
487 // spatialize
488         memset (target_chan, 0, sizeof(*target_chan));
489         VectorCopy(origin, target_chan->origin);
490         target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
491         target_chan->master_vol = vol;
492         target_chan->entnum = entnum;
493         target_chan->entchannel = entchannel;
494         SND_Spatialize(target_chan);
495
496         if (!target_chan->leftvol && !target_chan->rightvol)
497                 return;         // not audible at all
498
499 // new channel
500         sc = S_LoadSound (sfx);
501         if (!sc)
502         {
503                 target_chan->sfx = NULL;
504                 return;         // couldn't load the sound's data
505         }
506
507         target_chan->sfx = sfx;
508         target_chan->pos = 0.0;
509     target_chan->end = paintedtime + sc->length;        
510
511 // if an identical sound has also been started this frame, offset the pos
512 // a bit to keep it from just making the first one louder
513         check = &channels[NUM_AMBIENTS];
514     for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
515     {
516                 if (check == target_chan)
517                         continue;
518                 if (check->sfx == sfx && !check->pos)
519                 {
520                         skip = rand () % (int)(0.1*shm->speed);
521                         if (skip >= target_chan->end)
522                                 skip = target_chan->end - 1;
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_frametime * 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_frametime * 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