From ba74a824658f7f59add288d28006ff48bf46b963 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 3 Feb 2020 00:29:09 -0800 Subject: libs/sig: introduce a signal generator abstraction This adds a small framework of sorts for creating and composing signal generators. Two generators are implemented at this time; sig_ops_sin and sig_ops_mult sig_ops_sin accepts a hz variable and will produce a sine wave of that frequency. sig_ops_mult accepts two sig_t generators and multiplies their outputs Callers may construct their own sig_ops_t ops structs and supply them to sig_new(), but it's expected that libs/sig will grow a collection of commonly used generators which can then be used by simply passing their sig_ops_$foo to sig_new(). See the test code at the bottom of libs/sig/sig.c for some contrived sample usage. Note by composing multiple sig_ops_sin generators with a sig_ops_mult generator, one can already easily construct a synth-like LFO generator. Some obvious todos are to add triangle/sawtooth/square wave generators. More compositional generators may be interesting as well, like additive and subtractive for example. Those will need to implement clipping, as it's expected that the generators *always* stay within unity (0-1). No modules use this yet, but I expect to wire this up to rtv for driving knobs. --- src/libs/sig/sig.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/libs/sig/sig.c (limited to 'src/libs/sig/sig.c') diff --git a/src/libs/sig/sig.c b/src/libs/sig/sig.c new file mode 100644 index 0000000..e3d5a7c --- /dev/null +++ b/src/libs/sig/sig.c @@ -0,0 +1,100 @@ +#include +#include +#include + +#include "sig.h" + + +typedef struct sig_t { + const sig_ops_t *ops; + void *ctxt[]; +} sig_t; + + +/* return a new signal generator of ops type, configured according to va_list */ +sig_t * sig_new(const sig_ops_t *ops, ...) +{ + static const sig_ops_t null_ops; + size_t ctxt_size = 0; + sig_t *sig; + va_list ap; + + if (!ops) + ops = &null_ops; + + va_start(ap, ops); + if (ops->size) + ctxt_size = ops->size(ap); + va_end(ap); + + sig = calloc(1, sizeof(sig_t) + ctxt_size); + if (!sig) + return NULL; + + va_start(ap, ops); + if (ops->init) + ops->init(&sig->ctxt, ap); + va_end(ap); + + sig->ops = ops; + + return sig; +} + + +/* free a signal generator, always returns NULL */ +sig_t * sig_free(sig_t *sig) +{ + if (sig) { + if (sig->ops->destroy) + sig->ops->destroy(&sig->ctxt); + + free(sig); + } + + return NULL; +} + + +/* produce the value for time ticks_ms from the supplied signal generator, + * the returned value should always be kept in the range 0-1. + */ +float sig_output(sig_t *sig, unsigned ticks_ms) +{ + assert(sig); + assert(sig->ops); + + if (sig->ops->output) + return sig->ops->output(sig->ctxt, ticks_ms); + + return 0; +} + + +#ifdef TESTING + +#include + +int main(int argc, char *argv[]) +{ + sig_t *sig; + + sig = sig_new(NULL); + printf("null output=%f\n", sig_output(sig, 0)); + sig = sig_free(sig); + + sig = sig_new(&sig_ops_sin, 2.f); + for (unsigned i = 0; i < 1000; i++) + printf("sin 2hz output %i=%f\n", i, sig_output(sig, i)); + sig = sig_free(sig); + + sig = sig_new(&sig_ops_mult, + sig_new(&sig_ops_sin, 1.f), /* LFO @ 1hz */ + sig_new(&sig_ops_sin, 100.f) /* oscillator @ 100hz */ + ); + for (unsigned i = 0; i < 1000; i++) + printf("sin 100hz * 1hz output %i=%f\n", i, sig_output(sig, i)); + sig = sig_free(sig); +} + +#endif -- cgit v1.2.3