reduce a lot of duplicated code. should have done it this way in the first place. anyway, now I can get on with fixing the cut-off bug (I hope).

This commit is contained in:
Bill Currie 2007-03-26 11:44:52 +00:00 committed by Jeff Teunissen
parent fd8f530656
commit f2e524491d
8 changed files with 87 additions and 192 deletions

View file

@ -99,11 +99,14 @@ struct sfxbuffer_s {
unsigned pos; //!< position of tail within full stream
unsigned bps; //!< bytes per sample: 1 2 4 usually
/** paint samples into the mix buffer
\param offset offset into the mix buffer at which to start mixing
the channel
\param ch sound channel
\param buffer "this"
\param buffer sound data
\param count number of samples to paint
*/
void (*paint) (channel_t *ch, sfxbuffer_t *buffer, int count);
void (*paint) (int offset, channel_t *ch, void *buffer,
unsigned count);
/** Advance the position with the stream, updating the ring buffer as
necessary. Null for chached sounds.
\param buffer "this"
@ -378,6 +381,11 @@ void SND_PaintChannels(unsigned int endtime);
/** Initialize the scale table for painting of 8 bit samples.
*/
void SND_InitScaletable (void);
/** Set the paint function of the sfxbuffer
\param sc sfxbuffer to set.
*/
void SND_SetPaint (sfxbuffer_t *sc);
//@}
@ -538,37 +546,4 @@ sfxbuffer_t *SND_GetCache (long samples, int rate, int inwidth, int channels,
sfxblock_t *block, cache_allocator_t allocator);
//@}
/** \defgroup sound_render_mix_virt Mixer engine virtual functions.
\ingroup sound_render_mix
*/
//@{
/** paint 16 bit mono samples into the mix buffer with spacialization effects
\param ch sound channel
\param sc "this"
\param count number of samples to paint
*/
void SND_PaintChannelFrom8 (channel_t *ch, sfxbuffer_t *sc, int count);
/** paint 16 bit mono samples into the mix buffer with spacialization effects
\param ch sound channel
\param sc "this"
\param count number of samples to paint
*/
void SND_PaintChannelFrom16 (channel_t *ch, sfxbuffer_t *sc, int count);
/** paint 8 bit stereo samples into the mix buffer with spacialization effects
\param ch sound channel
\param sc "this"
\param count number of samples to paint
*/
void SND_PaintChannelStereo8 (channel_t *ch, sfxbuffer_t *sc, int count);
/** paint 16 bit stereo samples into the mix buffer with spacialization effects
\param ch sound channel
\param sc "this"
\param count number of samples to paint
*/
void SND_PaintChannelStereo16 (channel_t *ch, sfxbuffer_t *sc, int count);
//@}
#endif//__snd_render_h

View file

@ -282,6 +282,7 @@ flac_load (flacfile_t *ff, sfxblock_t *block, cache_allocator_t allocator)
sc->sfx = sfx;
if (flac_read (ff, data, info->datalen) < 0)
goto bail;
SND_SetPaint (sc);
resample (sc, data, info->samples, 0);
sc->head = sc->length;
bail:

View file

@ -329,8 +329,8 @@ s_pick_channel (int entnum, int entchannel, int looped)
}
_ch = &(*_ch)->next;
}
_ch = looped ? &looped_dynamic_channels : &dynamic_channels;
if ((ch = SND_AllocChannel ())) {
_ch = looped ? &looped_dynamic_channels : &free_channels;
if ((ch = SND_AllocChannel ()) && looped) {
ch->next = *_ch;
*_ch = ch;
}

View file

@ -47,9 +47,9 @@ static __attribute__ ((used)) const char rcsid[] =
#include "compat.h"
#include "snd_render.h"
cvar_t *snd_volume;
cvar_t *snd_volume;
unsigned int snd_paintedtime; // sample PAIRS
unsigned snd_paintedtime; // sample PAIRS
portable_samplepair_t snd_paintbuffer[PAINTBUFFER_SIZE * 2];
static int max_overpaint; // number of extra samples painted
@ -59,10 +59,10 @@ static int snd_scaletable[32][256];
/* CHANNEL MIXING */
static inline int
advance_channel (channel_t *ch, int count, unsigned int ltime)
advance_channel (channel_t *ch, int count, unsigned ltime)
{
if (!count || ltime >= ch->end) {
if (ch->sfx->loopstart != (unsigned int) -1) {
if (ch->sfx->loopstart != (unsigned) -1) {
ch->pos = ch->sfx->loopstart;
ch->end = ltime + ch->sfx->length - ch->pos;
} else { // channel just stopped
@ -73,23 +73,54 @@ advance_channel (channel_t *ch, int count, unsigned int ltime)
return 1;
}
void
SND_PaintChannels (unsigned int endtime)
static inline void
snd_paint_channel (channel_t *ch, sfxbuffer_t *sc, int count)
{
unsigned int end, ltime;
unsigned pos;
int offs = 0;
byte *samps;
if ((int) ch->pos < 0) {
ch->pos += count;
if ((int) ch->pos <= 0)
return;
offs = ch->pos;
count -= offs;
ch->pos = 0;
}
if (ch->pos < sc->pos)
sc->setpos (sc, ch->pos);
pos = (ch->pos - sc->pos + sc->tail) % sc->length;
samps = sc->data + pos * sc->bps;
if (pos + count > sc->length) {
unsigned sub = sc->length - pos;
sc->paint (offs, ch, samps, sub);
sc->paint (offs + sub, ch, sc->data, count - sub);
} else {
sc->paint (offs, ch, samps, count);
}
ch->pos += count;
}
void
SND_PaintChannels (unsigned endtime)
{
unsigned end, ltime;
int i, count;
channel_t *ch;
sfxbuffer_t *sc;
// clear the paint buffer
memset (snd_paintbuffer, 0, (end - snd_paintedtime) *
sizeof (portable_samplepair_t));
while (snd_paintedtime < endtime) {
// if snd_paintbuffer is smaller than DMA buffer
end = endtime;
if (endtime - snd_paintedtime > PAINTBUFFER_SIZE)
end = snd_paintedtime + PAINTBUFFER_SIZE;
// clear the paint buffer
memset (snd_paintbuffer, 0, (end - snd_paintedtime) *
sizeof (portable_samplepair_t));
max_overpaint = 0;
// paint in the channels.
@ -121,21 +152,14 @@ SND_PaintChannels (unsigned int endtime)
ltime = snd_paintedtime;
while (ltime < end) { // paint up to end
if (ch->end < end)
count = ch->end - ltime;
else
count = end - ltime;
count = (ch->end < end ? ch->end : end) - ltime;
if (count > 0) {
sc->paint (ch, sc, count);
snd_paint_channel (ch, sc, count);
if (sc->advance)
sc->advance (sc, count);
ltime += count;
}
ltime += count;
// if at end of loop, restart
if (!advance_channel (ch, count, ltime))
break;
}
@ -164,12 +188,12 @@ SND_InitScaletable (void)
}
static void
snd_paint_mono_8 (int offs, channel_t *ch, void *bytes, unsigned int count)
snd_paint_mono_8 (int offs, channel_t *ch, void *bytes, unsigned count)
{
unsigned char *sfx;
int data;
unsigned int i;
int *lscale, *rscale;
byte *sfx;
int data;
unsigned i;
int *lscale, *rscale;
portable_samplepair_t *pair;
if (ch->leftvol > 255)
@ -179,7 +203,7 @@ snd_paint_mono_8 (int offs, channel_t *ch, void *bytes, unsigned int count)
lscale = snd_scaletable[ch->leftvol >> 3];
rscale = snd_scaletable[ch->rightvol >> 3];
sfx = (unsigned char *) bytes;
sfx = (byte *) bytes;
pair = snd_paintbuffer + offs;
@ -191,12 +215,12 @@ snd_paint_mono_8 (int offs, channel_t *ch, void *bytes, unsigned int count)
}
static void
snd_paint_mono_16 (int offs, channel_t *ch, void *bytes, unsigned int count)
snd_paint_mono_16 (int offs, channel_t *ch, void *bytes, unsigned count)
{
int leftvol, rightvol;
unsigned int left_phase, right_phase; // Never allowed < 0 anyway
unsigned int i = 0;
signed short *sfx;
int leftvol, rightvol;
unsigned left_phase, right_phase; // Never allowed < 0 anyway
unsigned i = 0;
short *sfx;
portable_samplepair_t *pair;
leftvol = ch->leftvol;
@ -218,9 +242,9 @@ snd_paint_mono_16 (int offs, channel_t *ch, void *bytes, unsigned int count)
}
if (ch->oldphase != ch->phase) {
unsigned int old_phase_left, old_phase_right;
unsigned int new_phase_left, new_phase_right;
unsigned int count_left, count_right, c;
unsigned old_phase_left, old_phase_right;
unsigned new_phase_left, new_phase_right;
unsigned count_left, count_right, c;
if (ch->oldphase >= 0) {
old_phase_left = ch->oldphase;
@ -288,7 +312,7 @@ snd_paint_mono_16 (int offs, channel_t *ch, void *bytes, unsigned int count)
}
static void
snd_paint_stereo_8 (int offs, channel_t *ch, void *bytes, unsigned int count)
snd_paint_stereo_8 (int offs, channel_t *ch, void *bytes, unsigned count)
{
byte *samp;
portable_samplepair_t *pair;
@ -312,7 +336,7 @@ snd_paint_stereo_8 (int offs, channel_t *ch, void *bytes, unsigned int count)
}
static void
snd_paint_stereo_16 (int offs, channel_t *ch, void *bytes, unsigned int count)
snd_paint_stereo_16 (int offs, channel_t *ch, void *bytes, unsigned count)
{
short *samp;
portable_samplepair_t *pair;
@ -329,120 +353,18 @@ snd_paint_stereo_16 (int offs, channel_t *ch, void *bytes, unsigned int count)
}
void
SND_PaintChannelFrom8 (channel_t *ch, sfxbuffer_t *sc, int count)
SND_SetPaint (sfxbuffer_t *sc)
{
unsigned int pos;
int offs = 0;
byte *samps;
if ((int) ch->pos < 0) {
ch->pos += count;
if ((int) ch->pos < 0)
return;
offs = ch->pos;
ch->pos = 0;
}
if (ch->pos < sc->pos)
sc->setpos (sc, ch->pos);
pos = (ch->pos - sc->pos + sc->tail) % sc->length;
samps = sc->data + pos;
if (pos + count > sc->length) {
int sub = sc->length - pos;
snd_paint_mono_8 (offs, ch, samps, sub);
snd_paint_mono_8 (offs + sub, ch, sc->data, count - sub);
wavinfo_t *info = sc->sfx->wavinfo (sc->sfx);
if (snd_loadas8bit->int_val) {
if (info->channels == 2)
sc->paint = snd_paint_stereo_8;
else
sc->paint = snd_paint_mono_8;
} else {
snd_paint_mono_8 (offs, ch, samps, count);
if (info->channels == 2)
sc->paint = snd_paint_stereo_16;
else
sc->paint = snd_paint_mono_16;
}
ch->pos += count;
}
void
SND_PaintChannelFrom16 (channel_t *ch, sfxbuffer_t *sc, int count)
{
unsigned int pos;
int offs = 0;
short *samps;
if ((int) ch->pos < 0) {
ch->pos += count;
if ((int) ch->pos < 0)
return;
offs = ch->pos;
ch->pos = 0;
}
if (ch->pos < sc->pos)
sc->setpos (sc, ch->pos);
pos = (ch->pos - sc->pos + sc->tail) % sc->length;
samps = (short *) sc->data + pos;
if (pos + count > sc->length) {
unsigned int sub = sc->length - pos;
snd_paint_mono_16 (offs, ch, samps, sub);
snd_paint_mono_16 (offs + sub, ch, sc->data, count - sub);
} else {
snd_paint_mono_16 (offs, ch, samps, count);
}
ch->pos += count;
}
void
SND_PaintChannelStereo8 (channel_t *ch, sfxbuffer_t *sc, int count)
{
unsigned int pos;
int offs = 0;
short *samps;
if ((int) ch->pos < 0) {
ch->pos += count;
if ((int) ch->pos < 0)
return;
offs = ch->pos;
ch->pos = 0;
}
if (ch->pos < sc->pos)
sc->setpos (sc, ch->pos);
pos = (ch->pos - sc->pos + sc->tail) % sc->length;
samps = (short *) sc->data + pos;
if (pos + count > sc->length) {
unsigned int sub = sc->length - pos;
snd_paint_stereo_8 (offs, ch, samps, sub);
snd_paint_stereo_8 (offs + sub, ch, sc->data, count - sub);
} else {
snd_paint_stereo_8 (offs, ch, samps, count);
}
ch->pos += count;
}
void
SND_PaintChannelStereo16 (channel_t *ch, sfxbuffer_t *sc, int count)
{
unsigned int pos;
int offs = 0;
int *samps;
if ((int) ch->pos < 0) {
ch->pos += count;
if ((int) ch->pos < 0)
return;
offs = ch->pos;
ch->pos = 0;
}
if (ch->pos < sc->pos)
sc->setpos (sc, ch->pos);
pos = (ch->pos - sc->pos + sc->tail) % sc->length;
samps = (int *) sc->data + pos;
if (pos + count > sc->length) {
unsigned int sub = sc->length - pos;
snd_paint_stereo_16 (offs, ch, samps, sub);
snd_paint_stereo_16 (offs + sub, ch, sc->data, count - sub);
} else {
snd_paint_stereo_16 (offs, ch, samps, count);
}
ch->pos += count;
}

View file

@ -104,14 +104,12 @@ SND_ResampleMono (sfxbuffer_t *sc, byte *data, int length, void *prev)
if (snd_loadas8bit->int_val) {
sc->bps = outwidth = 1;
sc->paint = SND_PaintChannelFrom8;
if (prev) {
zero_s[0] = ((char *)prev)[0];
zero_b[0] = ((char *)prev)[0] + 128;
}
} else {
sc->bps = outwidth = 2;
sc->paint = SND_PaintChannelFrom16;
if (prev) {
zero_s[0] = ((short *)prev)[0];
zero_b[0] = (((short *)prev)[0] >> 8) + 128;
@ -246,7 +244,6 @@ SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev)
if (snd_loadas8bit->int_val) {
outwidth = 1;
sc->paint = SND_PaintChannelStereo8;
sc->bps = 2;
if (prev) {
zero_s.left = ((char *)prev)[0];
@ -256,7 +253,6 @@ SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev)
}
} else {
outwidth = 2;
sc->paint = SND_PaintChannelStereo16;
sc->bps = 4;
if (prev) {
zero_s.left = ((short *)prev)[0];
@ -399,11 +395,9 @@ SND_NoResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev)
if (snd_loadas8bit->int_val) {
outwidth = 1;
sc->paint = SND_PaintChannelStereo8;
sc->bps = 2;
} else {
outwidth = 2;
sc->paint = SND_PaintChannelStereo16;
sc->bps = 4;
}

View file

@ -153,6 +153,7 @@ SND_SFX_StreamOpen (sfx_t *sfx, void *file,
stream->buffer.advance = SND_StreamAdvance;
stream->buffer.setpos = SND_StreamSetPos;
stream->buffer.sfx = new_sfx;
SND_SetPaint (&stream->buffer);
stream->resample (&stream->buffer, 0, 0, 0); // get sfx setup properly
stream->seek (stream->file, 0, &stream->wavinfo);

View file

@ -182,6 +182,7 @@ vorbis_load (OggVorbis_File *vf, sfxblock_t *block, cache_allocator_t allocator)
sc->sfx = sfx;
if (vorbis_read (vf, data, info->datalen) < 0)
goto bail;
SND_SetPaint (sc);
resample (sc, data, info->samples, 0);
sc->head = sc->length;
bail:

View file

@ -71,6 +71,7 @@ wav_callback_load (void *object, cache_allocator_t allocator)
buffer = SND_GetCache (info->samples, info->rate, info->width,
info->channels, block, allocator);
buffer->sfx = sfx;
SND_SetPaint (buffer);
if (info->channels == 2)
SND_ResampleStereo (buffer, data, info->samples, 0);
else