A couple of small optimizations. If you are exiting early, avoid some stuff
only needed later. And reduce worst-case number of if tests in a few places.
This commit is contained in:
parent
7f3d5164e6
commit
2342dfcd8f
|
@ -346,26 +346,23 @@ static void
|
||||||
s_start_sound (int entnum, int entchannel, sfx_t *sfx, const vec3_t origin,
|
s_start_sound (int entnum, int entchannel, sfx_t *sfx, const vec3_t origin,
|
||||||
float fvol, float attenuation)
|
float fvol, float attenuation)
|
||||||
{
|
{
|
||||||
int ch_idx, vol;
|
int ch_idx, vol;
|
||||||
unsigned int skip;
|
unsigned int skip;
|
||||||
channel_t *target_chan, *check;
|
channel_t *target_chan, *check;
|
||||||
|
|
||||||
if (!sound_started)
|
if (!sound_started)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!sfx)
|
if (!sfx)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (nosound->int_val)
|
if (nosound->int_val)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
vol = fvol * 255;
|
|
||||||
|
|
||||||
// pick a channel to play on
|
// pick a channel to play on
|
||||||
target_chan = s_pick_channel (entnum, entchannel);
|
target_chan = s_pick_channel (entnum, entchannel);
|
||||||
if (!target_chan)
|
if (!target_chan)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
vol = fvol * 255;
|
||||||
|
|
||||||
// spatialize
|
// spatialize
|
||||||
memset (target_chan, 0, sizeof (*target_chan));
|
memset (target_chan, 0, sizeof (*target_chan));
|
||||||
VectorCopy (origin, target_chan->origin);
|
VectorCopy (origin, target_chan->origin);
|
||||||
|
@ -435,9 +432,7 @@ s_stop_sound (int entnum, int entchannel)
|
||||||
static void
|
static void
|
||||||
s_clear_buffer (void)
|
s_clear_buffer (void)
|
||||||
{
|
{
|
||||||
int clear;
|
int clear, i;
|
||||||
int i;
|
|
||||||
|
|
||||||
|
|
||||||
if (!sound_started || !shm || !shm->buffer)
|
if (!sound_started || !shm || !shm->buffer)
|
||||||
return;
|
return;
|
||||||
|
@ -500,7 +495,7 @@ s_static_sound (sfx_t *sfx, const vec3_t origin, float vol,
|
||||||
if (!sfx->retain (sfx))
|
if (!sfx->retain (sfx))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (sfx->loopstart == (unsigned int)-1) {
|
if (sfx->loopstart == (unsigned int) -1) {
|
||||||
Sys_Printf ("Sound %s not looped\n", sfx->name);
|
Sys_Printf ("Sound %s not looped\n", sfx->name);
|
||||||
sfx->release (sfx);
|
sfx->release (sfx);
|
||||||
return;
|
return;
|
||||||
|
@ -533,7 +528,6 @@ s_updateAmbientSounds (void)
|
||||||
|
|
||||||
if (!snd_ambient)
|
if (!snd_ambient)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// calc ambient sound levels
|
// calc ambient sound levels
|
||||||
if (!*render_data.worldmodel)
|
if (!*render_data.worldmodel)
|
||||||
return;
|
return;
|
||||||
|
@ -879,6 +873,7 @@ s_unblock_sound (void)
|
||||||
{
|
{
|
||||||
if (!snd_blocked)
|
if (!snd_blocked)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!--snd_blocked) {
|
if (!--snd_blocked) {
|
||||||
s_clear_buffer ();
|
s_clear_buffer ();
|
||||||
snd_output_funcs->pS_O_UnblockSound ();
|
snd_output_funcs->pS_O_UnblockSound ();
|
||||||
|
@ -1005,7 +1000,6 @@ s_init (void)
|
||||||
static void
|
static void
|
||||||
s_shutdown (void)
|
s_shutdown (void)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!sound_started)
|
if (!sound_started)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -270,7 +270,7 @@ SND_StreamAdvance (sfxbuffer_t *buffer, unsigned int count)
|
||||||
buffer->pos += count;
|
buffer->pos += count;
|
||||||
if (buffer->pos >= sfx->length) {
|
if (buffer->pos >= sfx->length) {
|
||||||
if (sfx->loopstart == (unsigned int)-1) {
|
if (sfx->loopstart == (unsigned int)-1) {
|
||||||
// reset the buffer and fill it incase it's needed again
|
// reset the buffer and fill it in case it's needed again
|
||||||
headpos = buffer->pos = 0;
|
headpos = buffer->pos = 0;
|
||||||
buffer->head = buffer->tail = 0;
|
buffer->head = buffer->tail = 0;
|
||||||
count = 0;
|
count = 0;
|
||||||
|
@ -343,8 +343,7 @@ sfxbuffer_t *
|
||||||
SND_GetCache (long samples, int rate, int inwidth, int channels,
|
SND_GetCache (long samples, int rate, int inwidth, int channels,
|
||||||
sfxblock_t *block, cache_allocator_t allocator)
|
sfxblock_t *block, cache_allocator_t allocator)
|
||||||
{
|
{
|
||||||
int len, size;
|
int len, size, width;
|
||||||
int width;
|
|
||||||
float stepscale;
|
float stepscale;
|
||||||
sfxbuffer_t *sc;
|
sfxbuffer_t *sc;
|
||||||
sfx_t *sfx = block->sfx;
|
sfx_t *sfx = block->sfx;
|
||||||
|
@ -352,7 +351,7 @@ SND_GetCache (long samples, int rate, int inwidth, int channels,
|
||||||
width = snd_loadas8bit->int_val ? 1 : 2;
|
width = snd_loadas8bit->int_val ? 1 : 2;
|
||||||
stepscale = (float) rate / shm->speed; // usually 0.5, 1, or 2
|
stepscale = (float) rate / shm->speed; // usually 0.5, 1, or 2
|
||||||
len = size = samples / stepscale;
|
len = size = samples / stepscale;
|
||||||
//printf ("%ld %d\n", samples, size);
|
// printf ("%ld %d\n", samples, size);
|
||||||
size *= width * channels;
|
size *= width * channels;
|
||||||
sc = allocator (&block->cache, sizeof (sfxbuffer_t) + size, sfx->name);
|
sc = allocator (&block->cache, sizeof (sfxbuffer_t) + size, sfx->name);
|
||||||
if (!sc)
|
if (!sc)
|
||||||
|
@ -396,7 +395,7 @@ SND_ResampleMono (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
||||||
stepscale = (float) inrate / shm->speed; // usually 0.5, 1, or 2
|
stepscale = (float) inrate / shm->speed; // usually 0.5, 1, or 2
|
||||||
|
|
||||||
outcount = length / stepscale;
|
outcount = length / stepscale;
|
||||||
//printf ("%d %d\n", length, outcount);
|
// printf ("%d %d\n", length, outcount);
|
||||||
|
|
||||||
sc->sfx->length = info->samples / stepscale;
|
sc->sfx->length = info->samples / stepscale;
|
||||||
if (info->loopstart != (unsigned int)-1)
|
if (info->loopstart != (unsigned int)-1)
|
||||||
|
@ -425,21 +424,25 @@ SND_ResampleMono (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
||||||
|
|
||||||
// resample / decimate to the current source rate
|
// resample / decimate to the current source rate
|
||||||
if (stepscale == 1) {
|
if (stepscale == 1) {
|
||||||
if (inwidth == 1 && outwidth == 1) {
|
if (inwidth == 1) {
|
||||||
for (i = 0; i < outcount; i++) {
|
if (outwidth == 1) {
|
||||||
*ob++ = *ib++ - 128;
|
for (i = 0; i < outcount; i++) {
|
||||||
|
*ob++ = *ib++ - 128;
|
||||||
|
}
|
||||||
|
} else if (outwidth == 2) {
|
||||||
|
for (i = 0; i < outcount; i++) {
|
||||||
|
*os++ = (*ib++ - 128) << 8;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (inwidth == 1 && outwidth == 2) {
|
} else if (inwidth == 2) {
|
||||||
for (i = 0; i < outcount; i++) {
|
if (outwidth == 1) {
|
||||||
*os++ = (*ib++ - 128) << 8;
|
for (i = 0; i < outcount; i++) {
|
||||||
}
|
*ob++ = LittleShort (*is++) >> 8;
|
||||||
} else if (inwidth == 2 && outwidth == 1) {
|
}
|
||||||
for (i = 0; i < outcount; i++) {
|
} else if (outwidth == 2) {
|
||||||
*ob++ = LittleShort (*is++) >> 8;
|
for (i = 0; i < outcount; i++) {
|
||||||
}
|
*os++ = LittleShort (*is++);
|
||||||
} else if (inwidth == 2 && outwidth == 2) {
|
}
|
||||||
for (i = 0; i < outcount; i++) {
|
|
||||||
*os++ = LittleShort (*is++);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -502,14 +505,13 @@ SND_ResampleMono (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
||||||
void
|
void
|
||||||
SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
||||||
{
|
{
|
||||||
int fracstep, outcount, sl, sr, samplefrac, srcsample, i;
|
int fracstep, outcount, outwidth, samplefrac, srcsample, sl, sr, i;
|
||||||
float stepscale;
|
float stepscale;
|
||||||
stereo8_t *ib, *ob, *pb;
|
stereo8_t *ib, *ob, *pb;
|
||||||
stereo16_t *is, *os, *ps;
|
stereo16_t *is, *os, *ps;
|
||||||
wavinfo_t *info = sc->sfx->wavinfo (sc->sfx);
|
wavinfo_t *info = sc->sfx->wavinfo (sc->sfx);
|
||||||
int inwidth = info->width;
|
int inwidth = info->width;
|
||||||
int inrate = info->rate;
|
int inrate = info->rate;
|
||||||
int outwidth;
|
|
||||||
stereo16_t zero_s;
|
stereo16_t zero_s;
|
||||||
stereo8_t zero_b;
|
stereo8_t zero_b;
|
||||||
|
|
||||||
|
@ -566,25 +568,29 @@ SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
||||||
|
|
||||||
// resample / decimate to the current source rate
|
// resample / decimate to the current source rate
|
||||||
if (stepscale == 1) {
|
if (stepscale == 1) {
|
||||||
if (inwidth == 1 && outwidth == 1) {
|
if (inwidth == 1) {
|
||||||
for (i = 0; i < outcount; i++, ob++, ib++) {
|
if (outwidth == 1) {
|
||||||
ob->left = ib->left - 128;
|
for (i = 0; i < outcount; i++, ob++, ib++) {
|
||||||
ob->right = ib->right - 128;
|
ob->left = ib->left - 128;
|
||||||
|
ob->right = ib->right - 128;
|
||||||
|
}
|
||||||
|
} else if (outwidth == 2) {
|
||||||
|
for (i = 0; i < outcount; i++, os++, ib++) {
|
||||||
|
os->left = (ib->left - 128) << 8;
|
||||||
|
os->right = (ib->right - 128) << 8;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (inwidth == 1 && outwidth == 2) {
|
} else if (inwidth == 2) {
|
||||||
for (i = 0; i < outcount; i++, os++, ib++) {
|
if (outwidth == 1) {
|
||||||
os->left = (ib->left - 128) << 8;
|
for (i = 0; i < outcount; i++, ob++, ib++) {
|
||||||
os->right = (ib->right - 128) << 8;
|
ob->left = LittleShort (is->left) >> 8;
|
||||||
}
|
ob->right = LittleShort (is->right) >> 8;
|
||||||
} else if (inwidth == 2 && outwidth == 1) {
|
}
|
||||||
for (i = 0; i < outcount; i++, ob++, ib++) {
|
} else if (outwidth == 2) {
|
||||||
ob->left = LittleShort (is->left) >> 8;
|
for (i = 0; i < outcount; i++, os++, is++) {
|
||||||
ob->right = LittleShort (is->right) >> 8;
|
os->left = LittleShort (is->left);
|
||||||
}
|
os->right = LittleShort (is->right);
|
||||||
} else if (inwidth == 2 && outwidth == 2) {
|
}
|
||||||
for (i = 0; i < outcount; i++, os++, is++) {
|
|
||||||
os->left = LittleShort (is->left);
|
|
||||||
os->right = LittleShort (is->right);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -697,25 +703,29 @@ SND_NoResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
||||||
if (!length)
|
if (!length)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (inwidth == 1 && outwidth == 1) {
|
if (inwidth == 1) {
|
||||||
for (i = 0; i < outcount; i++, ob++, ib++) {
|
if (outwidth == 1) {
|
||||||
ob->left = ib->left - 128;
|
for (i = 0; i < outcount; i++, ob++, ib++) {
|
||||||
ob->right = ib->right - 128;
|
ob->left = ib->left - 128;
|
||||||
|
ob->right = ib->right - 128;
|
||||||
|
}
|
||||||
|
} else if (outwidth == 2) {
|
||||||
|
for (i = 0; i < outcount; i++, os++, ib++) {
|
||||||
|
os->left = (ib->left - 128) << 8;
|
||||||
|
os->right = (ib->right - 128) << 8;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (inwidth == 1 && outwidth == 2) {
|
} else if (inwidth == 2) {
|
||||||
for (i = 0; i < outcount; i++, os++, ib++) {
|
if (outwidth == 1) {
|
||||||
os->left = (ib->left - 128) << 8;
|
for (i = 0; i < outcount; i++, ob++, ib++) {
|
||||||
os->right = (ib->right - 128) << 8;
|
ob->left = LittleShort (is->left) >> 8;
|
||||||
}
|
ob->right = LittleShort (is->right) >> 8;
|
||||||
} else if (inwidth == 2 && outwidth == 1) {
|
}
|
||||||
for (i = 0; i < outcount; i++, ob++, ib++) {
|
} else if (outwidth == 2) {
|
||||||
ob->left = LittleShort (is->left) >> 8;
|
for (i = 0; i < outcount; i++, os++, is++) {
|
||||||
ob->right = LittleShort (is->right) >> 8;
|
os->left = LittleShort (is->left);
|
||||||
}
|
os->right = LittleShort (is->right);
|
||||||
} else if (inwidth == 2 && outwidth == 2) {
|
}
|
||||||
for (i = 0; i < outcount; i++, os++, is++) {
|
|
||||||
os->left = LittleShort (is->left);
|
|
||||||
os->right = LittleShort (is->right);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
|
|
@ -451,6 +451,7 @@ SND_PaintChannelFrom16 (channel_t *ch, sfxbuffer_t *sc, int count)
|
||||||
|
|
||||||
if (pos + count > sc->length) {
|
if (pos + count > sc->length) {
|
||||||
unsigned int sub = sc->length - pos;
|
unsigned int sub = sc->length - pos;
|
||||||
|
|
||||||
snd_paint_mono_16 (0, ch, samps, sub);
|
snd_paint_mono_16 (0, ch, samps, sub);
|
||||||
snd_paint_mono_16 (sub, ch, sc->data, count - sub);
|
snd_paint_mono_16 (sub, ch, sc->data, count - sub);
|
||||||
} else {
|
} else {
|
||||||
|
@ -472,6 +473,7 @@ SND_PaintChannelStereo8 (channel_t *ch, sfxbuffer_t *sc, int count)
|
||||||
|
|
||||||
if (pos + count > sc->length) {
|
if (pos + count > sc->length) {
|
||||||
unsigned int sub = sc->length - pos;
|
unsigned int sub = sc->length - pos;
|
||||||
|
|
||||||
snd_paint_stereo_8 (0, ch, samps, sub);
|
snd_paint_stereo_8 (0, ch, samps, sub);
|
||||||
snd_paint_stereo_8 (sub, ch, sc->data, count - sub);
|
snd_paint_stereo_8 (sub, ch, sc->data, count - sub);
|
||||||
} else {
|
} else {
|
||||||
|
@ -493,6 +495,7 @@ SND_PaintChannelStereo16 (channel_t *ch, sfxbuffer_t *sc, int count)
|
||||||
|
|
||||||
if (pos + count > sc->length) {
|
if (pos + count > sc->length) {
|
||||||
unsigned int sub = sc->length - pos;
|
unsigned int sub = sc->length - pos;
|
||||||
|
|
||||||
snd_paint_stereo_16 (0, ch, samps, sub);
|
snd_paint_stereo_16 (0, ch, samps, sub);
|
||||||
snd_paint_stereo_16 (sub, ch, sc->data, count - sub);
|
snd_paint_stereo_16 (sub, ch, sc->data, count - sub);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue