-
Notifications
You must be signed in to change notification settings - Fork 2
/
vgen_osc.h
202 lines (169 loc) · 5.02 KB
/
vgen_osc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// .___
// ___ __ ____ ___ ___ ____ __| _/
// \ \/ // __ \\ \/ // __ \ / __ |
// \ /\ ___/ > <\ ___// /_/ |
// \_/ \___ >__/\_ \\___ >____ |
// \/ \/ \/ \/ __ .__
// ____ ____ ____ ________________ _/ |_|__| ____ ____
// / ___\_/ __ \ / \_/ __ \_ __ \__ \\ __\ |/ _ \ / \
// / /_/ > ___/| | \ ___/| | \// __ \| | | ( <_> ) | \
// \___ / \___ >___| /\___ >__| (____ /__| |__|\____/|___| /
// /_____/ \/ \/ \/ \/ \/
//
// (c) 2016 - 2020 Karsten Schmidt // ASL 2.0 licensed
#ifndef __vgen_osc_h__
#define __vgen_osc_h__
#include <math.h>
#include <vgen_math.h>
#define VG_OSC(fn) \
float fn(float phase, freq, amp) { \
return fn(phase, freq, amp, 0); \
} \
float fn(float phase, freq) { \
return fn(phase, freq, 1, 0); \
}
#define VG_OSCX(fn, x) \
float fn(float phase, freq, amp, dc) { \
return fn(phase, freq, amp, dc, x); \
} \
float fn(float phase, freq, amp) { \
return fn(phase, freq, amp, 0, x); \
} \
float fn(float phase, freq) { \
return fn(phase, freq, 1, 0, x); \
}
/**
* Sine oscillator
*/
float vg_osc_sin(float phase, freq, amp, dc) {
return dc + amp * sin(phase * freq * TAU);
}
VG_OSC(vg_osc_sin)
/**
* Cosine oscillator
*/
float vg_osc_cos(float phase, freq, amp, dc) {
return dc + amp * cos(phase * freq * TAU);
}
VG_OSC(vg_osc_cos)
/**
* Triangle oscillator
*/
float vg_osc_tri(float phase, freq, amp, dc) {
return dc + amp * (abs((phase * freq * 4) % 4 - 2) - 1);
}
VG_OSC(vg_osc_tri)
/**
* Concave curved triangle oscillator
*/
float vg_osc_tri_concave(float phase, freq, amp, dc) {
return dc + amp * (8 * pow(frac(phase * freq) - 0.5, 2) - 1);
}
VG_OSC(vg_osc_tri_concave)
/**
* Square oscillator (with configurable duty cycle)
*/
float vg_osc_square(float phase, freq, amp, dc, duty) {
return dc + amp * (((phase * freq) % 1) < duty ? 1 : -1);
}
VG_OSCX(vg_osc_square, 0.5)
/**
* Sawtooth oscillator
*/
float vg_osc_saw(float phase, freq, amp, dc) {
return dc + amp * (((phase * freq) % 1) * 2 - 1);
}
VG_OSC(vg_osc_saw)
/**
* Interpolated Sine-Sawtooth oscillator
*/
float vg_osc_sinsaw(float phase, freq, amp, dc, t) {
return vg_mix(vg_osc_sin(phase, freq, amp, dc),
vg_osc_saw(phase, freq, amp, dc), t);
}
VG_OSCX(vg_osc_sinsaw, 0.5)
/**
* Interpolated Sine-Square oscillator
*/
float vg_osc_sinsquare(float phase, freq, amp, dc, t) {
return vg_mix(vg_osc_sin(phase, freq, amp, dc),
vg_osc_square(phase, freq, amp, dc), t);
}
VG_OSCX(vg_osc_sinsquare, 0.5)
/**
* Interpolated Sine-Triangle oscillator
*/
float vg_osc_sintri(float phase, freq, amp, dc, t) {
return vg_mix(vg_osc_sin(phase, freq, amp, dc),
vg_osc_tri(phase, freq, amp, dc), t);
}
VG_OSCX(vg_osc_sintri, 0.5)
/**
* Interpolated Saw-Square oscillator
*/
float vg_osc_sawsquare(float phase, freq, amp, dc, t) {
return vg_mix(vg_osc_saw(phase, freq, amp, dc),
vg_osc_square(phase, freq, amp, dc), t);
}
VG_OSCX(vg_osc_sawsquare, 0.5)
/**
* Interpolated Saw-Triangle oscillator
*/
float vg_osc_sawtri(float phase, freq, amp, dc, t) {
return vg_mix(vg_osc_saw(phase, freq, amp, dc),
vg_osc_tri(phase, freq, amp, dc), t);
}
VG_OSCX(vg_osc_sawtri, 0.5)
/**
* Interpolated Square-Triangle oscillator
*/
float vg_osc_squaretri(float phase, freq, amp, dc, t) {
return vg_mix(vg_osc_square(phase, freq, amp, dc),
vg_osc_tri(phase, freq, amp, dc), t);
}
VG_OSCX(vg_osc_squaretri, 0.5)
/**
* Returns float array of `n` samples of given ramp param
*/
float[] vg_sample_ramp(string op_path; int n) {
float res[];
resize(res, n);
for (int nn = n - 1, i = nn; i >= 0; i--) {
res[i] = chramp(op_path, float(i) / nn);
}
return res;
}
/**
* Wavetable oscillator (array based)
* Use: vg_sample_ramp() to convert ramp into wave table
*/
float vg_osc_wavetable(float table[]; float phase, freq, amp, dc) {
int n = len(table);
int i = int(phase * freq * n) % n;
return dc + amp * table[i < 0 ? i + n : i];
}
/**
* Invokes oscillator by ID. Only single waveform versions are supported.
* E.g. `vg_osc_by_id("sin", ...)` => `vg_osc_sin(...)`
*/
float vg_osc_by_id(string id; float phase, freq, amp, dc) {
if (id == "sin") {
return vg_osc_sin(phase, freq, amp, dc);
}
if (id == "saw") {
return vg_osc_saw(phase, freq, amp, dc);
}
if (id == "square") {
return vg_osc_square(phase, freq, amp, dc);
}
if (id == "tri") {
return vg_osc_tri(phase, freq, amp, dc);
}
if (id == "tri_concave") {
return vg_osc_tri_concave(phase, freq, amp, dc);
}
return 0;
}
#undef VG_OSC
#undef VG_OSCX
#endif