summaryrefslogtreecommitdiffstats
path: root/src/control.c
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2023-06-27 22:37:53 +0300
committergramanas <anastasis.gramm2@gmail.com>2023-06-27 22:37:53 +0300
commit60c82a72fedd719f69c1f1de896aca00784a2881 (patch)
tree3679e0886fc12aa8eeeab60260347ee7a0c5ae0e /src/control.c
parente77d4d42cacd21bf80e4f47cba2fe85f5a5b0991 (diff)
downloadsynth-project-60c82a72fedd719f69c1f1de896aca00784a2881.tar.gz
synth-project-60c82a72fedd719f69c1f1de896aca00784a2881.tar.bz2
synth-project-60c82a72fedd719f69c1f1de896aca00784a2881.zip
Changes
Diffstat (limited to 'src/control.c')
-rw-r--r--src/control.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/control.c b/src/control.c
new file mode 100644
index 0000000..d952f7f
--- /dev/null
+++ b/src/control.c
@@ -0,0 +1,68 @@
+#include "control.h"
+#include <stdio.h>
+
+int
+cc_step(cc_t *cc, int steps)
+{
+ float tmp = cc->mod;
+ cc->mod += steps * cc->step;
+
+ if (cc->value+cc->mod > cc->max) {
+ cc->mod = 0;
+ cc->target = cc->max;
+ } else if (cc->value+cc->mod < cc->min) {
+ cc->mod = 0;
+ cc->target = cc->min;
+ }
+
+ return cc->mod != tmp;
+}
+
+void
+cc_prep(cc_t *cc)
+{
+ if (cc->mod) {
+ cc->target = cc->value + cc->mod;
+ }
+}
+
+void
+cc_fix(cc_t *cc)
+{
+ cc->value = cc->target;
+ cc->mod = 0;
+}
+
+void
+cc_reset(cc_t *cc)
+{
+ cc->target = cc->def;
+}
+
+float
+cc_iget(cc_t *cc, unsigned int i, unsigned int max)
+{
+ /*
+ v/t_s: value/time start
+ v/t_e: value/time end
+ y = m*x + b
+ m = (v_e - v_s) / (t_e - t_s)
+ b = v_s - m * t_s
+ */
+
+ float m = (cc->target - cc->value) / (max - 0);
+
+ float b = cc->value /* + m * 0 */;
+
+ return m * i + b;
+}
+
+const char *
+cc_to_str(cc_t *cc)
+{
+ static char buf[128];
+
+ snprintf(buf, sizeof buf, "[%s:v:%.2f_m:%.2f_t:%.2f]", cc->name, cc->value, cc->mod, cc->target);
+
+ return buf;
+}