-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParametricChorus.cpp
284 lines (244 loc) · 7.34 KB
/
ParametricChorus.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
273
274
275
276
277
278
279
280
281
282
283
284
#include "daisy_pod.h"
#include "daisysp.h"
#include "hilbert_pdn.h"
#include "iir_bp_fb.h"
#include "iir_lp_nf.h"
#include "table_osc.h"
#include "lfsr.h"
using namespace daisy;
using namespace daisysp;
//Hard coded sample rate
#define SAMPLE_RATE 48000
//ms of delay
#define NOM_DELAY (48 * 40)
#define MAX_MOD_DELAY NOM_DELAY
#define DELAY_UPDATE_FREQ 10.0
#define DELAY_UPDATE_SMPL (SAMPLE_RATE / DELAY_UPDATE_FREQ)
#define DEL_OP_COEF (1.0 / DELAY_UPDATE_SMPL)
#define MAX_DELAY (2 * NOM_DELAY)
#define DOSC_MAX 160
#define TABLE_FUNDAMENTAL 220
#define FIRST_BW 40
#define MAX_PS 24
#define SEMITONE 0.08334f
#define CATCH_THRESH 0.05f
DaisyPod hw;
// Filter bank of band pass filters
iir_bp_fb fb;
// Phase difference network per band
HilbertPDN pdn[BANDS];
// LUT oscs
TableOsc osc[BANDS];
TableOsc dosc[BANDS];
// LFSR noise sources
LFSR plfsr[BANDS];
LFSR dlfsr[BANDS];
// noise filtering
iir_lp_nf pnoise_filt[BANDS];
int delnoise_cnt = 0;
float cur_del[BANDS];
float target_del[BANDS];
static DelayLine<float, MAX_DELAY> DSY_SDRAM_BSS del[BANDS];
static Parameter delnoise, delfreq, deldepth, pnshift, pshift, bwmult, mix, spread;
float cur_delnoise, cur_delfreq, cur_deldepth, cur_pn, cur_ps, cur_bwm, cur_mix, cur_spread;
bool ps_noise_mode = true;
bool del_noise_mode = true;
typedef enum { MIX = 0, PHASE, DEL, LAST } page;
page cur_page;
#define BLINK_NUM 500
int blink_cnt = 0;
bool led_state = true;
void UpdateEncoder();
void UpdateButtons();
float CatchParam(float old, float cur, float thresh);
void AudioCallback(AudioHandle::InputBuffer in, AudioHandle::OutputBuffer out, size_t size)
{
float bw;
float oscf, band_freq;
bool update_delay = false;
delnoise_cnt += size;
if (delnoise_cnt > DELAY_UPDATE_SMPL) {
update_delay = true;
delnoise_cnt = 0;
}
// single channel in - break it down to bands
fb.Process(&in[0][0]);
// Pass it through the Hilbert transform and calculate per band modulation
bw = FIRST_BW;
for (size_t i = 0; i < BANDS; i++)
{
pdn[i].Process(&fb.out[i][0]);
if (ps_noise_mode) {
float pnoise = pnoise_filt[i].Process(plfsr[i].Process());
pnoise = (pnoise > 1.0) ? 1.0 : (pnoise < -1.0) ? -1.0 : pnoise;
oscf = (cur_pn * MAX_PS * SEMITONE) * pnoise * bw;
} else {
oscf = (cur_ps * MAX_PS * SEMITONE) * bw;
}
band_freq = (FIRST_BW << i);
oscf = ((band_freq + oscf) > (SAMPLE_RATE / 2)) ? ((SAMPLE_RATE / 2) - band_freq) : ((band_freq - oscf) < FIRST_BW) ? FIRST_BW : oscf;
osc[i].SetFreq(oscf);
if (del_noise_mode) {
if (update_delay) {
float tmp_dnoise = dlfsr[i].Process();
target_del[i] = NOM_DELAY + (cur_delnoise * tmp_dnoise * MAX_MOD_DELAY);
}
} else {
dosc[i].SetFreq(cur_delfreq * DOSC_MAX);
}
bw *= cur_bwm;
}
update_delay = false;
// apply modulation to PDN output per sample per band
for (size_t i = 0; i < size; i++)
{
out[0][i] = 0;
out[1][i] = 0;
float cur_sample_l = 0;
float cur_sample_r = 0;
float tmp_sample_l = 0;
float tmp_sample_r = 0;
bool even = true;
for (size_t j = 0; j < BANDS; j++)
{
osc[j].Process();
dosc[j].Process();
if (!del_noise_mode) {
float tmp_dnoise = dosc[j].InPhase(0);
cur_del[j] = NOM_DELAY + (tmp_dnoise * cur_deldepth * MAX_MOD_DELAY);
} else {
fonepole(cur_del[j], target_del[j], DEL_OP_COEF);
}
float fs = del[j].Read(cur_del[j]);
del[j].Write(((pdn[j].r_out[i] * osc[j].InPhase(0)) \
+ (pdn[j].i_out[i] * osc[j].QuadPhase(0))));
if (even) {
cur_sample_l += fs;
} else {
cur_sample_r += fs;
}
even = !even;
}
// spread and mix
tmp_sample_l = (1 + cur_spread) * cur_sample_l + (1 - cur_spread) * cur_sample_r;
tmp_sample_r = (1 + cur_spread) * cur_sample_r + (1 - cur_spread) * cur_sample_l;
out[0][i] = (tmp_sample_l * cur_mix) + ((1 - cur_mix) * in[0][i]);
out[1][i] = (tmp_sample_r * cur_mix) + ((1 - cur_mix) * in[0][i]);
}
blink_cnt++;
}
void UpdateEncoder()
{
cur_page = (page)(cur_page + hw.encoder.Increment());
if (cur_page >= LAST) { cur_page = MIX; }
switch(cur_page)
{
case MIX:
cur_mix = CatchParam(cur_mix, mix.Process(), CATCH_THRESH);
cur_spread = CatchParam(cur_spread, spread.Process(), CATCH_THRESH);
hw.led1.Set(1.0f,0,0);
break;
case PHASE:
if (ps_noise_mode) {
cur_pn = CatchParam(cur_pn, pnshift.Process(), CATCH_THRESH);
} else {
cur_ps = CatchParam(cur_ps, pshift.Process(), CATCH_THRESH);
}
cur_bwm = CatchParam(cur_bwm, bwmult.Process(), CATCH_THRESH);
hw.led1.Set(0,1.0f,0);
break;
case DEL:
if (del_noise_mode) {
cur_delnoise = CatchParam(cur_delnoise, delnoise.Process(), CATCH_THRESH);
} else {
cur_delfreq = CatchParam(cur_delfreq, delfreq.Process(), CATCH_THRESH);
cur_deldepth = CatchParam(cur_deldepth, deldepth.Process(), CATCH_THRESH);
}
hw.led1.Set(0,0,1.0f);
break;
case LAST:
default:
break;
}
}
void UpdateButtons()
{
if(hw.button1.RisingEdge())
ps_noise_mode = !ps_noise_mode;
if(hw.button2.RisingEdge())
del_noise_mode = !del_noise_mode;
hw.led2.Set(ps_noise_mode, 0, del_noise_mode);
}
float CatchParam(float old, float cur, float thresh)
{
return (abs(old - cur) < thresh) ? cur : old;
}
int main(void)
{
float sr;
float bw;
// make sure each lfsr starts on something different
uint32_t plfsr_init[BANDS] = {0xA5A5A5A5, 0x5A5A5A5A, 0xA050A050, 0x0A050A05, 0x50A050A0, 0x050A050A, 0x5555AAAA, 0xAAAA5555 };
uint32_t dlfsr_init[BANDS] = {0xC7C7C7C7, 0x7C7C7C7C, 0xC070C070, 0x0C070C07, 0x70C070C0, 0x070C070C, 0x7777CCCC, 0xCCCC7777 };
hw.Init();
sr = hw.AudioSampleRate();
// set up sine LUT
table_t t;
t.l = floor(sr / TABLE_FUNDAMENTAL);
t.t = (float *)malloc(t.l * sizeof(float));
for (size_t i = 0; i < t.l ; i++) {
float r = (2 * M_PI) * (i / (1.0 * t.l));
t.t[i] = sin(r);
}
// Init
bw = FIRST_BW;
for (size_t i = 0; i < BANDS; i++)
{
osc[i].Init(sr, &t, 0.0f);
osc[i].SetFreq(bw / 6);
dosc[i].Init(sr, &t, (float)dlfsr_init[i]);
dosc[i].SetFreq(5);
bw = bw * 2;
plfsr[i].Init(plfsr_init[i]);
dlfsr[i].Init(dlfsr_init[i]);
/*
* Coefs for a single pole lp filter generated by octave iir_lp.m
* fs = 48e3
* fc = 10
* some gain added to have an output in the range [-1:1]
* (output may actually be higher than that...)
*/
pnoise_filt[i].Init(0.054699, 0.054699, -0.99869);
del[i].Init();
}
//set parameters
delnoise.Init(hw.knob1, 0, 1, delnoise.EXPONENTIAL);
delfreq.Init( hw.knob1, 0, 1, delfreq.EXPONENTIAL);
deldepth.Init(hw.knob2, 0, 1, deldepth.LINEAR);
pshift.Init( hw.knob1, -1, 1, pshift.LINEAR);
pnshift.Init( hw.knob1, 0, 1, pnshift.LINEAR);
bwmult.Init( hw.knob2, 0, 2.1, bwmult.LINEAR);
mix.Init( hw.knob1, 0, 1, mix.LINEAR);
spread.Init( hw.knob2, 0, 1, mix.LINEAR);
// defaults
cur_page = MIX;
cur_delnoise = cur_delfreq = cur_deldepth = cur_pn = 0;
cur_mix = cur_spread = 0.5;
cur_ps = 0.3;
cur_bwm = 2.0;
// GO!
hw.StartAdc();
hw.StartAudio(AudioCallback);
while(1) {
hw.ProcessDigitalControls();
UpdateEncoder();
UpdateButtons();
hw.UpdateLeds();
if (blink_cnt >= BLINK_NUM) {
hw.seed.SetLed(led_state);
led_state = !led_state;
blink_cnt = 0;
}
hw.DelayMs(1);
}
}