From a88b3a6bd7de2f00457f628e70349fe383919efd Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 3 Feb 2020 14:57:57 -0800 Subject: libs/sig: add sig_ops_clamp Supply three sig_t *'s: value, min, max --- src/libs/sig/Makefile.am | 2 +- src/libs/sig/ops_clamp.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ src/libs/sig/sig.h | 1 + 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/libs/sig/ops_clamp.c (limited to 'src/libs') diff --git a/src/libs/sig/Makefile.am b/src/libs/sig/Makefile.am index de96772..ce99edc 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_const.c ops_floor.c ops_lerp.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_lerp.c ops_mult.c ops_pow.c ops_rand.c ops_round.c ops_sin.c sig.c sig.h libsig_a_CPPFLAGS = -I@top_srcdir@/src diff --git a/src/libs/sig/ops_clamp.c b/src/libs/sig/ops_clamp.c new file mode 100644 index 0000000..0afcf0c --- /dev/null +++ b/src/libs/sig/ops_clamp.c @@ -0,0 +1,67 @@ +#include + +#include "sig.h" + + +typedef struct ops_clamp_ctxt_t { + sig_t *value, *min, *max; +} ops_clamp_ctxt_t; + + +static size_t ops_clamp_size(va_list ap) +{ + return sizeof(ops_clamp_ctxt_t); +} + + +static void ops_clamp_init(void *context, va_list ap) +{ + ops_clamp_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_clamp_destroy(void *context) +{ + ops_clamp_ctxt_t *ctxt = context; + + assert(ctxt); + + sig_free(ctxt->value); + sig_free(ctxt->min); + sig_free(ctxt->max); +} + + +static float ops_clamp_output(void *context, unsigned ticks_ms) +{ + ops_clamp_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); + + if (value < min) + return min; + + if (value > max) + return max; + + return value; +} + + +sig_ops_t sig_ops_clamp = { + .size = ops_clamp_size, + .init = ops_clamp_init, + .destroy = ops_clamp_destroy, + .output = ops_clamp_output, +}; diff --git a/src/libs/sig/sig.h b/src/libs/sig/sig.h index 1de4854..194bdcf 100644 --- a/src/libs/sig/sig.h +++ b/src/libs/sig/sig.h @@ -29,6 +29,7 @@ extern sig_ops_t sig_ops_sqr; extern sig_ops_t sig_ops_abs; extern sig_ops_t sig_ops_ceil; +extern sig_ops_t sig_ops_clamp; extern sig_ops_t sig_ops_floor; extern sig_ops_t sig_ops_lerp; extern sig_ops_t sig_ops_mult; -- cgit v1.2.3