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