2007-12-17 15:43:35 -08:00
|
|
|
#ifndef _AL_FILTER_H_
|
|
|
|
#define _AL_FILTER_H_
|
|
|
|
|
2012-09-14 02:42:36 -07:00
|
|
|
#include "alMain.h"
|
2007-12-17 15:43:35 -08:00
|
|
|
|
2015-11-01 04:43:55 -08:00
|
|
|
#include "math_defs.h"
|
|
|
|
|
2007-12-17 15:43:35 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-05-11 10:09:52 -07:00
|
|
|
#define LOWPASSFREQREF (5000.0f)
|
2014-05-17 07:29:50 -07:00
|
|
|
#define HIGHPASSFREQREF (250.0f)
|
2012-09-14 02:52:37 -07:00
|
|
|
|
2013-06-06 03:24:44 -07:00
|
|
|
|
2015-11-01 04:43:55 -08:00
|
|
|
/* 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).
|
|
|
|
*/
|
2013-06-06 03:24:44 -07:00
|
|
|
|
2013-05-27 15:32:02 -07:00
|
|
|
typedef enum ALfilterType {
|
2014-05-17 02:09:43 -07:00
|
|
|
/** EFX-style low-pass filter, specifying a gain and reference frequency. */
|
2013-05-27 15:32:02 -07:00
|
|
|
ALfilterType_HighShelf,
|
2014-05-17 02:09:43 -07:00
|
|
|
/** EFX-style high-pass filter, specifying a gain and reference frequency. */
|
2013-05-27 15:32:02 -07:00
|
|
|
ALfilterType_LowShelf,
|
2015-11-01 04:43:55 -08:00
|
|
|
/** Peaking filter, specifying a gain and reference frequency. */
|
2013-05-27 15:32:02 -07:00
|
|
|
ALfilterType_Peaking,
|
2013-06-06 03:24:44 -07:00
|
|
|
|
2015-11-01 04:43:55 -08:00
|
|
|
/** Low-pass cut-off filter, specifying a cut-off frequency. */
|
2013-06-06 03:24:44 -07:00
|
|
|
ALfilterType_LowPass,
|
2015-11-01 04:43:55 -08:00
|
|
|
/** High-pass cut-off filter, specifying a cut-off frequency. */
|
2013-10-03 03:32:54 -07:00
|
|
|
ALfilterType_HighPass,
|
2015-11-01 04:43:55 -08:00
|
|
|
/** Band-pass filter, specifying a center frequency. */
|
2013-06-06 03:24:44 -07:00
|
|
|
ALfilterType_BandPass,
|
2013-05-27 15:32:02 -07:00
|
|
|
} ALfilterType;
|
|
|
|
|
|
|
|
typedef struct ALfilterState {
|
|
|
|
ALfloat x[2]; /* History of two last input samples */
|
|
|
|
ALfloat y[2]; /* History of two last output samples */
|
2016-07-26 00:03:44 -07:00
|
|
|
ALfloat b0, b1, b2; /* Transfer function coefficients "b" */
|
2016-12-21 21:35:50 -08:00
|
|
|
ALfloat a1, a2; /* Transfer function coefficients "a" (a0 is pre-applied) */
|
2013-05-27 15:32:02 -07:00
|
|
|
} ALfilterState;
|
2016-09-12 11:48:15 -07:00
|
|
|
/* Currently only a C-based filter process method is implemented. */
|
|
|
|
#define ALfilterState_process ALfilterState_processC
|
2013-05-27 15:32:02 -07:00
|
|
|
|
2018-01-13 09:14:46 -08:00
|
|
|
/**
|
|
|
|
* Calculates the rcpQ (i.e. 1/Q) coefficient for shelving filters, using the
|
2015-11-01 04:43:55 -08:00
|
|
|
* reference gain and shelf slope parameter.
|
2018-01-13 09:14:46 -08:00
|
|
|
* \param gain 0 < gain
|
|
|
|
* \param slope 0 < slope <= 1
|
2015-11-01 04:43:55 -08:00
|
|
|
*/
|
|
|
|
inline ALfloat calc_rcpQ_from_slope(ALfloat gain, ALfloat slope)
|
|
|
|
{
|
|
|
|
return sqrtf((gain + 1.0f/gain)*(1.0f/slope - 1.0f) + 2.0f);
|
|
|
|
}
|
2018-01-13 09:14:46 -08:00
|
|
|
/**
|
|
|
|
* Calculates the rcpQ (i.e. 1/Q) coefficient for filters, using the normalized
|
|
|
|
* reference frequency and bandwidth.
|
|
|
|
* \param f0norm 0 < f0norm < 0.5.
|
|
|
|
* \param bandwidth 0 < bandwidth
|
2015-11-01 04:43:55 -08:00
|
|
|
*/
|
2018-01-13 09:14:46 -08:00
|
|
|
inline ALfloat calc_rcpQ_from_bandwidth(ALfloat f0norm, ALfloat bandwidth)
|
2015-11-01 04:43:55 -08:00
|
|
|
{
|
2018-01-13 09:14:46 -08:00
|
|
|
ALfloat w0 = F_TAU * f0norm;
|
2015-11-01 04:43:55 -08:00
|
|
|
return 2.0f*sinhf(logf(2.0f)/2.0f*bandwidth*w0/sinf(w0));
|
|
|
|
}
|
|
|
|
|
2016-01-23 01:22:08 -08:00
|
|
|
inline void ALfilterState_clear(ALfilterState *filter)
|
|
|
|
{
|
|
|
|
filter->x[0] = 0.0f;
|
|
|
|
filter->x[1] = 0.0f;
|
|
|
|
filter->y[0] = 0.0f;
|
|
|
|
filter->y[1] = 0.0f;
|
|
|
|
}
|
|
|
|
|
2018-01-13 09:14:46 -08:00
|
|
|
/**
|
|
|
|
* Sets up the filter state for the specified filter type and its parameters.
|
|
|
|
*
|
|
|
|
* \param filter The filter object to prepare.
|
|
|
|
* \param type The type of filter for the object to apply.
|
|
|
|
* \param gain The gain for the reference frequency response. Only used by the
|
|
|
|
* Shelf and Peaking filter types.
|
|
|
|
* \param f0norm The normalized reference frequency (ref_freq / sample_rate).
|
|
|
|
* This is the center point for the Shelf, Peaking, and BandPass
|
|
|
|
* filter types, or the cutoff frequency for the LowPass and
|
|
|
|
* HighPass filter types.
|
|
|
|
* \param rcpQ The reciprocal of the Q coefficient for the filter's transition
|
|
|
|
* band. Can be generated from calc_rcpQ_from_slope or
|
|
|
|
* calc_rcpQ_from_bandwidth depending on the available data.
|
|
|
|
*/
|
|
|
|
void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat gain, ALfloat f0norm, ALfloat rcpQ);
|
2013-05-27 15:32:02 -07:00
|
|
|
|
2017-05-21 03:31:44 -07:00
|
|
|
inline void ALfilterState_copyParams(ALfilterState *restrict dst, const ALfilterState *restrict src)
|
|
|
|
{
|
|
|
|
dst->b0 = src->b0;
|
|
|
|
dst->b1 = src->b1;
|
|
|
|
dst->b2 = src->b2;
|
|
|
|
dst->a1 = src->a1;
|
|
|
|
dst->a2 = src->a2;
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:54:30 -08:00
|
|
|
void ALfilterState_processC(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *restrict src, ALsizei numsamples);
|
2014-05-18 05:02:34 -07:00
|
|
|
|
2017-01-16 08:54:30 -08:00
|
|
|
inline void ALfilterState_processPassthru(ALfilterState *filter, const ALfloat *restrict src, ALsizei numsamples)
|
2016-01-23 01:22:08 -08:00
|
|
|
{
|
|
|
|
if(numsamples >= 2)
|
|
|
|
{
|
|
|
|
filter->x[1] = src[numsamples-2];
|
|
|
|
filter->x[0] = src[numsamples-1];
|
|
|
|
filter->y[1] = src[numsamples-2];
|
|
|
|
filter->y[0] = src[numsamples-1];
|
|
|
|
}
|
|
|
|
else if(numsamples == 1)
|
|
|
|
{
|
|
|
|
filter->x[1] = filter->x[0];
|
|
|
|
filter->x[0] = src[0];
|
|
|
|
filter->y[1] = filter->y[0];
|
|
|
|
filter->y[0] = src[0];
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 22:34:46 -07:00
|
|
|
|
2013-05-27 15:32:02 -07:00
|
|
|
|
2018-01-13 08:07:03 -08:00
|
|
|
struct ALfilter;
|
|
|
|
|
|
|
|
typedef struct ALfilterVtable {
|
|
|
|
void (*const setParami)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALint val);
|
|
|
|
void (*const setParamiv)(struct ALfilter *filter, ALCcontext *context, ALenum param, const ALint *vals);
|
|
|
|
void (*const setParamf)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val);
|
|
|
|
void (*const setParamfv)(struct ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals);
|
|
|
|
|
|
|
|
void (*const getParami)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALint *val);
|
|
|
|
void (*const getParamiv)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALint *vals);
|
|
|
|
void (*const getParamf)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val);
|
|
|
|
void (*const getParamfv)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals);
|
|
|
|
} ALfilterVtable;
|
|
|
|
|
|
|
|
#define DEFINE_ALFILTER_VTABLE(T) \
|
|
|
|
const struct ALfilterVtable T##_vtable = { \
|
|
|
|
T##_setParami, T##_setParamiv, \
|
|
|
|
T##_setParamf, T##_setParamfv, \
|
|
|
|
T##_getParami, T##_getParamiv, \
|
|
|
|
T##_getParamf, T##_getParamfv, \
|
|
|
|
}
|
|
|
|
|
2011-09-11 07:34:03 -07:00
|
|
|
typedef struct ALfilter {
|
2007-12-17 15:43:35 -08:00
|
|
|
// Filter type (AL_FILTER_NULL, ...)
|
|
|
|
ALenum type;
|
|
|
|
|
2007-12-17 22:42:38 -08:00
|
|
|
ALfloat Gain;
|
|
|
|
ALfloat GainHF;
|
2014-05-14 01:24:18 -07:00
|
|
|
ALfloat HFReference;
|
2014-05-17 07:29:50 -07:00
|
|
|
ALfloat GainLF;
|
|
|
|
ALfloat LFReference;
|
2007-12-17 22:42:38 -08:00
|
|
|
|
2018-03-05 11:29:03 -08:00
|
|
|
const struct ALfilterVtable *vtab;
|
2011-09-11 07:34:03 -07:00
|
|
|
|
2012-04-19 22:28:01 -07:00
|
|
|
/* Self ID */
|
|
|
|
ALuint id;
|
2007-12-17 15:43:35 -08:00
|
|
|
} ALfilter;
|
2018-03-05 11:29:03 -08:00
|
|
|
#define ALfilter_setParami(o, c, p, v) ((o)->vtab->setParami(o, c, p, v))
|
|
|
|
#define ALfilter_setParamf(o, c, p, v) ((o)->vtab->setParamf(o, c, p, v))
|
|
|
|
#define ALfilter_setParamiv(o, c, p, v) ((o)->vtab->setParamiv(o, c, p, v))
|
|
|
|
#define ALfilter_setParamfv(o, c, p, v) ((o)->vtab->setParamfv(o, c, p, v))
|
|
|
|
#define ALfilter_getParami(o, c, p, v) ((o)->vtab->getParami(o, c, p, v))
|
|
|
|
#define ALfilter_getParamf(o, c, p, v) ((o)->vtab->getParamf(o, c, p, v))
|
|
|
|
#define ALfilter_getParamiv(o, c, p, v) ((o)->vtab->getParamiv(o, c, p, v))
|
|
|
|
#define ALfilter_getParamfv(o, c, p, v) ((o)->vtab->getParamfv(o, c, p, v))
|
2007-12-17 15:43:35 -08:00
|
|
|
|
2018-01-27 19:40:47 -08:00
|
|
|
void ReleaseALFilters(ALCdevice *device);
|
2007-12-17 17:08:44 -08:00
|
|
|
|
2007-12-17 15:43:35 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|