X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=snd_mix.c;h=6bec880eae3974665dcc699feff687b8a84779fb;hb=4e82f39f2505c30be145c98de8bb8aa166c6858c;hp=d7a62553cd0aeb41ac4e7e8a72c9602b2c3626ee;hpb=d013ad0e4dbcfc9c61ac6e3d0984f31972aad0be;p=xonotic%2Fdarkplaces.git diff --git a/snd_mix.c b/snd_mix.c index d7a62553..6bec880e 100644 --- a/snd_mix.c +++ b/snd_mix.c @@ -22,42 +22,29 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "snd_main.h" -typedef struct portable_samplepair_s -{ - int sample[SND_LISTENERS]; -} portable_sampleframe_t; - -// LordHavoc: was 512, expanded to 2048 -#define PAINTBUFFER_SIZE 2048 static portable_sampleframe_t paintbuffer[PAINTBUFFER_SIZE]; +static portable_sampleframe_t paintbuffer_unswapped[PAINTBUFFER_SIZE]; +extern speakerlayout_t snd_speakerlayout; // for querying the listeners -extern void SCR_CaptureVideo_SoundFrame(unsigned char *bufstereo16le, size_t length, int rate); +extern void SCR_CaptureVideo_SoundFrame(const portable_sampleframe_t *paintbuffer, size_t length); static void S_CaptureAVISound(size_t length) { size_t i; - unsigned char out[PAINTBUFFER_SIZE * 4]; - unsigned char* out_ptr; + unsigned int j; if (!cls.capturevideo.active) return; - // write the sound buffer as little endian 16bit interleaved stereo - for(i = 0, out_ptr = out; i < length; i++, out_ptr += 4) + // undo whatever swapping the channel layout (swapstereo, ALSA) did + for(j = 0; j < snd_speakerlayout.channels; ++j) { - int n0, n1; - - n0 = paintbuffer[i].sample[0]; - n0 = bound(-32768, n0, 32767); - out_ptr[0] = (unsigned char)n0; - out_ptr[1] = (unsigned char)(n0 >> 8); - - n1 = paintbuffer[i].sample[1]; - n1 = bound(-32768, n1, 32767); - out_ptr[2] = (unsigned char)n1; - out_ptr[3] = (unsigned char)(n1 >> 8); + unsigned int j0 = snd_speakerlayout.listeners[j].channel_unswapped; + for(i = 0; i < length; ++i) + paintbuffer_unswapped[i].sample[j0] = paintbuffer[i].sample[j]; } - SCR_CaptureVideo_SoundFrame(out, length, snd_renderbuffer->format.speed); + + SCR_CaptureVideo_SoundFrame(paintbuffer_unswapped, length); } static void S_ConvertPaintBuffer(const portable_sampleframe_t *painted_ptr, void *rb_ptr, int nbframes, int width, int channels) @@ -118,6 +105,10 @@ static void S_ConvertPaintBuffer(const portable_sampleframe_t *painted_ptr, void *snd_out++ = bound(-32768, val, 32767); } } + + // noise is really really annoying + if (cls.timedemo) + memset(rb_ptr, 0, nbframes * channels * width); } else // 8bit { @@ -174,6 +165,10 @@ static void S_ConvertPaintBuffer(const portable_sampleframe_t *painted_ptr, void *snd_out++ = bound(0, val, 255); } } + + // noise is really really annoying + if (cls.timedemo) + memset(rb_ptr, 128, nbframes * channels); } } @@ -188,23 +183,18 @@ CHANNEL MIXING static qboolean SND_PaintChannel (channel_t *ch, portable_sampleframe_t *paint, unsigned int count) { - int snd_vol, vol[SND_LISTENERS]; + int vol[SND_LISTENERS]; const snd_buffer_t *sb; unsigned int i, sb_offset; + sfx_t *sfx; - // If this channel manages its own volume - if (ch->flags & CHANNELFLAG_FULLVOLUME) - snd_vol = 256; - else - snd_vol = (int)(volume.value * 256); + sfx = ch->sfx; // fetch the volatile variable + if (!sfx) // given that this is called by the mixer thread, this never happens, but... + return false; - // calculate mixing volumes based on channel volumes and volume cvar - // also limit the volumes to values that won't clip + // move to the stack (do we need to?) for (i = 0;i < SND_LISTENERS;i++) - { - vol[i] = ch->listener_volume[i] * snd_vol; - vol[i] = bound(0, vol[i], 65536); - } + vol[i] = ch->listener_volume[i]; // if volumes are all zero, just return for (i = 0;i < SND_LISTENERS;i++) @@ -214,11 +204,11 @@ static qboolean SND_PaintChannel (channel_t *ch, portable_sampleframe_t *paint, return false; sb_offset = ch->pos; - sb = ch->sfx->fetcher->getsb (ch->sfx->fetcher_data, &ch->fetcher_data, &sb_offset, count); + 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", - ch->sfx->name); // , count); // or add this? FIXME + sfx->name); // , count); // or add this? FIXME return false; } else @@ -272,6 +262,15 @@ static qboolean SND_PaintChannel (channel_t *ch, portable_sampleframe_t *paint, samples += 2; } } + else if (vol[0] + vol[1] > 0 && ch->prologic_invert == -1) + { + for (i = 0;i < count;i++) + { + paint[i].sample[0] += (samples[0] * vol[0]) >> 8; + paint[i].sample[1] -= (samples[1] * vol[1]) >> 8; + samples += 2; + } + } else if (vol[0] + vol[1] > 0) { for (i = 0;i < count;i++) @@ -323,6 +322,15 @@ static qboolean SND_PaintChannel (channel_t *ch, portable_sampleframe_t *paint, samples += 1; } } + else if (vol[0] + vol[1] > 0 && ch->prologic_invert == -1) + { + for (i = 0;i < count;i++) + { + paint[i].sample[0] += (samples[0] * vol[0]) >> 8; + paint[i].sample[1] -= (samples[0] * vol[1]) >> 8; + samples += 1; + } + } else if (vol[0] + vol[1] > 0) { for (i = 0;i < count;i++) @@ -382,6 +390,15 @@ static qboolean SND_PaintChannel (channel_t *ch, portable_sampleframe_t *paint, samples += 2; } } + else if (vol[0] + vol[1] > 0 && ch->prologic_invert == -1) + { + for (i = 0;i < count;i++) + { + paint[i].sample[0] += (samples[0] * vol[0]) >> 16; + paint[i].sample[1] -= (samples[1] * vol[1]) >> 16; + samples += 2; + } + } else if (vol[0] + vol[1] > 0) { for (i = 0;i < count;i++) @@ -433,6 +450,15 @@ static qboolean SND_PaintChannel (channel_t *ch, portable_sampleframe_t *paint, samples += 1; } } + else if (vol[0] + vol[1] > 0 && ch->prologic_invert == -1) + { + for (i = 0;i < count;i++) + { + paint[i].sample[0] += (samples[0] * vol[0]) >> 16; + paint[i].sample[1] -= (samples[0] * vol[1]) >> 16; + samples += 1; + } + } else if (vol[0] + vol[1] > 0) { for (i = 0;i < count;i++) @@ -455,7 +481,7 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes) unsigned int i; channel_t *ch; unsigned int frames; - unsigned char *outbytes = stream; + unsigned char *outbytes = (unsigned char *) stream; // mix as many times as needed to fill the requested buffer while (bufferframes) @@ -472,8 +498,8 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes) for (i = 0; i < total_channels ; i++, ch++) { sfx_t *sfx; - unsigned int ltime; - unsigned int count; + int ltime; + int count; sfx = ch->sfx; if (sfx == NULL) @@ -482,30 +508,34 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes) continue; if (ch->flags & CHANNELFLAG_PAUSED) continue; + if (!sfx->total_length) + continue; + if (sfx->total_length > 1<<30) + Sys_Error("S_MixToBuffer: sfx corrupt\n"); ltime = 0; if (ch->pos < 0) { count = -ch->pos; - count = min(count, frames - ltime); + count = min(count, (int)frames - ltime); ch->pos += count; ltime += count; } - while (ltime < frames) + while (ltime < (int)frames) { // paint up to end of buffer or of input, whichever is lower count = sfx->total_length - ch->pos; - count = bound(0, count, frames - ltime); + count = bound(0, count, (int)frames - ltime); + // mix the remaining samples if (count) { SND_PaintChannel (ch, paintbuffer + ltime, count); ch->pos += count; ltime += count; } - // if at end of sfx, loop or stop the channel - if (ch->pos >= (int)sfx->total_length) + else { if (sfx->loopstart < sfx->total_length) ch->pos = sfx->loopstart; @@ -513,7 +543,7 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes) ch->pos = 0; else { - S_StopChannel (ch - channels); + S_StopChannel (ch - channels, false, false); break; } }