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_origin;
52 vec3_t listener_forward;
53 vec3_t listener_right;
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 submission_chunk\n", shm->submission_chunk);
127 Con_Printf("%5d speed\n", shm->speed);
128 Con_Printf("0x%x dma buffer\n", shm->buffer);
129 Con_Printf("%5d total_channels\n", total_channels);
138 void S_Startup (void)
142 if (!snd_initialized)
151 Con_Printf("S_Startup: SNDDMA_Init failed.\n");
167 Con_DPrintf("\nSound Initialization\n");
169 S_RawSamples_ClearQueue();
171 Cvar_RegisterVariable(&volume);
172 Cvar_RegisterVariable(&bgmvolume);
173 Cvar_RegisterVariable(&snd_staticvolume);
175 if (COM_CheckParm("-nosound") || COM_CheckParm("-safe"))
178 snd_mempool = Mem_AllocPool("sound");
180 if (COM_CheckParm("-simsound"))
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);
190 Cvar_RegisterVariable(&nosound);
191 Cvar_RegisterVariable(&snd_precache);
192 Cvar_RegisterVariable(&bgmbuffer);
193 Cvar_RegisterVariable(&ambient_level);
194 Cvar_RegisterVariable(&ambient_fade);
195 Cvar_RegisterVariable(&snd_noextraupdate);
196 Cvar_RegisterVariable(&snd_show);
197 Cvar_RegisterVariable(&_snd_mixahead);
198 Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
200 snd_initialized = true;
202 known_sfx = Mem_Alloc(snd_mempool, MAX_SFX*sizeof(sfx_t));
207 SND_InitScaletable ();
209 // create a piece of DMA memory
213 shm = (void *) Mem_Alloc(snd_mempool, sizeof(*shm));
214 shm->splitbuffer = 0;
215 shm->samplebits = 16;
218 shm->samples = 32768;
220 shm->soundalive = true;
221 shm->gamealive = true;
222 shm->submission_chunk = 1;
223 shm->buffer = Mem_Alloc(snd_mempool, shm->channels * shm->samples * (shm->samplebits / 8));
229 Con_DPrintf ("Sound sampling rate: %i\n", shm->speed);
231 // provides a tick sound until washed clean
233 ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav", false);
234 ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav", false);
236 S_StopAllSounds (true);
240 // =======================================================================
241 // Shutdown sound engine
242 // =======================================================================
244 void S_Shutdown(void)
260 // =======================================================================
262 // =======================================================================
270 sfx_t *S_FindName (char *name)
276 Host_Error ("S_FindName: NULL\n");
278 if (strlen(name) >= MAX_QPATH)
279 Host_Error ("Sound name too long: %s", name);
281 // see if already loaded
282 for (i = 0;i < num_sfx;i++)
283 if (!strcmp(known_sfx[i].name, name))
284 return &known_sfx[i];
286 if (num_sfx == MAX_SFX)
287 Sys_Error ("S_FindName: out of sfx_t");
290 memset(sfx, 0, sizeof(*sfx));
291 strcpy (sfx->name, name);
305 void S_TouchSound (char *name)
312 sfx = S_FindName (name);
321 sfx_t *S_PrecacheSound (char *name, int complain)
325 if (!sound_started || nosound.integer)
328 sfx = S_FindName (name);
331 if (snd_precache.integer)
332 S_LoadSound (sfx, complain);
338 //=============================================================================
345 channel_t *SND_PickChannel(int entnum, int entchannel)
351 // Check for replacement sound, or find the best one to replace
353 life_left = 0x7fffffff;
354 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
356 if (entchannel != 0 // channel 0 never overrides
357 && channels[ch_idx].entnum == entnum
358 && (channels[ch_idx].entchannel == entchannel || entchannel == -1) )
359 { // always override sound from same entity
360 first_to_die = ch_idx;
364 // don't let monster sounds override player sounds
365 if (channels[ch_idx].entnum == cl.viewentity && entnum != cl.viewentity && channels[ch_idx].sfx)
368 if (channels[ch_idx].end - paintedtime < life_left)
370 life_left = channels[ch_idx].end - paintedtime;
371 first_to_die = ch_idx;
375 if (first_to_die == -1)
378 if (channels[first_to_die].sfx)
379 channels[first_to_die].sfx = NULL;
381 return &channels[first_to_die];
389 void SND_Spatialize(channel_t *ch, int isstatic)
393 vec_t lscale, rscale, scale;
397 // anything coming from the view entity will always be full volume
398 // LordHavoc: make sounds with ATTN_NONE have no spatialization
399 if (ch->entnum == cl.viewentity || ch->dist_mult == 0)
401 ch->leftvol = ch->master_vol;
402 ch->rightvol = ch->master_vol;
406 // update sound origin if we know about the entity
407 if (ch->entnum > 0 && cls.state == ca_connected && cl_entities[ch->entnum].state_current.active)
409 //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]);
410 VectorCopy(cl_entities[ch->entnum].state_current.origin, ch->origin);
411 if (cl_entities[ch->entnum].state_current.modelindex && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->brush.TraceBox)
412 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);
415 // calculate stereo seperation and distance attenuation
417 VectorSubtract(ch->origin, listener_origin, source_vec);
419 dist = VectorNormalizeLength(source_vec) * ch->dist_mult;
421 dot = DotProduct(listener_right, source_vec);
423 if (shm->channels == 1)
434 // add in distance effect
435 scale = (1.0 - dist) * rscale;
436 ch->rightvol = (int) (ch->master_vol * scale);
437 if (ch->rightvol < 0)
440 scale = (1.0 - dist) * lscale;
441 ch->leftvol = (int) (ch->master_vol * scale);
446 // LordHavoc: allow adjusting volume of static sounds
449 ch->leftvol *= snd_staticvolume.value;
450 ch->rightvol *= snd_staticvolume.value;
455 // =======================================================================
456 // Start a sound effect
457 // =======================================================================
459 void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
461 channel_t *target_chan, *check;
478 // pick a channel to play on
479 target_chan = SND_PickChannel(entnum, entchannel);
484 memset (target_chan, 0, sizeof(*target_chan));
486 if (cls.state == ca_connected && entnum > 0 && cl_entities[entnum].state_current.active)
488 target_chan.follow = true;
489 VectorSubtract(origin, cl_entities[entnum].state_current.origin, target_chan.followoriginoffset);
492 VectorCopy(origin, target_chan->origin);
493 target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
494 target_chan->master_vol = vol;
495 target_chan->entnum = entnum;
496 target_chan->entchannel = entchannel;
497 SND_Spatialize(target_chan, false);
499 if (!target_chan->leftvol && !target_chan->rightvol)
500 return; // not audible at all
503 sc = S_LoadSound (sfx, true);
506 target_chan->sfx = NULL;
507 return; // couldn't load the sound's data
510 target_chan->sfx = sfx;
511 target_chan->pos = 0.0;
512 target_chan->end = paintedtime + sc->length;
514 // if an identical sound has also been started this frame, offset the pos
515 // a bit to keep it from just making the first one louder
516 check = &channels[NUM_AMBIENTS];
517 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
519 if (check == target_chan)
521 if (check->sfx == sfx && !check->pos)
523 // LordHavoc: fixed skip calculations
524 skip = 0.1 * shm->speed;
525 if (skip > sc->length)
528 skip = rand() % skip;
529 target_chan->pos += skip;
530 target_chan->end -= skip;
537 void S_StopSound(int entnum, int entchannel)
541 for (i=0 ; i<MAX_DYNAMIC_CHANNELS ; i++)
543 if (channels[i].entnum == entnum
544 && channels[i].entchannel == entchannel)
547 channels[i].sfx = NULL;
553 void S_StopAllSounds(qboolean clear)
558 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
559 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
565 void S_StopAllSoundsC (void)
567 S_StopAllSounds (true);
570 void S_ClearBuffer (void)
575 if (!sound_started || !shm || (!shm->buffer && !pDSBuf))
577 if (!sound_started || !shm || !shm->buffer)
581 if (shm->samplebits == 8)
596 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pData, &dwSize, NULL, NULL, 0)) != DS_OK)
598 if (hresult != DSERR_BUFFERLOST)
600 Con_Printf ("S_ClearBuffer: DS::Lock Sound Buffer Failed\n");
607 Con_Printf ("S_ClearBuffer: DS: couldn't restore buffer\n");
613 memset(pData, clear, shm->samples * shm->samplebits/8);
615 pDSBuf->lpVtbl->Unlock(pDSBuf, pData, dwSize, NULL, 0);
621 int setsize = shm->samples * shm->samplebits / 8;
622 char *buf = shm->buffer;
627 // on i586/i686 optimized versions of glibc, glibc *wrongly* IMO,
628 // reads the memory area before writing to it causing a seg fault
629 // since the memory is PROT_WRITE only and not PROT_READ|PROT_WRITE
630 // memset(shm->buffer, clear, shm->samples * shm->samplebits/8);
640 void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation)
648 if (total_channels == MAX_CHANNELS)
650 Con_Printf ("total_channels == MAX_CHANNELS\n");
654 sc = S_LoadSound (sfx, true);
658 if (sc->loopstart == -1)
659 Con_DPrintf("Quake compatibility warning: Static sound \"%s\" is not looped\n", sfx->name);
661 ss = &channels[total_channels++];
662 memset(ss, 0, sizeof(*ss));
663 ss->forceloop = true;
665 VectorCopy (origin, ss->origin);
666 ss->master_vol = vol;
667 ss->dist_mult = (attenuation/64) / sound_nominal_clip_dist;
668 ss->end = paintedtime + sc->length;
670 SND_Spatialize (ss, true);
674 //=============================================================================
678 S_UpdateAmbientSounds
681 void S_UpdateAmbientSounds (void)
686 qbyte ambientlevels[NUM_AMBIENTS];
688 // LordHavoc: kill ambient sounds until proven otherwise
689 for (ambient_channel = 0 ; ambient_channel < NUM_AMBIENTS;ambient_channel++)
690 channels[ambient_channel].sfx = NULL;
692 if (!snd_ambient || ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
695 cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_origin, ambientlevels, sizeof(ambientlevels));
697 // calc ambient sound levels
698 for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
700 if (ambient_sfx[ambient_channel] && ambient_sfx[ambient_channel]->silentlymissing)
702 chan = &channels[ambient_channel];
703 chan->forceloop = true;
704 chan->sfx = ambient_sfx[ambient_channel];
706 vol = ambient_level.value * ambientlevels[ambient_channel];
710 // don't adjust volume too fast
711 if (chan->master_vol < vol)
713 chan->master_vol += host_realframetime * ambient_fade.value;
714 if (chan->master_vol > vol)
715 chan->master_vol = vol;
717 else if (chan->master_vol > vol)
719 chan->master_vol -= host_realframetime * ambient_fade.value;
720 if (chan->master_vol < vol)
721 chan->master_vol = vol;
724 chan->leftvol = chan->rightvol = chan->master_vol;
733 Called once each time through the main loop
736 void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
743 if (!sound_started || (snd_blocked > 0))
746 VectorCopy(origin, listener_origin);
747 VectorCopy(forward, listener_forward);
748 VectorCopy(right, listener_right);
749 VectorCopy(up, listener_up);
751 // update general area ambient sound sources
752 S_UpdateAmbientSounds ();
756 // update spatialization for static and dynamic sounds
757 ch = channels+NUM_AMBIENTS;
758 for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
762 SND_Spatialize(ch, i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS); // respatialize channel
763 if (!ch->leftvol && !ch->rightvol)
766 // try to combine static sounds with a previous channel of the same
767 // sound effect so we don't mix five torches every frame
769 if (i > MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
771 // see if it can just use the last one
772 if (combine && combine->sfx == ch->sfx)
774 combine->leftvol += ch->leftvol;
775 combine->rightvol += ch->rightvol;
776 ch->leftvol = ch->rightvol = 0;
780 combine = channels+MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
781 for (j=MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS ; j<i; j++, combine++)
782 if (combine->sfx == ch->sfx)
785 if (j == total_channels)
791 combine->leftvol += ch->leftvol;
792 combine->rightvol += ch->rightvol;
793 ch->leftvol = ch->rightvol = 0;
803 if (snd_show.integer)
807 for (i=0 ; i<total_channels; i++, ch++)
808 if (ch->sfx && (ch->leftvol || ch->rightvol) )
811 Con_Printf ("----(%i)----\n", total);
818 void GetSoundtime(void)
822 static int oldsamplepos;
825 fullsamples = shm->samples / shm->channels;
827 // it is possible to miscount buffers if it has wrapped twice between
828 // calls to S_Update. Oh well.
830 soundtime = SNDDMA_GetSamples();
832 samplepos = SNDDMA_GetDMAPos();
835 if (samplepos < oldsamplepos)
837 buffers++; // buffer wrapped
839 if (paintedtime > 0x40000000)
840 { // time to chop things off to avoid 32 bit limits
842 paintedtime = fullsamples;
843 S_StopAllSounds (true);
846 oldsamplepos = samplepos;
848 soundtime = buffers*fullsamples + samplepos/shm->channels;
852 void IN_Accumulate (void);
854 void S_ExtraUpdate (void)
861 if (snd_noextraupdate.integer)
862 return; // don't pollute timings
871 if (!sound_started || (snd_blocked > 0))
877 // check to make sure that we haven't overshot
878 if (paintedtime < soundtime)
879 paintedtime = soundtime;
881 // mix ahead of current position
882 endtime = soundtime + _snd_mixahead.value * shm->speed;
883 samps = shm->samples >> (shm->channels-1);
884 if (endtime > (unsigned int)(soundtime + samps))
885 endtime = soundtime + samps;
888 // if the buffer was lost or stopped, restore it and/or restart it
894 if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK)
895 Con_Printf ("Couldn't get sound buffer status\n");
897 if (dwStatus & DSBSTATUS_BUFFERLOST)
898 pDSBuf->lpVtbl->Restore (pDSBuf);
900 if (!(dwStatus & DSBSTATUS_PLAYING))
901 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
906 S_PaintChannels (endtime);
912 ===============================================================================
916 ===============================================================================
928 if (!strrchr(Cmd_Argv(i), '.'))
930 strcpy(name, Cmd_Argv(i));
931 strcat(name, ".wav");
934 strcpy(name, Cmd_Argv(i));
935 sfx = S_PrecacheSound(name, true);
936 S_StartSound(-1, 0, sfx, listener_origin, 1.0, 1.0);
950 if (!strrchr(Cmd_Argv(i), '.'))
952 strcpy(name, Cmd_Argv(i));
953 strcat(name, ".wav");
956 strcpy(name, Cmd_Argv(i));
957 sfx = S_PrecacheSound(name, true);
958 S_StartSound(-1, 0, sfx, listener_origin, 1.0, 0.0);
973 if (!strrchr(Cmd_Argv(i), '.'))
975 strcpy(name, Cmd_Argv(i));
976 strcat(name, ".wav");
979 strcpy(name, Cmd_Argv(i));
980 sfx = S_PrecacheSound(name, true);
981 vol = atof(Cmd_Argv(i+1));
982 S_StartSound(-1, 0, sfx, listener_origin, vol, 1.0);
987 void S_SoundList(void)
995 for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
1000 size = sc->length*sc->width*(sc->stereo+1);
1002 if (sc->loopstart >= 0)
1006 Con_Printf("(%2db) %6i : %s\n",sc->width*8, size, sfx->name);
1008 Con_Printf ("Total resident: %i\n", total);
1012 void S_LocalSound (char *sound)
1016 if (nosound.integer)
1021 sfx = S_PrecacheSound (sound, true);
1024 Con_Printf ("S_LocalSound: can't precache %s\n", sound);
1027 S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
1031 void S_ClearPrecache (void)
1036 void S_BeginPrecaching (void)
1041 void S_EndPrecaching (void)
1046 #define RAWSAMPLESBUFFER 32768
1047 short s_rawsamplesbuffer[RAWSAMPLESBUFFER * 2];
1048 int s_rawsamplesbuffer_start;
1049 int s_rawsamplesbuffer_count;
1051 void S_RawSamples_Enqueue(short *samples, unsigned int length)
1054 //Con_Printf("S_RawSamples_Enqueue: %i samples\n", length);
1055 if (s_rawsamplesbuffer_count + length > RAWSAMPLESBUFFER)
1057 b2 = (s_rawsamplesbuffer_start + s_rawsamplesbuffer_count) % RAWSAMPLESBUFFER;
1058 if (b2 + length > RAWSAMPLESBUFFER)
1060 b3 = (b2 + length) % RAWSAMPLESBUFFER;
1061 memcpy(s_rawsamplesbuffer + b2 * 2, samples, (RAWSAMPLESBUFFER - b2) * sizeof(short[2]));
1062 memcpy(s_rawsamplesbuffer, samples + (RAWSAMPLESBUFFER - b2) * 2, b3 * sizeof(short[2]));
1065 memcpy(s_rawsamplesbuffer + b2 * 2, samples, length * sizeof(short[2]));
1066 s_rawsamplesbuffer_count += length;
1069 void S_RawSamples_Dequeue(int *samples, unsigned int length)
1077 if (l > s_rawsamplesbuffer_count)
1078 l = s_rawsamplesbuffer_count;
1079 b1 = (s_rawsamplesbuffer_start) % RAWSAMPLESBUFFER;
1080 if (b1 + l > RAWSAMPLESBUFFER)
1082 b2 = (b1 + l) % RAWSAMPLESBUFFER;
1083 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, (RAWSAMPLESBUFFER - b1) * sizeof(short[2]));
1084 //memcpy(samples + (RAWSAMPLESBUFFER - b1) * 2, s_rawsamplesbuffer, b2 * sizeof(short[2]));
1085 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = (RAWSAMPLESBUFFER - b1) * 2, i = 0;i < count;i++)
1087 for (out = samples + (RAWSAMPLESBUFFER - b1) * 2, in = s_rawsamplesbuffer, count = b2 * 2, i = 0;i < count;i++)
1089 //Con_Printf("S_RawSamples_Dequeue: buffer wrap %i %i\n", (RAWSAMPLESBUFFER - b1), b2);
1093 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, l * sizeof(short[2]));
1094 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = l * 2, i = 0;i < count;i++)
1096 //Con_Printf("S_RawSamples_Dequeue: normal %i\n", l);
1098 if (l < (int)length)
1100 memset(samples + l * 2, 0, (length - l) * sizeof(int[2]));
1101 //Con_Printf("S_RawSamples_Dequeue: padding with %i samples\n", length - l);
1103 s_rawsamplesbuffer_start = (s_rawsamplesbuffer_start + l) % RAWSAMPLESBUFFER;
1104 s_rawsamplesbuffer_count -= l;
1107 void S_RawSamples_ClearQueue(void)
1109 s_rawsamplesbuffer_count = 0;
1110 s_rawsamplesbuffer_start = 0;
1113 int S_RawSamples_QueueWantsMore(void)
1115 if (s_rawsamplesbuffer_count < min(shm->speed >> 1, RAWSAMPLESBUFFER >> 1))
1116 return RAWSAMPLESBUFFER - s_rawsamplesbuffer_count;
1121 void S_ResampleBuffer16Stereo(short *input, int inputlength, short *output, int outputlength)
1123 if (inputlength != outputlength)
1125 int i, position, stopposition, step;
1127 step = (float) inputlength * 256.0f / (float) outputlength;
1129 stopposition = (inputlength - 1) << 8;
1131 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1133 in = input + ((position >> 8) << 1);
1134 out[0] = (((in[1] - in[0]) * (position & 255)) >> 8) + in[0];
1135 out[1] = (((in[3] - in[2]) * (position & 255)) >> 8) + in[2];
1138 stopposition = inputlength << 8;
1139 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1141 in = input + ((position >> 8) << 1);
1148 memcpy(output, input, inputlength * sizeof(short[2]));
1151 int S_RawSamples_SampleRate(void)
1153 return shm != NULL ? shm->speed : 0;