summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-02-03 14:28:51 -0800
committerVito Caputo <vcaputo@pengaru.com>2020-02-03 14:31:20 -0800
commit54f6812ecb6469f7c3008ee0246a20f6d8cc1c46 (patch)
tree1ba96c881d058874ff2f99ea7815b56289ec4f1c /src
parent53ff16348973deef1295e123b20a218eb6b7e339 (diff)
libs/sig: add ops_{abs,ceil,floor,pow,round}
Added a simple test exercising pow and round
Diffstat (limited to 'src')
-rw-r--r--src/libs/sig/Makefile.am2
-rw-r--r--src/libs/sig/ops_abs.c53
-rw-r--r--src/libs/sig/ops_ceil.c53
-rw-r--r--src/libs/sig/ops_floor.c53
-rw-r--r--src/libs/sig/ops_pow.c55
-rw-r--r--src/libs/sig/ops_round.c53
-rw-r--r--src/libs/sig/sig.c13
-rw-r--r--src/libs/sig/sig.h5
8 files changed, 286 insertions, 1 deletions
diff --git a/src/libs/sig/Makefile.am b/src/libs/sig/Makefile.am
index 55032b4..de96772 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_const.c ops_lerp.c ops_mult.c ops_rand.c ops_sin.c sig.c sig.h
+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_CPPFLAGS = -I@top_srcdir@/src
diff --git a/src/libs/sig/ops_abs.c b/src/libs/sig/ops_abs.c
new file mode 100644
index 0000000..4adc255
--- /dev/null
+++ b/src/libs/sig/ops_abs.c
@@ -0,0 +1,53 @@
+#include <assert.h>
+#include <math.h>
+
+#include "sig.h"
+
+
+typedef struct ops_abs_ctxt_t {
+ sig_t *x;
+} ops_abs_ctxt_t;
+
+
+static size_t ops_abs_size(va_list ap)
+{
+ return sizeof(ops_abs_ctxt_t);
+}
+
+
+static void ops_abs_init(void *context, va_list ap)
+{
+ ops_abs_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ ctxt->x = va_arg(ap, sig_t *);
+}
+
+
+static void ops_abs_destroy(void *context)
+{
+ ops_abs_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ sig_free(ctxt->x);
+}
+
+
+static float ops_abs_output(void *context, unsigned ticks_ms)
+{
+ ops_abs_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ return fabsf(sig_output(ctxt->x, ticks_ms));
+}
+
+
+sig_ops_t sig_ops_abs = {
+ .size = ops_abs_size,
+ .init = ops_abs_init,
+ .destroy = ops_abs_destroy,
+ .output = ops_abs_output,
+};
diff --git a/src/libs/sig/ops_ceil.c b/src/libs/sig/ops_ceil.c
new file mode 100644
index 0000000..0944ff1
--- /dev/null
+++ b/src/libs/sig/ops_ceil.c
@@ -0,0 +1,53 @@
+#include <assert.h>
+#include <math.h>
+
+#include "sig.h"
+
+
+typedef struct ops_ceil_ctxt_t {
+ sig_t *x;
+} ops_ceil_ctxt_t;
+
+
+static size_t ops_ceil_size(va_list ap)
+{
+ return sizeof(ops_ceil_ctxt_t);
+}
+
+
+static void ops_ceil_init(void *context, va_list ap)
+{
+ ops_ceil_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ ctxt->x = va_arg(ap, sig_t *);
+}
+
+
+static void ops_ceil_destroy(void *context)
+{
+ ops_ceil_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ sig_free(ctxt->x);
+}
+
+
+static float ops_ceil_output(void *context, unsigned ticks_ms)
+{
+ ops_ceil_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ return ceilf(sig_output(ctxt->x, ticks_ms));
+}
+
+
+sig_ops_t sig_ops_ceil = {
+ .size = ops_ceil_size,
+ .init = ops_ceil_init,
+ .destroy = ops_ceil_destroy,
+ .output = ops_ceil_output,
+};
diff --git a/src/libs/sig/ops_floor.c b/src/libs/sig/ops_floor.c
new file mode 100644
index 0000000..85470c9
--- /dev/null
+++ b/src/libs/sig/ops_floor.c
@@ -0,0 +1,53 @@
+#include <assert.h>
+#include <math.h>
+
+#include "sig.h"
+
+
+typedef struct ops_floor_ctxt_t {
+ sig_t *x;
+} ops_floor_ctxt_t;
+
+
+static size_t ops_floor_size(va_list ap)
+{
+ return sizeof(ops_floor_ctxt_t);
+}
+
+
+static void ops_floor_init(void *context, va_list ap)
+{
+ ops_floor_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ ctxt->x = va_arg(ap, sig_t *);
+}
+
+
+static void ops_floor_destroy(void *context)
+{
+ ops_floor_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ sig_free(ctxt->x);
+}
+
+
+static float ops_floor_output(void *context, unsigned ticks_ms)
+{
+ ops_floor_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ return floorf(sig_output(ctxt->x, ticks_ms));
+}
+
+
+sig_ops_t sig_ops_floor = {
+ .size = ops_floor_size,
+ .init = ops_floor_init,
+ .destroy = ops_floor_destroy,
+ .output = ops_floor_output,
+};
diff --git a/src/libs/sig/ops_pow.c b/src/libs/sig/ops_pow.c
new file mode 100644
index 0000000..6e6176a
--- /dev/null
+++ b/src/libs/sig/ops_pow.c
@@ -0,0 +1,55 @@
+#include <assert.h>
+#include <math.h>
+
+#include "sig.h"
+
+
+typedef struct ops_pow_ctxt_t {
+ sig_t *x, *y;
+} ops_pow_ctxt_t;
+
+
+static size_t ops_pow_size(va_list ap)
+{
+ return sizeof(ops_pow_ctxt_t);
+}
+
+
+static void ops_pow_init(void *context, va_list ap)
+{
+ ops_pow_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ ctxt->x = va_arg(ap, sig_t *);
+ ctxt->y = va_arg(ap, sig_t *);
+}
+
+
+static void ops_pow_destroy(void *context)
+{
+ ops_pow_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ sig_free(ctxt->x);
+ sig_free(ctxt->y);
+}
+
+
+static float ops_pow_output(void *context, unsigned ticks_ms)
+{
+ ops_pow_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ return powf(sig_output(ctxt->x, ticks_ms), sig_output(ctxt->y, ticks_ms));
+}
+
+
+sig_ops_t sig_ops_pow = {
+ .size = ops_pow_size,
+ .init = ops_pow_init,
+ .destroy = ops_pow_destroy,
+ .output = ops_pow_output,
+};
diff --git a/src/libs/sig/ops_round.c b/src/libs/sig/ops_round.c
new file mode 100644
index 0000000..ddf8d66
--- /dev/null
+++ b/src/libs/sig/ops_round.c
@@ -0,0 +1,53 @@
+#include <assert.h>
+#include <math.h>
+
+#include "sig.h"
+
+
+typedef struct ops_round_ctxt_t {
+ sig_t *x;
+} ops_round_ctxt_t;
+
+
+static size_t ops_round_size(va_list ap)
+{
+ return sizeof(ops_round_ctxt_t);
+}
+
+
+static void ops_round_init(void *context, va_list ap)
+{
+ ops_round_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ ctxt->x = va_arg(ap, sig_t *);
+}
+
+
+static void ops_round_destroy(void *context)
+{
+ ops_round_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ sig_free(ctxt->x);
+}
+
+
+static float ops_round_output(void *context, unsigned ticks_ms)
+{
+ ops_round_ctxt_t *ctxt = context;
+
+ assert(ctxt);
+
+ return roundf(sig_output(ctxt->x, ticks_ms));
+}
+
+
+sig_ops_t sig_ops_round = {
+ .size = ops_round_size,
+ .init = ops_round_init,
+ .destroy = ops_round_destroy,
+ .output = ops_round_output,
+};
diff --git a/src/libs/sig/sig.c b/src/libs/sig/sig.c
index 865c6b4..4146cd3 100644
--- a/src/libs/sig/sig.c
+++ b/src/libs/sig/sig.c
@@ -119,6 +119,19 @@ int main(int argc, char *argv[])
for (unsigned i = 0; i < 1000; i++)
printf("sin 100hz * 1hz output %i=%f\n", i, sig_output(sig, i));
sig = sig_free(sig);
+
+ sig = sig_new(&sig_ops_pow, /* raise an ... */
+ sig_new(&sig_ops_sin, /* oscillator ... */
+ sig_new(&sig_ops_const, 10.f)), /* @ 10hz, */
+ sig_new(&sig_ops_round, /* to a rounded .. */
+ sig_new(&sig_ops_mult, sig_new(&sig_ops_const, 50.f), /* 50 X ... */
+ sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 1.f)) /* 1hz oscillator */
+ )
+ )
+ );
+ 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);
}
#endif
diff --git a/src/libs/sig/sig.h b/src/libs/sig/sig.h
index 32bf446..1de4854 100644
--- a/src/libs/sig/sig.h
+++ b/src/libs/sig/sig.h
@@ -27,7 +27,12 @@ extern sig_ops_t sig_ops_saw;
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_floor;
extern sig_ops_t sig_ops_lerp;
extern sig_ops_t sig_ops_mult;
+extern sig_ops_t sig_ops_pow;
+extern sig_ops_t sig_ops_round;
#endif
© All Rights Reserved