+ *snd_out++ = bound(-32768, painted_ptr->sample[0], 32767);
+ *snd_out++ = bound(-32768, painted_ptr->sample[1], 32767);
+ *snd_out++ = bound(-32768, painted_ptr->sample[2], 32767);
+ *snd_out++ = bound(-32768, painted_ptr->sample[3], 32767);
+ }
+ }
+ else if (channels == 2) // 2.0 stereo
+ {
+ for (i = 0; i < nbframes; i++, painted_ptr++)
+ {
+ *snd_out++ = bound(-32768, painted_ptr->sample[0], 32767);
+ *snd_out++ = bound(-32768, painted_ptr->sample[1], 32767);
+ }
+ }
+ else if (channels == 1) // 1.0 mono
+ {
+ for (i = 0; i < nbframes; i++, painted_ptr++)
+ {
+ val = (painted_ptr->sample[0] + painted_ptr->sample[1]) >> 1;
+ *snd_out++ = bound(-32768, val, 32767);
+ }
+ }
+
+ // noise is really really annoying
+ if (cls.timedemo)
+ memset(rb_ptr, 0, nbframes * channels * width);
+ }
+ else // 8bit
+ {
+ unsigned char *snd_out = (unsigned char*)rb_ptr;
+ if (channels == 8) // 7.1 surround
+ {
+ for (i = 0; i < nbframes; i++, painted_ptr++)
+ {
+ val = (painted_ptr->sample[0] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[1] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[2] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[3] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[4] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[5] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[6] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[7] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ }
+ }
+ else if (channels == 6) // 5.1 surround
+ {
+ for (i = 0; i < nbframes; i++, painted_ptr++)
+ {
+ val = (painted_ptr->sample[0] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[1] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[2] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[3] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[4] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[5] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ }
+ }
+ else if (channels == 4) // 4.0 surround
+ {
+ for (i = 0; i < nbframes; i++, painted_ptr++)
+ {
+ val = (painted_ptr->sample[0] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[1] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[2] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[3] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ }
+ }
+ else if (channels == 2) // 2.0 stereo
+ {
+ for (i = 0; i < nbframes; i++, painted_ptr++)
+ {
+ val = (painted_ptr->sample[0] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ val = (painted_ptr->sample[1] >> 8) + 128; *snd_out++ = bound(0, val, 255);
+ }
+ }
+ else if (channels == 1) // 1.0 mono
+ {
+ for (i = 0;i < nbframes;i++, painted_ptr++)
+ {
+ val = ((painted_ptr->sample[0] + painted_ptr->sample[1]) >> 9) + 128;
+ *snd_out++ = bound(0, val, 255);
+ }
+ }
+
+ // noise is really really annoying
+ if (cls.timedemo)
+ memset(rb_ptr, 128, nbframes * channels);
+ }
+}
+
+
+/*
+===============================================================================
+
+CHANNEL MIXING
+
+===============================================================================
+*/
+
+static qboolean SND_PaintChannel (channel_t *ch, portable_sampleframe_t *paint, unsigned int count)
+{
+ int vol[SND_LISTENERS];
+ const snd_buffer_t *sb;
+ unsigned int i, sb_offset;
+ sfx_t *sfx;
+
+ sfx = ch->sfx; // fetch the volatile variable
+ if (!sfx) // given that this is called by the mixer thread, this never happens, but...
+ return false;
+
+ // move to the stack (do we need to?)
+ for (i = 0;i < SND_LISTENERS;i++)
+ vol[i] = ch->listener_volume[i];
+
+ // if volumes are all zero, just return
+ for (i = 0;i < SND_LISTENERS;i++)
+ if (vol[i])
+ break;
+ if (i == SND_LISTENERS)
+ return false;
+
+ sb_offset = ch->pos;
+ sb = sfx->fetcher->getsb (sfx->fetcher_data, &ch->fetcher_data, &sb_offset, count);
+ if (sb == NULL)
+ {
+ Con_DPrintf("SND_PaintChannel: ERROR: can't get sound buffer from sfx \"%s\"\n",
+ sfx->name); // , count); // or add this? FIXME
+ return false;
+ }
+ else
+ {
+#if SND_LISTENERS != 8
+# error the following code only supports up to 8 channels, update it
+#endif
+ if (sb->format.width == 1)
+ {
+ const signed char *samples = (signed char*)sb->samples + (ch->pos - sb_offset) * sb->format.channels;
+
+ // Stereo sound support
+ if (sb->format.channels == 2)
+ {
+ if (vol[6] + vol[7] > 0)