From f2ed7720feb3826128588acbc54357e5443d8d55 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 17 Jun 2013 01:52:48 -0700 Subject: [PATCH] refactor, simple design program for calculating coeffs --- Makefile | 26 +++++++++++++++++--------- crap_eq.c | 4 ++-- crap_util.c | 21 ++++++++++++--------- crap_util.h | 6 ++++-- design.c | 28 ++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 design.c diff --git a/Makefile b/Makefile index dc844a2..642373a 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,18 @@ DISTNAME = crap VERSION = git +EXE = design SHOBJ = crap_eq.so MID = crap_util.o -OBJ = ${SHOBJ:.so=.o} ${MID} -SRC = ${OBJ:.o=.c} HEADERS = crap_util.h ladspa.h +OBJ = ${SHOBJ:.so=.o} ${EXE:=.o} ${MID} +SRC = ${OBJ:.o=.c} + PLACEBO_FLAGS = -fomit-frame-pointer -fstrength-reduce -funroll-loops -ffast-math ALL_CFLAGS = -O3 ${PLACEBO_FLAGS} -std=c99 -fPIC -Wall ${CFLAGS} -ALL_LDFLAGS = -nostartfiles -shared ${LDFLAGS} +ALL_LDFLAGS = -lm ${LDFLAGS} +SHARED_LDFLAGS = -nostartfiles -shared PREFIX ?= /usr/local EXEC_PREFIX ?= ${PREFIX} @@ -19,19 +22,24 @@ LADSPADIR ?= ${LIBDIR}/ladspa # should CFLAGS really be used in linking too? seems odd FULLNAME = ${DISTNAME}-${VERSION} -ALL = ${OBJ} ${SHOBJ} +ALL = ${OBJ} ${SHOBJ} ${EXE} LADSPADEST = ${DESTDIR}${LADSPADIR} all: options ${ALL} .PHONY: options options: - @echo "ALL_CFLAGS = ${ALL_CFLAGS}" - @echo "CPPFLAGS = ${CPPFLAGS}" - @echo "ALL_LDFLAGS = ${ALL_LDFLAGS}" - @echo "CC = ${CC}" + @echo "ALL_CFLAGS = ${ALL_CFLAGS}" + @echo "CPPFLAGS = ${CPPFLAGS}" + @echo "ALL_LDFLAGS = ${ALL_LDFLAGS}" + @echo "SHARED_LDFLAGS = ${SHARED_LDFLAGS}" + @echo "CC = ${CC}" -%.so: %.o +%.so: %.o ${MID} + @echo LD $< ${MID} -o $@ + @${CC} ${ALL_LDFLAGS} ${SHARED_LDFLAGS} $< ${MID} -o $@ + +%: %.o ${MID} @echo LD $< ${MID} -o $@ @${CC} ${ALL_LDFLAGS} $< ${MID} -o $@ diff --git a/crap_eq.c b/crap_eq.c index c44bd9a..93e98d1 100644 --- a/crap_eq.c +++ b/crap_eq.c @@ -111,7 +111,7 @@ instantiate_eq(const LADSPA_Descriptor *descriptor, ulong s_rate) { eq->old_chg[i] = g; eq->old_chf[i] = f; eq->old_chb[i] = b; - biquad_gen(&filters[i], 1, f, g, b, fs); + filters[i] = biquad_gen(1, f, g, b, fs); } return (LADSPA_Handle) eq; @@ -138,7 +138,7 @@ run_eq_for_real(LADSPA_Handle instance, ulong sample_count, int running) { eq->old_chg[i] = g; eq->old_chf[i] = f; eq->old_chb[i] = b; - biquad_gen(&filters[i], 1, f, g, b, fs); + filters[i] = biquad_gen(1, f, g, b, fs); } } diff --git a/crap_util.c b/crap_util.c index a5aeaa8..287e039 100644 --- a/crap_util.c +++ b/crap_util.c @@ -30,9 +30,9 @@ peaking(double cw, double Gf, double g) { biquad_interim highpass(double cw, double g) { return (biquad_interim) { - .b0 = 1 + cw*0.5, + .b0 = (1 + cw)*0.5, .b1 = -cw - 1, - .b2 = 1 + cw*0.5, + .b2 = (1 + cw)*0.5, .a0 = 1 + g, .a1 = -2*cw, .a2 = 1 - g @@ -92,8 +92,8 @@ orfanidi(double w0, double Gf, double g) { }; } -void -biquad_gen(biquad *bq, int type, double fc, double gain, double bw, double fs) { +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, Gf, Q, a_peak, a_pass; w0 = ANGULAR_LIM(fc, fs); @@ -111,11 +111,14 @@ biquad_gen(biquad *bq, int type, double fc, double gain, double bw, double fs) { if (type == 2) bqi = highpass(cw, a_pass); double a0r = 1/bqi.a0; - bq->b0 = a0r * bqi.b0; - bq->b1 = a0r * bqi.b1; - bq->b2 = a0r * bqi.b2; - bq->a1 = -a0r * bqi.a1; - bq->a2 = -a0r * bqi.a2; + + return (biquad) { + .b0 = a0r * bqi.b0, + .b1 = a0r * bqi.b1, + .b2 = a0r * bqi.b2, + .a1 = -a0r * bqi.a1, + .a2 = -a0r * bqi.a2 + }; } bq_t diff --git a/crap_util.h b/crap_util.h index 12df2c2..fe13c2c 100644 --- a/crap_util.h +++ b/crap_util.h @@ -23,6 +23,8 @@ /* 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) @@ -51,8 +53,8 @@ highpass(double cw, double g); biquad_interim orfanidi(double w0, double Gf, double g); -void -biquad_gen(biquad *bq, int type, double fc, double gain, double bw, double fs); +biquad +biquad_gen(int type, double fc, double gain, double bw, double fs); bq_t biquad_run(biquad *bq, bq_t x); diff --git a/design.c b/design.c new file mode 100644 index 0000000..1e89cd4 --- /dev/null +++ b/design.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "crap_util.h" + +void +usage() { + fputs("usage: design SAMPLERATE TYPE CENTERFREQ GAIN BANDWIDTH\n", stderr); + exit(1); +} + +int +main(int argc, char **argv) { + if (argc != 6) usage(); + double tv[5]; + for (int i = 1; i <= 5; i++) { + tv[i - 1]=strtold(argv[i], NULL); + if (errno) { + fprintf(stderr, "arg #%i failed to convert to double\n", i); + exit(1); + } + } + biquad bq = biquad_gen((int) tv[1], tv[2], tv[3], tv[4], tv[0]); + printf("b0 %+.18f\nb1 %+.18f\nb2 %+.18f\na1 %+.18f\na2 %+.18f\n",bq.b0,bq.b1,bq.b2,bq.a1,bq.a2); + return 0; +}