summaryrefslogtreecommitdiff
path: root/src/libs
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-02-03 14:57:57 -0800
committerVito Caputo <vcaputo@pengaru.com>2020-02-03 14:57:57 -0800
commita88b3a6bd7de2f00457f628e70349fe383919efd (patch)
tree7021b01300470291d4b2b8cbeb006e02003fe137 /src/libs
parent54f6812ecb6469f7c3008ee0246a20f6d8cc1c46 (diff)
libs/sig: add sig_ops_clamp
Supply three sig_t *'s: value, min, max
Diffstat (limited to 'src/libs')
-rw-r--r--src/libs/sig/Makefile.am2
-rw-r--r--src/libs/sig/ops_clamp.c67
-rw-r--r--src/libs/sig/sig.h1
3 files changed, 69 insertions, 1 deletions
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 <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,
+};
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;
© All Rights Reserved