sfx lowpass filter patch

cvar changes:

The "sndspeed" cvar / "-sndspeed" command-line option now control whether the low-pass filter is applied. If it's set to 11025 you get the low-pass filter, otherwise it's not used.

New "snd_mixspeed" cvar (and the "-mixspeed" command-line option); these default to 44100 and just control the sample rate we request from SDL. Not archived.

New “snd_filterquality” cvar, value can be 1-5. Not archived. The “5” setting closely matches the Windows resampler, and the “1” setting closely matches the OS X resampler. The default depends on the OS, “5” is used on windows builds, otherwise “1”, because I wanted the sfx to sound the same as they do with 0.85.9 on each platform. TODO is checking if a setting other than 1 sounds closer to the system resampler on linux (though it probably depends on the distro).

The lowpass filter is only used for sndspeed=11025 and snd_mixspeed=44100, though these are the defaults.


git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@952 af15c1b1-3010-417e-b628-4374ebc0bcbd
This commit is contained in:
Eric Wasylishen 2014-08-05 05:40:34 +00:00
parent d6e0a8c020
commit 931d4e9396
4 changed files with 244 additions and 28 deletions

View File

@ -168,6 +168,8 @@ extern vec3_t listener_right;
extern vec3_t listener_up;
extern cvar_t sndspeed;
extern cvar_t snd_mixspeed;
extern cvar_t snd_filterquality;
extern cvar_t sfxvolume;
extern cvar_t loadas8bit;

View File

