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_main.c -- main control for any streaming sound output device
27 #if SND_LISTENERS != 8
28 #error this data only supports up to 8 channel, update it!
30 typedef struct listener_s
38 typedef struct speakerlayout_s
41 unsigned int channels;
43 listener_t listeners[SND_LISTENERS];
47 static speakerlayout_t snd_speakerlayout;
52 void S_SoundList(void);
56 // =======================================================================
57 // Internal sound data & structures
58 // =======================================================================
60 channel_t channels[MAX_CHANNELS];
61 unsigned int total_channels;
64 cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
65 cvar_t snd_streaming = { CVAR_SAVE, "snd_streaming", "1"};
66 cvar_t snd_headphones = { CVAR_SAVE, "snd_headphones", "0"};
68 volatile dma_t *shm = 0;
71 vec3_t listener_origin;
72 matrix4x4_t listener_matrix[SND_LISTENERS];
73 vec_t sound_nominal_clip_dist=1000.0;
74 mempool_t *snd_mempool;
79 // Linked list of known sfx
80 sfx_t *known_sfx = NULL;
82 qboolean sound_spatialized = false;
84 // Fake dma is a synchronous faking of the DMA progress used for
85 // isolating performance in the renderer.
86 qboolean fakedma = false;
88 cvar_t bgmvolume = {CVAR_SAVE, "bgmvolume", "1"};
89 cvar_t volume = {CVAR_SAVE, "volume", "0.7"};
90 cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1"};
92 cvar_t nosound = {0, "nosound", "0"};
93 cvar_t snd_precache = {0, "snd_precache", "1"};
94 cvar_t bgmbuffer = {0, "bgmbuffer", "4096"};
95 cvar_t ambient_level = {0, "ambient_level", "0.3"};
96 cvar_t ambient_fade = {0, "ambient_fade", "100"};
97 cvar_t snd_noextraupdate = {0, "snd_noextraupdate", "0"};
98 cvar_t snd_show = {0, "snd_show", "0"};
99 cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.1"};
100 cvar_t snd_swapstereo = {CVAR_SAVE, "snd_swapstereo", "0"};
103 sfx_t* ambient_sfxs [2] = { NULL, NULL };
104 const char* ambient_names [2] = { "sound/ambience/water1.wav", "sound/ambience/wind2.wav" };
107 // ====================================================================
109 // ====================================================================
111 void S_FreeSfx (sfx_t *sfx, qboolean force);
114 void S_SoundInfo_f(void)
118 Con_Print("sound system not started\n");
122 Con_Printf("%5d speakers\n", shm->format.channels);
123 Con_Printf("%5d frames\n", shm->sampleframes);
124 Con_Printf("%5d samples\n", shm->samples);
125 Con_Printf("%5d samplepos\n", shm->samplepos);
126 Con_Printf("%5d samplebits\n", shm->format.width * 8);
127 Con_Printf("%5d speed\n", shm->format.speed);
128 Con_Printf("%p dma buffer\n", shm->buffer);
129 Con_Printf("%5u total_channels\n", total_channels);
135 if (!snd_initialized.integer)
139 memset((void *)shm, 0, sizeof(*shm));
141 // create a piece of DMA memory
144 shm->format.width = 2;
145 shm->format.speed = 22050;
146 shm->format.channels = 2;
147 shm->sampleframes = 16384;
148 shm->samples = shm->sampleframes * shm->format.channels;
150 shm->buffer = (unsigned char *)Mem_Alloc(snd_mempool, shm->samples * shm->format.width);
156 Con_Print("S_Startup: SNDDMA_Init failed.\n");
158 sound_spatialized = false;
163 Con_Printf("Sound format: %dHz, %d bit, %d channels\n", shm->format.speed,
164 shm->format.width * 8, shm->format.channels);
167 void S_Shutdown(void)
173 Mem_Free(shm->buffer);
178 sound_spatialized = false;
181 void S_Restart_f(void)
194 Con_DPrint("\nSound Initialization\n");
196 Cvar_RegisterVariable(&volume);
197 Cvar_RegisterVariable(&bgmvolume);
198 Cvar_RegisterVariable(&snd_staticvolume);
199 Cvar_RegisterVariable(&snd_headphones);
201 // COMMANDLINEOPTION: Sound: -nosound disables sound (including CD audio)
202 if (COM_CheckParm("-nosound") || COM_CheckParm("-safe"))
205 snd_mempool = Mem_AllocPool("sound", 0, NULL);
207 // COMMANDLINEOPTION: Sound: -simsound runs sound mixing but with no output
208 if (COM_CheckParm("-simsound"))
211 Cmd_AddCommand("play", S_Play);
212 Cmd_AddCommand("play2", S_Play2);
213 Cmd_AddCommand("playvol", S_PlayVol);
214 Cmd_AddCommand("stopsound", S_StopAllSounds);
215 Cmd_AddCommand("soundlist", S_SoundList);
216 Cmd_AddCommand("soundinfo", S_SoundInfo_f);
217 Cmd_AddCommand("snd_restart", S_Restart_f);
219 Cvar_RegisterVariable(&nosound);
220 Cvar_RegisterVariable(&snd_precache);
221 Cvar_RegisterVariable(&snd_initialized);
222 Cvar_RegisterVariable(&snd_streaming);
223 Cvar_RegisterVariable(&bgmbuffer);
224 Cvar_RegisterVariable(&ambient_level);
225 Cvar_RegisterVariable(&ambient_fade);
226 Cvar_RegisterVariable(&snd_noextraupdate);
227 Cvar_RegisterVariable(&snd_show);
228 Cvar_RegisterVariable(&_snd_mixahead);
229 Cvar_RegisterVariable(&snd_swapstereo); // for people with backwards sound wiring
231 Cvar_SetValueQuick(&snd_initialized, true);
235 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
236 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
246 Shutdown and free all resources
249 void S_Terminate (void)
255 while (known_sfx != NULL)
256 S_FreeSfx (known_sfx, true);
258 Cvar_SetValueQuick (&snd_initialized, false);
259 Mem_FreePool (&snd_mempool);
263 // =======================================================================
265 // =======================================================================
273 sfx_t *S_FindName (const char *name)
277 if (!snd_initialized.integer)
280 if (strlen (name) >= sizeof (sfx->name))
282 Con_Printf ("S_FindName: sound name too long (%s)\n", name);
286 // Look for this sound in the list of known sfx
287 for (sfx = known_sfx; sfx != NULL; sfx = sfx->next)
288 if(!strcmp (sfx->name, name))
291 // Add a sfx_t struct for this sound
292 sfx = (sfx_t *)Mem_Alloc (snd_mempool, sizeof (*sfx));
293 memset (sfx, 0, sizeof(*sfx));
294 strlcpy (sfx->name, name, sizeof (sfx->name));
295 sfx->memsize = sizeof(*sfx);
296 sfx->next = known_sfx;
309 void S_FreeSfx (sfx_t *sfx, qboolean force)
313 // Never free a locked sfx unless forced
314 if (!force && (sfx->locks > 0 || (sfx->flags & SFXFLAG_PERMANENTLOCK)))
317 Con_DPrintf ("S_FreeSfx: freeing %s\n", sfx->name);
319 // Remove it from the list of known sfx
320 if (sfx == known_sfx)
321 known_sfx = known_sfx->next;
326 for (prev_sfx = known_sfx; prev_sfx != NULL; prev_sfx = prev_sfx->next)
327 if (prev_sfx->next == sfx)
329 prev_sfx->next = sfx->next;
332 if (prev_sfx == NULL)
334 Con_Printf ("S_FreeSfx: Can't find SFX %s in the list!\n", sfx->name);
339 // Stop all channels using this sfx
340 for (i = 0; i < total_channels; i++)
341 if (channels[i].sfx == sfx)
345 if (sfx->fetcher != NULL && sfx->fetcher->free != NULL)
346 sfx->fetcher->free (sfx);
357 void S_ServerSounds (char serversound [][MAX_QPATH], unsigned int numsounds)
363 // Start the ambient sounds and make them loop
364 for (i = 0; i < sizeof (ambient_sfxs) / sizeof (ambient_sfxs[0]); i++)
366 // Precache it if it's not done (request a lock to make sure it will never be freed)
367 if (ambient_sfxs[i] == NULL)
368 ambient_sfxs[i] = S_PrecacheSound (ambient_names[i], false, true);
369 if (ambient_sfxs[i] != NULL)
371 // Add a lock to the SFX while playing. It will be
372 // removed by S_StopAllSounds at the end of the level
373 S_LockSfx (ambient_sfxs[i]);
375 channels[i].sfx = ambient_sfxs[i];
376 channels[i].flags |= CHANNELFLAG_FORCELOOP;
377 channels[i].master_vol = 0;
381 // Remove 1 lock from all sfx with the SFXFLAG_SERVERSOUND flag, and remove the flag
382 for (sfx = known_sfx; sfx != NULL; sfx = sfx->next)
383 if (sfx->flags & SFXFLAG_SERVERSOUND)
386 sfx->flags &= ~SFXFLAG_SERVERSOUND;
389 // Add 1 lock and the SFXFLAG_SERVERSOUND flag to each sfx in "serversound"
390 for (i = 1; i < numsounds; i++)
392 sfx = S_FindName (serversound[i]);
396 sfx->flags |= SFXFLAG_SERVERSOUND;
400 // Free all unlocked sfx
401 for (sfx = known_sfx;sfx;sfx = sfxnext)
404 S_FreeSfx (sfx, false);
415 sfx_t *S_PrecacheSound (const char *name, qboolean complain, qboolean lock)
419 if (!snd_initialized.integer)
422 sfx = S_FindName (name);
429 if (!nosound.integer && snd_precache.integer)
430 S_LoadSound(sfx, complain);
442 void S_LockSfx (sfx_t *sfx)
451 Remove a lock from a SFX
454 void S_UnlockSfx (sfx_t *sfx)
460 //=============================================================================
466 Picks a channel based on priorities, empty slots, number of channels
469 channel_t *SND_PickChannel(int entnum, int entchannel)
476 // Check for replacement sound, or find the best one to replace
478 life_left = 0x7fffffff;
479 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
481 ch = &channels[ch_idx];
484 // try to override an existing channel
485 if (ch->entnum == entnum && (ch->entchannel == entchannel || entchannel == -1) )
487 // always override sound from same entity
488 first_to_die = ch_idx;
496 // no sound on this channel
497 first_to_die = ch_idx;
504 // don't let monster sounds override player sounds
505 if (ch->entnum == cl.viewentity && entnum != cl.viewentity)
508 // don't override looped sounds
509 if ((ch->flags & CHANNELFLAG_FORCELOOP) || ch->sfx->loopstart >= 0)
513 if (ch->end - paintedtime < life_left)
515 life_left = ch->end - paintedtime;
516 first_to_die = ch_idx;
520 if (first_to_die == -1)
523 S_StopChannel (first_to_die);
525 return &channels[first_to_die];
532 Spatializes a channel
535 void SND_Spatialize(channel_t *ch, qboolean isstatic)
538 vec_t dist, mastervol, intensity, vol;
541 // update sound origin if we know about the entity
542 if (ch->entnum > 0 && cls.state == ca_connected && cl_entities[ch->entnum].state_current.active)
544 //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]);
545 VectorCopy(cl_entities[ch->entnum].state_current.origin, ch->origin);
546 if (cl_entities[ch->entnum].state_current.modelindex && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex] && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->soundfromcenter)
547 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);
550 mastervol = ch->master_vol;
551 // Adjust volume of static sounds
553 mastervol *= snd_staticvolume.value;
555 // anything coming from the view entity will always be full volume
556 // LordHavoc: make sounds with ATTN_NONE have no spatialization
557 if (ch->entnum == cl.viewentity || ch->dist_mult == 0)
559 for (i = 0;i < SND_LISTENERS;i++)
561 vol = mastervol * snd_speakerlayout.listeners[i].ambientvolume;
562 ch->listener_volume[i] = bound(0, vol, 255);
567 // calculate stereo seperation and distance attenuation
568 VectorSubtract(listener_origin, ch->origin, source_vec);
569 dist = VectorLength(source_vec);
570 intensity = mastervol * (1.0 - dist * ch->dist_mult);
573 for (i = 0;i < SND_LISTENERS;i++)
575 Matrix4x4_Transform(&listener_matrix[i], ch->origin, source_vec);
576 VectorNormalize(source_vec);
577 vol = intensity * max(0, source_vec[0] * snd_speakerlayout.listeners[i].dotscale + snd_speakerlayout.listeners[i].dotbias);
578 ch->listener_volume[i] = bound(0, vol, 255);
582 for (i = 0;i < SND_LISTENERS;i++)
583 ch->listener_volume[i] = 0;
588 // =======================================================================
589 // Start a sound effect
590 // =======================================================================
592 void S_PlaySfxOnChannel (sfx_t *sfx, channel_t *target_chan, unsigned int flags, vec3_t origin, float fvol, float attenuation, qboolean isstatic)
594 // Initialize the channel
595 memset (target_chan, 0, sizeof (*target_chan));
596 VectorCopy (origin, target_chan->origin);
597 target_chan->master_vol = fvol * 255;
598 target_chan->sfx = sfx;
599 target_chan->end = paintedtime + sfx->total_length;
600 target_chan->lastptime = paintedtime;
601 target_chan->flags = flags;
603 // If it's a static sound
606 if (sfx->loopstart == -1)
607 Con_DPrintf("Quake compatibility warning: Static sound \"%s\" is not looped\n", sfx->name);
608 target_chan->dist_mult = attenuation / (64.0f * sound_nominal_clip_dist);
611 target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
613 // Lock the SFX during play
618 int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
620 channel_t *target_chan, *check;
624 if (!shm || !sfx || nosound.integer)
628 Con_DPrintf ("S_StartSound: \"%s\" hasn't been precached\n", sfx->name);
632 if (entnum && entnum >= cl_max_entities)
633 CL_ExpandEntities(entnum);
635 // Pick a channel to play on
636 target_chan = SND_PickChannel(entnum, entchannel);
640 S_PlaySfxOnChannel (sfx, target_chan, CHANNELFLAG_NONE, origin, fvol, attenuation, false);
641 target_chan->entnum = entnum;
642 target_chan->entchannel = entchannel;
644 SND_Spatialize(target_chan, false);
646 // if an identical sound has also been started this frame, offset the pos
647 // a bit to keep it from just making the first one louder
648 check = &channels[NUM_AMBIENTS];
649 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
651 if (check == target_chan)
653 if (check->sfx == sfx && !check->pos)
655 skip = 0.1 * sfx->format.speed;
656 if (skip > (int)sfx->total_length)
657 skip = (int)sfx->total_length;
659 skip = rand() % skip;
660 target_chan->pos += skip;
661 target_chan->end -= skip;
666 return (target_chan - channels);
669 void S_StopChannel (unsigned int channel_ind)
673 if (channel_ind >= total_channels)
676 ch = &channels[channel_ind];
679 sfx_t *sfx = ch->sfx;
681 if (sfx->fetcher != NULL)
683 snd_fetcher_endsb_t fetcher_endsb = sfx->fetcher->endsb;
684 if (fetcher_endsb != NULL)
688 // Remove the lock it holds
697 qboolean S_SetChannelFlag (unsigned int ch_ind, unsigned int flag, qboolean value)
699 if (ch_ind >= total_channels)
702 if (flag != CHANNELFLAG_FORCELOOP &&
703 flag != CHANNELFLAG_PAUSED &&
704 flag != CHANNELFLAG_FULLVOLUME)
708 channels[ch_ind].flags |= flag;
710 channels[ch_ind].flags &= ~flag;
715 void S_StopSound(int entnum, int entchannel)
719 for (i = 0; i < MAX_DYNAMIC_CHANNELS; i++)
720 if (channels[i].entnum == entnum && channels[i].entchannel == entchannel)
727 void S_StopAllSounds (void)
735 for (i = 0; i < total_channels; i++)
738 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
739 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
741 // Clear sound buffer
742 pbuf = (unsigned char *)S_LockBuffer();
745 int setsize = shm->samples * shm->format.width;
746 int clear = (shm->format.width == 1) ? 0x80 : 0;
748 // FIXME: is it (still) true? (check with OSS and ALSA)
749 // on i586/i686 optimized versions of glibc, glibc *wrongly* IMO,
750 // reads the memory area before writing to it causing a seg fault
751 // since the memory is PROT_WRITE only and not PROT_READ|PROT_WRITE
752 //memset(shm->buffer, clear, shm->samples * shm->format.width);
760 void S_PauseGameSounds (qboolean toggle)
764 for (i = 0; i < total_channels; i++)
769 if (ch->sfx != NULL && ! (ch->flags & CHANNELFLAG_LOCALSOUND))
770 S_SetChannelFlag (i, CHANNELFLAG_PAUSED, toggle);
774 void S_SetChannelVolume (unsigned int ch_ind, float fvol)
776 channels[ch_ind].master_vol = fvol * 255;
785 void S_StaticSound (sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
787 channel_t *target_chan;
789 if (!shm || !sfx || nosound.integer)
793 Con_DPrintf ("S_StaticSound: \"%s\" hasn't been precached\n", sfx->name);
797 if (total_channels == MAX_CHANNELS)
799 Con_Print("S_StaticSound: total_channels == MAX_CHANNELS\n");
803 target_chan = &channels[total_channels++];
804 S_PlaySfxOnChannel (sfx, target_chan, CHANNELFLAG_FORCELOOP, origin, fvol, attenuation, true);
806 SND_Spatialize (target_chan, true);
810 //=============================================================================
814 S_UpdateAmbientSounds
818 void S_UpdateAmbientSounds (void)
824 unsigned char ambientlevels[NUM_AMBIENTS];
826 memset(ambientlevels, 0, sizeof(ambientlevels));
827 if (cl.worldmodel && cl.worldmodel->brush.AmbientSoundLevelsForPoint)
828 cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_origin, ambientlevels, sizeof(ambientlevels));
830 // Calc ambient sound levels
831 for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
833 chan = &channels[ambient_channel];
834 if (chan->sfx == NULL || chan->sfx->fetcher == NULL)
837 vol = ambientlevels[ambient_channel];
841 // Don't adjust volume too fast
842 if (chan->master_vol < vol)
844 chan->master_vol += host_realframetime * ambient_fade.value;
845 if (chan->master_vol > vol)
846 chan->master_vol = vol;
848 else if (chan->master_vol > vol)
850 chan->master_vol -= host_realframetime * ambient_fade.value;
851 if (chan->master_vol < vol)
852 chan->master_vol = vol;
855 for (i = 0;i < SND_LISTENERS;i++)
856 chan->listener_volume[i] = (int)(chan->master_vol * ambient_level.value * snd_speakerlayout.listeners[i].ambientvolume);
860 #define SND_SPEAKERLAYOUTS 5
861 static speakerlayout_t snd_speakerlayouts[SND_SPEAKERLAYOUTS] =
864 "surround71", 8, false,
866 {45, 0.2, 0.2, 0.5}, // front left
867 {315, 0.2, 0.2, 0.5}, // front right
868 {135, 0.2, 0.2, 0.5}, // rear left
869 {225, 0.2, 0.2, 0.5}, // rear right
870 {0, 0.2, 0.2, 0.5}, // front center
871 {0, 0, 0, 0}, // lfe (we don't have any good lfe sound sources and it would take some filtering work to generate them (and they'd probably still be wrong), so... no lfe)
872 {90, 0.2, 0.2, 0.5}, // side left
873 {180, 0.2, 0.2, 0.5}, // side right
877 "surround51", 6, false,
879 {45, 0.2, 0.2, 0.5}, // front left
880 {315, 0.2, 0.2, 0.5}, // front right
881 {135, 0.2, 0.2, 0.5}, // rear left
882 {225, 0.2, 0.2, 0.5}, // rear right
883 {0, 0.2, 0.2, 0.5}, // front center
884 {0, 0, 0, 0}, // lfe (we don't have any good lfe sound sources and it would take some filtering work to generate them (and they'd probably still be wrong), so... no lfe)
890 // these systems sometimes have a subwoofer as well, but it has no
891 // channel of its own
892 "surround40", 4, false,
894 {45, 0.3, 0.3, 0.8}, // front left
895 {315, 0.3, 0.3, 0.8}, // front right
896 {135, 0.3, 0.3, 0.8}, // rear left
897 {225, 0.3, 0.3, 0.8}, // rear right
905 "headphones", 2, true,
907 {90, 0.5, 0.5, 1}, // side left
908 {270, 0.5, 0.5, 1}, // side right
918 // these systems sometimes have a subwoofer as well, but it has no
919 // channel of its own
922 {45, 0.5, 0.5, 1}, // front left
923 {315, 0.5, 0.5, 1}, // front right
938 Called once each time through the main loop
941 void S_Update(const matrix4x4_t *listenermatrix)
943 unsigned int i, j, total;
944 channel_t *ch, *combine;
945 matrix4x4_t basematrix, rotatematrix;
947 if (!snd_initialized.integer || (snd_blocked > 0) || !shm)
950 Matrix4x4_Invert_Simple(&basematrix, listenermatrix);
951 Matrix4x4_OriginFromMatrix(listenermatrix, listener_origin);
953 // select speaker layout
954 for (i = 0;i < SND_SPEAKERLAYOUTS - 1;i++)
955 if (snd_speakerlayouts[i].channels == shm->format.channels && (!snd_speakerlayouts[i].headphones || snd_headphones.integer))
957 snd_speakerlayout = snd_speakerlayouts[i];
959 // calculate the current matrices
960 for (j = 0;j < SND_LISTENERS;j++)
962 Matrix4x4_CreateFromQuakeEntity(&rotatematrix, 0, 0, 0, 0, snd_speakerlayout.listeners[j].yawangle, 0, 1);
963 Matrix4x4_Concat(&listener_matrix[j], &basematrix, &rotatematrix);
966 // update general area ambient sound sources
967 S_UpdateAmbientSounds ();
971 // update spatialization for static and dynamic sounds
972 ch = channels+NUM_AMBIENTS;
973 for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
978 // respatialize channel
979 SND_Spatialize(ch, i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS);
981 // try to combine static sounds with a previous channel of the same
982 // sound effect so we don't mix five torches every frame
983 if (i > MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
985 // no need to merge silent channels
986 for (j = 0;j < SND_LISTENERS;j++)
987 if (ch->listener_volume[j])
989 if (j == SND_LISTENERS)
991 // if the last combine chosen isn't suitable, find a new one
992 if (!(combine && combine != ch && combine->sfx == ch->sfx))
996 for (j = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;j < i;j++)
998 if (channels[j].sfx == ch->sfx)
1000 combine = channels + j;
1005 if (combine && combine != ch && combine->sfx == ch->sfx)
1007 for (j = 0;j < SND_LISTENERS;j++)
1009 combine->listener_volume[j] += ch->listener_volume[j];
1010 ch->listener_volume[j] = 0;
1016 sound_spatialized = true;
1019 if (snd_show.integer)
1023 for (i=0 ; i<total_channels; i++, ch++)
1027 for (j = 0;j < SND_LISTENERS;j++)
1028 if (ch->listener_volume[j])
1030 if (j < SND_LISTENERS)
1035 Con_Printf("----(%u)----\n", total);
1041 void GetSoundtime(void)
1045 static int oldsamplepos;
1048 fullsamples = shm->sampleframes;
1050 // it is possible to miscount buffers if it has wrapped twice between
1051 // calls to S_Update. Oh well.
1052 samplepos = SNDDMA_GetDMAPos();
1054 if (samplepos < oldsamplepos)
1056 buffers++; // buffer wrapped
1058 if (paintedtime > 0x40000000)
1059 { // time to chop things off to avoid 32 bit limits
1061 paintedtime = fullsamples;
1065 oldsamplepos = samplepos;
1067 soundtime = buffers * fullsamples + samplepos / shm->format.channels;
1070 void S_ExtraUpdate (void)
1072 if (snd_noextraupdate.integer || !sound_spatialized)
1078 void S_Update_(void)
1082 if (!shm || (snd_blocked > 0))
1088 // check to make sure that we haven't overshot
1089 if (paintedtime < soundtime)
1090 paintedtime = soundtime;
1092 // mix ahead of current position
1093 endtime = soundtime + _snd_mixahead.value * shm->format.speed;
1094 endtime = min(endtime, (unsigned int)(soundtime + shm->sampleframes));
1096 S_PaintChannels (endtime);
1102 ===============================================================================
1106 ===============================================================================
1109 static void S_Play_Common(float fvol, float attenuation)
1112 char name[MAX_QPATH];
1116 while (i<Cmd_Argc())
1119 strlcpy(name, Cmd_Argv(i), sizeof(name));
1120 if (!strrchr(name, '.'))
1121 strlcat(name, ".wav", sizeof(name));
1124 // If we need to get the volume from the command line
1127 fvol = atof(Cmd_Argv(i));
1131 sfx = S_PrecacheSound (name, true, false);
1134 ch_ind = S_StartSound(-1, 0, sfx, listener_origin, fvol, attenuation);
1136 // Free the sfx if the file didn't exist
1138 S_FreeSfx (sfx, false);
1140 channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND;
1147 S_Play_Common (1.0f, 1.0f);
1152 S_Play_Common (1.0f, 0.0f);
1155 void S_PlayVol(void)
1157 S_Play_Common (-1.0f, 0.0f);
1160 void S_SoundList(void)
1167 for (sfx = known_sfx, i = 0; sfx != NULL; sfx = sfx->next, i++)
1169 if (sfx->fetcher != NULL)
1171 size = (int)sfx->memsize;
1173 Con_Printf ("%c%c%c%c(%2db, %6s) %8i : %s\n",
1174 (sfx->loopstart >= 0) ? 'L' : ' ',
1175 (sfx->flags & SFXFLAG_STREAMED) ? 'S' : ' ',
1176 (sfx->locks > 0) ? 'K' : ' ',
1177 (sfx->flags & SFXFLAG_PERMANENTLOCK) ? 'P' : ' ',
1178 sfx->format.width * 8,
1179 (sfx->format.channels == 1) ? "mono" : "stereo",
1184 Con_Printf (" ( unknown ) unloaded : %s\n", sfx->name);
1186 Con_Printf("Total resident: %i\n", total);
1190 qboolean S_LocalSound (const char *sound)
1195 if (!snd_initialized.integer || nosound.integer)
1198 sfx = S_PrecacheSound (sound, true, false);
1201 Con_Printf("S_LocalSound: can't precache %s\n", sound);
1205 // Local sounds must not be freed
1206 sfx->flags |= SFXFLAG_PERMANENTLOCK;
1208 ch_ind = S_StartSound (cl.viewentity, 0, sfx, vec3_origin, 1, 0);
1212 channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND;