don't compile crap_util separately (inline)

This commit is contained in:
Connor Olding 2013-11-10 13:17:39 -08:00
parent 8661e90a91
commit d227226200
3 changed files with 16 additions and 40 deletions

View file

@ -3,7 +3,7 @@ VERSION = git
EXE = design EXE = design
SHOBJ = crap_eq.so crap_eq_const.so SHOBJ = crap_eq.so crap_eq_const.so
MID = crap_util.o MID =
HEADERS = crap_util.h ladspa.h HEADERS = crap_util.h ladspa.h
OBJ = ${SHOBJ:.so=.o} ${EXE:=.o} ${MID} OBJ = ${SHOBJ:.so=.o} ${EXE:=.o} ${MID}

View file

@ -1,36 +1,18 @@
#ifndef M_PI #include "math.h"
#define M_PI 3.14159265358979323846264338327
#endif
#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 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) */ /* frequency to rads/sec (angular frequency) */
#define ANGULAR(fc, fs) (2 * M_PI / (fs) * (fc)) #define ANGULAR(fc, fs) (2 * M_PI / (fs) * (fc))
#define ANGULAR_LIM(fc, fs) (2 * M_PI / (fs) * LIMIT((fc), 1, (fs)/2)) #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; /* this is pretty gross;
* it's too easy to define BIQUAD_DOUBLE in one file and not another */ * it's too easy to define BIQUAD_DOUBLE in one file and not another */
#ifdef BIQUAD_DOUBLE #ifdef BIQUAD_DOUBLE
typedef double bq_t; typedef double bq_t;
#define IS_DENORMAL(f) (((*(uint64_t *)&f)&0x7FF0000000000000)==0)
#else #else
typedef float bq_t; typedef float bq_t;
#define IS_DENORMAL(f) (((*(uint32_t *)&f)&0x7F800000)==0)
#endif #endif
typedef struct { typedef struct {
@ -41,7 +23,7 @@ typedef struct {
double b0, b1, b2, a0, a1, a2; double b0, b1, b2, a0, a1, a2;
} biquad_interim; } biquad_interim;
void static void
biquad_init(biquad *bq); biquad_init(biquad *bq);
/* types: TODO: enum /* types: TODO: enum
@ -56,15 +38,16 @@ biquad_init(biquad *bq);
8: notch 8: notch
9: gain 9: gain
*/ */
biquad static biquad
biquad_gen(int type, double fc, double gain, double bw, double fs); biquad_gen(int type, double fc, double gain, double bw, double fs);
/* s-plane to z-plane */ /* s-plane to z-plane */
biquad_interim static biquad_interim
design(double cw, double sw, design(double cw, double sw,
double num0, double num1, double num2, double num0, double num1, double num2,
double den0, double den1, double den2); double den0, double den1, double den2);
bq_t static bq_t
biquad_run(biquad *bq, bq_t x); biquad_run(biquad *bq, bq_t x);
#include "crap_util_def.h"

View file

@ -2,19 +2,14 @@
#include <math.h> #include <math.h>
#include <stdint.h> #include <stdint.h>
#include "crap_util.h"
/* used to resemble https://github.com/swh/ladspa/blob/master/util/biquad.h */ /* used to resemble https://github.com/swh/ladspa/blob/master/util/biquad.h */
void static void
biquad_init(biquad *bq) { biquad_init(biquad *bq) {
bq->x1 = 0; bq->x1 = bq->x2 = bq->y1 = bq->y2 = 0;
bq->x2 = 0;
bq->y1 = 0;
bq->y2 = 0;
} }
biquad_interim static biquad_interim
design(double cw, double sw, design(double cw, double sw,
double num0, double num1, double num2, double num0, double num1, double num2,
double den0, double den1, double den2) { 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) { 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; double w0, cw, sw, A, As, Q;
w0 = ANGULAR_LIM(fc, fs); w0 = ANGULAR_LIM(fc, fs);
cw = cos(w0); cw = cos(w0);
sw = sin(w0); sw = sin(w0);
A = DB2LIN(gain/2); A = DB2LIN(gain/2);
As = sqrt(A); As = sqrt(A);
Q = 1/(4 * sinh(LN_2_2 * bw * w0/sw)); Q = 1/(4*sinh(M_LN2/2*bw*w0/sw));
//Q = bw? - (w0/M_PI)^2; //Q = M_SQRT1_2*(1 - SQR(w0/M_PI))/bw;
/* skip = (fabs(A - 1) <= TINY); */ /* skip = (fabs(A - 1) <= TINY); */
/* TODO: use enum for type instead of just int */
biquad_interim bqi; biquad_interim bqi;
if (type == 0) bqi = design(cw,sw, 1, A/Q, 1, 1, 1/A/Q, 1); 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); 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) { biquad_run(biquad *bq, bq_t x) {
bq_t y; bq_t y;
y = bq->b0 * x + bq->b1 * bq->x1 + bq->b2 * bq->x2 y = bq->b0 * x + bq->b1 * bq->x1 + bq->b2 * bq->x2
+ bq->a1 * bq->y1 + bq->a2 * bq->y2; + bq->a1 * bq->y1 + bq->a2 * bq->y2;
if (IS_DENORMAL(y)) y = 0;
bq->x2 = bq->x1; bq->x2 = bq->x1;
bq->x1 = x; bq->x1 = x;
bq->y2 = bq->y1; bq->y2 = bq->y1;
@ -78,4 +72,3 @@ biquad_run(biquad *bq, bq_t x) {
return y; return y;
} }