History

added scale node with example

Paul Batchelor committed 5 years ago

Commit b073f3d1 parent 14f4c7a8 files · +179 −0Browse files

Unified · Split

8 files
  1. Makefile +1 −0
  2. examples/Makefile +12 −0
  3. examples/build.sh +1 −0
  4. examples/ex2.c +64 −0
  5. nodes/config.mk +1 −0
  6. nodes/scale/config.mk +1 −0
  7. nodes/scale/scale.c +95 −0
  8. nodes/scale/scale.h +4 −0
Makefile+1−0
@@ -11,6 +11,7 @@ C99?=$(CC) -std=c99 · 10 unmodified lines
1111 CFLAGS += -Ipatchwerk -I.
1212 CFLAGS += -Wall
1313 CFLAGS += -O3
14+CFLAGS += -g
1415
1516 TANGLED=\
1617 bigverb.c bigverb.h \
examples/Makefile+12−0
@@ -0,0 +1,12 @@
1+EX=ex1.bin ex2.bin
2+
3+LDFLAGS=-L../
4+CFLAGS=-I../ -I../patchwerk -I../nodes
5+LIBS=-lsndkit
6+default: $(EX)
7+
8+%.bin: %.c
9+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) $(LIBS)
10+
11+clean:
12+ $(RM) $(EX)
examples/build.sh+1−0
@@ -1 +1,2 @@
11 gcc ex1.c -L../ -I../ -lsndkit -o ex1 -I../patchwerk -I../nodes
2+gcc ex2.c -L../ -I../ -lsndkit -o ex2 -I../patchwerk -I../nodes
examples/ex2.c+64−0
@@ -0,0 +1,64 @@
1+#include <math.h>
2+#include <stdio.h>
3+#include "patchwerk.h"
4+#include "core.h"
5+#include "wavout/wavout.h"
6+#include "sine/sine.h"
7+#include "scale/scale.h"
8+
9+int patch(sk_core *core)
10+{
11+ int rc;
12+
13+ /* paranoid error checking */
14+
15+ rc = sk_core_constant(core, 5);
16+ SK_ERROR_CHECK(rc);
17+ rc = sk_core_constant(core, 1);
18+ SK_ERROR_CHECK(rc);
19+ rc = sk_node_sine(core);
20+ SK_ERROR_CHECK(rc);
21+
22+ rc = sk_core_constant(core, 200);
23+ SK_ERROR_CHECK(rc);
24+ rc = sk_core_constant(core, 300);
25+ SK_ERROR_CHECK(rc);
26+ rc = sk_node_biscale(core);
27+ SK_ERROR_CHECK(rc);
28+
29+ rc = sk_core_constant(core, 0.1);
30+ SK_ERROR_CHECK(rc);
31+ rc = sk_node_sine(core);
32+ SK_ERROR_CHECK(rc);
33+ rc = sk_node_wavout(core, "test.wav");
34+ SK_ERROR_CHECK(rc);
35+
36+ return 0;
37+}
38+
39+int main(int argc, char *argv[])
40+{
41+ sk_core *core;
42+ unsigned int n;
43+ unsigned int nblocks;
44+ int rc;
45+
46+ core = sk_core_new(44100);
47+
48+ rc = patch(core);
49+
50+ if (rc) {
51+ fprintf(stderr, "Error code %d\n", rc);
52+ goto clean;
53+ }
54+
55+ nblocks = sk_core_seconds_to_blocks(core, 10);
56+
57+ for (n = 0; n < nblocks; n++) {
58+ sk_core_compute(core);
59+ }
60+
61+ clean:
62+ sk_core_del(core);
63+ return rc;
64+}
nodes/config.mk+1−0
@@ -1,2 +1,3 @@
11 include nodes/sine/config.mk
22 include nodes/wavout/config.mk
3+include nodes/scale/config.mk
nodes/scale/config.mk+1−0
@@ -0,0 +1 @@
1+OBJ+=nodes/scale/scale.o
nodes/scale/scale.c+95−0
@@ -0,0 +1,95 @@
1+#include <math.h>
2+#include <stdlib.h>
3+#include <stdint.h>
4+#include "patchwerk.h"
5+#include "core.h"
6+#include "../../scale.h"
7+
8+struct scale_n {
9+ pw_cable *in;
10+ pw_cable *min;
11+ pw_cable *max;
12+ pw_cable *out;
13+};
14+
15+static void biscale_compute(pw_node *node)
16+{
17+ int blksize;
18+ int n;
19+ struct scale_n *scale;
20+
21+ blksize = pw_node_blksize(node);
22+
23+ scale = (struct scale_n *)pw_node_get_data(node);
24+
25+ for (n = 0; n < blksize; n++) {
26+ PWFLT out, in, min, max;
27+
28+ in = pw_cable_get(scale->in, n);
29+ min = pw_cable_get(scale->min, n);
30+ max = pw_cable_get(scale->max, n);
31+ out = sk_biscale(in, min, max);
32+
33+ pw_cable_set(scale->out, n, out);
34+ }
35+}
36+
37+static void destroy(pw_node *node)
38+{
39+ pw_patch *patch;
40+ int rc;
41+ void *ud;
42+ rc = pw_node_get_patch(node, &patch);
43+ if (rc != PW_OK) return;
44+ pw_node_cables_free(node);
45+ ud = pw_node_get_data(node);
46+ pw_memory_free(patch, &ud);
47+}
48+
49+int sk_node_biscale(sk_core *core)
50+{
51+ pw_patch *patch;
52+ pw_node *node;
53+ int rc;
54+ sk_param in;
55+ sk_param min;
56+ sk_param max;
57+ void *ud;
58+ struct scale_n *scale;
59+
60+ rc = sk_param_get(core, &max);
61+ SK_ERROR_CHECK(rc);
62+ rc = sk_param_get(core, &min);
63+ SK_ERROR_CHECK(rc);
64+ rc = sk_param_get(core, &in);
65+ SK_ERROR_CHECK(rc);
66+
67+ patch = sk_core_patch(core);
68+
69+ rc = pw_memory_alloc(patch, sizeof(struct scale_n), &ud);
70+ SK_PW_ERROR_CHECK(rc);
71+ scale = (struct scale_n *)ud;
72+
73+ rc = pw_patch_new_node(patch, &node);
74+ SK_PW_ERROR_CHECK(rc);
75+
76+ rc = pw_node_cables_alloc(node, 4);
77+ SK_PW_ERROR_CHECK(rc);
78+
79+ pw_node_set_block(node, 3);
80+
81+ pw_node_get_cable(node, 0, &scale->in);
82+ pw_node_get_cable(node, 1, &scale->min);
83+ pw_node_get_cable(node, 2, &scale->max);
84+ pw_node_get_cable(node, 3, &scale->out);
85+
86+ pw_node_set_data(node, scale);
87+ pw_node_set_compute(node, biscale_compute);
88+ pw_node_set_destroy(node, destroy);
89+
90+ sk_param_set(core, node, &in, 0);
91+ sk_param_set(core, node, &min, 1);
92+ sk_param_set(core, node, &max, 2);
93+ sk_param_out(core, node, 3);
94+ return 0;
95+}
nodes/scale/scale.h+4−0
@@ -0,0 +1,4 @@
1+#ifndef SK_NODE_SCALE_H
2+#define SK_NODE_SCALE_H
3+int sk_node_biscale(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