-
Notifications
You must be signed in to change notification settings - Fork 13
/
gator.cpp
195 lines (181 loc) · 4.03 KB
/
gator.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
/*
* File: gator.cpp
*
* Kaossilator-style gate arpeggiator
*
* 2020 (c) Oleg Burdaev
* mailto: [email protected]
*
*/
#include "fxwrapper.h"
#include "osc_api.h"
#include "patterns.h"
static __sdram uint32_t s_pattern[PATTERN_SIZE];
static uint32_t s_pattern_index;
static bool s_restart;
static float s_sample_pos;
static uint32_t s_pattern_pos;
static uint32_t s_pattern_len;
static bool s_gate_a;
static bool s_attack_a;
static bool s_release_a;
static bool s_gate_b;
static bool s_attack_b;
static bool s_release_b;
#ifdef FX_MODFX_SUB
static bool s_gate_a_sub;
static bool s_attack_a_sub;
static bool s_release_a_sub;
static bool s_gate_b_sub;
static bool s_attack_b_sub;
static bool s_release_b_sub;
#endif
void unpack() {
uint32_t pos = 0;
uint8_t val;
s_pattern_len = 0;
for (uint32_t i = s_pattern_index; i--;)
while(patterns[pos++]);
while ((val = patterns[pos])) {
for (val >>= 4; val--; s_pattern[s_pattern_len++] = 1);
for (val = patterns[pos++] & 0x0F; val--; s_pattern[s_pattern_len++] = 0);
}
}
void init() {
s_sample_pos = 0.f;
s_pattern_pos = 0;
s_gate_a = false;
s_attack_a = false;
s_release_a = false;
s_gate_b = false;
s_attack_b = false;
s_release_b = false;
#ifdef FX_MODFX_SUB
s_gate_a_sub = false;
s_attack_a_sub = false;
s_release_a_sub = false;
s_gate_b_sub = false;
s_attack_b_sub = false;
s_release_b_sub = false;
#endif
unpack();
};
FX_INIT
{
s_pattern_index = 1;
init();
}
FX_PROCESS
{
float quant = (k_samplerate * 60 / PATTERN_QUANTIZE) / fx_get_bpmf();
#ifdef FX_MODFX
f32pair_t * __restrict y = (f32pair_t*)yn;
#endif
#ifdef FX_MODFX_SUB
f32pair_t * __restrict y_sub = (f32pair_t*)sub_yn;
for (f32pair_t * __restrict x = (f32pair_t*)xn, * __restrict x_sub = (f32pair_t*)sub_xn; frames--; x++, x_sub++) {
#else
for (f32pair_t * __restrict x = (f32pair_t*)xn; frames--; x++) {
#endif
f32pair_t valp = *x;
if (si_fabsf(valp.a) < .001f) {
if (s_attack_a) {
s_gate_a = true;
s_attack_a = false;
}
if (s_release_a) {
s_gate_a = false;
s_release_a = false;
}
}
if (si_fabsf(valp.b) < .001f) {
if (s_attack_b) {
s_gate_b = true;
s_attack_b = false;
}
if (s_release_b) {
s_gate_b = false;
s_release_b = false;
}
}
if (!s_gate_a)
valp.a = 0.f;
if (!s_gate_b)
valp.b = 0.f;
#ifdef FX_MODFX
*(y++) = valp;
#else
*x = valp;
#endif
#ifdef FX_MODFX_SUB
valp = *x_sub;
if (si_fabsf(valp.a) < .001f) {
if (s_attack_a_sub) {
s_gate_a_sub = true;
s_attack_a_sub = false;
}
if (s_release_a_sub) {
s_gate_a_sub = false;
s_release_a_sub = false;
}
}
if (si_fabsf(valp.b) < .001f) {
if (s_attack_b_sub) {
s_gate_b_sub = true;
s_attack_b_sub = false;
}
if (s_release_b_sub) {
s_gate_b_sub = false;
s_release_b_sub = false;
}
}
if (!s_gate_a_sub)
valp.a = 0.f;
if (!s_gate_b_sub)
valp.b = 0.f;
*(y_sub++) = valp;
#endif
if (++s_sample_pos >= quant) {
s_sample_pos -= quant;
if (++s_pattern_pos >= s_pattern_len)
s_pattern_pos = 0;
if (s_pattern[s_pattern_pos]) {
s_attack_a = !s_gate_a;
s_attack_b = !s_gate_b;
#ifdef FX_MODFX_SUB
s_attack_a_sub = !s_gate_a_sub;
s_attack_b_sub = !s_gate_b_sub;
#endif
} else {
s_release_a = s_gate_a;
s_release_b = s_gate_b;
#ifdef FX_MODFX_SUB
s_release_a_sub = s_gate_a_sub;
s_release_b_sub = s_gate_b_sub;
#endif
}
}
}
}
FX_PARAM
{
const float valf = q31_to_f32(value);
switch (index) {
case FX_PARAM_TIME:
s_pattern_index = clipminmaxf(0.f, valf * PATTERN_COUNT, PATTERN_COUNT - 1);
unpack();
break;
case FX_PARAM_DEPTH:
if (s_restart != (valf > .5f)) {
init();
s_restart = !s_restart;
}
break;
#ifndef FX_MODFX
case FX_PARAM_SHIFT_DEPTH:
break;
#endif
default:
break;
}
}