diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2020-02-03 15:59:56 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2020-02-03 16:04:18 -0800 |
commit | 59b276edf6cd3d53fab1d45819a34ef1627e3389 (patch) | |
tree | 4beb238d5fd9a10298ed99c393709c87df1cc723 /src/libs/sig/ops_scale.c | |
parent | 1c1ae5ee7243e731c7b44a11400e19bb8dab047b (diff) |
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.
Diffstat (limited to 'src/libs/sig/ops_scale.c')
-rw-r--r-- | src/libs/sig/ops_scale.c | 67 |
1 files changed, 67 insertions, 0 deletions
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 <assert.h> + +#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, +}; |