refactor, simple design program for calculating coeffs

This commit is contained in:
Connor Olding 2013-06-17 01:52:48 -07:00
parent 1b26a973af
commit f2ed7720fe
5 changed files with 63 additions and 22 deletions

View file

@ -1,15 +1,18 @@
DISTNAME = crap DISTNAME = crap
VERSION = git VERSION = git
EXE = design
SHOBJ = crap_eq.so SHOBJ = crap_eq.so
MID = crap_util.o MID = crap_util.o
OBJ = ${SHOBJ:.so=.o} ${MID}
SRC = ${OBJ:.o=.c}
HEADERS = crap_util.h ladspa.h 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 PLACEBO_FLAGS = -fomit-frame-pointer -fstrength-reduce -funroll-loops -ffast-math
ALL_CFLAGS = -O3 ${PLACEBO_FLAGS} -std=c99 -fPIC -Wall ${CFLAGS} 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 PREFIX ?= /usr/local
EXEC_PREFIX ?= ${PREFIX} EXEC_PREFIX ?= ${PREFIX}
@ -19,7 +22,7 @@ LADSPADIR ?= ${LIBDIR}/ladspa
# should CFLAGS really be used in linking too? seems odd # should CFLAGS really be used in linking too? seems odd
FULLNAME = ${DISTNAME}-${VERSION} FULLNAME = ${DISTNAME}-${VERSION}
ALL = ${OBJ} ${SHOBJ} ALL = ${OBJ} ${SHOBJ} ${EXE}
LADSPADEST = ${DESTDIR}${LADSPADIR} LADSPADEST = ${DESTDIR}${LADSPADIR}
all: options ${ALL} all: options ${ALL}
@ -29,9 +32,14 @@ options:
@echo "ALL_CFLAGS = ${ALL_CFLAGS}" @echo "ALL_CFLAGS = ${ALL_CFLAGS}"
@echo "CPPFLAGS = ${CPPFLAGS}" @echo "CPPFLAGS = ${CPPFLAGS}"
@echo "ALL_LDFLAGS = ${ALL_LDFLAGS}" @echo "ALL_LDFLAGS = ${ALL_LDFLAGS}"
@echo "SHARED_LDFLAGS = ${SHARED_LDFLAGS}"
@echo "CC = ${CC}" @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 $@ @echo LD $< ${MID} -o $@
@${CC} ${ALL_LDFLAGS} $< ${MID} -o $@ @${CC} ${ALL_LDFLAGS} $< ${MID} -o $@

View file

@ -111,7 +111,7 @@ instantiate_eq(const LADSPA_Descriptor *descriptor, ulong s_rate) {
eq->old_chg[i] = g; eq->old_chg[i] = g;
eq->old_chf[i] = f; eq->old_chf[i] = f;
eq->old_chb[i] = b; 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; 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_chg[i] = g;
eq->old_chf[i] = f; eq->old_chf[i] = f;
eq->old_chb[i] = b; eq->old_chb[i] = b;
biquad_gen(&filters[i], 1, f, g, b, fs); filters[i] = biquad_gen(1, f, g, b, fs);
} }
} }

View file

@ -30,9 +30,9 @@ peaking(double cw, double Gf, double g) {
biquad_interim biquad_interim
highpass(double cw, double g) { highpass(double cw, double g) {
return (biquad_interim) { return (biquad_interim) {
.b0 = 1 + cw*0.5, .b0 = (1 + cw)*0.5,
.b1 = -cw - 1, .b1 = -cw - 1,
.b2 = 1 + cw*0.5, .b2 = (1 + cw)*0.5,
.a0 = 1 + g, .a0 = 1 + g,
.a1 = -2*cw, .a1 = -2*cw,
.a2 = 1 - g .a2 = 1 - g
@ -92,8 +92,8 @@ orfanidi(double w0, double Gf, double g) {
}; };
} }
void biquad
biquad_gen(biquad *bq, 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 */ /* TODO: use enum for type instead of just int */
double w0, cw, sw, Gf, Q, a_peak, a_pass; double w0, cw, sw, Gf, Q, a_peak, a_pass;
w0 = ANGULAR_LIM(fc, fs); 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); if (type == 2) bqi = highpass(cw, a_pass);
double a0r = 1/bqi.a0; double a0r = 1/bqi.a0;
bq->b0 = a0r * bqi.b0;
bq->b1 = a0r * bqi.b1; return (biquad) {
bq->b2 = a0r * bqi.b2; .b0 = a0r * bqi.b0,
bq->a1 = -a0r * bqi.a1; .b1 = a0r * bqi.b1,
bq->a2 = -a0r * bqi.a2; .b2 = a0r * bqi.b2,
.a1 = -a0r * bqi.a1,
.a2 = -a0r * bqi.a2
};
} }
bq_t bq_t

View file

@ -23,6 +23,8 @@
/* http://musicdsp.org/showone.php?id=51 */ /* http://musicdsp.org/showone.php?id=51 */
/* http://musicdsp.org/files/denormal.pdf */ /* 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 #ifdef BIQUAD_DOUBLE
typedef double bq_t; typedef double bq_t;
#define IS_DENORMAL(f) (((*(uint64_t *)&f)&0x7FF0000000000000)==0) #define IS_DENORMAL(f) (((*(uint64_t *)&f)&0x7FF0000000000000)==0)
@ -51,8 +53,8 @@ highpass(double cw, double g);
biquad_interim biquad_interim
orfanidi(double w0, double Gf, double g); orfanidi(double w0, double Gf, double g);
void biquad
biquad_gen(biquad *bq, int type, double fc, double gain, double bw, double fs); biquad_gen(int type, double fc, double gain, double bw, double fs);
bq_t bq_t
biquad_run(biquad *bq, bq_t x); biquad_run(biquad *bq, bq_t x);

28
design.c Normal file
View file

@ -0,0 +1,28 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <errno.h>
#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;
}