summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-02-03 05:41:11 -0800
committerVito Caputo <vcaputo@pengaru.com>2020-02-03 05:45:49 -0800
commitde64b7014b9dc54fe06b52e14dfc685d72f6ab0b (patch)
tree6f6460fa1b109628b8266c0338484106eba895ce /src
parentfff934678207e71bc873b12916fd3c690b8862cc (diff)
libs/sig: add sig_ops_rand random generator
(Ab)uses rand_r by feeding ticks_ms as seedp for pseudo-random numbers deterministically derived from ticks_ms.
Diffstat (limited to 'src')
-rw-r--r--src/libs/sig/Makefile.am2
-rw-r--r--src/libs/sig/ops_rand.c16
-rw-r--r--src/libs/sig/sig.c6
-rw-r--r--src/libs/sig/sig.h1
4 files changed, 24 insertions, 1 deletions
diff --git a/src/libs/sig/Makefile.am b/src/libs/sig/Makefile.am
index 7c35742..55032b4 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_sin.c sig.c sig.h
+libsig_a_SOURCES = ops_const.c ops_lerp.c ops_mult.c ops_rand.c ops_sin.c sig.c sig.h
libsig_a_CPPFLAGS = -I@top_srcdir@/src
diff --git a/src/libs/sig/ops_rand.c b/src/libs/sig/ops_rand.c
new file mode 100644
index 0000000..49954c6
--- /dev/null
+++ b/src/libs/sig/ops_rand.c
@@ -0,0 +1,16 @@
+#include <assert.h>
+#include <stdlib.h>
+
+#include "sig.h"
+
+
+static float ops_rand_output(void *context, unsigned ticks_ms)
+{
+ return rand_r(&ticks_ms) * 1.f / (float)RAND_MAX;
+}
+
+
+sig_ops_t sig_ops_rand = {
+ .output = ops_rand_output,
+};
+
diff --git a/src/libs/sig/sig.c b/src/libs/sig/sig.c
index cd179e2..fea83b6 100644
--- a/src/libs/sig/sig.c
+++ b/src/libs/sig/sig.c
@@ -83,6 +83,12 @@ int main(int argc, char *argv[])
printf("null output=%f\n", sig_output(sig, 0));
sig = sig_free(sig);
+ sig = sig_new(&sig_ops_rand);
+ for (unsigned j = 0; j < 2; j++) {
+ for (unsigned i = 0; i < 10; i++)
+ printf("rand j=%u i=%u output=%f\n", j, i, sig_output(sig, i));
+ }
+
sig = sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 2.f));
for (unsigned i = 0; i < 1000; i++)
printf("sin 2hz output %i=%f\n", i, sig_output(sig, i));
diff --git a/src/libs/sig/sig.h b/src/libs/sig/sig.h
index 0a374f5..32bf446 100644
--- a/src/libs/sig/sig.h
+++ b/src/libs/sig/sig.h
@@ -18,6 +18,7 @@ sig_t * sig_free(sig_t *sig);
float sig_output(sig_t *sig, unsigned ticks_ms);
extern sig_ops_t sig_ops_const;
+extern sig_ops_t sig_ops_rand;
extern sig_ops_t sig_ops_sin;
/* TODO:
© All Rights Reserved