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];
48 unsigned int total_channels;
51 static qboolean snd_ambient = 1;
52 cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
53 cvar_t snd_streaming = { CVAR_SAVE, "snd_streaming", "1"};
55 // pointer should go away
56 volatile dma_t *shm = 0;
59 vec3_t listener_vieworigin;
60 vec3_t listener_viewforward;
61 vec3_t listener_viewleft;
62 vec3_t listener_viewup;
63 vec_t sound_nominal_clip_dist=1000.0;
64 mempool_t *snd_mempool;
71 //LordHavoc: increased the client sound limit from 512 to 4096 for the Nehahra movie
73 sfx_t *known_sfx; // allocated [MAX_SFX]
76 sfx_t *ambient_sfx[NUM_AMBIENTS];
78 int sound_started = 0;
80 cvar_t bgmvolume = {CVAR_SAVE, "bgmvolume", "1"};
81 cvar_t volume = {CVAR_SAVE, "volume", "0.7"};
82 cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1"};
84 cvar_t nosound = {0, "nosound", "0"};
85 cvar_t snd_precache = {0, "snd_precache", "1"};
86 cvar_t bgmbuffer = {0, "bgmbuffer", "4096"};
87 cvar_t ambient_level = {0, "ambient_level", "0.3"};
88 cvar_t ambient_fade = {0, "ambient_fade", "100"};
89 cvar_t snd_noextraupdate = {0, "snd_noextraupdate", "0"};
90 cvar_t snd_show = {0, "snd_show", "0"};
91 cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.1"};
92 cvar_t snd_swapstereo = {CVAR_SAVE, "snd_swapstereo", "0"};
95 // ====================================================================
96 // User-setable variables
97 // ====================================================================
101 // Fake dma is a synchronous faking of the DMA progress used for
102 // isolating performance in the renderer. The fakedma_updates is
103 // number of times S_Update() is called per second.
106 qboolean fakedma = false;
107 int fakedma_updates = 15;
110 void S_AmbientOff (void)
116 void S_AmbientOn (void)
122 void S_SoundInfo_f(void)
124 if (!sound_started || !shm)
126 Con_Print("sound system not started\n");
130 Con_Printf("%5d stereo\n", shm->format.channels - 1);
131 Con_Printf("%5d samples\n", shm->samples);
132 Con_Printf("%5d samplepos\n", shm->samplepos);
133 Con_Printf("%5d samplebits\n", shm->format.width * 8);
134 Con_Printf("%5d speed\n", shm->format.speed);
135 Con_Printf("0x%x dma buffer\n", shm->buffer);
136 Con_Printf("%5u total_channels\n", total_channels);
139 void S_UnloadSounds(void)
142 for (i = 0;i < num_sfx;i++)
143 S_UnloadSound(known_sfx + i);
146 void S_LoadSounds(void)
149 for (i = 0;i < num_sfx;i++)
150 S_LoadSound(known_sfx + i, false);
155 if (!snd_initialized.integer)
159 memset((void *)shm, 0, sizeof(*shm));
161 // create a piece of DMA memory
165 shm->format.width = 2;
166 shm->format.speed = 22050;
167 shm->format.channels = 2;
168 shm->samples = 32768;
170 shm->buffer = Mem_Alloc(snd_mempool, shm->format.channels * shm->samples * shm->format.width);
176 Con_Print("S_Startup: SNDDMA_Init failed.\n");
185 Con_DPrintf("Sound sampling rate: %i\n", shm->format.speed);
189 S_StopAllSounds(true);
192 void S_Shutdown(void)
200 Mem_Free(shm->buffer);
208 void S_Restart_f(void)
221 Con_DPrint("\nSound Initialization\n");
223 S_RawSamples_ClearQueue();
225 Cvar_RegisterVariable(&volume);
226 Cvar_RegisterVariable(&bgmvolume);
227 Cvar_RegisterVariable(&snd_staticvolume);
229 if (COM_CheckParm("-nosound") || COM_CheckParm("-safe"))
232 snd_mempool = Mem_AllocPool("sound");
234 if (COM_CheckParm("-simsound"))
237 Cmd_AddCommand("play", S_Play);
238 Cmd_AddCommand("play2", S_Play2);
239 Cmd_AddCommand("playvol", S_PlayVol);
240 Cmd_AddCommand("stopsound", S_StopAllSoundsC);
241 Cmd_AddCommand("soundlist", S_SoundList);
242 Cmd_AddCommand("soundinfo", S_SoundInfo_f);
243 Cmd_AddCommand("snd_restart", S_Restart_f);
245 Cvar_RegisterVariable(&nosound);
246 Cvar_RegisterVariable(&snd_precache);
247 Cvar_RegisterVariable(&snd_initialized);
248 Cvar_RegisterVariable(&snd_streaming);
249 Cvar_RegisterVariable(&bgmbuffer);
250 Cvar_RegisterVariable(&ambient_level);
251 Cvar_RegisterVariable(&ambient_fade);
252 Cvar_RegisterVariable(&snd_noextraupdate);
253 Cvar_RegisterVariable(&snd_show);
254 Cvar_RegisterVariable(&_snd_mixahead);
255 Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
257 Cvar_SetValueQuick(&snd_initialized, true);
259 known_sfx = Mem_Alloc(snd_mempool, MAX_SFX*sizeof(sfx_t));
262 SND_InitScaletable ();
264 ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav", false);
265 ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav", false);
267 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
268 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
274 // =======================================================================
276 // =======================================================================
284 sfx_t *S_GetCached (const char *name)
288 if (!snd_initialized.integer)
292 Host_Error("S_IsCached: NULL\n");
294 if (strlen(name) >= MAX_QPATH)
295 Host_Error("Sound name too long: %s", name);
297 for(i = 0;i < num_sfx;i++)
298 if(!strcmp(known_sfx[i].name, name))
299 return &known_sfx[i];
310 sfx_t *S_FindName (char *name)
315 if (!snd_initialized.integer)
319 Host_Error("S_FindName: NULL\n");
321 if (strlen(name) >= MAX_QPATH)
322 Host_Error("Sound name too long: %s", name);
324 // see if already loaded
325 for (i = 0;i < num_sfx;i++)
326 if (!strcmp(known_sfx[i].name, name))
327 return &known_sfx[i];
329 if (num_sfx == MAX_SFX)
330 Sys_Error("S_FindName: out of sfx_t");
332 sfx = &known_sfx[num_sfx++];
333 memset(sfx, 0, sizeof(*sfx));
334 strncpy(sfx->name, name, sizeof(sfx->name));
345 void S_TouchSound (char *name)
349 sfx = S_FindName (name);
351 // Set the "used" flag for this sound
353 sfx->flags |= SFXFLAG_USED;
361 Reset the "used" flag of all precached sounds
364 void S_ClearUsed (void)
368 for (i = 0; i < num_sfx; i++)
369 known_sfx[i].flags &= ~SFXFLAG_USED;
377 Free all precached sounds without the "used" flag
380 void S_PurgeUnused (void)
385 for (i = 0; i < num_sfx; i++)
388 if (! (sfx->flags & SFXFLAG_USED))
400 sfx_t *S_PrecacheSound (char *name, int complain)
404 if (!snd_initialized.integer)
407 sfx = S_FindName(name);
409 if (!nosound.integer && snd_precache.integer)
410 S_LoadSound(sfx, complain);
416 //=============================================================================
422 Picks a channel based on priorities, empty slots, number of channels
425 channel_t *SND_PickChannel(int entnum, int entchannel)
432 // Check for replacement sound, or find the best one to replace
434 life_left = 0x7fffffff;
435 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++)
437 ch = &channels[ch_idx];
438 if (entchannel != 0 // channel 0 never overrides
439 && ch->entnum == entnum
440 && (ch->entchannel == entchannel || entchannel == -1) )
441 { // always override sound from same entity
442 first_to_die = ch_idx;
446 // don't let monster sounds override player sounds
447 if (ch->entnum == cl.viewentity && entnum != cl.viewentity && ch->sfx)
450 // don't override looped sounds
451 if (ch->forceloop || (ch->sfx != NULL && ch->sfx->loopstart >= 0))
454 if (ch->end - paintedtime < life_left)
456 life_left = ch->end - paintedtime;
457 first_to_die = ch_idx;
461 if (first_to_die == -1)
464 if (channels[first_to_die].sfx)
465 channels[first_to_die].sfx = NULL;
467 return &channels[first_to_die];
474 Spatializes a channel
477 void SND_Spatialize(channel_t *ch, int isstatic)
479 vec_t dist, scale, pan;
482 // anything coming from the view entity will always be full volume
483 // LordHavoc: make sounds with ATTN_NONE have no spatialization
484 if (ch->entnum == cl.viewentity || ch->dist_mult == 0)
486 ch->leftvol = ch->master_vol;
487 ch->rightvol = ch->master_vol;
491 // update sound origin if we know about the entity
492 if (ch->entnum > 0 && cls.state == ca_connected && cl_entities[ch->entnum].state_current.active)
494 //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]);
495 VectorCopy(cl_entities[ch->entnum].state_current.origin, ch->origin);
496 if (cl_entities[ch->entnum].state_current.modelindex && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->soundfromcenter)
497 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);
500 // calculate stereo seperation and distance attenuation
501 VectorSubtract(ch->origin, listener_vieworigin, source_vec);
502 dist = VectorNormalizeLength(source_vec);
504 scale = ch->master_vol * (1.0 - (dist * ch->dist_mult));
506 pan = scale * DotProduct(listener_viewleft, source_vec);
507 // calculate the volumes
508 ch->leftvol = (int) (scale + pan);
509 ch->rightvol = (int) (scale - pan);
512 // LordHavoc: allow adjusting volume of static sounds
515 ch->leftvol *= snd_staticvolume.value;
516 ch->rightvol *= snd_staticvolume.value;
520 ch->leftvol = bound(0, ch->leftvol, 255);
521 ch->rightvol = bound(0, ch->rightvol, 255);
525 // =======================================================================
526 // Start a sound effect
527 // =======================================================================
529 void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
531 channel_t *target_chan, *check;
536 if (!sound_started || !sfx || !sfx->fetcher || nosound.integer)
541 // pick a channel to play on
542 target_chan = SND_PickChannel(entnum, entchannel);
547 memset (target_chan, 0, sizeof(*target_chan));
548 VectorCopy(origin, target_chan->origin);
549 target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
550 target_chan->master_vol = vol;
551 target_chan->entnum = entnum;
552 target_chan->entchannel = entchannel;
553 SND_Spatialize(target_chan, false);
555 // LordHavoc: spawn the sound anyway because the player might teleport to it
556 //if (!target_chan->leftvol && !target_chan->rightvol)
557 // return; // not audible at all
560 if (!S_LoadSound (sfx, true))
562 target_chan->sfx = NULL;
563 return; // couldn't load the sound's data
566 target_chan->sfx = sfx;
567 target_chan->pos = 0.0;
568 target_chan->end = paintedtime + sfx->total_length;
569 target_chan->lastptime = paintedtime;
571 // if an identical sound has also been started this frame, offset the pos
572 // a bit to keep it from just making the first one louder
573 check = &channels[NUM_AMBIENTS];
574 for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
576 if (check == target_chan)
578 if (check->sfx == sfx && !check->pos)
580 // LordHavoc: fixed skip calculations
581 skip = 0.1 * sfx->format.speed;
582 if (skip > sfx->total_length)
583 skip = sfx->total_length;
585 skip = rand() % skip;
586 target_chan->pos += skip;
587 target_chan->end -= skip;
593 void S_StopSound(int entnum, int entchannel)
597 for (i=0 ; i<MAX_DYNAMIC_CHANNELS ; i++)
599 if (channels[i].entnum == entnum
600 && channels[i].entchannel == entchannel)
603 channels[i].sfx = NULL;
609 void S_StopAllSounds(qboolean clear)
611 total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
612 memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
618 void S_StopAllSoundsC(void)
620 S_StopAllSounds(true);
623 void S_ClearBuffer(void)
627 if (!sound_started || !shm)
630 if (shm->format.width == 1)
645 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, (LPVOID*)&pData, &dwSize, NULL, NULL, 0)) != DS_OK)
647 if (hresult != DSERR_BUFFERLOST)
649 Con_Print("S_ClearBuffer: DS::Lock Sound Buffer Failed\n");
656 Con_Print("S_ClearBuffer: DS: couldn't restore buffer\n");
662 memset(pData, clear, shm->samples * shm->format.width);
664 pDSBuf->lpVtbl->Unlock(pDSBuf, pData, dwSize, NULL, 0);
671 int setsize = shm->samples * shm->format.width;
672 char *buf = shm->buffer;
677 // on i586/i686 optimized versions of glibc, glibc *wrongly* IMO,
678 // reads the memory area before writing to it causing a seg fault
679 // since the memory is PROT_WRITE only and not PROT_READ|PROT_WRITE
680 // memset(shm->buffer, clear, shm->samples * shm->samplebits/8);
690 void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation)
697 if (total_channels == MAX_CHANNELS)
699 Con_Print("total_channels == MAX_CHANNELS\n");
703 if (!S_LoadSound (sfx, true))
706 if (sfx->loopstart == -1)
707 Con_DPrintf("Quake compatibility warning: Static sound \"%s\" is not looped\n", sfx->name);
709 ss = &channels[total_channels++];
710 memset(ss, 0, sizeof(*ss));
711 ss->forceloop = true;
713 VectorCopy (origin, ss->origin);
714 ss->master_vol = vol;
715 ss->dist_mult = (attenuation/64) / sound_nominal_clip_dist;
716 ss->end = paintedtime + sfx->total_length;
717 ss->lastptime = paintedtime;
719 SND_Spatialize (ss, true);
723 //=============================================================================
727 S_UpdateAmbientSounds
730 void S_UpdateAmbientSounds (void)
735 qbyte ambientlevels[NUM_AMBIENTS];
737 // LordHavoc: kill ambient sounds until proven otherwise
738 for (ambient_channel = 0 ; ambient_channel < NUM_AMBIENTS;ambient_channel++)
739 channels[ambient_channel].sfx = NULL;
741 if (!snd_ambient || ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
744 cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_vieworigin, ambientlevels, sizeof(ambientlevels));
746 // calc ambient sound levels
747 for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
749 if (ambient_sfx[ambient_channel] &&
750 (ambient_sfx[ambient_channel]->flags & SFXFLAG_SILENTLYMISSING))
752 chan = &channels[ambient_channel];
753 chan->forceloop = true;
754 chan->sfx = ambient_sfx[ambient_channel];
756 vol = ambient_level.value * ambientlevels[ambient_channel];
760 // don't adjust volume too fast
761 if (chan->master_vol < vol)
763 chan->master_vol += host_realframetime * ambient_fade.value;
764 if (chan->master_vol > vol)
765 chan->master_vol = vol;
767 else if (chan->master_vol > vol)
769 chan->master_vol -= host_realframetime * ambient_fade.value;
770 if (chan->master_vol < vol)
771 chan->master_vol = vol;
774 chan->leftvol = chan->rightvol = chan->master_vol;
783 Called once each time through the main loop
786 void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up)
788 unsigned int i, j, total;
789 channel_t *ch, *combine;
791 if (!snd_initialized.integer || (snd_blocked > 0))
794 VectorCopy(origin, listener_vieworigin);
795 VectorCopy(forward, listener_viewforward);
796 VectorCopy(left, listener_viewleft);
797 VectorCopy(up, listener_viewup);
799 // update general area ambient sound sources
800 S_UpdateAmbientSounds ();
804 // update spatialization for static and dynamic sounds
805 ch = channels+NUM_AMBIENTS;
806 for (i=NUM_AMBIENTS ; i<total_channels; i++, ch++)
810 SND_Spatialize(ch, i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS); // respatialize channel
811 if (!ch->leftvol && !ch->rightvol)
814 // try to combine static sounds with a previous channel of the same
815 // sound effect so we don't mix five torches every frame
817 if (i > MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
819 // see if it can just use the last one
820 if (combine && combine->sfx == ch->sfx)
822 combine->leftvol += ch->leftvol;
823 combine->rightvol += ch->rightvol;
824 ch->leftvol = ch->rightvol = 0;
828 combine = channels+MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
829 for (j=MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS ; j<i; j++, combine++)
830 if (combine->sfx == ch->sfx)
833 if (j == total_channels)
839 combine->leftvol += ch->leftvol;
840 combine->rightvol += ch->rightvol;
841 ch->leftvol = ch->rightvol = 0;
851 if (snd_show.integer)
855 for (i=0 ; i<total_channels; i++, ch++)
856 if (ch->sfx && (ch->leftvol || ch->rightvol) )
859 Con_Printf("----(%u)----\n", total);
866 void GetSoundtime(void)
870 static int oldsamplepos;
873 fullsamples = shm->samples / shm->format.channels;
875 // it is possible to miscount buffers if it has wrapped twice between
876 // calls to S_Update. Oh well.
877 samplepos = SNDDMA_GetDMAPos();
879 if (samplepos < oldsamplepos)
881 buffers++; // buffer wrapped
883 if (paintedtime > 0x40000000)
884 { // time to chop things off to avoid 32 bit limits
886 paintedtime = fullsamples;
887 S_StopAllSounds (true);
890 oldsamplepos = samplepos;
892 soundtime = buffers * fullsamples + samplepos / shm->format.channels;
895 void IN_Accumulate (void);
897 void S_ExtraUpdate (void)
904 if (snd_noextraupdate.integer)
905 return; // don't pollute timings
914 if (!sound_started || (snd_blocked > 0))
920 // check to make sure that we haven't overshot
921 if (paintedtime < soundtime)
922 paintedtime = soundtime;
924 // mix ahead of current position
925 endtime = soundtime + _snd_mixahead.value * shm->format.speed;
926 samps = shm->samples >> (shm->format.channels - 1);
927 if (endtime > (unsigned int)(soundtime + samps))
928 endtime = soundtime + samps;
931 // if the buffer was lost or stopped, restore it and/or restart it
937 if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DS_OK)
938 Con_Print("Couldn't get sound buffer status\n");
940 if (dwStatus & DSBSTATUS_BUFFERLOST)
941 pDSBuf->lpVtbl->Restore (pDSBuf);
943 if (!(dwStatus & DSBSTATUS_PLAYING))
944 pDSBuf->lpVtbl->Play(pDSBuf, 0, 0, DSBPLAY_LOOPING);
949 S_PaintChannels (endtime);
955 ===============================================================================
959 ===============================================================================
962 static void S_Play_Common(float fvol, float attenuation)
971 if (!strrchr(Cmd_Argv(i), '.'))
972 snprintf(name, sizeof(name), "%s.wav", Cmd_Argv(i));
974 strlcpy(name, Cmd_Argv(i), sizeof(name));
975 sfx = S_PrecacheSound(name, true);
977 // If we need to get the volume from the command line
980 fvol = atof(Cmd_Argv(i+1));
986 S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
992 S_Play_Common (1.0f, 1.0f);
997 S_Play_Common (1.0f, 0.0f);
1000 void S_PlayVol(void)
1002 S_Play_Common (-1.0f, 0.0f);
1005 void S_SoundList(void)
1012 for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
1014 if (sfx->fetcher != NULL)
1016 size = sfx->mempool->totalsize;
1018 Con_Printf("%c(%2db) %7i : %s\n", sfx->loopstart >= 0 ? 'L' : ' ', sfx->format.width * 8, size, sfx->name);
1021 Con_Printf("Total resident: %i\n", total);
1025 void S_LocalSound (char *sound)
1029 if (!snd_initialized.integer || nosound.integer)
1032 sfx = S_PrecacheSound (sound, true);
1035 Con_Printf("S_LocalSound: can't precache %s\n", sound);
1038 S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
1042 #define RAWSAMPLESBUFFER 32768
1043 short s_rawsamplesbuffer[RAWSAMPLESBUFFER * 2];
1044 int s_rawsamplesbuffer_start;
1045 size_t s_rawsamplesbuffer_count;
1047 void S_RawSamples_Enqueue(short *samples, unsigned int length)
1050 //Con_Printf("S_RawSamples_Enqueue: %i samples\n", length);
1051 if (s_rawsamplesbuffer_count + length > RAWSAMPLESBUFFER)
1053 b2 = (s_rawsamplesbuffer_start + s_rawsamplesbuffer_count) % RAWSAMPLESBUFFER;
1054 if (b2 + length > RAWSAMPLESBUFFER)
1056 b3 = (b2 + length) % RAWSAMPLESBUFFER;
1057 memcpy(s_rawsamplesbuffer + b2 * 2, samples, (RAWSAMPLESBUFFER - b2) * sizeof(short[2]));
1058 memcpy(s_rawsamplesbuffer, samples + (RAWSAMPLESBUFFER - b2) * 2, b3 * sizeof(short[2]));
1061 memcpy(s_rawsamplesbuffer + b2 * 2, samples, length * sizeof(short[2]));
1062 s_rawsamplesbuffer_count += length;
1065 void S_RawSamples_Dequeue(int *samples, unsigned int length)
1074 if (l > s_rawsamplesbuffer_count)
1075 l = s_rawsamplesbuffer_count;
1076 b1 = (s_rawsamplesbuffer_start) % RAWSAMPLESBUFFER;
1077 if (b1 + l > RAWSAMPLESBUFFER)
1079 b2 = (b1 + l) % RAWSAMPLESBUFFER;
1080 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, (RAWSAMPLESBUFFER - b1) * sizeof(short[2]));
1081 //memcpy(samples + (RAWSAMPLESBUFFER - b1) * 2, s_rawsamplesbuffer, b2 * sizeof(short[2]));
1082 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = (RAWSAMPLESBUFFER - b1) * 2, i = 0;i < count;i++)
1084 for (out = samples + (RAWSAMPLESBUFFER - b1) * 2, in = s_rawsamplesbuffer, count = b2 * 2, i = 0;i < count;i++)
1086 //Con_Printf("S_RawSamples_Dequeue: buffer wrap %i %i\n", (RAWSAMPLESBUFFER - b1), b2);
1090 //memcpy(samples, s_rawsamplesbuffer + b1 * 2, l * sizeof(short[2]));
1091 for (out = samples, in = s_rawsamplesbuffer + b1 * 2, count = l * 2, i = 0;i < count;i++)
1093 //Con_Printf("S_RawSamples_Dequeue: normal %i\n", l);
1095 if (l < (int)length)
1097 memset(samples + l * 2, 0, (length - l) * sizeof(int[2]));
1098 //Con_Printf("S_RawSamples_Dequeue: padding with %i samples\n", length - l);
1100 s_rawsamplesbuffer_start = (s_rawsamplesbuffer_start + l) % RAWSAMPLESBUFFER;
1101 s_rawsamplesbuffer_count -= l;
1104 void S_RawSamples_ClearQueue(void)
1106 s_rawsamplesbuffer_count = 0;
1107 s_rawsamplesbuffer_start = 0;
1110 int S_RawSamples_QueueWantsMore(void)
1112 if (shm != NULL && s_rawsamplesbuffer_count < min(shm->format.speed >> 1, RAWSAMPLESBUFFER >> 1))
1113 return RAWSAMPLESBUFFER - s_rawsamplesbuffer_count;
1118 void S_ResampleBuffer16Stereo(short *input, int inputlength, short *output, int outputlength)
1120 if (inputlength != outputlength)
1122 int i, position, stopposition, step;
1124 step = (float) inputlength * 256.0f / (float) outputlength;
1126 stopposition = (inputlength - 1) << 8;
1128 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1130 in = input + ((position >> 8) << 1);
1131 out[0] = (((in[1] - in[0]) * (position & 255)) >> 8) + in[0];
1132 out[1] = (((in[3] - in[2]) * (position & 255)) >> 8) + in[2];
1135 stopposition = inputlength << 8;
1136 for (i = 0;i < outputlength && position < stopposition;i++, position += step)
1138 in = input + ((position >> 8) << 1);
1145 memcpy(output, input, inputlength * sizeof(short[2]));
1148 int S_RawSamples_SampleRate(void)
1150 return shm != NULL ? shm->format.speed : 0;