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
31 void S_SoundList(void);
33 void S_StopAllSounds(qboolean clear);
34 void S_StopAllSoundsC(void);
36 // =======================================================================
37 // Internal sound data & structures
38 // =======================================================================
40 channel_t channels[MAX_CHANNELS];
44 static qboolean snd_ambient = 1;
45 qboolean snd_initialized = false;
47 // pointer should go away
48 volatile dma_t *shm = 0;
51 vec3_t listener_vieworigin;
52 vec3_t listener_viewforward;
53 vec3_t listener_viewleft;
54 vec3_t listener_viewup;
55 vec_t sound_nominal_clip_dist=1000.0;
56 mempool_t *snd_mempool;
63 //LordHavoc: increased the client sound limit from 512 to 4096 for the Nehahra movie
65 sfx_t *known_sfx; // allocated [MAX_SFX]
68 sfx_t *ambient_sfx[NUM_AMBIENTS];
70 int sound_started = 0;
72 cvar_t bgmvolume = {CVAR_SAVE, "bgmvolume", "1"};
73 cvar_t volume = {CVAR_SAVE, "volume", "0.7"};
74 cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1"};
76 cvar_t nosound = {0, "nosound", "0"};
77 cvar_t snd_precache = {0, "snd_precache", "1"};
78 cvar_t bgmbuffer = {0, "bgmbuffer", "4096"};
79 cvar_t ambient_level = {0, "ambient_level", "0.3"};
80 cvar_t ambient_fade = {0, "ambient_fade", "100"};
81 cvar_t snd_noextraupdate = {0, "snd_noextraupdate", "0"};
82 cvar_t snd_show = {0, "snd_show", "0"};
83 cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.1"};
84 cvar_t snd_swapstereo = {CVAR_SAVE, "snd_swapstereo", "0"};
87 // ====================================================================
88 // User-setable variables
89 // ====================================================================
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.
98 qboolean fakedma = false;
99 int fakedma_updates = 15;
102 void S_AmbientOff (void)
108 void S_AmbientOn (void)
114 void S_SoundInfo_f(void)
116 if (!sound_started || !shm)
118 Con_Printf ("sound system not started\n");
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 speed\n", shm->speed);
127 Con_Printf("0x%x dma buffer\n", shm->buffer);
128 Con_Printf("%5d total_channels\n", total_channels);
131 void S_UnloadSounds(void)
134 for (i = 0;i < num_sfx;i++)
135 S_UnloadSound(known_sfx + i);
138 void S_LoadSounds(void)
141 for (i = 0;i < num_sfx;i++)
142 S_LoadSound(known_sfx + i, false);
147 if (!snd_initialized)
151 memset((void *)shm, 0, sizeof(*shm));
153 // create a piece of DMA memory
157 shm->samplebits = 16;
160 shm->samples = 32768;
162 shm->buffer = Mem_Alloc(snd_mempool, shm->channels * shm->samples * (shm->samplebits / 8));
168 Con_Printf("S_Startup: SNDDMA_Init failed.\n");
177 Con_DPrintf("Sound sampling rate: %i\n", shm->speed);
181 S_StopAllSounds(true);
184 void S_Shutdown(void)
192 Mem_Free(shm->buffer);
200 void S_Restart_f(void)
213 Con_DPrintf("\nSound Initialization\n");
215 S_RawSamples_ClearQueue();
217 Cvar_RegisterVariable(&volume);
218 Cvar_RegisterVariable(&bgmvolume);
219 Cvar_RegisterVariable(&snd_staticvolume);
221 if (COM_CheckParm("-nosound") || COM_CheckParm("-safe"))
224 snd_mempool = Mem_AllocPool("sound");
226 if (COM_CheckParm("-simsound"))
229 Cmd_AddCommand("play", S_Play);
230 Cmd_AddCommand("play2", S_Play2);
231 Cmd_AddCommand("playvol", S_PlayVol);
232 Cmd_AddCommand("stopsound", S_StopAllSoundsC);
233 Cmd_AddCommand("soundlist", S_SoundList);
234 Cmd_AddCommand("soundinfo", S_SoundInfo_f);
235 Cmd_AddCommand("snd_restart", S_Restart_f);
237 Cvar_RegisterVariable(&nosound);
238 Cvar_RegisterVariable(&snd_precache);
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 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);
255 ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav", false);
257 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
258 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
262 // =======================================================================
264 // =======================================================================
272 sfx_t *S_GetCached (const char *name)
276 if (!snd_initialized)
280 Host_Error("S_IsCached: NULL\n");
282 if (strlen(name) >= MAX_QPATH)
283 Host_Error("Sound name too long: %s", name);
285 for(i = 0;i < num_sfx;i++)
286 if(!strcmp(known_sfx[i].name, name))
287 return &known_sfx[i];
298 sfx_t *S_FindName (char *name)
303 if (!snd_initialized)
307 Host_Error("S_FindName: NULL\n");
309 if (strlen(name) >= MAX_QPATH)
310 Host_Error("Sound name too long: %s", name);
312 // see if already loaded
313 for (i = 0;i < num_sfx;i++)
314 if (!strcmp(known_sfx[i].name, name))
315 return &known_sfx[i];
317 if (num_sfx == MAX_SFX)
318 Sys_Error("S_FindName: out of sfx_t");
320 sfx = &known_sfx[num_sfx++];
321 memset(sfx, 0, sizeof(*sfx));
322 snprintf(sfx->name, sizeof(sfx->name), "%s", name);
333 void S_TouchSound (char *name)
344 sfx_t *S_PrecacheSound (char *name, int complain)
348 if (!snd_initialized)
351 sfx = S_FindName(name);
353 if (!nosound.integer && snd_precache.integer)
354 S_LoadSound(sfx, complain);
360 //=============================================================================
367 channel_t *SND_PickChannel(int entnum, int entchannel)
373 // Check for replacement sound, or find the best one to replace
375 life_left = 0x7fffffff;
376 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
378 if (entchannel != 0 // channel 0 never overrides
379 && channels[ch_idx].entnum == entnum
380 && (channels[ch_idx].entchannel == entchannel || entchannel == -1) )
381 { // always override sound from same entity
382 first_to_die = ch_idx;
386 // don't let monster sounds override player sounds
387 if (channels[ch_idx].entnum == cl.viewentity && entnum != cl.viewentity && channels[ch_idx].sfx)
390 if (channels[ch_idx].end - paintedtime < life_left)
392 life_left = channels[ch_idx].end - paintedtime;
393 first_to_die = ch_idx;
397 if (first_to_die == -1)
400 if (channels[first_to_die].sfx)
401 channels[first_to_die].sfx = NULL;
403 return &channels[first_to_die];
411 void SND_Spatialize(channel_t *ch, int isstatic)
413 vec_t dist, scale, pan;
416 // anything coming from the view entity will always be full volume
417 // LordHavoc: make sounds with ATTN_NONE have no spatialization
418 if (ch->entnum == cl.viewentity || ch->dist_mult == 0)
420 ch->leftvol = ch->master_vol;
421 ch->rightvol = ch->master_vol;
425 // update sound origin if we know about the entity
426 if (ch->entnum > 0 && cls.state == ca_connected && cl_entities[ch->entnum].state_current.active)
428 //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]);
429 VectorCopy(cl_entities[ch->entnum].state_current.origin, ch->origin);
430 if (cl_entities[ch->entnum].state_current.modelindex && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->soundfromcenter)
431 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);
434 // calculate stereo seperation and distance attenuation
435 VectorSubtract(ch->origin, listener_vieworigin, source_vec);
436 dist = VectorNormalizeLength(source_vec);
438 scale = ch->master_vol * (1.0 - (dist * ch->dist_mult));
440 pan = scale * DotProduct(listener_viewleft, source_vec);
441 // calculate the volumes
442 ch->leftvol = (int) (scale + pan);
443 ch->rightvol = (int) (scale - pan);
446 // LordHavoc: allow adjusting volume of static sounds
449 ch->leftvol *= snd_staticvolume.value;
450 ch->rightvol *= snd_staticvolume.value;
454 ch->leftvol = bound(0, ch->leftvol, 255);
455 ch->rightvol = bound(0, ch->rightvol, 255);
459 // =======================================================================
460 // Start a sound effect
461 // =======================================================================
463 void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
465 channel_t *target_chan, *check;
471 if (!sound_started || !sfx || nosound.integer)
476 // pick a channel to play on
477 target_chan = SND_PickChannel(entnum, entchannel);
482 memset (target_chan, 0, sizeof(*target_chan));
483 VectorCopy(origin, target_chan->origin);
484 target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
485 target_chan->master_vol = vol;
486 target_chan->entnum = entnum;
487 target_chan->entchannel = entchannel;
488 SND_Spatialize(target_chan, false);
490 if (!target_chan->leftvol && !target_chan->rightvol)
491 return; // not audible at all
494 sc = S_LoadSound (sfx, true);
497 target_chan->sfx = NULL;
498 return; // couldn't load the sound's data
501 target_chan->sfx = sfx;
502 target_chan->pos = 0.0;
503 target_chan->end = paintedtime + sc->length;
505 // if an identical sound has also been started this frame, offset the pos
506 // a bit to keep it from just making the first one louder
507 check = &channels[NUM_AMBIENTS];
508 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
510 if (check == target_chan)
512 if (check->sfx == sfx && !check->pos)
514 // LordHavoc: fixed skip calculations
515 skip = 0.1 * sc->speed;
516 if (skip > sc->length)
519 skip = rand() % skip;
520 target_chan->pos += skip;
521 target_chan->end -= skip;
527 void S_StopSound(int entnum, int entchannel)
531 for (i=0 ; i<MAX_DYNAMIC_CHANNELS ; i++)
533 if (channels[i].entnum == entnum
534 && channels[i].entchannel == entchannel)
537 channels[i].sfx = NULL;
543 void S_StopAllSounds(qboolean clear)
545 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
546 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
552 void S_StopAllSoundsC(void)
554 S_StopAllSounds(true);
557 void S_ClearBuffer(void)
561 if (!sound_started || !shm)
564 if (shm->samplebits == 8)
579 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pData, &dwSize, NULL, NULL, 0)) != DS_OK)
581 if (hresult != DSERR_BUFFERLOST)
583 Con_Printf ("S_ClearBuffer: DS::Lock Sound Buffer Failed\n");
590 Con_Printf ("S_ClearBuffer: DS: couldn't restore buffer\n");
596 memset(pData, clear, shm->samples * shm->samplebits/8);
598 pDSBuf->lpVtbl->Unlock(pDSBuf, pData, dwSize, NULL, 0);
605 int setsize = shm->samples * shm->samplebits / 8;
606 char *buf = shm->buffer;
611 // on i586/i686 optimized versions of glibc, glibc *wrongly* IMO,
612 // reads the memory area before writing to it causing a seg fault
613 // since the memory is PROT_WRITE only and not PROT_READ|PROT_WRITE
614 // memset(shm->buffer, clear, shm->samples * shm->samplebits/8);
624 void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation)
632 if (total_channels == MAX_CHANNELS)
634 Con_Printf ("total_channels == MAX_CHANNELS\n");
638 sc = S_LoadSound (sfx, true);
642 if (sc->loopstart == -1)
643 Con_DPrintf("Quake compatibility warning: Static sound \"%s\" is not looped\n", sfx->name);
645 ss = &channels[total_channels++];
646 memset(ss, 0, sizeof(*ss));
647 ss->forceloop = true;
649 VectorCopy (origin, ss->origin);
650 ss->master_vol = vol;
651 ss->dist_mult = (attenuation/64) / sound_nominal_clip_dist;
652 ss->end = paintedtime + sc->length;
654 SND_Spatialize (ss, true);
658 //=============================================================================
662 S_UpdateAmbientSounds
665 void S_UpdateAmbientSounds (void)
670 qbyte ambientlevels[NUM_AMBIENTS];
672 // LordHavoc: kill ambient sounds until proven otherwise
673 for (ambient_channel = 0 ; ambient_channel < NUM_AMBIENTS;ambient_channel++)
674 channels[ambient_channel].sfx = NULL;
676 if (!snd_ambient || ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
679 cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_vieworigin, ambientlevels, sizeof(ambientlevels));
681 // calc ambient sound levels
682 for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
684 if (ambient_sfx[ambient_channel] && ambient_sfx[ambient_channel]->silentlymissing)
686 chan = &channels[ambient_channel];
687 chan->forceloop = true;
688 chan->sfx = ambient_sfx[ambient_channel];
690 vol = ambient_level.value * ambientlevels[ambient_channel];
694 // don't adjust volume too fast
695 if (chan->master_vol < vol)
697 chan->master_vol += host_realframetime * ambient_fade.value;
698 if (chan->master_vol > vol)
699 chan->master_vol = vol;
701 else if (chan->master_vol > vol)
703 chan->master_vol -= host_realframetime * ambient_fade.value;
704 if (chan->master_vol < vol)
705 chan->master_vol = vol;
708 chan->leftvol = chan->rightvol = chan->master_vol;
717 Called once each time through the main loop
720 void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up)
727 if (!snd_initialized || (snd_blocked > 0))
730 VectorCopy(origin, listener_vieworigin);
731 VectorCopy(forward, listener_viewforward);
732 VectorCopy(left, listener_viewleft);
733 VectorCopy(up, listener_viewup);
735 // update general area ambient sound sources
736 S_UpdateAmbientSounds ();
740 // update spatialization for static and dynamic sounds
741 ch = channels+NUM_AMBIENTS;
742 for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
746 SND_Spatialize(ch, i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS); // respatialize channel
747 if (!ch->leftvol && !ch->rightvol)
750 // try to combine static sounds with a previous channel of the same
751 // sound effect so we don't mix five torches every frame
753 if (i > MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
755 // see if it can just use the last one
756 if (combine && combine->sfx == ch->sfx)
758 combine->leftvol += ch->leftvol;
759 combine->rightvol += ch->rightvol;
760 ch->leftvol = ch->rightvol = 0;
764 combine = channels+MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
765 for (j=MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS ; j<i; j++, combine++)
766 if (combine->sfx == ch->sfx)
769 if (j == total_channels)
775 combine->leftvol += ch->leftvol;
776 combine->rightvol += ch->rightvol;
777 ch->leftvol = ch->rightvol = 0;
787 if (snd_show.integer)
791 for (i=0 ; i<total_channels; i++, ch++)
792 if (ch->sfx && (ch->leftvol || ch->rightvol) )
795 Con_Printf ("----(%i)----\n", total);
802 void GetSoundtime(void)
806 static int oldsamplepos;
809 fullsamples = shm->samples / shm->channels;
811 // it is possible to miscount buffers if it has wrapped twice between
812 // calls to S_Update. Oh well.
814 soundtime = SNDDMA_GetSamples();
816 samplepos = SNDDMA_GetDMAPos();
819 if (samplepos < oldsamplepos)
821 buffers++; // buffer wrapped
823 if (paintedtime > 0x40000000)
824 { // time to chop things off to avoid 32 bit limits
826 paintedtime = fullsamples;
827 S_StopAllSounds (true);
830 oldsamplepos = samplepos;
832 soundtime = buffers*fullsamples + samplepos/shm->channels;
836 void IN_Accumulate (void);
838 void S_ExtraUpdate (void)
845 if (snd_noextraupdate.integer)
846 return; // don't pollute timings
855 if (!sound_started || (snd_blocked > 0))
861 // check to make sure that we haven't overshot
862 if (paintedtime < soundtime)
863 paintedtime = soundtime;
865 // mix ahead of current position
866 endtime = soundtime + _snd_mixahead.value * shm->speed;
867 samps = shm->samples >> (shm->channels-1);
868 if (endtime > (unsigned int)(soundtime + samps))
869 endtime = soundtime + samps;
872 // if the buffer was lost or stopped, restore it and/or restart it
878 if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK)
879 Con_Printf ("Couldn't get sound buffer status\n");
881 if (dwStatus & DSBSTATUS_BUFFERLOST)
882 pDSBuf->lpVtbl->Restore (pDSBuf);
884 if (!(dwStatus & DSBSTATUS_PLAYING))
885 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
890 S_PaintChannels (endtime);
896 ===============================================================================
900 ===============================================================================
903 static void S_Play_Common(float fvol, float attenuation)
912 if (!strrchr(Cmd_Argv(i), '.'))
913 snprintf(name, sizeof(name), "%s.wav", Cmd_Argv(i));
915 strlcpy(name, Cmd_Argv(i), sizeof(name));
916 sfx = S_PrecacheSound(name, true);
918 // If we need to get the volume from the command line
921 fvol = atof(Cmd_Argv(i+1));
927 S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
933 S_Play_Common (1.0f, 1.0f);
938 S_Play_Common (1.0f, 0.0f);
943 S_Play_Common (-1.0f, 0.0f);
946 void S_SoundList(void)
954 for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
959 size = sc->length*sc->width*(sc->stereo+1);
961 Con_Printf("%c(%2db) %6i : %s\n", sc->loopstart >= 0 ? 'L' : ' ',sc->width*8, size, sfx->name);
964 Con_Printf("Total resident: %i\n", total);
968 void S_LocalSound (char *sound)
972 if (!snd_initialized || nosound.integer)
975 sfx = S_PrecacheSound (sound, true);
978 Con_Printf ("S_LocalSound: can't precache %s\n", sound);
981 S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
985 void S_ClearPrecache (void)
990 void S_BeginPrecaching (void)
995 void S_EndPrecaching (void)
1000 #define RAWSAMPLESBUFFER 32768
1001 short s_rawsamplesbuffer[RAWSAMPLESBUFFER * 2];
1002 int s_rawsamplesbuffer_start;
1003 int s_rawsamplesbuffer_count;
1005 void S_RawSamples_Enqueue(short *samples, unsigned int length)
1008 //Con_Printf("S_RawSamples_Enqueue: %i samples\n", length);
1009 if (s_rawsamplesbuffer_count + length > RAWSAMPLESBUFFER)
1011 b2 = (s_rawsamplesbuffer_start + s_rawsamplesbuffer_count) % RAWSAMPLESBUFFER;
1012 if (b2 + length > RAWSAMPLESBUFFER)
1014 b3 = (b2 + length) % RAWSAMPLESBUFFER;
1015 memcpy(s_rawsamplesbuffer + b2 * 2, samples, (RAWSAMPLESBUFFER - b2) * sizeof(short[2]));
1016 memcpy(s_rawsamplesbuffer, samples + (RAWSAMPLESBUFFER - b2) * 2, b3 * sizeof(short[2]));
1019 memcpy(s_rawsamplesbuffer + b2 * 2, samples, length * sizeof(short[2]));
1020 s_rawsamplesbuffer_count += length;
1023 void S_RawSamples_Dequeue(int *samples, unsigned int length)
1031 if (l > s_rawsamplesbuffer_count)
1032 l = s_rawsamplesbuffer_count;
1033 b1 = (s_rawsamplesbuffer_start) % RAWSAMPLESBUFFER;
1034 if (b1 + l > RAWSAMPLESBUFFER)
1036 b2 = (b1 + l) % RAWSAMPLESBUFFER;
1037 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, (RAWSAMPLESBUFFER - b1) * sizeof(short[2]));
1038 //memcpy(samples + (RAWSAMPLESBUFFER - b1) * 2, s_rawsamplesbuffer, b2 * sizeof(short[2]));
1039 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = (RAWSAMPLESBUFFER - b1) * 2, i = 0;i < count;i++)
1041 for (out = samples + (RAWSAMPLESBUFFER - b1) * 2, in = s_rawsamplesbuffer, count = b2 * 2, i = 0;i < count;i++)
1043 //Con_Printf("S_RawSamples_Dequeue: buffer wrap %i %i\n", (RAWSAMPLESBUFFER - b1), b2);
1047 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, l * sizeof(short[2]));
1048 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = l * 2, i = 0;i < count;i++)
1050 //Con_Printf("S_RawSamples_Dequeue: normal %i\n", l);
1052 if (l < (int)length)
1054 memset(samples + l * 2, 0, (length - l) * sizeof(int[2]));
1055 //Con_Printf("S_RawSamples_Dequeue: padding with %i samples\n", length - l);
1057 s_rawsamplesbuffer_start = (s_rawsamplesbuffer_start + l) % RAWSAMPLESBUFFER;
1058 s_rawsamplesbuffer_count -= l;
1061 void S_RawSamples_ClearQueue(void)
1063 s_rawsamplesbuffer_count = 0;
1064 s_rawsamplesbuffer_start = 0;
1067 int S_RawSamples_QueueWantsMore(void)
1069 if (shm != NULL && s_rawsamplesbuffer_count < min(shm->speed >> 1, RAWSAMPLESBUFFER >> 1))
1070 return RAWSAMPLESBUFFER - s_rawsamplesbuffer_count;
1075 void S_ResampleBuffer16Stereo(short *input, int inputlength, short *output, int outputlength)
1077 if (inputlength != outputlength)
1079 int i, position, stopposition, step;
1081 step = (float) inputlength * 256.0f / (float) outputlength;
1083 stopposition = (inputlength - 1) << 8;
1085 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1087 in = input + ((position >> 8) << 1);
1088 out[0] = (((in[1] - in[0]) * (position & 255)) >> 8) + in[0];
1089 out[1] = (((in[3] - in[2]) * (position & 255)) >> 8) + in[2];
1092 stopposition = inputlength << 8;
1093 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1095 in = input + ((position >> 8) << 1);
1102 memcpy(output, input, inputlength * sizeof(short[2]));
1105 int S_RawSamples_SampleRate(void)
1107 return shm != NULL ? shm->speed : 0;