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
17This tangles to =chaosnoise.c= and =chaosnoise.h=. Defining17This tangles to =chaosnoise.c= and =chaosnoise.h=. Defining
18=SK_CHAOSNOISE_PRIV= will expose the core struct in the18=SK_CHAOSNOISE_PRIV= will expose the core struct in the
19header file.19header 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
20* Struct48* Struct
21Data for ChaosNoise is contained in a struct called49Data for ChaosNoise is contained in a struct called
22=chaosnoise=.50=sk_chaosnoise=.
51
52#+NAME: typedefs
53#+BEGIN_SRC c
54typedef struct sk_chaosnoise sk_chaosnoise;
55#+END_SRC
56
57#+NAME: structs
58#+BEGIN_SRC c
59struct sk_chaosnoise {
60 <<sk_chaosnoise>>
61};
62#+END_SRC
23* Filter Memory63* Filter Memory
24chaosnoise is technically a 2-pole filter, requiring64chaosnoise is technically a 2-pole filter, requiring
25memory for 2 samples of signal.65memory for 2 samples of signal.
66
67#+NAME: sk_chaosnoise
68#+BEGIN_SRC c
69SKFLT y[2];
70#+END_SRC
71
72#+NAME: init
73#+BEGIN_SRC c
74cn->y[0] = init;
75cn->y[1] = 0;
76#+END_SRC
26* Sample and Hold Constants and Parameters77* Sample and Hold Constants and Parameters
27The signal is clocked with a sample and hold generator. This78The signal is clocked with a sample and hold generator. This
28sample and hold will use a fixed-point phasor to keep track79sample and hold will use a fixed-point phasor to keep track
@@ -33,42 +84,156 @@ found in these pages. · 4 unmodified lines
33A 32-bit integer called =phs= keeps track of the phase84A 32-bit integer called =phs= keeps track of the phase
34position.85position.
35 86
87#+NAME: sk_chaosnoise
88#+BEGIN_SRC c
89long phs;
90#+END_SRC
91
92#+NAME: init
93#+BEGIN_SRC c
94cn->phs = 0;
95#+END_SRC
96
36The constant =SK_CHAOSNOISE_PHSMAX= defines the maximum97The constant =SK_CHAOSNOISE_PHSMAX= defines the maximum
37length (and resolution) of the phasor, set to be98length (and resolution) of the phasor, set to be
38=0x1000000=.99=0x1000000=.
39 100
101#+NAME: macros
102#+BEGIN_SRC c
103#define SK_CHAOSNOISE_PHSMAX 0x1000000L
104#+END_SRC
105
40The constant =SK_CHAOSNOISE_PHSMSK= defines the masking106The constant =SK_CHAOSNOISE_PHSMSK= defines the masking
41value of the phasor, set to be =0x0FFFFFF=.107value of the phasor, set to be =0x0FFFFFF=.
42 108
109#+NAME: macros
110#+BEGIN_SRC c
111#define SK_CHAOSNOISE_PHSMSK 0x0FFFFFFL
112#+END_SRC
113
43The floating point value =maxlens= is114The floating point value =maxlens= is
44=SK_CHAOSNOISE_PHSMAX=, converted to seconds. It is used115=SK_CHAOSNOISE_PHSMAX=, converted to seconds. It is used
45to calculate the increment amount for the phase position.116to calculate the increment amount for the phase position.
46When a rate value (in units of Hz, or cycles/second) get117When a rate value (in units of Hz, or cycles/second) get
47multiplied by this value, the result is a value in units118multiplied by this value, the result is a value in units
48of phase cycles.119of phase cycles.
120
121#+NAME: sk_chaosnoise
122#+BEGIN_SRC c
123SKFLT maxlens;
124#+END_SRC
125
126#+NAME: init
127#+BEGIN_SRC c
128cn->maxlens = SK_CHAOSNOISE_PHSMAX / (SKFLT) sr;
129#+END_SRC
49* Parameters130* Parameters
50** Chaos131** Chaos
51The =chaos= parameter is variable that controls the chaos132The =chaos= parameter is variable that controls the chaos
52level. It is typically a value between 1 and 2.133level. It is typically a value between 1 and 2.
53 134
54It can be set with =sk_chaosnoise_chaos=.135It can be set with =sk_chaosnoise_chaos=.
136
137#+NAME: funcdefs
138#+BEGIN_SRC c
139void sk_chaosnoise_chaos(sk_chaosnoise *cn, SKFLT chaos);
140#+END_SRC
141
142#+NAME: funcs
143#+BEGIN_SRC c
144void 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
152SKFLT chaos;
153#+END_SRC
154
155#+NAME: init
156#+BEGIN_SRC c
157sk_chaosnoise_chaos(cn, 1.5);
158#+END_SRC
55** Rate159** Rate
56The =rate= value controls the rate of computation. This160The =rate= value controls the rate of computation. This
57is a value measure in Hertz.161is a value measure in Hertz.
58 162
59It can be set with =sk_chaosnoise_rate=.163It can be set with =sk_chaosnoise_rate=.
164
165#+NAME: funcdefs
166#+BEGIN_SRC c
167void sk_chaosnoise_rate(sk_chaosnoise *cn, SKFLT rate);
168#+END_SRC
169
170#+NAME: funcs
171#+BEGIN_SRC c
172void 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
180SKFLT rate;
181#+END_SRC
182
183#+NAME: init
184#+BEGIN_SRC c
185sk_chaosnoise_rate(cn, 8000);
186#+END_SRC
60* Initialization187* Initialization
61Initialized with =sk_chaosnoise_init=. Sampling rate and an188Initialized with =sk_chaosnoise_init=. Sampling rate and an
62initial value must be supplied as arguments. The initial189initial value must be supplied as arguments. The initial
63value is a floating point value that expects to be in range190value is a floating point value that expects to be in range
640-1.1910-1.
192
193#+NAME: funcdefs
194#+BEGIN_SRC c
195void sk_chaosnoise_init(sk_chaosnoise *cn, int sr, SKFLT init);
196#+END_SRC
197
198#+NAME: funcs
199#+BEGIN_SRC c
200void sk_chaosnoise_init(sk_chaosnoise *cn, int sr, SKFLT init)
201{
202 <<init>>
203}
204#+END_SRC
65* Computation205* Computation
66A single sample of audio is computed with206A single sample of audio is computed with
67=sk_chaosnoise_tick=.207=sk_chaosnoise_tick=.
68 208
209#+NAME: funcdefs
210#+BEGIN_SRC c
211SKFLT sk_chaosnoise_tick(sk_chaosnoise *cn);
212#+END_SRC
213
214#+NAME: funcs
215#+BEGIN_SRC c
216SKFLT 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
69First, the phasor value is updated by an increment value,229First, the phasor value is updated by an increment value,
70computed with =rate*maxlens=.230computed with =rate*maxlens=.
71 231
232#+NAME: update_phasor
233#+BEGIN_SRC c
234cn->phs += floor(cn->rate * cn->maxlens);
235#+END_SRC
236
72If the phasor reaches (or goes above) the end, a new237If the phasor reaches (or goes above) the end, a new
73sample gets computed.238sample gets computed.
74 239
@@ -84,5 +249,22 @@ set to be =0.05=. · 9 unmodified lines
84After the output value is computed, the coefficients are249After the output value is computed, the coefficients are
85updated.250updated.
86 251
252#+NAME: compute_noise
253#+BEGIN_SRC c
254if (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
87The output is always set to be the filter memory variable264The output is always set to be the filter memory variable
88=y[0]=.265=y[0]=.
266
267#+NAME: copy_to_out
268#+BEGIN_SRC c
269out = cn->y[0];
270#+END_SRC
mkdb.janet+1−0
@@ -24,6 +24,7 @@ · 23 unmodified lines
24(ww-add-link "bitnoise" "bitnoise.org")24(ww-add-link "bitnoise" "bitnoise.org")
25(ww-add-link "keywords" "keywords.org")25(ww-add-link "keywords" "keywords.org")
26(ww-add-link "valp1" "valp1.org")26(ww-add-link "valp1" "valp1.org")
27(ww-add-link "chaosnoise" "chaosnoise.org")
27 28
28# sync and close29# sync and close
29 30

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