@ -76,6 +76,16 @@ cvar_t precache = {"precache", "1", CVAR_NONE};
cvar_t loadas8bit = {"loadas8bit", "0", CVAR_NONE};
cvar_t sndspeed = {"sndspeed", "11025", CVAR_NONE};
cvar_t snd_mixspeed = {"snd_mixspeed", "44100", CVAR_NONE};
#if defined(_WIN32)
#define SND_FILTERQUALITY_DEFAULT "5"
#else
#define SND_FILTERQUALITY_DEFAULT "1"
#endif
cvar_t snd_filterquality = {"snd_filterquality", SND_FILTERQUALITY_DEFAULT,
CVAR_NONE};
static cvar_t nosound = {"nosound", "0", CVAR_NONE};
static cvar_t ambient_level = {"ambient_level", "0.3", CVAR_NONE};
@ -108,6 +118,14 @@ static void SND_Callback_sfxvolume (cvar_t *var)
SND_InitScaletable ();
}
static void SND_Callback_snd_filterquality (cvar_t *var)
{
if (snd_filterquality.value < 1 || snd_filterquality.value > 5)
{
Con_Printf ("snd_filterquality must be between 1 and 5\n");
Cvar_SetQuick (&snd_filterquality, SND_FILTERQUALITY_DEFAULT);
}
}
/*
================
@ -161,7 +179,9 @@ void S_Init (void)
Cvar_RegisterVariable(&snd_show);
Cvar_RegisterVariable(&_snd_mixahead);
Cvar_RegisterVariable(&sndspeed);
Cvar_RegisterVariable(&snd_mixspeed);
Cvar_RegisterVariable(&snd_filterquality);
if (safemode || COM_CheckParm("-nosound"))
return;
@ -178,6 +198,12 @@ void S_Init (void)
{
Cvar_SetQuick (&sndspeed, com_argv[i+1]);
}
i = COM_CheckParm("-mixspeed");
if (i && i < com_argc-1)
{
Cvar_SetQuick (&snd_mixspeed, com_argv[i+1]);
}
if (host_parms->memsize < 0x800000)
{
@ -186,6 +212,7 @@ void S_Init (void)
}
Cvar_SetCallback(&sfxvolume, SND_Callback_sfxvolume);
Cvar_SetCallback(&snd_filterquality, &SND_Callback_snd_filterquality);
SND_InitScaletable ();

View File

@ -149,6 +149,180 @@ static void S_TransferPaintBuffer (int endtime)
}
}
/*
==============
S_MakeBlackmanWindowKernel
Makes a lowpass filter kernel, from equation 16-4 in
"The Scientist and Engineer's Guide to Digital Signal Processing"
M is the kernel size (not counting the center point), must be even
kernel has room for M+1 floats
f_c is the filter cutoff frequency, as a fraction of the samplerate
==============
*/
static void S_MakeBlackmanWindowKernel(float *kernel, int M, float f_c)
{
int i;
for (i = 0; i <= M; i++)
{
if (i == M/2)
{
kernel[i] = 2 * M_PI * f_c;
}
else
{
kernel[i] = ( sin(2 * M_PI * f_c * (i - M/2.0)) / (i - (M/2.0)) )
* (0.42 - 0.5*cos(2 * M_PI * i / (double)M)
+ 0.08*cos(4 * M_PI * i / (double)M) );
}
}
// normalize the kernel so all of the values sum to 1
{
float sum = 0;
for (i = 0; i <= M; i++)
{
sum += kernel[i];
}
for (i = 0; i <= M; i++)
{
kernel[i] /= sum;
}
}
}
typedef struct {
float *memory; // kernelsize floats
float *kernel; // kernelsize floats
int kernelsize; // M+1, rounded up to be a multiple of 16
int M; // M value used to make kernel, even
int parity; // 0-3
float f_c; // cutoff frequency, [0..1], fraction of sample rate
} filter_t;
static void S_UpdateFilter(filter_t *filter, int M, float f_c)
{
if (filter->f_c != f_c || filter->M != M)
{
if (filter->memory != NULL) free(filter->memory);
if (filter->kernel != NULL) free(filter->kernel);
filter->M = M;
filter->f_c = f_c;
filter->parity = 0;
// M + 1 rounded up to the next multiple of 16
filter->kernelsize = (M + 1) + 16 - ((M + 1) % 16);
filter->memory = calloc(filter->kernelsize, sizeof(float));
filter->kernel = calloc(filter->kernelsize, sizeof(float));
S_MakeBlackmanWindowKernel(filter->kernel, M, f_c);
}
}
/*
==============
S_ApplyFilter
Lowpass-filter the given buffer containing 44100Hz audio.
As an optimization, it decimates the audio to 11025Hz (setting every sample
position that's not a multiple of 4 to 0), then convoluting with the filter
kernel is 4x faster, because we can skip 3/4 of the input samples that are
known to be 0 and skip 3/4 of the filter kernel.
==============
*/
static void S_ApplyFilter(filter_t *filter, int *data, int stride, int count)
{
int i, j;
float *input;
const int kernelsize = filter->kernelsize;
const float *kernel = filter->kernel;
int parity;
input = malloc(sizeof(float) * (filter->kernelsize + count));
// set up the input buffer
// memory holds the previous filter->kernelsize samples of input.
memcpy(input, filter->memory, filter->kernelsize * sizeof(float));
for (i=0; i<count; i++)
{
input[filter->kernelsize+i] = data[i * stride] / (32768.0 * 256.0);
}
// copy out the last filter->kernelsize samples to 'memory' for next time
memcpy(filter->memory, input + count, filter->kernelsize * sizeof(float));
// apply the filter
parity = filter->parity;
for (i=0; i<count; i++)
{
const float *input_plus_i = input + i;
float val[4] = {0, 0, 0, 0};
for (j = (4 - parity) % 4; j < kernelsize; j+=16)
{
val[0] += kernel[j] * input_plus_i[j];
val[1] += kernel[j+4] * input_plus_i[j+4];
val[2] += kernel[j+8] * input_plus_i[j+8];
val[3] += kernel[j+12] * input_plus_i[j+12];
}
// 4.0 factor is to increase volume by 12 dB; this is to make up the
// volume drop caused by the zero-filling this filter does.
data[i * stride] = (val[0] + val[1] + val[2] + val[3])
* (32768.0 * 256.0 * 4.0);
parity = (parity + 1) % 4;
}
filter->parity = parity;
free(input);
}
/*
==============
S_LowpassFilter
lowpass filters 24-bit integer samples in 'data' (stored in 32-bit ints).
assumes 44100Hz sample rate, and lowpasses at around 5kHz
memory should be a zero-filled filter_t struct
==============
*/
static void S_LowpassFilter(int *data, int stride, int count,
filter_t *memory)
{
int M;
float bw, f_c;
switch ((int)snd_filterquality.value)
{
case 1:
M = 126; bw = 0.900; break;
case 2:
M = 150; bw = 0.915; break;
case 3:
M = 174; bw = 0.930; break;
case 4:
M = 198; bw = 0.945; break;
case 5:
default:
M = 222; bw = 0.960; break;
}
f_c = (bw * 11025 / 2.0) / 44100.0;
S_UpdateFilter(memory, M, f_c);
S_ApplyFilter(memory, data, stride, count);
}
/*
===============================================================================
@ -178,32 +352,7 @@ void S_PaintChannels (int endtime)
end = paintedtime + PAINTBUFFER_SIZE;
// clear the paint buffer
if (s_rawend < paintedtime)
{ // clear
memset(paintbuffer, 0, (end - paintedtime) * sizeof(portable_samplepair_t));
}
else
{ // copy from the streaming sound source
int s;
int stop;
stop = (end < s_rawend) ? end : s_rawend;
for (i = paintedtime; i < stop; i++)
{
s = i & (MAX_RAW_SAMPLES - 1);
paintbuffer[i - paintedtime] = s_rawsamples[s];
}
// if (i != end)
// Con_Printf ("partial stream\n");
// else
// Con_Printf ("full stream\n");
for ( ; i < end; i++)
{
paintbuffer[i - paintedtime].left =
paintbuffer[i - paintedtime].right = 0;
}
}
memset(paintbuffer, 0, (end - paintedtime) * sizeof(portable_samplepair_t));
// paint in the channels.
ch = snd_channels;
@ -255,6 +404,44 @@ void S_PaintChannels (int endtime)
}
}
// clip each sample to 0dB, then reduce by 6dB (to leave some headroom for
// the lowpass filter and the music). the lowpass will smooth out the
// clipping
for (i=0; i<end-paintedtime; i++)
{
paintbuffer[i].left = CLAMP(-32768 << 8, paintbuffer[i].left, 32767 << 8) >> 1;
paintbuffer[i].right = CLAMP(-32768 << 8, paintbuffer[i].right, 32767 << 8) >> 1;
}
// apply a lowpass filter
if (sndspeed.value == 11025 && shm->speed == 44100)
{
static filter_t memory_l, memory_r;
S_LowpassFilter((int *)paintbuffer, 2, end - paintedtime, &memory_l);
S_LowpassFilter(((int *)paintbuffer) + 1, 2, end - paintedtime, &memory_r);
}
// paint in the music
if (s_rawend >= paintedtime)
{ // copy from the streaming sound source
int s;
int stop;
stop = (end < s_rawend) ? end : s_rawend;
for (i = paintedtime; i < stop; i++)
{
s = i & (MAX_RAW_SAMPLES - 1);
// lower music by 6db to match sfx
paintbuffer[i - paintedtime].left += s_rawsamples[s].left >> 1;
paintbuffer[i - paintedtime].right += s_rawsamples[s].right >> 1;
}
// if (i != end)
// Con_Printf ("partial stream\n");
// else
// Con_Printf ("full stream\n");
}
// transfer out according to DMA format
S_TransferPaintBuffer(end);
paintedtime = end;

View File

@ -86,7 +86,7 @@ qboolean SNDDMA_Init (dma_t *dma)
}
/* Set up the desired format */
desired.freq = tmp = sndspeed.value;
desired.freq = tmp = snd_mixspeed.value;
desired.format = (loadas8bit.value) ? AUDIO_U8 : AUDIO_S16SYS;
desired.channels = 2; /* = desired_channels; */
if (desired.freq <= 11025)