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 // =======================================================================
44 // Internal sound data & structures
45 // =======================================================================
47 channel_t channels[MAX_CHANNELS];
51 static qboolean snd_ambient = 1;
52 cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
54 // pointer should go away
55 volatile dma_t *shm = 0;
58 vec3_t listener_vieworigin;
59 vec3_t listener_viewforward;
60 vec3_t listener_viewleft;
61 vec3_t listener_viewup;
62 vec_t sound_nominal_clip_dist=1000.0;
63 mempool_t *snd_mempool;
70 //LordHavoc: increased the client sound limit from 512 to 4096 for the Nehahra movie
72 sfx_t *known_sfx; // allocated [MAX_SFX]
75 sfx_t *ambient_sfx[NUM_AMBIENTS];
77 int sound_started = 0;
79 cvar_t bgmvolume = {CVAR_SAVE, "bgmvolume", "1"};
80 cvar_t volume = {CVAR_SAVE, "volume", "0.7"};
81 cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1"};
83 cvar_t nosound = {0, "nosound", "0"};
84 cvar_t snd_precache = {0, "snd_precache", "1"};
85 cvar_t bgmbuffer = {0, "bgmbuffer", "4096"};
86 cvar_t ambient_level = {0, "ambient_level", "0.3"};
87 cvar_t ambient_fade = {0, "ambient_fade", "100"};
88 cvar_t snd_noextraupdate = {0, "snd_noextraupdate", "0"};
89 cvar_t snd_show = {0, "snd_show", "0"};
90 cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.1"};
91 cvar_t snd_swapstereo = {CVAR_SAVE, "snd_swapstereo", "0"};
94 // ====================================================================
95 // User-setable variables
96 // ====================================================================
100 // Fake dma is a synchronous faking of the DMA progress used for
101 // isolating performance in the renderer. The fakedma_updates is
102 // number of times S_Update() is called per second.
105 qboolean fakedma = false;
106 int fakedma_updates = 15;
109 void S_AmbientOff (void)
115 void S_AmbientOn (void)
121 void S_SoundInfo_f(void)
123 if (!sound_started || !shm)
125 Con_Printf ("sound system not started\n");
129 Con_Printf("%5d stereo\n", shm->channels - 1);
130 Con_Printf("%5d samples\n", shm->samples);
131 Con_Printf("%5d samplepos\n", shm->samplepos);
132 Con_Printf("%5d samplebits\n", shm->samplebits);
133 Con_Printf("%5d speed\n", shm->speed);
134 Con_Printf("0x%x dma buffer\n", shm->buffer);
135 Con_Printf("%5d total_channels\n", total_channels);
138 void S_UnloadSounds(void)
141 for (i = 0;i < num_sfx;i++)
142 S_UnloadSound(known_sfx + i);
145 void S_LoadSounds(void)
148 for (i = 0;i < num_sfx;i++)
149 S_LoadSound(known_sfx + i, false);
154 if (!snd_initialized.integer)
158 memset((void *)shm, 0, sizeof(*shm));
160 // create a piece of DMA memory
164 shm->samplebits = 16;
167 shm->samples = 32768;
169 shm->buffer = Mem_Alloc(snd_mempool, shm->channels * shm->samples * (shm->samplebits / 8));
175 Con_Printf("S_Startup: SNDDMA_Init failed.\n");
184 Con_DPrintf("Sound sampling rate: %i\n", shm->speed);
188 S_StopAllSounds(true);
191 void S_Shutdown(void)
199 Mem_Free(shm->buffer);
207 void S_Restart_f(void)
220 Con_DPrintf("\nSound Initialization\n");
222 S_RawSamples_ClearQueue();
224 Cvar_RegisterVariable(&volume);
225 Cvar_RegisterVariable(&bgmvolume);
226 Cvar_RegisterVariable(&snd_staticvolume);
228 if (COM_CheckParm("-nosound") || COM_CheckParm("-safe"))
231 snd_mempool = Mem_AllocPool("sound");
233 if (COM_CheckParm("-simsound"))
236 Cmd_AddCommand("play", S_Play);
237 Cmd_AddCommand("play2", S_Play2);
238 Cmd_AddCommand("playvol", S_PlayVol);
239 Cmd_AddCommand("stopsound", S_StopAllSoundsC);
240 Cmd_AddCommand("soundlist", S_SoundList);
241 Cmd_AddCommand("soundinfo", S_SoundInfo_f);
242 Cmd_AddCommand("snd_restart", S_Restart_f);
244 Cvar_RegisterVariable(&nosound);
245 Cvar_RegisterVariable(&snd_precache);
246 Cvar_RegisterVariable(&snd_initialized);
247 Cvar_RegisterVariable(&bgmbuffer);
248 Cvar_RegisterVariable(&ambient_level);
249 Cvar_RegisterVariable(&ambient_fade);
250 Cvar_RegisterVariable(&snd_noextraupdate);
251 Cvar_RegisterVariable(&snd_show);
252 Cvar_RegisterVariable(&_snd_mixahead);
253 Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
255 Cvar_SetValueQuick(&snd_initialized, true);
257 known_sfx = Mem_Alloc(snd_mempool, MAX_SFX*sizeof(sfx_t));
260 SND_InitScaletable ();
262 ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav", false);
263 ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav", false);
265 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
266 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
272 // =======================================================================
274 // =======================================================================
282 sfx_t *S_GetCached (const char *name)
286 if (!snd_initialized.integer)
290 Host_Error("S_IsCached: NULL\n");
292 if (strlen(name) >= MAX_QPATH)
293 Host_Error("Sound name too long: %s", name);
295 for(i = 0;i < num_sfx;i++)
296 if(!strcmp(known_sfx[i].name, name))
297 return &known_sfx[i];
308 sfx_t *S_FindName (char *name)
313 if (!snd_initialized.integer)
317 Host_Error("S_FindName: NULL\n");
319 if (strlen(name) >= MAX_QPATH)
320 Host_Error("Sound name too long: %s", name);
322 // see if already loaded
323 for (i = 0;i < num_sfx;i++)
324 if (!strcmp(known_sfx[i].name, name))
325 return &known_sfx[i];
327 if (num_sfx == MAX_SFX)
328 Sys_Error("S_FindName: out of sfx_t");
330 sfx = &known_sfx[num_sfx++];
331 memset(sfx, 0, sizeof(*sfx));
332 snprintf(sfx->name, sizeof(sfx->name), "%s", name);
343 void S_TouchSound (char *name)
346 // TODO: set the "used" flag for this sound
356 void S_ClearUsed (void)
358 // TODO: reset the "used" flag of all precached sounds
368 void S_PurgeUnused (void)
370 // TODO: free all precached sounds without the "used" flag
380 sfx_t *S_PrecacheSound (char *name, int complain)
384 if (!snd_initialized.integer)
387 sfx = S_FindName(name);
389 if (!nosound.integer && snd_precache.integer)
390 S_LoadSound(sfx, complain);
396 //=============================================================================
403 channel_t *SND_PickChannel(int entnum, int entchannel)
409 // Check for replacement sound, or find the best one to replace
411 life_left = 0x7fffffff;
412 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
414 if (entchannel != 0 // channel 0 never overrides
415 && channels[ch_idx].entnum == entnum
416 && (channels[ch_idx].entchannel == entchannel || entchannel == -1) )
417 { // always override sound from same entity
418 first_to_die = ch_idx;
422 // don't let monster sounds override player sounds
423 if (channels[ch_idx].entnum == cl.viewentity && entnum != cl.viewentity && channels[ch_idx].sfx)
426 if (channels[ch_idx].end - paintedtime < life_left)
428 life_left = channels[ch_idx].end - paintedtime;
429 first_to_die = ch_idx;
433 if (first_to_die == -1)
436 if (channels[first_to_die].sfx)
437 channels[first_to_die].sfx = NULL;
439 return &channels[first_to_die];
447 void SND_Spatialize(channel_t *ch, int isstatic)
449 vec_t dist, scale, pan;
452 // anything coming from the view entity will always be full volume
453 // LordHavoc: make sounds with ATTN_NONE have no spatialization
454 if (ch->entnum == cl.viewentity || ch->dist_mult == 0)
456 ch->leftvol = ch->master_vol;
457 ch->rightvol = ch->master_vol;
461 // update sound origin if we know about the entity
462 if (ch->entnum > 0 && cls.state == ca_connected && cl_entities[ch->entnum].state_current.active)
464 //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]);
465 VectorCopy(cl_entities[ch->entnum].state_current.origin, ch->origin);
466 if (cl_entities[ch->entnum].state_current.modelindex && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->soundfromcenter)
467 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);
470 // calculate stereo seperation and distance attenuation
471 VectorSubtract(ch->origin, listener_vieworigin, source_vec);
472 dist = VectorNormalizeLength(source_vec);
474 scale = ch->master_vol * (1.0 - (dist * ch->dist_mult));
476 pan = scale * DotProduct(listener_viewleft, source_vec);
477 // calculate the volumes
478 ch->leftvol = (int) (scale + pan);
479 ch->rightvol = (int) (scale - pan);
482 // LordHavoc: allow adjusting volume of static sounds
485 ch->leftvol *= snd_staticvolume.value;
486 ch->rightvol *= snd_staticvolume.value;
490 ch->leftvol = bound(0, ch->leftvol, 255);
491 ch->rightvol = bound(0, ch->rightvol, 255);
495 // =======================================================================
496 // Start a sound effect
497 // =======================================================================
499 void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
501 channel_t *target_chan, *check;
507 if (!sound_started || !sfx || !sfx->sfxcache || nosound.integer)
512 // pick a channel to play on
513 target_chan = SND_PickChannel(entnum, entchannel);
518 memset (target_chan, 0, sizeof(*target_chan));
519 VectorCopy(origin, target_chan->origin);
520 target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
521 target_chan->master_vol = vol;
522 target_chan->entnum = entnum;
523 target_chan->entchannel = entchannel;
524 SND_Spatialize(target_chan, false);
526 // LordHavoc: spawn the sound anyway because the player might teleport to it
527 //if (!target_chan->leftvol && !target_chan->rightvol)
528 // return; // not audible at all
531 sc = S_LoadSound (sfx, true);
534 target_chan->sfx = NULL;
535 return; // couldn't load the sound's data
538 target_chan->sfx = sfx;
539 target_chan->pos = 0.0;
540 target_chan->end = paintedtime + sc->length;
542 // if an identical sound has also been started this frame, offset the pos
543 // a bit to keep it from just making the first one louder
544 check = &channels[NUM_AMBIENTS];
545 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
547 if (check == target_chan)
549 if (check->sfx == sfx && !check->pos)
551 // LordHavoc: fixed skip calculations
552 skip = 0.1 * sc->speed;
553 if (skip > sc->length)
556 skip = rand() % skip;
557 target_chan->pos += skip;
558 target_chan->end -= skip;
564 void S_StopSound(int entnum, int entchannel)
568 for (i=0 ; i<MAX_DYNAMIC_CHANNELS ; i++)
570 if (channels[i].entnum == entnum
571 && channels[i].entchannel == entchannel)
574 channels[i].sfx = NULL;
580 void S_StopAllSounds(qboolean clear)
582 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
583 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
589 void S_StopAllSoundsC(void)
591 S_StopAllSounds(true);
594 void S_ClearBuffer(void)
598 if (!sound_started || !shm)
601 if (shm->samplebits == 8)
616 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, (LPVOID*)&pData, &dwSize, NULL, NULL, 0)) != DS_OK)
618 if (hresult != DSERR_BUFFERLOST)
620 Con_Printf ("S_ClearBuffer: DS::Lock Sound Buffer Failed\n");
627 Con_Printf ("S_ClearBuffer: DS: couldn't restore buffer\n");
633 memset(pData, clear, shm->samples * shm->samplebits/8);
635 pDSBuf->lpVtbl->Unlock(pDSBuf, pData, dwSize, NULL, 0);
642 int setsize = shm->samples * shm->samplebits / 8;
643 char *buf = shm->buffer;
648 // on i586/i686 optimized versions of glibc, glibc *wrongly* IMO,
649 // reads the memory area before writing to it causing a seg fault
650 // since the memory is PROT_WRITE only and not PROT_READ|PROT_WRITE
651 // memset(shm->buffer, clear, shm->samples * shm->samplebits/8);
661 void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation)
669 if (total_channels == MAX_CHANNELS)
671 Con_Printf ("total_channels == MAX_CHANNELS\n");
675 sc = S_LoadSound (sfx, true);
679 if (sc->loopstart == -1)
680 Con_DPrintf("Quake compatibility warning: Static sound \"%s\" is not looped\n", sfx->name);
682 ss = &channels[total_channels++];
683 memset(ss, 0, sizeof(*ss));
684 ss->forceloop = true;
686 VectorCopy (origin, ss->origin);
687 ss->master_vol = vol;
688 ss->dist_mult = (attenuation/64) / sound_nominal_clip_dist;
689 ss->end = paintedtime + sc->length;
691 SND_Spatialize (ss, true);
695 //=============================================================================
699 S_UpdateAmbientSounds
702 void S_UpdateAmbientSounds (void)
707 qbyte ambientlevels[NUM_AMBIENTS];
709 // LordHavoc: kill ambient sounds until proven otherwise
710 for (ambient_channel = 0 ; ambient_channel < NUM_AMBIENTS;ambient_channel++)
711 channels[ambient_channel].sfx = NULL;
713 if (!snd_ambient || ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
716 cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_vieworigin, ambientlevels, sizeof(ambientlevels));
718 // calc ambient sound levels
719 for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
721 if (ambient_sfx[ambient_channel] && ambient_sfx[ambient_channel]->silentlymissing)
723 chan = &channels[ambient_channel];
724 chan->forceloop = true;
725 chan->sfx = ambient_sfx[ambient_channel];
727 vol = ambient_level.value * ambientlevels[ambient_channel];
731 // don't adjust volume too fast
732 if (chan->master_vol < vol)
734 chan->master_vol += host_realframetime * ambient_fade.value;
735 if (chan->master_vol > vol)
736 chan->master_vol = vol;
738 else if (chan->master_vol > vol)
740 chan->master_vol -= host_realframetime * ambient_fade.value;
741 if (chan->master_vol < vol)
742 chan->master_vol = vol;
745 chan->leftvol = chan->rightvol = chan->master_vol;
754 Called once each time through the main loop
757 void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up)
764 if (!snd_initialized.integer || (snd_blocked > 0))
767 VectorCopy(origin, listener_vieworigin);
768 VectorCopy(forward, listener_viewforward);
769 VectorCopy(left, listener_viewleft);
770 VectorCopy(up, listener_viewup);
772 // update general area ambient sound sources
773 S_UpdateAmbientSounds ();
777 // update spatialization for static and dynamic sounds
778 ch = channels+NUM_AMBIENTS;
779 for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
783 SND_Spatialize(ch, i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS); // respatialize channel
784 if (!ch->leftvol && !ch->rightvol)
787 // try to combine static sounds with a previous channel of the same
788 // sound effect so we don't mix five torches every frame
790 if (i > MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
792 // see if it can just use the last one
793 if (combine && combine->sfx == ch->sfx)
795 combine->leftvol += ch->leftvol;
796 combine->rightvol += ch->rightvol;
797 ch->leftvol = ch->rightvol = 0;
801 combine = channels+MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
802 for (j=MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS ; j<i; j++, combine++)
803 if (combine->sfx == ch->sfx)
806 if (j == total_channels)
812 combine->leftvol += ch->leftvol;
813 combine->rightvol += ch->rightvol;
814 ch->leftvol = ch->rightvol = 0;
824 if (snd_show.integer)
828 for (i=0 ; i<total_channels; i++, ch++)
829 if (ch->sfx && (ch->leftvol || ch->rightvol) )
832 Con_Printf ("----(%i)----\n", total);
839 void GetSoundtime(void)
843 static int oldsamplepos;
846 fullsamples = shm->samples / shm->channels;
848 // it is possible to miscount buffers if it has wrapped twice between
849 // calls to S_Update. Oh well.
850 samplepos = SNDDMA_GetDMAPos();
852 if (samplepos < oldsamplepos)
854 buffers++; // buffer wrapped
856 if (paintedtime > 0x40000000)
857 { // time to chop things off to avoid 32 bit limits
859 paintedtime = fullsamples;
860 S_StopAllSounds (true);
863 oldsamplepos = samplepos;
865 soundtime = buffers*fullsamples + samplepos/shm->channels;
868 void IN_Accumulate (void);
870 void S_ExtraUpdate (void)
877 if (snd_noextraupdate.integer)
878 return; // don't pollute timings
887 if (!sound_started || (snd_blocked > 0))
893 // check to make sure that we haven't overshot
894 if (paintedtime < soundtime)
895 paintedtime = soundtime;
897 // mix ahead of current position
898 endtime = soundtime + _snd_mixahead.value * shm->speed;
899 samps = shm->samples >> (shm->channels-1);
900 if (endtime > (unsigned int)(soundtime + samps))
901 endtime = soundtime + samps;
904 // if the buffer was lost or stopped, restore it and/or restart it
910 if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DS_OK)
911 Con_Printf ("Couldn't get sound buffer status\n");
913 if (dwStatus & DSBSTATUS_BUFFERLOST)
914 pDSBuf->lpVtbl->Restore (pDSBuf);
916 if (!(dwStatus & DSBSTATUS_PLAYING))
917 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
922 S_PaintChannels (endtime);
928 ===============================================================================
932 ===============================================================================
935 static void S_Play_Common(float fvol, float attenuation)
944 if (!strrchr(Cmd_Argv(i), '.'))
945 snprintf(name, sizeof(name), "%s.wav", Cmd_Argv(i));
947 strlcpy(name, Cmd_Argv(i), sizeof(name));
948 sfx = S_PrecacheSound(name, true);
950 // If we need to get the volume from the command line
953 fvol = atof(Cmd_Argv(i+1));
959 S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
965 S_Play_Common (1.0f, 1.0f);
970 S_Play_Common (1.0f, 0.0f);
975 S_Play_Common (-1.0f, 0.0f);
978 void S_SoundList(void)
986 for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
991 size = sc->length*sc->width*(sc->stereo+1);
993 Con_Printf("%c(%2db) %6i : %s\n", sc->loopstart >= 0 ? 'L' : ' ',sc->width*8, size, sfx->name);
996 Con_Printf("Total resident: %i\n", total);
1000 void S_LocalSound (char *sound)
1004 if (!snd_initialized.integer || nosound.integer)
1007 sfx = S_PrecacheSound (sound, true);
1010 Con_Printf ("S_LocalSound: can't precache %s\n", sound);
1013 S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
1017 #define RAWSAMPLESBUFFER 32768
1018 short s_rawsamplesbuffer[RAWSAMPLESBUFFER * 2];
1019 int s_rawsamplesbuffer_start;
1020 int s_rawsamplesbuffer_count;
1022 void S_RawSamples_Enqueue(short *samples, unsigned int length)
1025 //Con_Printf("S_RawSamples_Enqueue: %i samples\n", length);
1026 if (s_rawsamplesbuffer_count + length > RAWSAMPLESBUFFER)
1028 b2 = (s_rawsamplesbuffer_start + s_rawsamplesbuffer_count) % RAWSAMPLESBUFFER;
1029 if (b2 + length > RAWSAMPLESBUFFER)
1031 b3 = (b2 + length) % RAWSAMPLESBUFFER;
1032 memcpy(s_rawsamplesbuffer + b2 * 2, samples, (RAWSAMPLESBUFFER - b2) * sizeof(short[2]));
1033 memcpy(s_rawsamplesbuffer, samples + (RAWSAMPLESBUFFER - b2) * 2, b3 * sizeof(short[2]));
1036 memcpy(s_rawsamplesbuffer + b2 * 2, samples, length * sizeof(short[2]));
1037 s_rawsamplesbuffer_count += length;
1040 void S_RawSamples_Dequeue(int *samples, unsigned int length)
1048 if (l > s_rawsamplesbuffer_count)
1049 l = s_rawsamplesbuffer_count;
1050 b1 = (s_rawsamplesbuffer_start) % RAWSAMPLESBUFFER;
1051 if (b1 + l > RAWSAMPLESBUFFER)
1053 b2 = (b1 + l) % RAWSAMPLESBUFFER;
1054 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, (RAWSAMPLESBUFFER - b1) * sizeof(short[2]));
1055 //memcpy(samples + (RAWSAMPLESBUFFER - b1) * 2, s_rawsamplesbuffer, b2 * sizeof(short[2]));
1056 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = (RAWSAMPLESBUFFER - b1) * 2, i = 0;i < count;i++)
1058 for (out = samples + (RAWSAMPLESBUFFER - b1) * 2, in = s_rawsamplesbuffer, count = b2 * 2, i = 0;i < count;i++)
1060 //Con_Printf("S_RawSamples_Dequeue: buffer wrap %i %i\n", (RAWSAMPLESBUFFER - b1), b2);
1064 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, l * sizeof(short[2]));
1065 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = l * 2, i = 0;i < count;i++)
1067 //Con_Printf("S_RawSamples_Dequeue: normal %i\n", l);
1069 if (l < (int)length)
1071 memset(samples + l * 2, 0, (length - l) * sizeof(int[2]));
1072 //Con_Printf("S_RawSamples_Dequeue: padding with %i samples\n", length - l);
1074 s_rawsamplesbuffer_start = (s_rawsamplesbuffer_start + l) % RAWSAMPLESBUFFER;
1075 s_rawsamplesbuffer_count -= l;
1078 void S_RawSamples_ClearQueue(void)
1080 s_rawsamplesbuffer_count = 0;
1081 s_rawsamplesbuffer_start = 0;
1084 int S_RawSamples_QueueWantsMore(void)
1086 if (shm != NULL && s_rawsamplesbuffer_count < min(shm->speed >> 1, RAWSAMPLESBUFFER >> 1))
1087 return RAWSAMPLESBUFFER - s_rawsamplesbuffer_count;
1092 void S_ResampleBuffer16Stereo(short *input, int inputlength, short *output, int outputlength)
1094 if (inputlength != outputlength)
1096 int i, position, stopposition, step;
1098 step = (float) inputlength * 256.0f / (float) outputlength;
1100 stopposition = (inputlength - 1) << 8;
1102 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1104 in = input + ((position >> 8) << 1);
1105 out[0] = (((in[1] - in[0]) * (position & 255)) >> 8) + in[0];
1106 out[1] = (((in[3] - in[2]) * (position & 255)) >> 8) + in[2];
1109 stopposition = inputlength << 8;
1110 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1112 in = input + ((position >> 8) << 1);
1119 memcpy(output, input, inputlength * sizeof(short[2]));
1122 int S_RawSamples_SampleRate(void)
1124 return shm != NULL ? shm->speed : 0;