2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // snd_dma.c -- main control for any streaming sound output device
27 extern DWORD gSndBufSize;
28 extern LPDIRECTSOUND pDS;
29 extern LPDIRECTSOUNDBUFFER pDSBuf;
38 void S_SoundList(void);
40 void S_StopAllSounds(qboolean clear);
41 void S_StopAllSoundsC(void);
43 void S_ClearBuffer (void);
46 // =======================================================================
47 // Internal sound data & structures
48 // =======================================================================
50 channel_t channels[MAX_CHANNELS];
51 unsigned int total_channels;
54 cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
55 cvar_t snd_streaming = { CVAR_SAVE, "snd_streaming", "1"};
57 // pointer should go away
58 volatile dma_t *shm = 0;
61 vec3_t listener_vieworigin;
62 vec3_t listener_viewforward;
63 vec3_t listener_viewleft;
64 vec3_t listener_viewup;
65 vec_t sound_nominal_clip_dist=1000.0;
66 mempool_t *snd_mempool;
73 //LordHavoc: increased the client sound limit from 512 to 4096 for the Nehahra movie
75 sfx_t *known_sfx; // allocated [MAX_SFX]
78 sfx_t *ambient_sfx[NUM_AMBIENTS];
80 int sound_started = 0;
82 cvar_t bgmvolume = {CVAR_SAVE, "bgmvolume", "1"};
83 cvar_t volume = {CVAR_SAVE, "volume", "0.7"};
84 cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1"};
86 cvar_t nosound = {0, "nosound", "0"};
87 cvar_t snd_precache = {0, "snd_precache", "1"};
88 cvar_t bgmbuffer = {0, "bgmbuffer", "4096"};
89 cvar_t ambient_level = {0, "ambient_level", "0.3"};
90 cvar_t ambient_fade = {0, "ambient_fade", "100"};
91 cvar_t snd_noextraupdate = {0, "snd_noextraupdate", "0"};
92 cvar_t snd_show = {0, "snd_show", "0"};
93 cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.1"};
94 cvar_t snd_swapstereo = {CVAR_SAVE, "snd_swapstereo", "0"};
97 // ====================================================================
98 // User-setable variables
99 // ====================================================================
103 // Fake dma is a synchronous faking of the DMA progress used for
104 // isolating performance in the renderer. The fakedma_updates is
105 // number of times S_Update() is called per second.
108 qboolean fakedma = false;
109 int fakedma_updates = 15;
112 void S_SoundInfo_f(void)
114 if (!sound_started || !shm)
116 Con_Print("sound system not started\n");
120 Con_Printf("%5d stereo\n", shm->format.channels - 1);
121 Con_Printf("%5d samples\n", shm->samples);
122 Con_Printf("%5d samplepos\n", shm->samplepos);
123 Con_Printf("%5d samplebits\n", shm->format.width * 8);
124 Con_Printf("%5d speed\n", shm->format.speed);
125 Con_Printf("%p dma buffer\n", shm->buffer);
126 Con_Printf("%5u total_channels\n", total_channels);
129 void S_UnloadSounds(void)
132 for (i = 0;i < num_sfx;i++)
133 S_UnloadSound(known_sfx + i);
136 void S_LoadSounds(void)
139 for (i = 0;i < num_sfx;i++)
140 S_LoadSound(known_sfx + i, false);
145 if (!snd_initialized.integer)
149 memset((void *)shm, 0, sizeof(*shm));
151 // create a piece of DMA memory
155 shm->format.width = 2;
156 shm->format.speed = 22050;
157 shm->format.channels = 2;
158 shm->samples = 32768;
160 shm->buffer = Mem_Alloc(snd_mempool, shm->format.channels * shm->samples * shm->format.width);
166 Con_Print("S_Startup: SNDDMA_Init failed.\n");
175 Con_DPrintf("Sound sampling rate: %i\n", shm->format.speed);
179 S_StopAllSounds(true);
182 void S_Shutdown(void)
190 Mem_Free(shm->buffer);
198 void S_Restart_f(void)
211 Con_DPrint("\nSound Initialization\n");
213 S_RawSamples_ClearQueue();
215 Cvar_RegisterVariable(&volume);
216 Cvar_RegisterVariable(&bgmvolume);
217 Cvar_RegisterVariable(&snd_staticvolume);
219 if (COM_CheckParm("-nosound") || COM_CheckParm("-safe"))
222 snd_mempool = Mem_AllocPool("sound");
224 if (COM_CheckParm("-simsound"))
227 Cmd_AddCommand("play", S_Play);
228 Cmd_AddCommand("play2", S_Play2);
229 Cmd_AddCommand("playvol", S_PlayVol);
230 Cmd_AddCommand("stopsound", S_StopAllSoundsC);
231 Cmd_AddCommand("soundlist", S_SoundList);
232 Cmd_AddCommand("soundinfo", S_SoundInfo_f);
233 Cmd_AddCommand("snd_restart", S_Restart_f);
235 Cvar_RegisterVariable(&nosound);
236 Cvar_RegisterVariable(&snd_precache);
237 Cvar_RegisterVariable(&snd_initialized);
238 Cvar_RegisterVariable(&snd_streaming);
239 Cvar_RegisterVariable(&bgmbuffer);
240 Cvar_RegisterVariable(&ambient_level);
241 Cvar_RegisterVariable(&ambient_fade);
242 Cvar_RegisterVariable(&snd_noextraupdate);
243 Cvar_RegisterVariable(&snd_show);
244 Cvar_RegisterVariable(&_snd_mixahead);
245 Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
247 Cvar_SetValueQuick(&snd_initialized, true);
249 known_sfx = Mem_Alloc(snd_mempool, MAX_SFX*sizeof(sfx_t));
252 SND_InitScaletable ();
254 ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav", false, true);
255 ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav", false, true);
257 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
258 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
264 // =======================================================================
266 // =======================================================================
274 sfx_t *S_GetCached (const char *name, qboolean stdpath)
276 char namebuffer [MAX_QPATH];
280 if (!snd_initialized.integer)
284 Host_Error("S_GetCached: NULL");
286 // Add the default sound directory to the path
287 len = snprintf (namebuffer, sizeof (namebuffer), stdpath ? "sound/%s" : "%s", name);
288 if (len >= sizeof (namebuffer))
289 Host_Error ("S_GetCached: sound name too long (%s)", name);
291 for(i = 0;i < num_sfx;i++)
292 if(!strcmp(known_sfx[i].name, namebuffer))
293 return &known_sfx[i];
304 sfx_t *S_FindName (const char *name, qboolean stdpath)
308 sfx = S_GetCached (name, stdpath);
310 // If we haven't allocated a sfx_t struct for it yet
313 if (num_sfx == MAX_SFX)
314 Sys_Error ("S_FindName: out of sfx_t");
316 sfx = &known_sfx[num_sfx++];
317 memset (sfx, 0, sizeof(*sfx));
318 snprintf (sfx->name, sizeof (sfx->name), stdpath ? "sound/%s" : "%s", name);
330 void S_TouchSound (const char *name, qboolean stdpath)
334 sfx = S_FindName (name, stdpath);
336 // Set the "used" flag for this sound
338 sfx->flags |= SFXFLAG_USED;
346 Reset the "used" flag of all precached sounds
349 void S_ClearUsed (void)
353 for (i = 0; i < num_sfx; i++)
354 known_sfx[i].flags &= ~SFXFLAG_USED;
362 Free all precached sounds without the "used" flag
365 void S_PurgeUnused (void)
370 for (i = 0; i < num_sfx; i++)
373 if (! (sfx->flags & SFXFLAG_USED))
385 sfx_t *S_PrecacheSound (const char *name, qboolean complain, qboolean stdpath)
389 if (!snd_initialized.integer)
392 sfx = S_FindName (name, stdpath);
394 if (!nosound.integer && snd_precache.integer)
395 S_LoadSound(sfx, complain);
401 //=============================================================================
407 Picks a channel based on priorities, empty slots, number of channels
410 channel_t *SND_PickChannel(int entnum, int entchannel)
417 // Check for replacement sound, or find the best one to replace
419 life_left = 0x7fffffff;
420 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
422 ch = &channels[ch_idx];
423 if (entchannel != 0 // channel 0 never overrides
424 && ch->entnum == entnum
425 && (ch->entchannel == entchannel || entchannel == -1) )
426 { // always override sound from same entity
427 first_to_die = ch_idx;
431 // don't let monster sounds override player sounds
432 if (ch->entnum == cl.viewentity && entnum != cl.viewentity && ch->sfx)
435 // don't override looped sounds
436 if ((ch->flags & CHANNELFLAG_FORCELOOP) != 0 ||
437 (ch->sfx != NULL && ch->sfx->loopstart >= 0))
440 if (ch->end - paintedtime < life_left)
442 life_left = ch->end - paintedtime;
443 first_to_die = ch_idx;
447 if (first_to_die == -1)
450 if (channels[first_to_die].sfx)
451 channels[first_to_die].sfx = NULL;
453 return &channels[first_to_die];
460 Spatializes a channel
463 void SND_Spatialize(channel_t *ch, int isstatic)
465 vec_t dist, scale, pan;
468 // anything coming from the view entity will always be full volume
469 // LordHavoc: make sounds with ATTN_NONE have no spatialization
470 if (ch->entnum == cl.viewentity || ch->dist_mult == 0)
472 ch->leftvol = ch->master_vol;
473 ch->rightvol = ch->master_vol;
477 // update sound origin if we know about the entity
478 if (ch->entnum > 0 && cls.state == ca_connected && cl_entities[ch->entnum].state_current.active)
480 //Con_Printf("-- entnum %i origin %f %f %f neworigin %f %f %f\n", ch->entnum, ch->origin[0], ch->origin[1], ch->origin[2], cl_entities[ch->entnum].state_current.origin[0], cl_entities[ch->entnum].state_current.origin[1], cl_entities[ch->entnum].state_current.origin[2]);
481 VectorCopy(cl_entities[ch->entnum].state_current.origin, ch->origin);
482 if (cl_entities[ch->entnum].state_current.modelindex && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->soundfromcenter)
483 VectorMAMAM(1.0f, ch->origin, 0.5f, cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->normalmins, 0.5f, cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->normalmaxs, ch->origin);
486 // calculate stereo seperation and distance attenuation
487 VectorSubtract(ch->origin, listener_vieworigin, source_vec);
488 dist = VectorNormalizeLength(source_vec);
490 scale = ch->master_vol * (1.0 - (dist * ch->dist_mult));
492 pan = scale * DotProduct(listener_viewleft, source_vec);
493 // calculate the volumes
494 ch->leftvol = (int) (scale + pan);
495 ch->rightvol = (int) (scale - pan);
498 // LordHavoc: allow adjusting volume of static sounds
501 ch->leftvol *= snd_staticvolume.value;
502 ch->rightvol *= snd_staticvolume.value;
506 ch->leftvol = bound(0, ch->leftvol, 255);
507 ch->rightvol = bound(0, ch->rightvol, 255);
511 // =======================================================================
512 // Start a sound effect
513 // =======================================================================
515 int S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
517 channel_t *target_chan, *check;
522 if (!sound_started || !sfx || !sfx->fetcher || nosound.integer)
527 // pick a channel to play on
528 target_chan = SND_PickChannel(entnum, entchannel);
533 memset (target_chan, 0, sizeof(*target_chan));
534 VectorCopy(origin, target_chan->origin);
535 target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
536 target_chan->master_vol = vol;
537 target_chan->entnum = entnum;
538 target_chan->entchannel = entchannel;
539 SND_Spatialize(target_chan, false);
541 // LordHavoc: spawn the sound anyway because the player might teleport to it
542 //if (!target_chan->leftvol && !target_chan->rightvol)
543 // return; // not audible at all
546 if (!S_LoadSound (sfx, true))
548 target_chan->sfx = NULL;
549 return -1; // couldn't load the sound's data
552 target_chan->sfx = sfx;
553 target_chan->flags = CHANNELFLAG_NONE;
554 target_chan->pos = 0.0;
555 target_chan->end = paintedtime + sfx->total_length;
556 target_chan->lastptime = paintedtime;
558 // if an identical sound has also been started this frame, offset the pos
559 // a bit to keep it from just making the first one louder
560 check = &channels[NUM_AMBIENTS];
561 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
563 if (check == target_chan)
565 if (check->sfx == sfx && !check->pos)
567 // LordHavoc: fixed skip calculations
568 skip = 0.1 * sfx->format.speed;
569 if (skip > sfx->total_length)
570 skip = sfx->total_length;
572 skip = rand() % skip;
573 target_chan->pos += skip;
574 target_chan->end -= skip;
579 return (target_chan - channels);
582 void S_StopChannel (unsigned int channel_ind)
586 if (channel_ind >= total_channels)
589 ch = &channels[channel_ind];
592 if (ch->sfx->fetcher != NULL)
594 snd_fetcher_end_t fetcher_end = ch->sfx->fetcher->end;
595 if (fetcher_end != NULL)
603 void S_PauseChannel (unsigned int channel_ind, qboolean toggle)
606 channels[channel_ind].flags |= CHANNELFLAG_PAUSED;
608 channels[channel_ind].flags &= ~CHANNELFLAG_PAUSED;
611 void S_LoopChannel (unsigned int channel_ind, qboolean toggle)
614 channels[channel_ind].flags |= CHANNELFLAG_FORCELOOP;
616 channels[channel_ind].flags &= ~CHANNELFLAG_FORCELOOP;
619 void S_StopSound(int entnum, int entchannel)
623 for (i = 0; i < MAX_DYNAMIC_CHANNELS; i++)
624 if (channels[i].entnum == entnum && channels[i].entchannel == entchannel)
631 void S_StopAllSounds(qboolean clear)
635 for (i = 0; i < total_channels; i++)
638 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
639 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
645 void S_StopAllSoundsC(void)
647 S_StopAllSounds(true);
650 void S_PauseGameSounds (void)
654 for (i = 0; i < total_channels; i++)
659 if (ch->sfx != NULL && ! (ch->flags & CHANNELFLAG_LOCALSOUND))
660 ch->flags |= CHANNELFLAG_PAUSED;
664 void S_ResumeGameSounds (void)
668 for (i = 0; i < total_channels; i++)
673 if (ch->sfx != NULL && ! (ch->flags & CHANNELFLAG_LOCALSOUND))
674 ch->flags &= ~CHANNELFLAG_PAUSED;
678 void S_SetChannelVolume (unsigned int ch_ind, float fvol)
680 channels[ch_ind].master_vol = fvol * 255;
684 void S_ClearBuffer(void)
688 if (!sound_started || !shm)
691 if (shm->format.width == 1)
706 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, (LPVOID*)&pData, &dwSize, NULL, NULL, 0)) != DS_OK)
708 if (hresult != DSERR_BUFFERLOST)
710 Con_Print("S_ClearBuffer: DS::Lock Sound Buffer Failed\n");
717 Con_Print("S_ClearBuffer: DS: couldn't restore buffer\n");
723 memset(pData, clear, shm->samples * shm->format.width);
725 pDSBuf->lpVtbl->Unlock(pDSBuf, pData, dwSize, NULL, 0);
732 int setsize = shm->samples * shm->format.width;
733 char *buf = shm->buffer;
738 // on i586/i686 optimized versions of glibc, glibc *wrongly* IMO,
739 // reads the memory area before writing to it causing a seg fault
740 // since the memory is PROT_WRITE only and not PROT_READ|PROT_WRITE
741 // memset(shm->buffer, clear, shm->samples * shm->samplebits/8);
751 void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation)
758 if (total_channels == MAX_CHANNELS)
760 Con_Print("total_channels == MAX_CHANNELS\n");
764 if (!S_LoadSound (sfx, true))
767 if (sfx->loopstart == -1)
768 Con_DPrintf("Quake compatibility warning: Static sound \"%s\" is not looped\n", sfx->name);
770 ss = &channels[total_channels++];
771 memset(ss, 0, sizeof(*ss));
772 ss->flags = CHANNELFLAG_FORCELOOP;
774 VectorCopy (origin, ss->origin);
775 ss->master_vol = vol;
776 ss->dist_mult = (attenuation/64) / sound_nominal_clip_dist;
777 ss->end = paintedtime + sfx->total_length;
778 ss->lastptime = paintedtime;
780 SND_Spatialize (ss, true);
784 //=============================================================================
788 S_UpdateAmbientSounds
791 void S_UpdateAmbientSounds (void)
796 qbyte ambientlevels[NUM_AMBIENTS];
798 // LordHavoc: kill ambient sounds until proven otherwise
799 for (ambient_channel = 0 ; ambient_channel < NUM_AMBIENTS;ambient_channel++)
800 channels[ambient_channel].sfx = NULL;
802 if (ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
805 cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_vieworigin, ambientlevels, sizeof(ambientlevels));
807 // calc ambient sound levels
808 for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
810 if (ambient_sfx[ambient_channel] &&
811 (ambient_sfx[ambient_channel]->flags & SFXFLAG_SILENTLYMISSING))
813 chan = &channels[ambient_channel];
814 chan->flags |= CHANNELFLAG_FORCELOOP;
815 chan->sfx = ambient_sfx[ambient_channel];
817 vol = ambient_level.value * ambientlevels[ambient_channel];
821 // don't adjust volume too fast
822 if (chan->master_vol < vol)
824 chan->master_vol += host_realframetime * ambient_fade.value;
825 if (chan->master_vol > vol)
826 chan->master_vol = vol;
828 else if (chan->master_vol > vol)
830 chan->master_vol -= host_realframetime * ambient_fade.value;
831 if (chan->master_vol < vol)
832 chan->master_vol = vol;
835 chan->leftvol = chan->rightvol = chan->master_vol;
844 Called once each time through the main loop
847 void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up)
849 unsigned int i, j, total;
850 channel_t *ch, *combine;
852 if (!snd_initialized.integer || (snd_blocked > 0))
855 VectorCopy(origin, listener_vieworigin);
856 VectorCopy(forward, listener_viewforward);
857 VectorCopy(left, listener_viewleft);
858 VectorCopy(up, listener_viewup);
860 // update general area ambient sound sources
861 S_UpdateAmbientSounds ();
865 // update spatialization for static and dynamic sounds
866 ch = channels+NUM_AMBIENTS;
867 for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
871 SND_Spatialize(ch, i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS); // respatialize channel
872 if (!ch->leftvol && !ch->rightvol)
875 // try to combine static sounds with a previous channel of the same
876 // sound effect so we don't mix five torches every frame
878 if (i > MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
880 // see if it can just use the last one
881 if (combine && combine->sfx == ch->sfx)
883 combine->leftvol += ch->leftvol;
884 combine->rightvol += ch->rightvol;
885 ch->leftvol = ch->rightvol = 0;
889 combine = channels+MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
890 for (j=MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS ; j<i; j++, combine++)
891 if (combine->sfx == ch->sfx)
894 if (j == total_channels)
900 combine->leftvol += ch->leftvol;
901 combine->rightvol += ch->rightvol;
902 ch->leftvol = ch->rightvol = 0;
912 if (snd_show.integer)
916 for (i=0 ; i<total_channels; i++, ch++)
917 if (ch->sfx && (ch->leftvol || ch->rightvol) )
920 Con_Printf("----(%u)----\n", total);
927 void GetSoundtime(void)
931 static int oldsamplepos;
934 fullsamples = shm->samples / shm->format.channels;
936 // it is possible to miscount buffers if it has wrapped twice between
937 // calls to S_Update. Oh well.
938 samplepos = SNDDMA_GetDMAPos();
940 if (samplepos < oldsamplepos)
942 buffers++; // buffer wrapped
944 if (paintedtime > 0x40000000)
945 { // time to chop things off to avoid 32 bit limits
947 paintedtime = fullsamples;
948 S_StopAllSounds (true);
951 oldsamplepos = samplepos;
953 soundtime = buffers * fullsamples + samplepos / shm->format.channels;
956 void IN_Accumulate (void);
958 void S_ExtraUpdate (void)
965 if (snd_noextraupdate.integer)
966 return; // don't pollute timings
975 if (!sound_started || (snd_blocked > 0))
981 // check to make sure that we haven't overshot
982 if (paintedtime < soundtime)
983 paintedtime = soundtime;
985 // mix ahead of current position
986 endtime = soundtime + _snd_mixahead.value * shm->format.speed;
987 samps = shm->samples >> (shm->format.channels - 1);
988 if (endtime > (unsigned int)(soundtime + samps))
989 endtime = soundtime + samps;
992 // if the buffer was lost or stopped, restore it and/or restart it
998 if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DS_OK)
999 Con_Print("Couldn't get sound buffer status\n");
1001 if (dwStatus & DSBSTATUS_BUFFERLOST)
1002 pDSBuf->lpVtbl->Restore (pDSBuf);
1004 if (!(dwStatus & DSBSTATUS_PLAYING))
1005 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
1010 S_PaintChannels (endtime);
1016 ===============================================================================
1020 ===============================================================================
1023 static void S_Play_Common(float fvol, float attenuation)
1030 while (i<Cmd_Argc())
1032 if (!strrchr(Cmd_Argv(i), '.'))
1033 snprintf(name, sizeof(name), "%s.wav", Cmd_Argv(i));
1035 strlcpy(name, Cmd_Argv(i), sizeof(name));
1036 sfx = S_PrecacheSound(name, true, true);
1038 // If we need to get the volume from the command line
1041 fvol = atof(Cmd_Argv(i+1));
1047 ch_ind = S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
1049 channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND;
1055 S_Play_Common (1.0f, 1.0f);
1060 S_Play_Common (1.0f, 0.0f);
1063 void S_PlayVol(void)
1065 S_Play_Common (-1.0f, 0.0f);
1068 void S_SoundList(void)
1075 for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
1077 if (sfx->fetcher != NULL)
1079 size = sfx->mempool->totalsize;
1081 Con_Printf ("%c%c(%2db, %6s) %8i : %s\n",
1082 (sfx->loopstart >= 0) ? 'L' : ' ',
1083 (sfx->flags & SFXFLAG_STREAMED) ? 'S' : ' ',
1084 sfx->format.width * 8,
1085 (sfx->format.channels == 2) ? "stereo" : "mono",
1090 Con_Printf("Total resident: %i\n", total);
1094 void S_LocalSound (const char *sound, qboolean stdpath)
1099 if (!snd_initialized.integer || nosound.integer)
1102 sfx = S_PrecacheSound (sound, true, stdpath);
1105 Con_Printf("S_LocalSound: can't precache %s\n", sound);
1109 ch_ind = S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
1111 channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND;
1115 #define RAWSAMPLESBUFFER 32768
1116 short s_rawsamplesbuffer[RAWSAMPLESBUFFER * 2];
1117 int s_rawsamplesbuffer_start;
1118 size_t s_rawsamplesbuffer_count;
1120 void S_RawSamples_Enqueue(short *samples, unsigned int length)
1123 //Con_Printf("S_RawSamples_Enqueue: %i samples\n", length);
1124 if (s_rawsamplesbuffer_count + length > RAWSAMPLESBUFFER)
1126 b2 = (s_rawsamplesbuffer_start + s_rawsamplesbuffer_count) % RAWSAMPLESBUFFER;
1127 if (b2 + length > RAWSAMPLESBUFFER)
1129 b3 = (b2 + length) % RAWSAMPLESBUFFER;
1130 memcpy(s_rawsamplesbuffer + b2 * 2, samples, (RAWSAMPLESBUFFER - b2) * sizeof(short[2]));
1131 memcpy(s_rawsamplesbuffer, samples + (RAWSAMPLESBUFFER - b2) * 2, b3 * sizeof(short[2]));
1134 memcpy(s_rawsamplesbuffer + b2 * 2, samples, length * sizeof(short[2]));
1135 s_rawsamplesbuffer_count += length;
1138 void S_RawSamples_Dequeue(int *samples, unsigned int length)
1147 if (l > s_rawsamplesbuffer_count)
1148 l = s_rawsamplesbuffer_count;
1149 b1 = (s_rawsamplesbuffer_start) % RAWSAMPLESBUFFER;
1150 if (b1 + l > RAWSAMPLESBUFFER)
1152 b2 = (b1 + l) % RAWSAMPLESBUFFER;
1153 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, (RAWSAMPLESBUFFER - b1) * sizeof(short[2]));
1154 //memcpy(samples + (RAWSAMPLESBUFFER - b1) * 2, s_rawsamplesbuffer, b2 * sizeof(short[2]));
1155 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = (RAWSAMPLESBUFFER - b1) * 2, i = 0;i < count;i++)
1157 for (out = samples + (RAWSAMPLESBUFFER - b1) * 2, in = s_rawsamplesbuffer, count = b2 * 2, i = 0;i < count;i++)
1159 //Con_Printf("S_RawSamples_Dequeue: buffer wrap %i %i\n", (RAWSAMPLESBUFFER - b1), b2);
1163 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, l * sizeof(short[2]));
1164 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = l * 2, i = 0;i < count;i++)
1166 //Con_Printf("S_RawSamples_Dequeue: normal %i\n", l);
1168 if (l < (int)length)
1170 memset(samples + l * 2, 0, (length - l) * sizeof(int[2]));
1171 //Con_Printf("S_RawSamples_Dequeue: padding with %i samples\n", length - l);
1173 s_rawsamplesbuffer_start = (s_rawsamplesbuffer_start + l) % RAWSAMPLESBUFFER;
1174 s_rawsamplesbuffer_count -= l;
1177 void S_RawSamples_ClearQueue(void)
1179 s_rawsamplesbuffer_count = 0;
1180 s_rawsamplesbuffer_start = 0;
1183 int S_RawSamples_QueueWantsMore(void)
1185 if (shm != NULL && s_rawsamplesbuffer_count < min(shm->format.speed >> 1, RAWSAMPLESBUFFER >> 1))
1186 return RAWSAMPLESBUFFER - s_rawsamplesbuffer_count;
1191 void S_ResampleBuffer16Stereo(short *input, int inputlength, short *output, int outputlength)
1193 if (inputlength != outputlength)
1195 int i, position, stopposition, step;
1197 step = (float) inputlength * 256.0f / (float) outputlength;
1199 stopposition = (inputlength - 1) << 8;
1201 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1203 in = input + ((position >> 8) << 1);
1204 out[0] = (((in[1] - in[0]) * (position & 255)) >> 8) + in[0];
1205 out[1] = (((in[3] - in[2]) * (position & 255)) >> 8) + in[2];
1208 stopposition = inputlength << 8;
1209 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1211 in = input + ((position >> 8) << 1);
1218 memcpy(output, input, inputlength * sizeof(short[2]));
1221 int S_RawSamples_SampleRate(void)
1223 return shm != NULL ? shm->format.speed : 0;