diff --git a/Makefile b/Makefile index 9453213..f71accc 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ VERSION = git EXE = design SHOBJ = crap_eq.so crap_eq_const.so -MID = crap_util.o +MID = HEADERS = crap_util.h ladspa.h OBJ = ${SHOBJ:.so=.o} ${EXE:=.o} ${MID} diff --git a/crap_util.h b/crap_util.h index 9996589..8dc1c26 100644 --- a/crap_util.h +++ b/crap_util.h @@ -1,36 +1,18 @@ -#ifndef M_PI -#define M_PI 3.14159265358979323846264338327 -#endif +#include "math.h" -#ifndef M_LN2 -#define M_LN2 0.69314718055994530942 -#endif - -/* ln(2)/2 */ -#define LN_2_2 0.3465735902799726547086 - -#define SQR(x) ((x)*(x)) -#define DB2LIN(x) ((x) > -90 ? pow(10, (x) * 0.05) : 0) - -/* branchless, supposedly kills denormals when l=1 u=1 */ -#define CLIP(v,l,u) (fabs(v-l)+(l+u)-fabs(v-u))*0.5; -/* branches, but smaller and generic with no side effects */ #define LIMIT(v,l,u) ((v)<(l)?(l):((v)>(u)?(u):(v))) +#define DB2LIN(x) ((x) > -90 ? pow(10, (x) * 0.05) : 0) /* frequency to rads/sec (angular frequency) */ #define ANGULAR(fc, fs) (2 * M_PI / (fs) * (fc)) #define ANGULAR_LIM(fc, fs) (2 * M_PI / (fs) * LIMIT((fc), 1, (fs)/2)) -/* http://musicdsp.org/showone.php?id=51 */ -/* http://musicdsp.org/files/denormal.pdf */ /* this is pretty gross; * it's too easy to define BIQUAD_DOUBLE in one file and not another */ #ifdef BIQUAD_DOUBLE typedef double bq_t; -#define IS_DENORMAL(f) (((*(uint64_t *)&f)&0x7FF0000000000000)==0) #else typedef float bq_t; -#define IS_DENORMAL(f) (((*(uint32_t *)&f)&0x7F800000)==0) #endif typedef struct { @@ -41,7 +23,7 @@ typedef struct { double b0, b1, b2, a0, a1, a2; } biquad_interim; -void +static void biquad_init(biquad *bq); /* types: TODO: enum @@ -56,15 +38,16 @@ biquad_init(biquad *bq); 8: notch 9: gain */ -biquad +static biquad biquad_gen(int type, double fc, double gain, double bw, double fs); /* s-plane to z-plane */ -biquad_interim +static biquad_interim design(double cw, double sw, double num0, double num1, double num2, double den0, double den1, double den2); -bq_t +static bq_t biquad_run(biquad *bq, bq_t x); +#include "crap_util_def.h" diff --git a/crap_util.c b/crap_util_def.h similarity index 90% rename from crap_util.c rename to crap_util_def.h index 2c2fd6a..78da2e8 100644 --- a/crap_util.c +++ b/crap_util_def.h @@ -2,19 +2,14 @@ #include #include -#include "crap_util.h" - /* used to resemble https://github.com/swh/ladspa/blob/master/util/biquad.h */ -void +static void biquad_init(biquad *bq) { - bq->x1 = 0; - bq->x2 = 0; - bq->y1 = 0; - bq->y2 = 0; + bq->x1 = bq->x2 = bq->y1 = bq->y2 = 0; } -biquad_interim +static biquad_interim design(double cw, double sw, double num0, double num1, double num2, double den0, double den1, double den2) { @@ -28,19 +23,19 @@ design(double cw, double sw, }; } -biquad +static biquad biquad_gen(int type, double fc, double gain, double bw, double fs) { - /* TODO: use enum for type instead of just int */ double w0, cw, sw, A, As, Q; w0 = ANGULAR_LIM(fc, fs); cw = cos(w0); sw = sin(w0); A = DB2LIN(gain/2); As = sqrt(A); - Q = 1/(4 * sinh(LN_2_2 * bw * w0/sw)); - //Q = bw? - (w0/M_PI)^2; + Q = 1/(4*sinh(M_LN2/2*bw*w0/sw)); + //Q = M_SQRT1_2*(1 - SQR(w0/M_PI))/bw; /* skip = (fabs(A - 1) <= TINY); */ + /* TODO: use enum for type instead of just int */ biquad_interim bqi; if (type == 0) bqi = design(cw,sw, 1, A/Q, 1, 1, 1/A/Q, 1); if (type == 1) bqi = design(cw,sw, 1, As/Q, A, 1, 1/As/Q, 1/A); @@ -64,13 +59,12 @@ biquad_gen(int type, double fc, double gain, double bw, double fs) { }; } -bq_t +static bq_t biquad_run(biquad *bq, bq_t x) { bq_t y; y = bq->b0 * x + bq->b1 * bq->x1 + bq->b2 * bq->x2 + bq->a1 * bq->y1 + bq->a2 * bq->y2; - if (IS_DENORMAL(y)) y = 0; bq->x2 = bq->x1; bq->x1 = x; bq->y2 = bq->y1; @@ -78,4 +72,3 @@ biquad_run(biquad *bq, bq_t x) { return y; } -