forked from joeSeggiola/eurorack
/
stages.cc
executable file
·400 lines (344 loc) · 11.9 KB
/
stages.cc
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2017 Emilie Gillet.
//
// Author: Emilie Gillet (emilie.o.gillet@gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
#include <stm32f37x_conf.h>
#include "stmlib/dsp/dsp.h"
#include "stmlib/dsp/dsp.h"
#include "stmlib/dsp/units.h"
#include "stages/chain_state.h"
#include "stages/drivers/dac.h"
#include "stages/drivers/gate_inputs.h"
#include "stages/drivers/leds.h"
#include "stages/drivers/serial_link.h"
#include "stages/drivers/system.h"
#include "stages/cv_reader.h"
#include "stages/factory_test.h"
#include "stages/io_buffer.h"
#include "stages/oscillator.h"
#include "stages/resources.h"
#include "stages/envelope.h"
#include "stages/segment_generator.h"
#include "stages/settings.h"
#include "stages/ui.h"
using namespace stages;
using namespace std;
using namespace stmlib;
const bool skip_factory_test = false;
const bool test_adc_noise = false;
ChainState chain_state;
CvReader cv_reader;
Dac dac;
FactoryTest factory_test;
GateFlags no_gate[kBlockSize];
GateInputs gate_inputs;
SegmentGenerator segment_generator[kNumChannels];
Oscillator oscillator[kNumChannels];
IOBuffer io_buffer;
Envelope eg[kNumChannels];
SerialLink left_link;
SerialLink right_link;
Settings settings;
Ui ui;
// Default interrupt handlers.
extern "C" {
void NMI_Handler() { }
void HardFault_Handler() { while (1); }
void MemManage_Handler() { while (1); }
void BusFault_Handler() { while (1); }
void UsageFault_Handler() { while (1); }
void SVC_Handler() { }
void DebugMon_Handler() { }
void PendSV_Handler() { }
}
// SysTick and 32kHz handles
extern "C" {
void SysTick_Handler() {
IWDG_ReloadCounter();
ui.Poll();
if (!skip_factory_test) {
factory_test.Poll();
}
}
}
IOBuffer::Slice FillBuffer(size_t size) {
IOBuffer::Slice s = io_buffer.NextSlice(size);
gate_inputs.Read(s, size);
if (io_buffer.new_block()) {
cv_reader.Read(s.block);
gate_inputs.ReadNormalization(s.block);
}
return s;
}
SegmentGenerator::Output out[kBlockSize];
static float note_lp[kNumChannels] = { 0, 0, 0, 0, 0, 0 };
void Process(IOBuffer::Block* block, size_t size) {
chain_state.Update(
*block,
&settings,
&segment_generator[0],
out);
for (size_t channel = 0; channel < kNumChannels; ++channel) {
bool led_state = segment_generator[channel].Process(
block->input_patched[channel] ? block->input[channel] : no_gate,
out,
size);
ui.set_slider_led(channel, led_state, 5);
if (test_adc_noise) {
float note = block->cv_slider[channel];
ONE_POLE(note_lp[channel], note, 0.0001f);
float cents = (note - note_lp[channel]) * 1200.0f * 0.5f;
CONSTRAIN(cents, -1.0f, +1.0f)
for (size_t i = 0; i < size; ++i) {
out[i].value = cents;
}
}
for (size_t i = 0; i < size; ++i) {
block->output[channel][i] = settings.dac_code(channel, out[i].value);
}
}
}
void ProcessSixEg(IOBuffer::Block* block, size_t size) {
// Slider LEDs
ui.set_slider_led(0, eg[0].HasDelay (), 1);
ui.set_slider_led(1, eg[0].HasAttack (), 1);
ui.set_slider_led(2, eg[0].HasHold (), 1);
ui.set_slider_led(3, eg[0].HasDecay (), 1);
ui.set_slider_led(4, eg[0].HasSustain(), 1);
ui.set_slider_led(5, eg[0].HasRelease(), 1);
// Wait 1sec at boot before checking gates
static int egGateWarmTime = 4000;
if (egGateWarmTime > 0) egGateWarmTime--;
for (size_t ch = 0; ch < kNumChannels; ch++) {
// Set pots params
eg[ch].SetAttackCurve (block->pot[1]);
eg[ch].SetDecayCurve (block->pot[3]);
eg[ch].SetReleaseCurve(block->pot[5]);
// Set slider params
eg[ch].SetDelayLength (block->cv_slider[0]);
eg[ch].SetAttackLength (block->cv_slider[1]);
eg[ch].SetHoldLength (block->cv_slider[2]);
eg[ch].SetDecayLength (block->cv_slider[3]);
eg[ch].SetSustainLevel (block->cv_slider[4]);
eg[ch].SetReleaseLength(block->cv_slider[5]);
// Gate or button?
bool gate = ui.switches().pressed(ch);
if (!gate && egGateWarmTime == 0 && block->input_patched[ch]) {
for (size_t i = 0; i < size; i++) {
if (block->input[ch][i] & GATE_FLAG_HIGH) {
gate = true;
break;
}
}
}
eg[ch].Gate(gate);
ui.set_led(ch, gate ? LED_COLOR_RED : LED_COLOR_OFF);
// Compute value and set as output
float value = eg[ch].Value();
for (size_t i = 0; i < size; i++) {
block->output[ch][i] = settings.dac_code(ch, value);
}
// Display current stage
switch (eg[ch].CurrentStage()) {
case DELAY:
case ATTACK:
case HOLD:
case DECAY:
ui.set_led(ch, LED_COLOR_GREEN);
break;
case SUSTAIN:
ui.set_led(ch, LED_COLOR_YELLOW);
break;
case RELEASE:
ui.set_led(ch, LED_COLOR_RED);
break;
default:
ui.set_led(ch, LED_COLOR_OFF);
break;
}
}
}
float ouroboros_ratios[] = {
0.25f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 8.0f, 8.0f
};
float this_channel[kBlockSize];
float sum[kBlockSize];
float channel_amplitude[kNumChannels];
float previous_amplitude[kNumChannels];
void ProcessOuroboros(IOBuffer::Block* block, size_t size) {
const float coarse = (block->cv_slider[0] - 0.5f) * 96.0f;
const float fine = block->pot[0] * 2.0f - 1.0f;
const uint8_t range = (settings.state().segment_configuration[0] >> 10) & 0x3;
const float range_mult =
(range == 0x01) ? 1.0f / 128.0f :
(range == 0x02) ? 1.0f / (128.0f * 16.0f) :
1.0f;
const bool lfo = range != 0x00;
const float f0 = SemitonesToRatio(coarse + fine) * 261.6255f / kSampleRate * range_mult;
std::fill(&sum[0], &sum[size], 0.0f);
bool alternate = (MultiMode) settings.state().multimode == MULTI_MODE_OUROBOROS_ALTERNATE;
float *blockHarmonic = alternate ? block->cv_slider : block->pot;
float *blockAmplitude = alternate ? block->pot : block->cv_slider;
for (int channel = kNumChannels - 1; channel >= 0; --channel) {
const float harmonic = blockHarmonic[channel] * 9.999f;
MAKE_INTEGRAL_FRACTIONAL(harmonic);
harmonic_fractional = 8.0f * (harmonic_fractional - 0.5f) + 0.5f;
CONSTRAIN(harmonic_fractional, 0.0f, 1.0f);
const float ratio = channel == 0 ? 1.0f : Crossfade(
ouroboros_ratios[harmonic_integral],
ouroboros_ratios[harmonic_integral + 1],
harmonic_fractional);
const float amplitude = channel == 0
? 1.0f
: std::max(blockAmplitude[channel], 0.0f);
bool trigger = false;
for (size_t i = 0; i < size; ++i) {
trigger = trigger || (block->input[channel][i] & GATE_FLAG_RISING);
}
if (trigger || !block->input_patched[channel]) {
channel_amplitude[channel] = 1.0f;
} else {
channel_amplitude[channel] *= 0.999f;
}
// For some reason, trigger can be true when not input is patched.
if (block->input_patched[channel] && trigger && lfo) {
oscillator[channel].Init();
}
ui.set_slider_led(
channel, channel_amplitude[channel] * amplitude > 0.001f, 1);
const float f = f0 * ratio;
uint8_t waveshape = (settings.state().segment_configuration[channel] & 0b01110000) >> 4;
switch (waveshape) {
case 0:
oscillator[channel].Render<OSCILLATOR_SHAPE_SINE>(
f, 0.5f, this_channel, size);
break;
case 1:
oscillator[channel].Render<OSCILLATOR_SHAPE_TRIANGLE>(
f, 0.5f, this_channel, size);
break;
case 2:
case 3:
oscillator[channel].Render<OSCILLATOR_SHAPE_SQUARE>(
f, 0.5f, this_channel, size);
break;
case 4:
oscillator[channel].Render<OSCILLATOR_SHAPE_SAW>(
f, 0.5f, this_channel, size);
break;
case 5:
oscillator[channel].Render<OSCILLATOR_SHAPE_SQUARE>(
f, 0.75f, this_channel, size);
break;
case 6:
case 7:
oscillator[channel].Render<OSCILLATOR_SHAPE_SQUARE>(
f, 0.9f, this_channel, size);
break;
}
ParameterInterpolator am(
&previous_amplitude[channel],
amplitude * amplitude * channel_amplitude[channel],
size);
for (size_t i = 0; i < size; ++i) {
sum[i] += this_channel[i] * am.Next();
}
const float gain = channel == 0 ? 0.2f : 0.66f;
// Don't bother interpolating over lfo amplitude as we don't apply pinging to the single LFO outs
const float lfo_amp = lfo ? amplitude : 1.0f;
const float* source = channel == 0 ? sum : this_channel;
for (size_t i = 0; i < size; ++i) {
block->output[channel][i] = settings.dac_code(channel, source[i] * gain * lfo_amp);
}
}
}
void ProcessTest(IOBuffer::Block* block, size_t size) {
for (size_t channel = 0; channel < kNumChannels; channel++) {
// Pot position affects LED color
const float pot = block->pot[channel];
ui.set_led(channel, pot > 0.5f ? LED_COLOR_GREEN : LED_COLOR_OFF);
// Gete input and button turn the LED red
bool gate = false;
bool button = ui.switches().pressed(channel);
if (block->input_patched[channel]) {
for (size_t i = 0; i < size; i++) {
gate = gate || (block->input[channel][i] & GATE_FLAG_HIGH);
}
}
if (gate || button) {
ui.set_led(channel, LED_COLOR_RED);
}
// Slider position (summed with input CV) affects output value
const float output = (gate || button) ? 1.0f : block->cv_slider[channel];
ui.set_slider_led(channel, output > 0.001f, 1);
for (size_t i = 0; i < size; i++) {
block->output[channel][i] = settings.dac_code(channel, output);
}
}
}
void Init() {
System sys;
sys.Init(true);
dac.Init(int(kSampleRate), 2);
gate_inputs.Init();
io_buffer.Init();
bool freshly_baked = !settings.Init();
for (size_t i = 0; i < kNumChannels; ++i) {
segment_generator[i].Init(&settings);
eg[i].Init();
oscillator[i].Init();
}
std::fill(&no_gate[0], &no_gate[kBlockSize], GATE_FLAG_LOW);
cv_reader.Init(&settings, &chain_state);
ui.Init(&settings, &chain_state, &cv_reader);
if (freshly_baked && !skip_factory_test) {
factory_test.Start(&settings, &cv_reader, &gate_inputs, &ui);
ui.set_factory_test(true);
} else {
chain_state.Init(&left_link, &right_link);
}
sys.StartTimers();
dac.Start(&FillBuffer);
}
int main(void) {
Init();
while (1) {
if (factory_test.running()) {
io_buffer.Process(&FactoryTest::ProcessFn);
} else if (chain_state.discovering_neighbors()) {
io_buffer.Process(&Process); // Still discovering neighbors, dont't process alternative multi-modes
} else {
switch ((MultiMode) settings.state().multimode) {
case MULTI_MODE_SIX_EG:
io_buffer.Process(&ProcessSixEg);
break;
case MULTI_MODE_OUROBOROS:
case MULTI_MODE_OUROBOROS_ALTERNATE:
io_buffer.Process(&ProcessOuroboros);
break;
default:
io_buffer.Process(&Process);
break;
}
}
}
}