aboutsummaryrefslogtreecommitdiffstats
path: root/OpenAL32
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2015-11-01 04:43:55 -0800
committerChris Robinson <[email protected]>2015-11-01 05:41:06 -0800
commitc57f57192067e2d68cfe4ab0fc9479d2453bfbda (patch)
treefb9dabd6296c87ff72f4271774ae9951898d9227 /OpenAL32
parentf094d94608e00b1b08bd8c607d16651072323bb5 (diff)
Pass in the Q parameter for setting the filter parameters
Also better handle the peaking filter gain.
Diffstat (limited to 'OpenAL32')
-rw-r--r--OpenAL32/Include/alFilter.h43
-rw-r--r--OpenAL32/Include/alu.h13
-rw-r--r--OpenAL32/alFilter.c12
3 files changed, 41 insertions, 27 deletions
diff --git a/OpenAL32/Include/alFilter.h b/OpenAL32/Include/alFilter.h
index 9772f432..3c65fd9f 100644
--- a/OpenAL32/Include/alFilter.h
+++ b/OpenAL32/Include/alFilter.h
@@ -3,6 +3,8 @@
#include "alMain.h"
+#include "math_defs.h"
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -11,23 +13,29 @@ extern "C" {
#define HIGHPASSFREQREF (250.0f)
-/* Filters implementation is based on the "Cookbook formulae for audio *
- * EQ biquad filter coefficients" by Robert Bristow-Johnson *
- * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt */
+/* Filters implementation is based on the "Cookbook formulae for audio
+ * EQ biquad filter coefficients" by Robert Bristow-Johnson
+ * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
+ */
+/* Implementation note: For the shelf filters, the specified gain is for the
+ * reference frequency, which is the centerpoint of the transition band. This
+ * better matches EFX filter design. To set the gain for the shelf itself, use
+ * the square root of the desired linear gain (or halve the dB gain).
+ */
typedef enum ALfilterType {
/** EFX-style low-pass filter, specifying a gain and reference frequency. */
ALfilterType_HighShelf,
/** EFX-style high-pass filter, specifying a gain and reference frequency. */
ALfilterType_LowShelf,
- /** Peaking filter, specifying a gain, reference frequency, and bandwidth. */
+ /** Peaking filter, specifying a gain and reference frequency. */
ALfilterType_Peaking,
- /** Low-pass cut-off filter, specifying a cut-off frequency and bandwidth. */
+ /** Low-pass cut-off filter, specifying a cut-off frequency. */
ALfilterType_LowPass,
- /** High-pass cut-off filter, specifying a cut-off frequency and bandwidth. */
+ /** High-pass cut-off filter, specifying a cut-off frequency. */
ALfilterType_HighPass,
- /** Band-pass filter, specifying a center frequency and bandwidth. */
+ /** Band-pass filter, specifying a center frequency. */
ALfilterType_BandPass,
} ALfilterType;
@@ -41,8 +49,27 @@ typedef struct ALfilterState {
} ALfilterState;
#define ALfilterState_process(a, ...) ((a)->process((a), __VA_ARGS__))
+/* Calculates the rcpQ (i.e. 1/Q) coefficient for shelving filters, using the
+ * reference gain and shelf slope parameter.
+ * 0 < gain
+ * 0 < slope <= 1
+ */
+inline ALfloat calc_rcpQ_from_slope(ALfloat gain, ALfloat slope)
+{
+ return sqrtf((gain + 1.0f/gain)*(1.0f/slope - 1.0f) + 2.0f);
+}
+/* Calculates the rcpQ (i.e. 1/Q) coefficient for filters, using the frequency
+ * multiple (i.e. ref_freq / sampling_freq) and bandwidth.
+ * 0 < freq_mult < 0.5.
+ */
+inline ALfloat calc_rcpQ_from_bandwidth(ALfloat freq_mult, ALfloat bandwidth)
+{
+ ALfloat w0 = F_TAU * freq_mult;
+ return 2.0f*sinhf(logf(2.0f)/2.0f*bandwidth*w0/sinf(w0));
+}
+
void ALfilterState_clear(ALfilterState *filter);
-void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat gain, ALfloat freq_mult, ALfloat bandwidth);
+void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat gain, ALfloat freq_mult, ALfloat rcpQ);
inline ALfloat ALfilterState_processSingle(ALfilterState *filter, ALfloat sample)
{
diff --git a/OpenAL32/Include/alu.h b/OpenAL32/Include/alu.h
index 126fffb5..27cf6713 100644
--- a/OpenAL32/Include/alu.h
+++ b/OpenAL32/Include/alu.h
@@ -16,18 +16,7 @@
#include "hrtf.h"
#include "align.h"
-
-
-#define F_PI (3.14159265358979323846f)
-#define F_PI_2 (1.57079632679489661923f)
-#define F_TAU (6.28318530717958647692f)
-
-#ifndef FLT_EPSILON
-#define FLT_EPSILON (1.19209290e-07f)
-#endif
-
-#define DEG2RAD(x) ((ALfloat)(x) * (F_PI/180.0f))
-#define RAD2DEG(x) ((ALfloat)(x) * (180.0f/F_PI))
+#include "math_defs.h"
#define MAX_PITCH (255)
diff --git a/OpenAL32/alFilter.c b/OpenAL32/alFilter.c
index bced3a7f..3cf82c32 100644
--- a/OpenAL32/alFilter.c
+++ b/OpenAL32/alFilter.c
@@ -32,6 +32,8 @@
extern inline struct ALfilter *LookupFilter(ALCdevice *device, ALuint id);
extern inline struct ALfilter *RemoveFilter(ALCdevice *device, ALuint id);
extern inline ALfloat ALfilterState_processSingle(ALfilterState *filter, ALfloat sample);
+extern inline ALfloat calc_rcpQ_from_slope(ALfloat gain, ALfloat slope);
+extern inline ALfloat calc_rcpQ_from_bandwidth(ALfloat freq_mult, ALfloat bandwidth);
static void InitFilterParams(ALfilter *filter, ALenum type);
@@ -336,7 +338,7 @@ void ALfilterState_clear(ALfilterState *filter)
filter->y[1] = 0.0f;
}
-void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat gain, ALfloat freq_mult, ALfloat bandwidth)
+void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat gain, ALfloat freq_mult, ALfloat rcpQ)
{
ALfloat alpha, sqrtgain_alpha_2;
ALfloat w0, sin_w0, cos_w0;
@@ -347,12 +349,12 @@ void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat g
w0 = F_TAU * freq_mult;
sin_w0 = sinf(w0);
cos_w0 = cosf(w0);
+ alpha = sin_w0/2.0f * rcpQ;
/* Calculate filter coefficients depending on filter type */
switch(type)
{
case ALfilterType_HighShelf:
- alpha = sin_w0/2.0f*sqrtf((gain + 1.0f/gain)*(1.0f/0.75f - 1.0f) + 2.0f);
sqrtgain_alpha_2 = 2.0f * sqrtf(gain) * alpha;
filter->b[0] = gain*((gain+1.0f) + (gain-1.0f)*cos_w0 + sqrtgain_alpha_2);
filter->b[1] = -2.0f*gain*((gain-1.0f) + (gain+1.0f)*cos_w0 );
@@ -362,7 +364,6 @@ void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat g
filter->a[2] = (gain+1.0f) - (gain-1.0f)*cos_w0 - sqrtgain_alpha_2;
break;
case ALfilterType_LowShelf:
- alpha = sin_w0/2.0f*sqrtf((gain + 1.0f/gain)*(1.0f/0.75f - 1.0f) + 2.0f);
sqrtgain_alpha_2 = 2.0f * sqrtf(gain) * alpha;
filter->b[0] = gain*((gain+1.0f) - (gain-1.0f)*cos_w0 + sqrtgain_alpha_2);
filter->b[1] = 2.0f*gain*((gain-1.0f) - (gain+1.0f)*cos_w0 );
@@ -372,7 +373,7 @@ void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat g
filter->a[2] = (gain+1.0f) + (gain-1.0f)*cos_w0 - sqrtgain_alpha_2;
break;
case ALfilterType_Peaking:
- alpha = sin_w0 * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sin_w0);
+ gain = sqrtf(gain);
filter->b[0] = 1.0f + alpha * gain;
filter->b[1] = -2.0f * cos_w0;
filter->b[2] = 1.0f - alpha * gain;
@@ -382,7 +383,6 @@ void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat g
break;
case ALfilterType_LowPass:
- alpha = sin_w0 * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sin_w0);
filter->b[0] = (1.0f - cos_w0) / 2.0f;
filter->b[1] = 1.0f - cos_w0;
filter->b[2] = (1.0f - cos_w0) / 2.0f;
@@ -391,7 +391,6 @@ void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat g
filter->a[2] = 1.0f - alpha;
break;
case ALfilterType_HighPass:
- alpha = sin_w0 * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sin_w0);
filter->b[0] = (1.0f + cos_w0) / 2.0f;
filter->b[1] = -(1.0f + cos_w0);
filter->b[2] = (1.0f + cos_w0) / 2.0f;
@@ -400,7 +399,6 @@ void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat g
filter->a[2] = 1.0f - alpha;
break;
case ALfilterType_BandPass:
- alpha = sin_w0 * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sin_w0);
filter->b[0] = alpha;
filter->b[1] = 0;
filter->b[2] = -alpha;