Add lowpass filter

This commit is contained in:
Eric Wasylishen 2014-07-22 18:39:05 -06:00
parent 1bd564d340
commit 07e4fff2af
6 changed files with 193 additions and 37 deletions

View file

@ -826,7 +826,10 @@
GCC_ENABLE_FIX_AND_CONTINUE = YES; GCC_ENABLE_FIX_AND_CONTINUE = YES;
GCC_MODEL_TUNING = G5; GCC_MODEL_TUNING = G5;
GCC_OPTIMIZATION_LEVEL = 0; GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = SDL_FRAMEWORK; GCC_PREPROCESSOR_DEFINITIONS = (
SDL_FRAMEWORK,
USE_CODEC_VORBIS,
);
GCC_VERSION = com.apple.compilers.llvmgcc42; GCC_VERSION = com.apple.compilers.llvmgcc42;
HEADER_SEARCH_PATHS = ( HEADER_SEARCH_PATHS = (
codecs/include, codecs/include,
@ -859,7 +862,7 @@
"$(FRAMEWORK_SEARCH_PATHS_QUOTED_FOR_TARGET_1)", "$(FRAMEWORK_SEARCH_PATHS_QUOTED_FOR_TARGET_1)",
); );
FRAMEWORK_SEARCH_PATHS_QUOTED_FOR_TARGET_1 = "\"$(SRCROOT)\""; FRAMEWORK_SEARCH_PATHS_QUOTED_FOR_TARGET_1 = "\"$(SRCROOT)\"";
GCC_GENERATE_DEBUGGING_SYMBOLS = NO; GCC_GENERATE_DEBUGGING_SYMBOLS = YES;
GCC_MODEL_TUNING = G5; GCC_MODEL_TUNING = G5;
GCC_PREPROCESSOR_DEFINITIONS = ( GCC_PREPROCESSOR_DEFINITIONS = (
USE_CODEC_MP3, USE_CODEC_MP3,
@ -914,6 +917,7 @@
x86_64, x86_64,
i386, i386,
); );
COPY_PHASE_STRIP = NO;
FRAMEWORK_SEARCH_PATHS = ( FRAMEWORK_SEARCH_PATHS = (
/Library/Frameworks, /Library/Frameworks,
"$(FRAMEWORK_SEARCH_PATHS)", "$(FRAMEWORK_SEARCH_PATHS)",
@ -948,9 +952,9 @@
ARCHS = ( ARCHS = (
x86_64, x86_64,
i386, i386,
ppc,
); );
DEPLOYMENT_POSTPROCESSING = YES; COPY_PHASE_STRIP = NO;
DEPLOYMENT_POSTPROCESSING = NO;
FRAMEWORK_SEARCH_PATHS = ( FRAMEWORK_SEARCH_PATHS = (
/Library/Frameworks, /Library/Frameworks,
"$(FRAMEWORK_SEARCH_PATHS)", "$(FRAMEWORK_SEARCH_PATHS)",
@ -1000,7 +1004,7 @@
"SDKROOT[arch=i386]" = "$(DEVELOPER_SDK_DIR)/MacOSX10.4u.sdk"; "SDKROOT[arch=i386]" = "$(DEVELOPER_SDK_DIR)/MacOSX10.4u.sdk";
"SDKROOT[arch=ppc]" = "$(DEVELOPER_SDK_DIR)/MacOSX10.4u.sdk"; "SDKROOT[arch=ppc]" = "$(DEVELOPER_SDK_DIR)/MacOSX10.4u.sdk";
"SDKROOT[arch=x86_64]" = "$(DEVELOPER_SDK_DIR)/MacOSX10.6.sdk"; "SDKROOT[arch=x86_64]" = "$(DEVELOPER_SDK_DIR)/MacOSX10.6.sdk";
VALID_ARCHS = "i386 x86_64 ppc"; VALID_ARCHS = "i386 x86_64";
}; };
name = Release; name = Release;
}; };

View file

@ -21,8 +21,8 @@
</BuildActionEntries> </BuildActionEntries>
</BuildAction> </BuildAction>
<TestAction <TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.GDB" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.GDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES" shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug"> buildConfiguration = "Debug">
<Testables> <Testables>
@ -38,8 +38,8 @@
</MacroExpansion> </MacroExpansion>
</TestAction> </TestAction>
<LaunchAction <LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.GDB" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.GDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
displayScaleIsEnabled = "NO" displayScaleIsEnabled = "NO"
displayScale = "1.00" displayScale = "1.00"
launchStyle = "0" launchStyle = "0"
@ -59,9 +59,13 @@
</BuildableProductRunnable> </BuildableProductRunnable>
<CommandLineArguments> <CommandLineArguments>
<CommandLineArgument <CommandLineArgument
argument = "-basedir /Applications/Quake" argument = "-basedir /Users/ericwa/Quake"
isEnabled = "YES"> isEnabled = "YES">
</CommandLineArgument> </CommandLineArgument>
<CommandLineArgument
argument = "-sndspeed 44100"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument <CommandLineArgument
argument = "-nolauncher" argument = "-nolauncher"
isEnabled = "NO"> isEnabled = "NO">

View file

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

View file

