-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
317 lines (264 loc) · 8.33 KB
/
main.cpp
File metadata and controls
317 lines (264 loc) · 8.33 KB
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
#include "IEngine.h"
#include "ModeFM.h"
#include "ModeString.h"
#include "ModeSwarm.h"
#include "ModeWavetable.h"
#include "daisy_legio.h"
#include "daisysp.h"
#include <new>
using namespace daisy;
using namespace daisysp;
DaisyLegio hw;
// Engines
ModeWavetable engine_wt;
ModeFM engine_fm;
ModeSwarm engine_swarm;
ModeString engine_string;
IEngine *engines[] = {&engine_wt, &engine_fm, &engine_swarm, &engine_string};
enum FxEngine { ENG_WT, ENG_FM, ENG_SWARM, ENG_STRING };
FxEngine current_engine = ENG_WT;
enum UiState { UI_NORMAL, UI_SECONDARY, UI_ENVELOPE };
UiState ui_state = UI_NORMAL;
// Timings
uint32_t encoder_press_start = 0;
static constexpr uint32_t kLongPressSecondary = 2000; // 2 seconds
static constexpr uint32_t kLongPressEnvelope = 4000; // 4 seconds
// Envelope ADR Parameters
float env_attack = 0.01f;
float env_decay = 0.5f;
float env_release = 0.5f;
Adsr envelope;
// Soft Knobs for Envelope to prevent jumps
IEngine::SoftKnob env_knob_decay;
IEngine::SoftKnob env_knob_release;
// 1V/Oct and Gate
float current_pitch = 440.0f;
float env_out = 0.0f;
bool last_gate_state = false;
// Master Effects
// Master Effects
ReverbSc reverb;
float reverb_send = 0.1f;
float delay_send = 0.0f;
// Stereo Delay Lines in SDRAM (64MB available)
DelayLine<float, 48000> DSY_SDRAM_BSS delay_l;
DelayLine<float, 48000> DSY_SDRAM_BSS delay_r;
// Soft Limiter / Maximizer Helper
float SoftMaximizer(float x) {
// Cubic Soft Clip suitable for maximizing loudness
// Pre-gain: Boost input level to drive the limiter
x *= 2.0f;
if (x < -1.0f)
return -0.66f; // Hard limit floor (approx 2/3)
if (x > 1.0f)
return 0.66f; // Hard limit ceiling
return x - (x * x * x) / 3.0f;
}
void AudioCallback(AudioHandle::InputBuffer in, AudioHandle::OutputBuffer out,
size_t size) {
hw.ProcessAnalogControls();
// 1. INPUTS MAPPING (GLOBAL)
float pitch_knob = hw.GetKnobValue(DaisyLegio::CONTROL_PITCH);
float midi_note = 36.0f + (pitch_knob * 60.0f);
current_pitch = mtof(midi_note);
// Gate handling
bool gate_state = hw.Gate();
if (gate_state && !last_gate_state) {
envelope.Retrigger(false);
engine_string.Trigger();
}
last_gate_state = gate_state;
for (int i = 0; i < 4; i++) {
engines[i]->SetFreq(current_pitch);
}
// Get Delay Amount
float delay_send = engines[current_engine]->GetDelayAmount();
for (size_t i = 0; i < size; i++) {
float sig_l = 0.0f, sig_r = 0.0f;
env_out = envelope.Process(gate_state);
engines[current_engine]->Process(0, 0, &sig_l, &sig_r);
sig_l *= env_out;
sig_r *= env_out;
// --- REVERB ---
float rev_l, rev_r;
reverb.Process(sig_l * reverb_send, sig_r * reverb_send, &rev_l, &rev_r);
// --- DELAY ---
float del_out_l = delay_l.Read();
float del_out_r = delay_r.Read();
// Feedback Loop (0.4f = 40% Feedback)
delay_l.Write((sig_l * delay_send) + (del_out_l * 0.4f));
delay_r.Write((sig_r * delay_send) + (del_out_r * 0.4f));
// Sum and Apply Soft Limiter (Maximizer)
float final_l = sig_l + rev_l + del_out_l;
float final_r = sig_r + rev_r + del_out_r;
out[0][i] = SoftMaximizer(final_l);
out[1][i] = SoftMaximizer(final_r);
}
}
int main(void) {
hw.Init();
hw.StartAdc();
float sample_rate = hw.AudioSampleRate();
// Init Engines
for (int i = 0; i < 4; i++) {
engines[i]->Init(sample_rate);
}
// Init Envelope (ADR mode using ADSR with Sustain=0)
envelope.Init(sample_rate);
envelope.SetSustainLevel(0.0f);
env_knob_decay.Init(0.5f);
env_knob_release.Init(0.5f);
// Init Effects
reverb.Init(sample_rate);
reverb.SetFeedback(0.85f);
reverb.SetLpFreq(12000.0f);
// Init Delay
delay_l.Init();
delay_r.Init();
delay_l.SetDelay(24000.0f); // 0.5s Left
delay_r.SetDelay(36000.0f); // 0.75s Right
hw.StartAudio(AudioCallback);
uint32_t led_tick = 0;
uint32_t encoder_press_start = 0;
float p1_val = 0.5f;
const uint32_t kHoldEnvelope = 1000;
const uint32_t kHoldSecondary = 2500;
// LED blink state for slow/fast feedback
bool blink_state = false;
while (1) {
hw.ProcessDigitalControls();
led_tick = System::GetNow();
// Global Blink Timer (Fast: 100ms, Slow: 500ms approx)
blink_state = (led_tick / 150) % 2;
// 1. Encoder Handling (Press)
if (hw.encoder.Pressed()) {
if (encoder_press_start == 0) {
encoder_press_start = led_tick;
}
// If holding, we just wait. Feedback is handled in LED section.
} else {
// Release Event
if (encoder_press_start > 0) {
uint32_t hold_time = led_tick - encoder_press_start;
encoder_press_start = 0;
if (hold_time < kHoldEnvelope) {
// SHORT PRESS: Back to Normal OR Next Engine
if (ui_state != UI_NORMAL) {
ui_state = UI_NORMAL;
} else {
current_engine = (FxEngine)((current_engine + 1) % 4);
}
} else if (hold_time < kHoldSecondary) {
// MEDIUM PRESS: Envelope Mode
ui_state = UI_ENVELOPE;
} else {
// LONG PRESS: Secondary Mode
ui_state = UI_SECONDARY;
}
}
}
// --- CONTROL UPDATE ---
float k1 = hw.GetKnobValue(DaisyLegio::CONTROL_KNOB_TOP);
float k2 = hw.GetKnobValue(DaisyLegio::CONTROL_KNOB_BOTTOM);
int s1 = hw.sw[0].Read();
int s2 = hw.sw[1].Read();
// P1 (Encoder): Standard relative increment
float enc_inc = (float)hw.encoder.Increment() * 0.05f;
p1_val += enc_inc;
if (p1_val < 0.0f)
p1_val = 0.0f;
if (p1_val > 1.0f)
p1_val = 1.0f;
// --- ENGINE / PARAMETER UPDATE ---
if (ui_state == UI_ENVELOPE) {
// ENVELOPE MODE: Encoder=Attack, Top=Decay, Bot=Release
// Use P1 (Accumulated) for Attack
env_attack = fmap(p1_val, 0.001f, 2.0f, Mapping::LOG);
float raw_dec = env_knob_decay.Process(k1);
env_decay = fmap(raw_dec, 0.01f, 5.0f, Mapping::LOG);
float raw_rel = env_knob_release.Process(k2);
env_release = fmap(raw_rel, 0.01f, 5.0f, Mapping::LOG);
envelope.SetAttackTime(env_attack);
envelope.SetDecayTime(env_decay);
envelope.SetReleaseTime(env_release);
} else {
// Unlatch Envelope Knobs so they are ready when we return
env_knob_decay.Unlatch();
env_knob_release.Unlatch();
// NORMAL OR SECONDARY MODE
bool is_secondary = (ui_state == UI_SECONDARY);
// Pass relative increment to Engine for Parameter accumulation
engines[current_engine]->UpdateControls(enc_inc, k1, k2, s1, s2,
is_secondary);
}
// Update Global Reverb AND Delay Send based on Engine State
reverb_send = engines[current_engine]->GetReverbAmount();
delay_send = engines[current_engine]->GetDelayAmount();
// --- LED LOGIC ---
float r = 0, g = 0, b = 0;
// Determine Base Color based on Engine
float er = 0, eg = 0, eb = 0;
switch (current_engine) {
case ENG_WT:
er = 1.0f;
break; // Red
case ENG_FM:
eg = 1.0f;
break; // Green
case ENG_SWARM:
eb = 1.0f;
break; // Blue
case ENG_STRING:
er = eg = eb = 1.0f;
break; // White
}
if (encoder_press_start > 0) {
// BUTTON HELD FEEDBACK
uint32_t hold_time = led_tick - encoder_press_start;
if (hold_time > kHoldSecondary) {
// Fast Blink Engine Color (Ready for Secondary)
if ((led_tick % 150) < 75) {
r = er;
g = eg;
b = eb;
}
} else if (hold_time > kHoldEnvelope) {
// Fast Blink Yellow (Ready for Envelope)
if ((led_tick % 150) < 75) {
r = 1.0f;
g = 1.0f;
b = 0.0f;
}
} else {
// Steady Engine Color (Short Press Range)
r = er;
g = eg;
b = eb;
}
} else {
// STEADY STATE DISPLAY
if (ui_state == UI_ENVELOPE) {
// Steady Yellow
r = 1.0f;
g = 1.0f;
b = 0.0f;
} else if (ui_state == UI_SECONDARY) {
// Slow Blink Engine Color
if ((led_tick % 600) < 300) {
r = er;
g = eg;
b = eb;
}
} else {
// Normal: Steady Engine Color
r = er;
g = eg;
b = eb;
}
}
hw.SetLed(DaisyLegio::LED_LEFT, r, g, b);
hw.SetLed(DaisyLegio::LED_RIGHT, r, g, b);
hw.UpdateLeds();
System::Delay(1);
}
}