diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2020-02-03 14:57:57 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2020-02-03 14:57:57 -0800 |
commit | a88b3a6bd7de2f00457f628e70349fe383919efd (patch) | |
tree | 7021b01300470291d4b2b8cbeb006e02003fe137 /src/libs/sig/ops_clamp.c | |
parent | 54f6812ecb6469f7c3008ee0246a20f6d8cc1c46 (diff) |
libs/sig: add sig_ops_clamp
Supply three sig_t *'s: value, min, max
Diffstat (limited to 'src/libs/sig/ops_clamp.c')
-rw-r--r-- | src/libs/sig/ops_clamp.c | 67 |
1 files changed, 67 insertions, 0 deletions
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 <assert.h> + +#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, +}; |