@ -76,6 +76,7 @@ cvar_t precache = {"precache", "1", CVAR_NONE};
cvar_t loadas8bit = {"loadas8bit", "0", CVAR_NONE}; cvar_t loadas8bit = {"loadas8bit", "0", CVAR_NONE};
cvar_t sndspeed = {"sndspeed", "11025", CVAR_NONE}; cvar_t sndspeed = {"sndspeed", "11025", CVAR_NONE};
cvar_t snd_mixspeed = {"snd_mixspeed", "44100", CVAR_NONE};
static cvar_t nosound = {"nosound", "0", CVAR_NONE}; static cvar_t nosound = {"nosound", "0", CVAR_NONE};
static cvar_t ambient_level = {"ambient_level", "0.3", CVAR_NONE}; static cvar_t ambient_level = {"ambient_level", "0.3", CVAR_NONE};
@ -161,6 +162,7 @@ void S_Init (void)
Cvar_RegisterVariable(&snd_show); Cvar_RegisterVariable(&snd_show);
Cvar_RegisterVariable(&_snd_mixahead); Cvar_RegisterVariable(&_snd_mixahead);
Cvar_RegisterVariable(&sndspeed); Cvar_RegisterVariable(&sndspeed);
Cvar_RegisterVariable(&snd_mixspeed);
if (safemode || COM_CheckParm("-nosound")) if (safemode || COM_CheckParm("-nosound"))
return; return;
@ -178,6 +180,12 @@ void S_Init (void)
{ {
Cvar_SetQuick (&sndspeed, com_argv[i+1]); 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) if (host_parms->memsize < 0x800000)
{ {

View file

@ -149,6 +149,128 @@ static void S_TransferPaintBuffer (int endtime)
} }
} }
/*
==============
S_MakeBlackmanWindowKernel
Based on equation 16-4 from
"The Scientist and Engineer's Guide to Digital Signal Processing"
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;
}
}
}
// must be divisible by 4
#define FILTER_KERNEL_SIZE 48
/*
==============
S_LowpassFilter
lowpass filters 24-bit integer samples in 'data' (stored in 32-bit ints).
f_c is the filter cutoff frequency, as a fraction of the samplerate
memory must be an array of FILTER_KERNEL_SIZE floats
==============
*/
static void S_LowpassFilter(float f_c, int *data, int stride, int count,
float *memory)
{
int i;
// M is the "kernel size" parameter for makekernel() - must be even.
// FILTER_KERNEL_SIZE size is M+1, rounded up to be divisible by 4
const int M = FILTER_KERNEL_SIZE - 2;
float input[FILTER_KERNEL_SIZE + count];
static float kernel[FILTER_KERNEL_SIZE];
static float kernel_fc;
if (f_c <= 0 || f_c > 0.5)
return;
if (count < FILTER_KERNEL_SIZE)
{
Con_Warning("S_LowpassFilter: not enough samples");
return;
}
// prepare the kernel
if (kernel_fc != f_c)
{
S_MakeBlackmanWindowKernel(kernel, M, f_c);
kernel_fc = f_c;
}
// set up the input buffer
// memory holds the previous FILTER_KERNEL_SIZE samples of input.
for (i=0; i<FILTER_KERNEL_SIZE; i++)
{
input[i] = memory[i];
}
for (i=0; i<count; i++)
{
input[FILTER_KERNEL_SIZE+i] = data[i * stride] / (32768.0 * 256.0);
}
// copy out the last FILTER_KERNEL_SIZE samples to 'memory' for next time
memcpy(memory, input + count, FILTER_KERNEL_SIZE * sizeof(float));
// apply the filter
for (i=0; i<count; i++)
{
float val[4] = {0, 0, 0, 0};
int j;
for (j=0; j<=M; j+=4)
{
val[0] += kernel[j] * input[M + i - j];
val[1] += kernel[j+1] * input[M + i - (j+1)];
val[2] += kernel[j+2] * input[M + i - (j+2)];
val[3] += kernel[j+3] * input[M + i - (j+3)];
}
data[i * stride] = (val[0] + val[1] + val[2] + val[3])
* (32768.0 * 256.0);
}
}
/* /*
=============================================================================== ===============================================================================
@ -178,32 +300,7 @@ void S_PaintChannels (int endtime)
end = paintedtime + PAINTBUFFER_SIZE; end = paintedtime + PAINTBUFFER_SIZE;
// clear the paint buffer // clear the paint buffer
if (s_rawend < paintedtime) memset(paintbuffer, 0, (end - paintedtime) * sizeof(portable_samplepair_t));
{ // 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;
}
}
// paint in the channels. // paint in the channels.
ch = snd_channels; ch = snd_channels;
@ -255,6 +352,48 @@ 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 < shm->speed)
{
static float memory_l[FILTER_KERNEL_SIZE];
static float memory_r[FILTER_KERNEL_SIZE];
const float cutoff_freq = (sndspeed.value * 0.45) / shm->speed;
S_LowpassFilter(cutoff_freq, (int *)paintbuffer, 2, end - paintedtime, memory_l);
S_LowpassFilter(cutoff_freq, ((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 // transfer out according to DMA format
S_TransferPaintBuffer(end); S_TransferPaintBuffer(end);
paintedtime = end; paintedtime = end;

View file

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