#include "quakedef.h"
#ifdef _WIN32
-#include "winquake.h"
+#include <windows.h>
+#include <dsound.h>
+extern DWORD gSndBufSize;
+extern LPDIRECTSOUND pDS;
+extern LPDIRECTSOUNDBUFFER pDSBuf;
#endif
+#include "ogg.h"
+
+
void S_Play(void);
void S_PlayVol(void);
void S_Play2(void);
int snd_blocked = 0;
static qboolean snd_ambient = 1;
-qboolean snd_initialized = false;
+cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
// pointer should go away
volatile dma_t *shm = 0;
volatile dma_t sn;
-vec3_t listener_origin;
-vec3_t listener_forward;
-vec3_t listener_right;
-vec3_t listener_up;
+vec3_t listener_vieworigin;
+vec3_t listener_viewforward;
+vec3_t listener_viewleft;
+vec3_t listener_viewup;
vec_t sound_nominal_clip_dist=1000.0;
mempool_t *snd_mempool;
void S_Startup(void)
{
- if (!snd_initialized)
+ if (!snd_initialized.integer)
return;
shm = &sn;
Cvar_RegisterVariable(&nosound);
Cvar_RegisterVariable(&snd_precache);
+ Cvar_RegisterVariable(&snd_initialized);
Cvar_RegisterVariable(&bgmbuffer);
Cvar_RegisterVariable(&ambient_level);
Cvar_RegisterVariable(&ambient_fade);
Cvar_RegisterVariable(&_snd_mixahead);
Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
- snd_initialized = true;
+ Cvar_SetValueQuick(&snd_initialized, true);
known_sfx = Mem_Alloc(snd_mempool, MAX_SFX*sizeof(sfx_t));
num_sfx = 0;
total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
+
+ OGG_OpenLibrary ();
}
// Load a sound
// =======================================================================
+/*
+=========
+S_GetCached
+
+=========
+*/
+sfx_t *S_GetCached (const char *name)
+{
+ int i;
+
+ if (!snd_initialized.integer)
+ return NULL;
+
+ if (!name)
+ Host_Error("S_IsCached: NULL\n");
+
+ if (strlen(name) >= MAX_QPATH)
+ Host_Error("Sound name too long: %s", name);
+
+ for(i = 0;i < num_sfx;i++)
+ if(!strcmp(known_sfx[i].name, name))
+ return &known_sfx[i];
+
+ return NULL;
+}
+
/*
==================
S_FindName
int i;
sfx_t *sfx;
- if (!snd_initialized)
+ if (!snd_initialized.integer)
return NULL;
if (!name)
*/
void S_TouchSound (char *name)
{
- S_FindName(name);
+ sfx_t *sfx;
+
+ sfx = S_FindName (name);
+
+ // Set the "used" flag for this sound
+ if (sfx != NULL)
+ sfx->flags |= SFXFLAG_USED;
+}
+
+
+/*
+==================
+S_ClearUsed
+
+Reset the "used" flag of all precached sounds
+==================
+*/
+void S_ClearUsed (void)
+{
+ int i;
+
+ for (i = 0; i < num_sfx; i++)
+ known_sfx[i].flags &= ~SFXFLAG_USED;
}
+
+/*
+==================
+S_PurgeUnused
+
+Free all precached sounds without the "used" flag
+==================
+*/
+void S_PurgeUnused (void)
+{
+ int i;
+ sfx_t *sfx;
+
+ for (i = 0; i < num_sfx; i++)
+ {
+ sfx = &known_sfx[i];
+ if (! (sfx->flags & SFXFLAG_USED))
+ S_UnloadSound (sfx);
+ }
+}
+
+
/*
==================
S_PrecacheSound
{
sfx_t *sfx;
- if (!snd_initialized)
+ if (!snd_initialized.integer)
return NULL;
sfx = S_FindName(name);
{
//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]);
VectorCopy(cl_entities[ch->entnum].state_current.origin, ch->origin);
- if (cl_entities[ch->entnum].state_current.modelindex && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->brush.TraceBox)
+ if (cl_entities[ch->entnum].state_current.modelindex && cl.model_precache[cl_entities[ch->entnum].state_current.modelindex]->soundfromcenter)
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);
}
// calculate stereo seperation and distance attenuation
- VectorSubtract(ch->origin, listener_origin, source_vec);
+ VectorSubtract(ch->origin, listener_vieworigin, source_vec);
dist = VectorNormalizeLength(source_vec);
// distance
scale = ch->master_vol * (1.0 - (dist * ch->dist_mult));
// panning
- pan = scale * DotProduct(listener_right, source_vec);
+ pan = scale * DotProduct(listener_viewleft, source_vec);
// calculate the volumes
- ch->leftvol = (int) (scale - pan);
- ch->rightvol = (int) (scale + pan);
+ ch->leftvol = (int) (scale + pan);
+ ch->rightvol = (int) (scale - pan);
}
// LordHavoc: allow adjusting volume of static sounds
int ch_idx;
int skip;
- if (!sound_started || !sfx || nosound.integer)
+ if (!sound_started || !sfx || !sfx->sfxcache || nosound.integer)
return;
vol = fvol*255;
target_chan->entchannel = entchannel;
SND_Spatialize(target_chan, false);
- if (!target_chan->leftvol && !target_chan->rightvol)
- return; // not audible at all
+ // LordHavoc: spawn the sound anyway because the player might teleport to it
+ //if (!target_chan->leftvol && !target_chan->rightvol)
+ // return; // not audible at all
// new channel
sc = S_LoadSound (sfx, true);
reps = 0;
- while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pData, &dwSize, NULL, NULL, 0)) != DS_OK)
+ while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, (LPVOID*)&pData, &dwSize, NULL, NULL, 0)) != DS_OK)
{
if (hresult != DSERR_BUFFERLOST)
{
if (!snd_ambient || ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
return;
- cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_origin, ambientlevels, sizeof(ambientlevels));
+ cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_vieworigin, ambientlevels, sizeof(ambientlevels));
// calc ambient sound levels
for (ambient_channel = 0 ; ambient_channel< NUM_AMBIENTS ; ambient_channel++)
{
- if (ambient_sfx[ambient_channel] && ambient_sfx[ambient_channel]->silentlymissing)
+ if (ambient_sfx[ambient_channel] &&
+ (ambient_sfx[ambient_channel]->flags & SFXFLAG_SILENTLYMISSING))
continue;
chan = &channels[ambient_channel];
chan->forceloop = true;
Called once each time through the main loop
============
*/
-void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
+void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up)
{
int i, j;
int total;
channel_t *ch;
channel_t *combine;
- if (!snd_initialized || (snd_blocked > 0))
+ if (!snd_initialized.integer || (snd_blocked > 0))
return;
- VectorCopy(origin, listener_origin);
- VectorCopy(forward, listener_forward);
- VectorCopy(right, listener_right);
- VectorCopy(up, listener_up);
+ VectorCopy(origin, listener_vieworigin);
+ VectorCopy(forward, listener_viewforward);
+ VectorCopy(left, listener_viewleft);
+ VectorCopy(up, listener_viewup);
// update general area ambient sound sources
S_UpdateAmbientSounds ();
// it is possible to miscount buffers if it has wrapped twice between
// calls to S_Update. Oh well.
-#ifdef __sun__
- soundtime = SNDDMA_GetSamples();
-#else
samplepos = SNDDMA_GetDMAPos();
-
if (samplepos < oldsamplepos)
{
buffers++; // buffer wrapped
oldsamplepos = samplepos;
soundtime = buffers*fullsamples + samplepos/shm->channels;
-#endif
}
void IN_Accumulate (void);
if (pDSBuf)
{
- if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK)
+ if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DS_OK)
Con_Printf ("Couldn't get sound buffer status\n");
if (dwStatus & DSBSTATUS_BUFFERLOST)
===============================================================================
*/
-void S_Play(void)
+static void S_Play_Common(float fvol, float attenuation)
{
int i;
char name[256];
while (i<Cmd_Argc())
{
if (!strrchr(Cmd_Argv(i), '.'))
+ snprintf(name, sizeof(name), "%s.wav", Cmd_Argv(i));
+ else
+ strlcpy(name, Cmd_Argv(i), sizeof(name));
+ sfx = S_PrecacheSound(name, true);
+
+ // If we need to get the volume from the command line
+ if (fvol == -1.0f)
{
- strcpy(name, Cmd_Argv(i));
- strcat(name, ".wav");
+ fvol = atof(Cmd_Argv(i+1));
+ i += 2;
}
else
- strcpy(name, Cmd_Argv(i));
- sfx = S_PrecacheSound(name, true);
- S_StartSound(-1, 0, sfx, listener_origin, 1.0, 1.0);
- i++;
+ i++;
+
+ S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
}
}
-void S_Play2(void)
+void S_Play(void)
{
- int i;
- char name[256];
- sfx_t *sfx;
+ S_Play_Common (1.0f, 1.0f);
+}
- i = 1;
- while (i<Cmd_Argc())
- {
- if (!strrchr(Cmd_Argv(i), '.'))
- {
- strcpy(name, Cmd_Argv(i));
- strcat(name, ".wav");
- }
- else
- strcpy(name, Cmd_Argv(i));
- sfx = S_PrecacheSound(name, true);
- S_StartSound(-1, 0, sfx, listener_origin, 1.0, 0.0);
- i++;
- }
+void S_Play2(void)
+{
+ S_Play_Common (1.0f, 0.0f);
}
void S_PlayVol(void)
{
- int i;
- float vol;
- char name[256];
- sfx_t *sfx;
-
- i = 1;
- while (i<Cmd_Argc())
- {
- if (!strrchr(Cmd_Argv(i), '.'))
- {
- strcpy(name, Cmd_Argv(i));
- strcat(name, ".wav");
- }
- else
- strcpy(name, Cmd_Argv(i));
- sfx = S_PrecacheSound(name, true);
- vol = atof(Cmd_Argv(i+1));
- S_StartSound(-1, 0, sfx, listener_origin, vol, 1.0);
- i+=2;
- }
+ S_Play_Common (-1.0f, 0.0f);
}
void S_SoundList(void)
{
sfx_t *sfx;
- if (!snd_initialized || nosound.integer)
+ if (!snd_initialized.integer || nosound.integer)
return;
sfx = S_PrecacheSound (sound, true);
}
-void S_ClearPrecache (void)
-{
-}
-
-
-void S_BeginPrecaching (void)
-{
-}
-
-
-void S_EndPrecaching (void)
-{
-}
-
-
#define RAWSAMPLESBUFFER 32768
short s_rawsamplesbuffer[RAWSAMPLESBUFFER * 2];
int s_rawsamplesbuffer_start;