-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwaves2.cpp
272 lines (236 loc) · 7.12 KB
/
waves2.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* File: waves2.cpp
*
*/
#include "userosc.h"
typedef struct State {
float w0;
float phase;
const float *wave0;
float shape;
float shapez;
float shiftshape;
float lfo;
float lfoz;
uint8_t flags;
/* parameter values */
uint8_t w_index;
float mod_int;
uint32_t ev_t1;
uint32_t ev_t2;
float lfo_att;
float amp_int;
/* envelope internal variables */
uint32_t ev_t;
uint32_t ev_t1t2;
float ev_value;
float ev_slope1;
float ev_slope2;
float mod_init;
/* actual intensities */
float lfo_int;
float ev_int;
} State;
enum {
k_flags_none = 0,
k_flag_reset = 1<<0,
k_flag_wave0 = 1<<1,
k_flag_envelope = 1<<2,
};
#define FLT_NOT_INITIALIZED M_E
static State s_state;
inline void update_wave(uint8_t idx) {
static const uint8_t k_a_thr = k_waves_a_cnt;
static const uint8_t k_b_thr = k_a_thr + k_waves_b_cnt;
static const uint8_t k_c_thr = k_b_thr + k_waves_c_cnt;
static const uint8_t k_d_thr = k_c_thr + k_waves_d_cnt;
static const uint8_t k_e_thr = k_d_thr + k_waves_e_cnt;
static const uint8_t k_f_thr = k_e_thr + k_waves_f_cnt;
const float * const * table;
if (idx < k_a_thr) {
table = wavesA;
} else if (idx < k_b_thr) {
table = wavesB;
idx -= k_a_thr;
} else if (idx < k_c_thr) {
table = wavesC;
idx -= k_b_thr;
} else if (idx < k_d_thr) {
table = wavesD;
idx -= k_c_thr;
} else if (idx < k_e_thr) {
table = wavesE;
idx -= k_d_thr;
} else { // if (idx < k_f_thr) {
table = wavesF;
idx -= k_e_thr;
}
s_state.wave0 = table[idx];
}
void OSC_INIT(uint32_t platform, uint32_t api)
{
s_state.w0 = 0.f;
s_state.phase = 0.f;
s_state.wave0 = wavesA[0];
s_state.shape = 0.f;
s_state.shapez = FLT_NOT_INITIALIZED;
s_state.lfoz = FLT_NOT_INITIALIZED;
s_state.lfo_att = FLT_NOT_INITIALIZED;
s_state.mod_int = 0;
s_state.amp_int = 0;
s_state.ev_t1 = 0;
s_state.ev_t2 = 0;
s_state.shiftshape = 0.f;
s_state.flags = k_flags_none;
}
inline void recalc_envelope() {
float lfo_int = 1.0 - s_state.lfo_att;
float ev_int = s_state.mod_int;
float sum = lfo_int + fabsf(ev_int);
if (sum > 1.0) {
lfo_int = lfo_int / sum;
ev_int = ev_int / sum;
}
lfo_int *= (1 - s_state.shape);
s_state.lfo_int = lfo_int;
ev_int *= (1 - s_state.shape);
s_state.ev_slope1 = (s_state.ev_t1 != 0) ? 1. / s_state.ev_t1 : 0;
s_state.ev_slope2 = (s_state.ev_t2 != 0) ? -1. / s_state.ev_t2 : 0;
s_state.ev_t1t2 = s_state.ev_t1 + s_state.ev_t2;
s_state.ev_int = ev_int;
s_state.mod_init = (ev_int < 0) ? -ev_int : 0;
}
__fast_inline float my_osc_wave_scanf(const float *wave, const float phase, const float multi)
{
float sig;
float p0 = phase * multi;
if (p0 >= 1.0) {
float coeff = fasterpow2f(s_state.shiftshape * 7);
sig = osc_wave_scanf(wave, p0 - 1.0);
sig *= fastpowf((multi - p0) / (multi - 1), coeff);
} else {
sig = osc_wave_scanf(wave, p0);
}
return sig;
}
void OSC_CYCLE(const user_osc_param_t * const params,
int32_t *yn,
const uint32_t frames)
{
const uint8_t flags = s_state.flags;
s_state.flags = k_flags_none;
if (flags & k_flag_wave0) {
update_wave(s_state.w_index);
}
s_state.lfo = q31_to_f32(params->shape_lfo);
const float w0 = s_state.w0 = osc_w0f_for_note((params->pitch)>>8, params->pitch & 0xFF);
float phase = (flags & k_flag_reset) ? 0.f : s_state.phase;
const float *wave0 = s_state.wave0;
q31_t * __restrict y = (q31_t *) yn;
const q31_t * y_e = y + frames;
const float shape = s_state.shape;
float shapez = s_state.shapez;
float lfoz = s_state.lfoz;
if (lfoz == FLT_NOT_INITIALIZED) {
lfoz = s_state.lfo;
}
if (flags & k_flag_envelope) {
recalc_envelope();
}
const float lfo_inc = (s_state.lfo - lfoz) / frames;
const float lfo_max = s_state.lfo_int;
uint32_t ev_t = (flags & k_flag_reset) ? 0 : s_state.ev_t;
const float ev_int = s_state.ev_int;
float ev_value = (flags & k_flag_reset) ? 0 : s_state.ev_value;
const float amp_int = s_state.amp_int;
const uint32_t ev_t1 = s_state.ev_t1;
const uint32_t ev_t1t2 = s_state.ev_t1t2;
const float ev_slope1 = s_state.ev_slope1;
const float ev_slope2 = s_state.ev_slope2;
const float mod_init = s_state.mod_init;
for (; y != y_e; ) {
shapez = linintf(0.002f, shapez, shape);
float mod_value = mod_init + ev_int * ev_value;
float inv_width = powf(2.0, (shapez + lfo_max * lfoz + mod_value) * 3);
float sig = my_osc_wave_scanf(wave0, phase, inv_width);
sig *= 1 - amp_int + amp_int * ev_value;
*(y++) = f32_to_q31(sig);
phase += w0;
phase -= (uint32_t)phase;
lfoz += lfo_inc;
if (ev_t < ev_t1) {
ev_value += ev_slope1;
} else if (ev_t == ev_t1) {
ev_value = 1.;
} else if (ev_t < ev_t1t2) {
ev_value += ev_slope2;
} else {
ev_value = 0.;
}
ev_t++;
}
s_state.phase = phase;
s_state.shapez = shapez;
s_state.lfoz = lfoz;
s_state.ev_t = ev_t;
s_state.ev_value = ev_value;
}
void OSC_NOTEON(const user_osc_param_t * const params)
{
s_state.flags |= k_flag_reset;
}
void OSC_NOTEOFF(const user_osc_param_t * const params)
{
(void)params;
}
void OSC_PARAM(uint16_t index, uint16_t value)
{
const float valf = param_val_to_f32(value);
switch (index) {
case k_user_osc_param_id1:
/* wave */
{
static const uint8_t cnt = k_waves_a_cnt + k_waves_b_cnt \
+k_waves_c_cnt +k_waves_d_cnt +k_waves_e_cnt +k_waves_f_cnt;
s_state.w_index = value % cnt;
s_state.flags |= k_flag_wave0;
}
break;
case k_user_osc_param_id2:
/* ShapeMod Intensity */
s_state.mod_int = clip1m1f((value - 100) * 0.01f);
s_state.flags |= k_flag_envelope;
break;
case k_user_osc_param_id3:
/* Mod Attack */
s_state.ev_t1 = (powf(9, 0.01 * value) - 1) * k_samplerate;
s_state.flags |= k_flag_envelope;
break;
case k_user_osc_param_id4:
/* Mod Decay */
s_state.ev_t2 = (powf(9, 0.01 * value) - 1) * k_samplerate;
s_state.flags |= k_flag_envelope;
break;
case k_user_osc_param_id5:
/* ShapeLFO Attenuation */
s_state.lfo_att = clip01f(value * 0.01f);
s_state.flags |= k_flag_envelope;
break;
case k_user_osc_param_id6:
/* Envelope to amplitude intensity */
s_state.amp_int = clip01f(value * 0.01f);
break;
case k_user_osc_param_shape:
s_state.shape = valf;
if (s_state.shapez == FLT_NOT_INITIALIZED) {
s_state.shapez = valf;
}
s_state.flags |= k_flag_envelope;
break;
case k_user_osc_param_shiftshape:
s_state.shiftshape = valf;
break;
default:
break;
}
}