History

added osc to LIL

Paul Batchelor committed 5 years ago

Commit f8edee71 parent ab390828 files · +206 −1Browse files

Unified · Split

8 files
  1. core.org +36 −1
  2. nodes/config.mk +1 −0
  3. nodes/gensine/gensine.c +7 −0
  4. nodes/loader.c +14 −0
  5. nodes/osc/config.mk +2 −0
  6. nodes/osc/l_osc.c +34 −0
  7. nodes/osc/osc.c +108 −0
  8. nodes/osc/osc.h +4 −0
core.org+36−1
@@ -395,6 +395,37 @@ int sk_param_get(sk_core *core, sk_param *p) · 394 unmodified lines
395395 return 0;
396396 }
397397 #+END_SRC
398+
399+For situations where only constants are allowed, use
400+=sk_param_get_constant=.
401+
402+#+NAME: funcdefs
403+#+BEGIN_SRC c
404+int sk_param_get_constant(sk_core *core, SKFLT *val);
405+#+END_SRC
406+
407+#+NAME: funcs
408+#+BEGIN_SRC c
409+int sk_param_get_constant(sk_core *core, SKFLT *val)
410+{
411+ sk_stack *stk;
412+ sk_stacklet *s;
413+ int rc;
414+
415+ stk = &core->stack;
416+
417+ rc = sk_stack_pop(stk, &s);
418+ SK_ERROR_CHECK(rc);
419+
420+ if (s->type != SK_TYPE_CONSTANT) {
421+ /* Wrong type! */
422+ return 1;
423+ }
424+
425+ *val = s->f;
426+ return 0;
427+}
428+#+END_SRC
398429 ** Setting a Parameter
399430 Set a parameter with =sk_param_set=.
400431
@@ -922,7 +953,11 @@ int sk_core_table_pop(sk_core *core, sk_table **tab) · 521 unmodified lines
922953
923954 SK_ERROR_CHECK(rc);
924955
925− if (s->type != SK_TYPE_TABLE) return 1;
956+ if (s->type != SK_TYPE_TABLE) {
957+ printf("uh oh type is %d, %g\n", s->type, s->f);
958+ printf("%d items on stack\n", core->stack.pos);
959+ return 1;
960+ }
926961
927962 *tab = (sk_table *)s->ptr;
928963
nodes/config.mk+1−0
@@ -6,3 +6,4 @@ include nodes/phasor/config.mk · 5 unmodified lines
66 include nodes/rephasor/config.mk
77 include nodes/tabnew/config.mk
88 include nodes/gensine/config.mk
9+include nodes/osc/config.mk
nodes/gensine/gensine.c+7−0
@@ -21,10 +21,17 @@ static lil_value_t gensine(lil_t lil, size_t argc, lil_value_t *argv) · 20 unmodified lines
2121
2222 if (rc) {
2323 /* TODO: error handling */
24+ printf("uh oh\n");
2425 }
2526
2627 sk_gen_sine(sk_table_data(tab), sk_table_size(tab));
2728
29+ rc = sk_core_table_push(core, tab);
30+
31+ if (rc) {
32+ /* TODO: error handling */
33+ }
34+
2835 return NULL;
2936 }
3037
nodes/loader.c+14−0
@@ -13,6 +13,7 @@ void sklil_load_phasor(lil_t lil); · 12 unmodified lines
1313 void sklil_load_rephasor(lil_t lil);
1414 void sklil_load_tabnew(lil_t lil);
1515 void sklil_load_gensine(lil_t lil);
16+void sklil_load_osc(lil_t lil);
1617
1718 void sklil_nodes(lil_t lil)
1819 {
@@ -24,6 +25,7 @@ void sklil_nodes(lil_t lil) · 5 unmodified lines
2425 sklil_load_rephasor(lil);
2526 sklil_load_tabnew(lil);
2627 sklil_load_gensine(lil);
28+ sklil_load_osc(lil);
2729 }
2830
2931 static lil_value_t computes(lil_t lil, size_t argc, lil_value_t *argv)
@@ -37,6 +39,17 @@ static lil_value_t computes(lil_t lil, size_t argc, lil_value_t *argv) · 7 unmodified lines
3739 return NULL;
3840 }
3941
42+static lil_value_t param(lil_t lil, size_t argc, lil_value_t *argv)
43+{
44+ sk_core *core;
45+ core = lil_get_data(lil);
46+
47+ SKLIL_ARITY_CHECK(lil, "param", argc, 1);
48+
49+ sklil_param(core, argv[0]);
50+ return NULL;
51+}
52+
4053 void sklil_loader(lil_t lil)
4154 {
4255 sk_core *core;
@@ -44,6 +57,7 @@ void sklil_loader(lil_t lil) · 1 unmodified line
4457 lil_set_data(lil, core);
4558 sklil_nodes(lil);
4659 lil_register(lil, "computes", computes);
60+ lil_register(lil, "param", param);
4761 }
4862
4963 void sklil_clean(lil_t lil)
nodes/osc/config.mk+2−0
@@ -0,0 +1,2 @@
1+OBJ+=nodes/osc/osc.o
2+OBJ+=nodes/osc/l_osc.o
nodes/osc/l_osc.c+34−0
@@ -0,0 +1,34 @@
1+#include <stdlib.h>
2+#include <stdio.h>
3+#include <string.h>
4+#include "lil/lil.h"
5+#include "patchwerk/patchwerk.h"
6+#include "core.h"
7+#include "sklil.h"
8+#include "osc.h"
9+
10+static lil_value_t osc(lil_t lil, size_t argc, lil_value_t *argv)
11+{
12+ sk_core *core;
13+ int rc;
14+ core = lil_get_data(lil);
15+
16+ SKLIL_ARITY_CHECK(lil, "osc", argc, 4);
17+
18+ /* skip param 0 */
19+ sklil_param(core, argv[1]);
20+ sklil_param(core, argv[2]);
21+ sklil_param(core, argv[3]);
22+
23+ rc = sk_node_osc(core);
24+
25+ if (rc) {
26+ /* TODO: error handling */
27+ }
28+ return NULL;
29+}
30+
31+void sklil_load_osc(lil_t lil)
32+{
33+ lil_register(lil, "osc", osc);
34+}
nodes/osc/osc.c+108−0
@@ -0,0 +1,108 @@
1+#include <math.h>
2+#include <stdlib.h>
3+#include <stdint.h>
4+#include "patchwerk.h"
5+#include "core.h"
6+#define SK_OSC_PRIV
7+#include "dsp/osc.h"
8+
9+struct osc_n {
10+ pw_cable *freq;
11+ pw_cable *amp;
12+ pw_cable *out;
13+ sk_osc osc;
14+};
15+
16+static void compute(pw_node *node)
17+{
18+ int blksize;
19+ int n;
20+ struct osc_n *osc;
21+
22+ blksize = pw_node_blksize(node);
23+
24+ osc = (struct osc_n *)pw_node_get_data(node);
25+
26+ for (n = 0; n < blksize; n++) {
27+ PWFLT out, f, a;
28+ f = pw_cable_get(osc->freq, n);
29+ a = pw_cable_get(osc->amp, n);
30+
31+ sk_osc_freq(&osc->osc, f);
32+ sk_osc_amp(&osc->osc, a);
33+ out = sk_osc_tick(&osc->osc);
34+
35+ pw_cable_set(osc->out, n, out);
36+ }
37+}
38+
39+static void destroy(pw_node *node)
40+{
41+ pw_patch *patch;
42+ int rc;
43+ void *ud;
44+ rc = pw_node_get_patch(node, &patch);
45+ if (rc != PW_OK) return;
46+ pw_node_cables_free(node);
47+ ud = pw_node_get_data(node);
48+ pw_memory_free(patch, &ud);
49+}
50+
51+int sk_node_osc(sk_core *core)
52+{
53+ pw_patch *patch;
54+ pw_node *node;
55+ int rc;
56+ sk_param freq;
57+ sk_param amp;
58+ void *ud;
59+ struct osc_n *osc;
60+ int sr;
61+ sk_table *tab;
62+ SKFLT iphs;
63+
64+ rc = sk_param_get_constant(core, &iphs);
65+ SK_ERROR_CHECK(rc);
66+
67+ rc = sk_param_get(core, &amp);
68+ SK_ERROR_CHECK(rc);
69+
70+ rc = sk_param_get(core, &freq);
71+ SK_ERROR_CHECK(rc);
72+
73+ rc = sk_core_table_pop(core, &tab);
74+ SK_ERROR_CHECK(rc);
75+
76+ patch = sk_core_patch(core);
77+
78+ rc = pw_memory_alloc(patch, sizeof(struct osc_n), &ud);
79+ SK_PW_ERROR_CHECK(rc);
80+ osc = (struct osc_n *)ud;
81+
82+ sr = pw_patch_srate_get(patch);
83+ sk_osc_init(&osc->osc, sr,
84+ sk_table_data(tab),
85+ sk_table_size(tab),
86+ iphs);
87+
88+ rc = pw_patch_new_node(patch, &node);
89+ SK_PW_ERROR_CHECK(rc);
90+
91+ rc = pw_node_cables_alloc(node, 3);
92+ SK_PW_ERROR_CHECK(rc);
93+
94+ pw_node_set_block(node, 2);
95+
96+ pw_node_get_cable(node, 0, &osc->freq);
97+ pw_node_get_cable(node, 1, &osc->amp);
98+ pw_node_get_cable(node, 2, &osc->out);
99+
100+ pw_node_set_data(node, osc);
101+ pw_node_set_compute(node, compute);
102+ pw_node_set_destroy(node, destroy);
103+
104+ sk_param_set(core, node, &freq, 0);
105+ sk_param_set(core, node, &amp, 1);
106+ sk_param_out(core, node, 2);
107+ return 0;
108+}
nodes/osc/osc.h+4−0
@@ -0,0 +1,4 @@
1+#ifndef SK_NODE_OSC_H
2+#define SK_NODE_OSC_H
3+int sk_node_osc(sk_core *core);
4+#endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t