From 59b276edf6cd3d53fab1d45819a34ef1627e3389 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 3 Feb 2020 15:59:56 -0800 Subject: libs/sig: add sig_ops_scale w/test example This assumes the input value is always 0-1, but may be used to produce values in any signed range, mapped linearly to the input. --- src/libs/sig/Makefile.am | 2 +- src/libs/sig/ops_scale.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ src/libs/sig/sig.c | 13 ++++++++++ src/libs/sig/sig.h | 1 + 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/libs/sig/ops_scale.c (limited to 'src/libs/sig') diff --git a/src/libs/sig/Makefile.am b/src/libs/sig/Makefile.am index 64715fe..e53a4bd 100644 --- a/src/libs/sig/Makefile.am +++ b/src/libs/sig/Makefile.am @@ -1,3 +1,3 @@ noinst_LIBRARIES = libsig.a -libsig_a_SOURCES = ops_abs.c ops_ceil.c ops_clamp.c ops_const.c ops_floor.c ops_inv.c ops_lerp.c ops_max.c ops_min.c ops_mult.c ops_pow.c ops_rand.c ops_round.c ops_sin.c sig.c sig.h +libsig_a_SOURCES = ops_abs.c ops_ceil.c ops_clamp.c ops_const.c ops_floor.c ops_inv.c ops_lerp.c ops_max.c ops_min.c ops_mult.c ops_pow.c ops_rand.c ops_round.c ops_scale.c ops_sin.c sig.c sig.h libsig_a_CPPFLAGS = -I@top_srcdir@/src diff --git a/src/libs/sig/ops_scale.c b/src/libs/sig/ops_scale.c new file mode 100644 index 0000000..22c0363 --- /dev/null +++ b/src/libs/sig/ops_scale.c @@ -0,0 +1,67 @@ +#include + +#include "sig.h" + + +typedef struct ops_scale_ctxt_t { + sig_t *value, *min, *max; +} ops_scale_ctxt_t; + + +static size_t ops_scale_size(va_list ap) +{ + return sizeof(ops_scale_ctxt_t); +} + + +/* expects three sig_t's: value, min, max. + * min is assumed to be always <= max, + * and value is assumed to always be 0-1 + */ +static void ops_scale_init(void *context, va_list ap) +{ + ops_scale_ctxt_t *ctxt = context; + + assert(ctxt); + + ctxt->value = va_arg(ap, sig_t *); + ctxt->min = va_arg(ap, sig_t *); + ctxt->max = va_arg(ap, sig_t *); +} + + +static void ops_scale_destroy(void *context) +{ + ops_scale_ctxt_t *ctxt = context; + + assert(ctxt); + + sig_free(ctxt->value); + sig_free(ctxt->min); + sig_free(ctxt->max); +} + + +static float ops_scale_output(void *context, unsigned ticks_ms) +{ + ops_scale_ctxt_t *ctxt = context; + float value, min, max; + + assert(ctxt); + + value = sig_output(ctxt->value, ticks_ms); + min = sig_output(ctxt->min, ticks_ms); + max = sig_output(ctxt->max, ticks_ms); + + assert(max >= min); + + return value * (max - min) + min; +} + + +sig_ops_t sig_ops_scale = { + .size = ops_scale_size, + .init = ops_scale_init, + .destroy = ops_scale_destroy, + .output = ops_scale_output, +}; diff --git a/src/libs/sig/sig.c b/src/libs/sig/sig.c index 4146cd3..c68a420 100644 --- a/src/libs/sig/sig.c +++ b/src/libs/sig/sig.c @@ -132,6 +132,19 @@ int main(int argc, char *argv[]) for (unsigned i = 0; i < 1000; i++) printf("sin 10hz ^ (sin 1hz * 50) output %i=%f\n", i, sig_output(sig, i)); sig = sig_free(sig); + + sig = sig_new(&sig_ops_scale, /* scale a */ + sig_new(&sig_ops_lerp, /* linear interpolation */ + sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 10.f)), /* between one 10hz oscillator */ + sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 33.f)), /* and another 33hz oscillator */ + sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 2.f)) /* weighted by a 2hz oscillator */ + ), + sig_new(&sig_ops_const, -100.f), sig_new(&sig_ops_const, 100.f) /* to the range -100 .. +100 */ + ); + for (unsigned i = 0; i < 1000; i++) + printf("scale(lerp(sin(10hz), sin(33hz), sin(2hz)), -100, +100) output %i=%f\n", i, sig_output(sig, i)); + sig = sig_free(sig); + } #endif diff --git a/src/libs/sig/sig.h b/src/libs/sig/sig.h index 3456af9..b54bfb7 100644 --- a/src/libs/sig/sig.h +++ b/src/libs/sig/sig.h @@ -38,5 +38,6 @@ extern sig_ops_t sig_ops_min; extern sig_ops_t sig_ops_mult; extern sig_ops_t sig_ops_pow; extern sig_ops_t sig_ops_round; +extern sig_ops_t sig_ops_scale; #endif -- cgit v1.2.3