From 2342dfcd8ffa38608eeb55f24e3df2992cfa38bd Mon Sep 17 00:00:00 2001 From: Ragnvald Maartmann-Moe IV Date: Mon, 1 Mar 2004 11:51:01 +0000 Subject: [PATCH] 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. --- libs/audio/renderer/snd_dma.c | 20 ++---- libs/audio/renderer/snd_mem.c | 124 ++++++++++++++++++---------------- libs/audio/renderer/snd_mix.c | 3 + 3 files changed, 77 insertions(+), 70 deletions(-) diff --git a/libs/audio/renderer/snd_dma.c b/libs/audio/renderer/snd_dma.c index 5ac92f908..a5f549cee 100644 --- a/libs/audio/renderer/snd_dma.c +++ b/libs/audio/renderer/snd_dma.c @@ -346,26 +346,23 @@ static void s_start_sound (int entnum, int entchannel, sfx_t *sfx, const vec3_t origin, float fvol, float attenuation) { - int ch_idx, vol; + int ch_idx, vol; unsigned int skip; - channel_t *target_chan, *check; + channel_t *target_chan, *check; if (!sound_started) return; - if (!sfx) return; - if (nosound->int_val) return; - - vol = fvol * 255; - // pick a channel to play on target_chan = s_pick_channel (entnum, entchannel); if (!target_chan) return; + vol = fvol * 255; + // spatialize memset (target_chan, 0, sizeof (*target_chan)); VectorCopy (origin, target_chan->origin); @@ -435,9 +432,7 @@ s_stop_sound (int entnum, int entchannel) static void s_clear_buffer (void) { - int clear; - int i; - + int clear, i; if (!sound_started || !shm || !shm->buffer) return; @@ -500,7 +495,7 @@ s_static_sound (sfx_t *sfx, const vec3_t origin, float vol, if (!sfx->retain (sfx)) return; - if (sfx->loopstart == (unsigned int)-1) { + if (sfx->loopstart == (unsigned int) -1) { Sys_Printf ("Sound %s not looped\n", sfx->name); sfx->release (sfx); return; @@ -533,7 +528,6 @@ s_updateAmbientSounds (void) if (!snd_ambient) return; - // calc ambient sound levels if (!*render_data.worldmodel) return; @@ -879,6 +873,7 @@ s_unblock_sound (void) { if (!snd_blocked) return; + if (!--snd_blocked) { s_clear_buffer (); snd_output_funcs->pS_O_UnblockSound (); @@ -1005,7 +1000,6 @@ s_init (void) static void s_shutdown (void) { - if (!sound_started) return; diff --git a/libs/audio/renderer/snd_mem.c b/libs/audio/renderer/snd_mem.c index 7e4f97840..9a2d8b012 100644 --- a/libs/audio/renderer/snd_mem.c +++ b/libs/audio/renderer/snd_mem.c @@ -270,7 +270,7 @@ SND_StreamAdvance (sfxbuffer_t *buffer, unsigned int count) buffer->pos += count; if (buffer->pos >= sfx->length) { 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; buffer->head = buffer->tail = 0; count = 0; @@ -343,8 +343,7 @@ sfxbuffer_t * SND_GetCache (long samples, int rate, int inwidth, int channels, sfxblock_t *block, cache_allocator_t allocator) { - int len, size; - int width; + int len, size, width; float stepscale; sfxbuffer_t *sc; 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; stepscale = (float) rate / shm->speed; // usually 0.5, 1, or 2 len = size = samples / stepscale; -//printf ("%ld %d\n", samples, size); +// printf ("%ld %d\n", samples, size); size *= width * channels; sc = allocator (&block->cache, sizeof (sfxbuffer_t) + size, sfx->name); 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 outcount = length / stepscale; -//printf ("%d %d\n", length, outcount); +// printf ("%d %d\n", length, outcount); sc->sfx->length = info->samples / stepscale; 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 if (stepscale == 1) { - if (inwidth == 1 && outwidth == 1) { - for (i = 0; i < outcount; i++) { - *ob++ = *ib++ - 128; + if (inwidth == 1) { + if (outwidth == 1) { + 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) { - for (i = 0; i < outcount; i++) { - *os++ = (*ib++ - 128) << 8; - } - } else if (inwidth == 2 && outwidth == 1) { - for (i = 0; i < outcount; i++) { - *ob++ = LittleShort (*is++) >> 8; - } - } else if (inwidth == 2 && outwidth == 2) { - for (i = 0; i < outcount; i++) { - *os++ = LittleShort (*is++); + } else if (inwidth == 2) { + if (outwidth == 1) { + for (i = 0; i < outcount; i++) { + *ob++ = LittleShort (*is++) >> 8; + } + } else if (outwidth == 2) { + for (i = 0; i < outcount; i++) { + *os++ = LittleShort (*is++); + } } } } else { @@ -502,14 +505,13 @@ SND_ResampleMono (sfxbuffer_t *sc, byte *data, int length, void *prev) void 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; stereo8_t *ib, *ob, *pb; stereo16_t *is, *os, *ps; wavinfo_t *info = sc->sfx->wavinfo (sc->sfx); int inwidth = info->width; int inrate = info->rate; - int outwidth; stereo16_t zero_s; 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 if (stepscale == 1) { - if (inwidth == 1 && outwidth == 1) { - for (i = 0; i < outcount; i++, ob++, ib++) { - ob->left = ib->left - 128; - ob->right = ib->right - 128; + if (inwidth == 1) { + if (outwidth == 1) { + for (i = 0; i < outcount; i++, ob++, ib++) { + 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) { - for (i = 0; i < outcount; i++, os++, ib++) { - os->left = (ib->left - 128) << 8; - os->right = (ib->right - 128) << 8; - } - } else if (inwidth == 2 && outwidth == 1) { - for (i = 0; i < outcount; i++, ob++, ib++) { - ob->left = LittleShort (is->left) >> 8; - ob->right = LittleShort (is->right) >> 8; - } - } 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 if (inwidth == 2) { + if (outwidth == 1) { + for (i = 0; i < outcount; i++, ob++, ib++) { + ob->left = LittleShort (is->left) >> 8; + ob->right = LittleShort (is->right) >> 8; + } + } else if (outwidth == 2) { + for (i = 0; i < outcount; i++, os++, is++) { + os->left = LittleShort (is->left); + os->right = LittleShort (is->right); + } } } } else { @@ -697,25 +703,29 @@ SND_NoResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev) if (!length) return; - if (inwidth == 1 && outwidth == 1) { - for (i = 0; i < outcount; i++, ob++, ib++) { - ob->left = ib->left - 128; - ob->right = ib->right - 128; + if (inwidth == 1) { + if (outwidth == 1) { + for (i = 0; i < outcount; i++, ob++, ib++) { + 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) { - for (i = 0; i < outcount; i++, os++, ib++) { - os->left = (ib->left - 128) << 8; - os->right = (ib->right - 128) << 8; - } - } else if (inwidth == 2 && outwidth == 1) { - for (i = 0; i < outcount; i++, ob++, ib++) { - ob->left = LittleShort (is->left) >> 8; - ob->right = LittleShort (is->right) >> 8; - } - } 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 if (inwidth == 2) { + if (outwidth == 1) { + for (i = 0; i < outcount; i++, ob++, ib++) { + ob->left = LittleShort (is->left) >> 8; + ob->right = LittleShort (is->right) >> 8; + } + } else if (outwidth == 2) { + for (i = 0; i < outcount; i++, os++, is++) { + os->left = LittleShort (is->left); + os->right = LittleShort (is->right); + } } } { diff --git a/libs/audio/renderer/snd_mix.c b/libs/audio/renderer/snd_mix.c index 48ea5e699..a249dc8c2 100644 --- a/libs/audio/renderer/snd_mix.c +++ b/libs/audio/renderer/snd_mix.c @@ -451,6 +451,7 @@ SND_PaintChannelFrom16 (channel_t *ch, sfxbuffer_t *sc, int count) if (pos + count > sc->length) { unsigned int sub = sc->length - pos; + snd_paint_mono_16 (0, ch, samps, sub); snd_paint_mono_16 (sub, ch, sc->data, count - sub); } else { @@ -472,6 +473,7 @@ SND_PaintChannelStereo8 (channel_t *ch, sfxbuffer_t *sc, int count) if (pos + count > sc->length) { unsigned int sub = sc->length - pos; + snd_paint_stereo_8 (0, ch, samps, sub); snd_paint_stereo_8 (sub, ch, sc->data, count - sub); } else { @@ -493,6 +495,7 @@ SND_PaintChannelStereo16 (channel_t *ch, sfxbuffer_t *sc, int count) if (pos + count > sc->length) { unsigned int sub = sc->length - pos; + snd_paint_stereo_16 (0, ch, samps, sub); snd_paint_stereo_16 (sub, ch, sc->data, count - sub); } else {