History

chaosnoise: initial code

Paul Batchelor committed 6 years ago

Commit 1b337ed1 parent 4c89eb72 files · +184 −1Browse files

Unified · Split

2 files
  1. chaosnoise.org +183 −1
  2. mkdb.janet +1 −0
chaosnoise.org+183−1
@@ -17,12 +17,63 @@ is can be used to change the tone quality of the algorithm. · 16 unmodified lines
1717 This tangles to =chaosnoise.c= and =chaosnoise.h=. Defining
1818 =SK_CHAOSNOISE_PRIV= will expose the core struct in the
1919 header file.
20+
21+#+NAME: chaosnoise.c
22+#+BEGIN_SRC c :tangle chaosnoise.c
23+#include <math.h>
24+#define SK_CHAOSNOISE_PRIV
25+#include "chaosnoise.h"
26+<<macros>>
27+<<funcs>>
28+#+END_SRC
29+
30+#+NAME: chaosnoise.h
31+#+BEGIN_SRC c :tangle chaosnoise.h
32+#ifndef SK_CHAOSNOISE_H
33+#define SK_CHAOSNOISE_H
34+
35+#ifndef SKFLT
36+#define SKFLT float
37+#endif
38+
39+<<typedefs>>
40+<<funcdefs>>
41+
42+#ifdef SK_CHAOSNOISE_PRIV
43+<<structs>>
44+#endif
45+
46+#endif
47+#+END_SRC
2048 * Struct
2149 Data for ChaosNoise is contained in a struct called
22−=chaosnoise=.
50+=sk_chaosnoise=.
51+
52+#+NAME: typedefs
53+#+BEGIN_SRC c
54+typedef struct sk_chaosnoise sk_chaosnoise;
55+#+END_SRC
56+
57+#+NAME: structs
58+#+BEGIN_SRC c
59+struct sk_chaosnoise {
60+ <<sk_chaosnoise>>
61+};
62+#+END_SRC
2363 * Filter Memory
2464 chaosnoise is technically a 2-pole filter, requiring
2565 memory for 2 samples of signal.
66+
67+#+NAME: sk_chaosnoise
68+#+BEGIN_SRC c
69+SKFLT y[2];
70+#+END_SRC
71+
72+#+NAME: init
73+#+BEGIN_SRC c
74+cn->y[0] = init;
75+cn->y[1] = 0;
76+#+END_SRC
2677 * Sample and Hold Constants and Parameters
2778 The signal is clocked with a sample and hold generator. This
2879 sample and hold will use a fixed-point phasor to keep track
@@ -33,42 +84,156 @@ found in these pages. · 4 unmodified lines
3384 A 32-bit integer called =phs= keeps track of the phase
3485 position.
3586
87+#+NAME: sk_chaosnoise
88+#+BEGIN_SRC c
89+long phs;
90+#+END_SRC
91+
92+#+NAME: init
93+#+BEGIN_SRC c
94+cn->phs = 0;
95+#+END_SRC
96+
3697 The constant =SK_CHAOSNOISE_PHSMAX= defines the maximum
3798 length (and resolution) of the phasor, set to be
3899 =0x1000000=.
39100
101+#+NAME: macros
102+#+BEGIN_SRC c
103+#define SK_CHAOSNOISE_PHSMAX 0x1000000L
104+#+END_SRC
105+
40106 The constant =SK_CHAOSNOISE_PHSMSK= defines the masking
41107 value of the phasor, set to be =0x0FFFFFF=.
42108
109+#+NAME: macros
110+#+BEGIN_SRC c
111+#define SK_CHAOSNOISE_PHSMSK 0x0FFFFFFL
112+#+END_SRC
113+
43114 The floating point value =maxlens= is
44115 =SK_CHAOSNOISE_PHSMAX=, converted to seconds. It is used
45116 to calculate the increment amount for the phase position.
46117 When a rate value (in units of Hz, or cycles/second) get
47118 multiplied by this value, the result is a value in units
48119 of phase cycles.
120+
121+#+NAME: sk_chaosnoise
122+#+BEGIN_SRC c
123+SKFLT maxlens;
124+#+END_SRC
125+
126+#+NAME: init
127+#+BEGIN_SRC c
128+cn->maxlens = SK_CHAOSNOISE_PHSMAX / (SKFLT) sr;
129+#+END_SRC
49130 * Parameters
50131 ** Chaos
51132 The =chaos= parameter is variable that controls the chaos
52133 level. It is typically a value between 1 and 2.
53134
54135 It can be set with =sk_chaosnoise_chaos=.
136+
137+#+NAME: funcdefs
138+#+BEGIN_SRC c
139+void sk_chaosnoise_chaos(sk_chaosnoise *cn, SKFLT chaos);
140+#+END_SRC
141+
142+#+NAME: funcs
143+#+BEGIN_SRC c
144+void sk_chaosnoise_chaos(sk_chaosnoise *cn, SKFLT chaos)
145+{
146+ cn->chaos = chaos;
147+}
148+#+END_SRC
149+
150+#+NAME: sk_chaosnoise
151+#+BEGIN_SRC c
152+SKFLT chaos;
153+#+END_SRC
154+
155+#+NAME: init
156+#+BEGIN_SRC c
157+sk_chaosnoise_chaos(cn, 1.5);
158+#+END_SRC
55159 ** Rate
56160 The =rate= value controls the rate of computation. This
57161 is a value measure in Hertz.
58162
59163 It can be set with =sk_chaosnoise_rate=.
164+
165+#+NAME: funcdefs
166+#+BEGIN_SRC c
167+void sk_chaosnoise_rate(sk_chaosnoise *cn, SKFLT rate);
168+#+END_SRC
169+
170+#+NAME: funcs
171+#+BEGIN_SRC c
172+void sk_chaosnoise_rate(sk_chaosnoise *cn, SKFLT rate)
173+{
174+ cn->rate = rate;
175+}
176+#+END_SRC
177+
178+#+NAME: sk_chaosnoise
179+#+BEGIN_SRC c
180+SKFLT rate;
181+#+END_SRC
182+
183+#+NAME: init
184+#+BEGIN_SRC c
185+sk_chaosnoise_rate(cn, 8000);
186+#+END_SRC
60187 * Initialization
61188 Initialized with =sk_chaosnoise_init=. Sampling rate and an
62189 initial value must be supplied as arguments. The initial
63190 value is a floating point value that expects to be in range
64191 0-1.
192+
193+#+NAME: funcdefs
194+#+BEGIN_SRC c
195+void sk_chaosnoise_init(sk_chaosnoise *cn, int sr, SKFLT init);
196+#+END_SRC
197+
198+#+NAME: funcs
199+#+BEGIN_SRC c
200+void sk_chaosnoise_init(sk_chaosnoise *cn, int sr, SKFLT init)
201+{
202+ <<init>>
203+}
204+#+END_SRC
65205 * Computation
66206 A single sample of audio is computed with
67207 =sk_chaosnoise_tick=.
68208
209+#+NAME: funcdefs
210+#+BEGIN_SRC c
211+SKFLT sk_chaosnoise_tick(sk_chaosnoise *cn);
212+#+END_SRC
213+
214+#+NAME: funcs
215+#+BEGIN_SRC c
216+SKFLT sk_chaosnoise_tick(sk_chaosnoise *cn)
217+{
218+ SKFLT out;
219+ out = 0;
220+
221+ <<update_phasor>>
222+ <<compute_noise>>
223+ <<copy_to_out>>
224+
225+ return out;
226+}
227+#+END_SRC
228+
69229 First, the phasor value is updated by an increment value,
70230 computed with =rate*maxlens=.
71231
232+#+NAME: update_phasor
233+#+BEGIN_SRC c
234+cn->phs += floor(cn->rate * cn->maxlens);
235+#+END_SRC
236+
72237 If the phasor reaches (or goes above) the end, a new
73238 sample gets computed.
74239
@@ -84,5 +249,22 @@ set to be =0.05=. · 9 unmodified lines
84249 After the output value is computed, the coefficients are
85250 updated.
86251
252+#+NAME: compute_noise
253+#+BEGIN_SRC c
254+if (cn->phs >= SK_CHAOSNOISE_PHSMAX) {
255+ SKFLT y;
256+
257+ cn->phs &= SK_CHAOSNOISE_PHSMSK;
258+ y = fabs(cn->chaos * cn->y[0] - cn->y[1] - 0.05);
259+ cn->y[1] = cn->y[0];
260+ cn->y[0] = y;
261+}
262+#+END_SRC
263+
87264 The output is always set to be the filter memory variable
88265 =y[0]=.
266+
267+#+NAME: copy_to_out
268+#+BEGIN_SRC c
269+out = cn->y[0];
270+#+END_SRC
mkdb.janet+1−0
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424 (ww-add-link "bitnoise" "bitnoise.org")
2525 (ww-add-link "keywords" "keywords.org")
2626 (ww-add-link "valp1" "valp1.org")
27+(ww-add-link "chaosnoise" "chaosnoise.org")
2728
2829 # sync and close
2930

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