📄 snd_dma.c
字号:
*p = 0;
}
}
// if we can't figure it out, they're male
if (!model[0])
strcpy(model, "male");
// see if we already know of the model specific sound
Com_sprintf (sexedFilename, sizeof(sexedFilename), "#players/%s/%s", model, base+1);
sfx = S_FindName (sexedFilename, false);
if (!sfx)
{
// no, so see if it exists
FS_FOpenFile (&sexedFilename[1], &f);
if (f)
{
// yes, close the file and register it
FS_FCloseFile (f);
sfx = S_RegisterSound (sexedFilename);
}
else
{
// no, revert to the male sound in the pak0.pak
Com_sprintf (maleFilename, sizeof(maleFilename), "player/%s/%s", "male", base+1);
sfx = S_AliasName (sexedFilename, maleFilename);
}
}
return sfx;
}
// =======================================================================
// Start a sound effect
// =======================================================================
/*
====================
S_StartSound
Validates the parms and ques the sound up
if pos is NULL, the sound will be dynamically sourced from the entity
Entchannel 0 will never override a playing sound
====================
*/
void S_StartSound(vec3_t origin, int entnum, int entchannel, sfx_t *sfx, float fvol, float attenuation, float timeofs)
{
sfxcache_t *sc;
int vol;
playsound_t *ps, *sort;
int start;
if (!sound_started)
return;
if (!sfx)
return;
if (sfx->name[0] == '*')
sfx = S_RegisterSexedSound(&cl_entities[entnum].current, sfx->name);
// make sure the sound is loaded
sc = S_LoadSound (sfx);
if (!sc)
return; // couldn't load the sound's data
vol = fvol*255;
// make the playsound_t
ps = S_AllocPlaysound ();
if (!ps)
return;
if (origin)
{
VectorCopy (origin, ps->origin);
ps->fixed_origin = true;
}
else
ps->fixed_origin = false;
ps->entnum = entnum;
ps->entchannel = entchannel;
ps->attenuation = attenuation;
ps->volume = vol;
ps->sfx = sfx;
// drift s_beginofs
start = cl.frame.servertime * 0.001 * dma.speed + s_beginofs;
if (start < paintedtime)
{
start = paintedtime;
s_beginofs = start - (cl.frame.servertime * 0.001 * dma.speed);
}
else if (start > paintedtime + 0.3 * dma.speed)
{
start = paintedtime + 0.1 * dma.speed;
s_beginofs = start - (cl.frame.servertime * 0.001 * dma.speed);
}
else
{
s_beginofs-=10;
}
if (!timeofs)
ps->begin = paintedtime;
else
ps->begin = start + timeofs * dma.speed;
// sort into the pending sound list
for (sort = s_pendingplays.next ;
sort != &s_pendingplays && sort->begin < ps->begin ;
sort = sort->next)
;
ps->next = sort;
ps->prev = sort->prev;
ps->next->prev = ps;
ps->prev->next = ps;
}
/*
==================
S_StartLocalSound
==================
*/
void S_StartLocalSound (char *sound)
{
sfx_t *sfx;
if (!sound_started)
return;
sfx = S_RegisterSound (sound);
if (!sfx)
{
Com_Printf ("S_StartLocalSound: can't cache %s\n", sound);
return;
}
S_StartSound (NULL, cl.playernum+1, 0, sfx, 1, 1, 0);
}
/*
==================
S_ClearBuffer
==================
*/
void S_ClearBuffer (void)
{
int clear;
if (!sound_started)
return;
s_rawend = 0;
if (dma.samplebits == 8)
clear = 0x80;
else
clear = 0;
SNDDMA_BeginPainting ();
if (dma.buffer)
memset(dma.buffer, clear, dma.samples * dma.samplebits/8);
SNDDMA_Submit ();
}
/*
==================
S_StopAllSounds
==================
*/
void S_StopAllSounds(void)
{
int i;
if (!sound_started)
return;
// clear all the playsounds
memset(s_playsounds, 0, sizeof(s_playsounds));
s_freeplays.next = s_freeplays.prev = &s_freeplays;
s_pendingplays.next = s_pendingplays.prev = &s_pendingplays;
for (i=0 ; i<MAX_PLAYSOUNDS ; i++)
{
s_playsounds[i].prev = &s_freeplays;
s_playsounds[i].next = s_freeplays.next;
s_playsounds[i].prev->next = &s_playsounds[i];
s_playsounds[i].next->prev = &s_playsounds[i];
}
// clear all the channels
memset(channels, 0, sizeof(channels));
S_ClearBuffer ();
}
/*
==================
S_AddLoopSounds
Entities with a ->sound field will generated looped sounds
that are automatically started, stopped, and merged together
as the entities are sent to the client
==================
*/
void S_AddLoopSounds (void)
{
int i, j;
int sounds[MAX_EDICTS];
int left, right, left_total, right_total;
channel_t *ch;
sfx_t *sfx;
sfxcache_t *sc;
int num;
entity_state_t *ent;
if (cl_paused->value)
return;
if (cls.state != ca_active)
return;
if (!cl.sound_prepped)
return;
for (i=0 ; i<cl.frame.num_entities ; i++)
{
num = (cl.frame.parse_entities + i)&(MAX_PARSE_ENTITIES-1);
ent = &cl_parse_entities[num];
sounds[i] = ent->sound;
}
for (i=0 ; i<cl.frame.num_entities ; i++)
{
if (!sounds[i])
continue;
sfx = cl.sound_precache[sounds[i]];
if (!sfx)
continue; // bad sound effect
sc = sfx->cache;
if (!sc)
continue;
num = (cl.frame.parse_entities + i)&(MAX_PARSE_ENTITIES-1);
ent = &cl_parse_entities[num];
// find the total contribution of all sounds of this type
S_SpatializeOrigin (ent->origin, 255.0, SOUND_LOOPATTENUATE,
&left_total, &right_total);
for (j=i+1 ; j<cl.frame.num_entities ; j++)
{
if (sounds[j] != sounds[i])
continue;
sounds[j] = 0; // don't check this again later
num = (cl.frame.parse_entities + j)&(MAX_PARSE_ENTITIES-1);
ent = &cl_parse_entities[num];
S_SpatializeOrigin (ent->origin, 255.0, SOUND_LOOPATTENUATE,
&left, &right);
left_total += left;
right_total += right;
}
if (left_total == 0 && right_total == 0)
continue; // not audible
// allocate a channel
ch = S_PickChannel(0, 0);
if (!ch)
return;
if (left_total > 255)
left_total = 255;
if (right_total > 255)
right_total = 255;
ch->leftvol = left_total;
ch->rightvol = right_total;
ch->autosound = true; // remove next frame
ch->sfx = sfx;
ch->pos = paintedtime % sc->length;
ch->end = paintedtime + sc->length - ch->pos;
}
}
//=============================================================================
/*
============
S_RawSamples
Cinematic streaming and voice over network
============
*/
void S_RawSamples (int samples, int rate, int width, int channels, byte *data)
{
int i;
int src, dst;
float scale;
if (!sound_started)
return;
if (s_rawend < paintedtime)
s_rawend = paintedtime;
scale = (float)rate / dma.speed;
//Com_Printf ("%i < %i < %i\n", soundtime, paintedtime, s_rawend);
if (channels == 2 && width == 2)
{
if (scale == 1.0)
{ // optimized case
for (i=0 ; i<samples ; i++)
{
dst = s_rawend&(MAX_RAW_SAMPLES-1);
s_rawend++;
s_rawsamples[dst].left =
LittleShort(((short *)data)[i*2]) << 8;
s_rawsamples[dst].right =
LittleShort(((short *)data)[i*2+1]) << 8;
}
}
else
{
for (i=0 ; ; i++)
{
src = i*scale;
if (src >= samples)
break;
dst = s_rawend&(MAX_RAW_SAMPLES-1);
s_rawend++;
s_rawsamples[dst].left =
LittleShort(((short *)data)[src*2]) << 8;
s_rawsamples[dst].right =
LittleShort(((short *)data)[src*2+1]) << 8;
}
}
}
else if (channels == 1 && width == 2)
{
for (i=0 ; ; i++)
{
src = i*scale;
if (src >= samples)
break;
dst = s_rawend&(MAX_RAW_SAMPLES-1);
s_rawend++;
s_rawsamples[dst].left =
LittleShort(((short *)data)[src]) << 8;
s_rawsamples[dst].right =
LittleShort(((short *)data)[src]) << 8;
}
}
else if (channels == 2 && width == 1)
{
for (i=0 ; ; i++)
{
src = i*scale;
if (src >= samples)
break;
dst = s_rawend&(MAX_RAW_SAMPLES-1);
s_rawend++;
s_rawsamples[dst].left =
((char *)data)[src*2] << 16;
s_rawsamples[dst].right =
((char *)data)[src*2+1] << 16;
}
}
else if (channels == 1 && width == 1)
{
for (i=0 ; ; i++)
{
src = i*scale;
if (src >= samples)
break;
dst = s_rawend&(MAX_RAW_SAMPLES-1);
s_rawend++;
s_rawsamples[dst].left =
(((byte *)data)[src]-128) << 16;
s_rawsamples[dst].right = (((byte *)data)[src]-128) << 16;
}
}
}
//=============================================================================
/*
============
S_Update
Called once each time through the main loop
============
*/
void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
{
int i;
int total;
channel_t *ch;
channel_t *combine;
if (!sound_started)
return;
// if the laoding plaque is up, clear everything
// out to make sure we aren't looping a dirty
// dma buffer while loading
if (cls.disable_screen)
{
S_ClearBuffer ();
return;
}
// rebuild scale tables if volume is modified
if (s_volume->modified)
S_InitScaletable ();
VectorCopy(origin, listener_origin);
VectorCopy(forward, listener_forward);
VectorCopy(right, listener_right);
VectorCopy(up, listener_up);
combine = NULL;
// update spatialization for dynamic sounds
ch = channels;
for (i=0 ; i<MAX_CHANNELS; i++, ch++)
{
if (!ch->sfx)
continue;
if (ch->autosound)
{ // autosounds are regenerated fresh each frame
memset (ch, 0, sizeof(*ch));
continue;
}
S_Spatialize(ch); // respatialize channel
if (!ch->leftvol && !ch->rightvol)
{
memset (ch, 0, sizeof(*ch));
continue;
}
}
// add loopsounds
S_AddLoopSounds ();
//
// debugging output
//
if (s_show->value)
{
total = 0;
ch = channels;
for (i=0 ; i<MAX_CHANNELS; i++, ch++)
if (ch->sfx && (ch->leftvol || ch->rightvol) )
{
Com_Printf ("%3i %3i %s\n", ch->leftvol, ch->rightvol, ch->sfx->name);
total++;
}
Com_Printf ("----(%i)---- painted: %i\n", total, paintedtime);
}
// mix some sound
S_Update_();
}
void GetSoundtime(void)
{
int samplepos;
static int buffers;
static int oldsamplepos;
int fullsamples;
fullsamples = dma.samples / dma.channels;
// it is possible to miscount buffers if it has wrapped twice between
// calls to S_Update. Oh well.
samplepos = SNDDMA_GetDMAPos();
if (samplepos < oldsamplepos)
{
buffers++; // buffer wrapped
if (paintedtime > 0x40000000)
{ // time to chop things off to avoid 32 bit limits
buffers = 0;
paintedtime = fullsamples;
S_StopAllSounds ();
}
}
oldsamplepos = samplepos;
soundtime = buffers*fullsamples + samplepos/dma.channels;
}
void S_Update_(void)
{
unsigned endtime;
int samps;
if (!sound_started)
return;
SNDDMA_BeginPainting ();
if (!dma.buffer)
return;
// Updates DMA time
GetSoundtime();
// check to make sure that we haven't overshot
if (paintedtime < soundtime)
{
Com_DPrintf ("S_Update_ : overflow\n");
paintedtime = soundtime;
}
// mix ahead of current position
endtime = soundtime + s_mixahead->value * dma.speed;
//endtime = (soundtime + 4096) & ~4095;
// mix to an even submission block size
endtime = (endtime + dma.submission_chunk-1)
& ~(dma.submission_chunk-1);
samps = dma.samples >> (dma.channels-1);
if (endtime - soundtime > samps)
endtime = soundtime + samps;
S_PaintChannels (endtime);
SNDDMA_Submit ();
}
/*
===============================================================================
console functions
===============================================================================
*/
void S_Play(void)
{
int i;
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_RegisterSound(name);
S_StartSound(NULL, cl.playernum+1, 0, sfx, 1.0, 1.0, 0);
i++;
}
}
void S_SoundList(void)
{
int i;
sfx_t *sfx;
sfxcache_t *sc;
int size, total;
total = 0;
for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
{
if (!sfx->registration_sequence)
continue;
sc = sfx->cache;
if (sc)
{
size = sc->length*sc->width*(sc->stereo+1);
total += size;
if (sc->loopstart >= 0)
Com_Printf ("L");
else
Com_Printf (" ");
Com_Printf("(%2db) %6i : %s\n",sc->width*8, size, sfx->name);
}
else
{
if (sfx->name[0] == '*')
Com_Printf(" placeholder : %s\n", sfx->name);
else
Com_Printf(" not loaded : %s\n", sfx->name);
}
}
Com_Printf ("Total resident: %i\n", total);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -