Repository navigation
Expand file tree
/
Copy pathnuut.cpp
More file actions
698 lines (603 loc) · 22.6 KB
/
Copy pathnuut.cpp
File metadata and controls
698 lines (603 loc) · 22.6 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#include "daisy_seed.h"
#include <cmath>
#include "daisysp.h"
#include "Utility/delayline.h"
#include "touch/pads.h"
#include "touch/knobs.h"
#include "touch/switches.h"
#include "nuut/voice.h"
#include "nuut/planets.h"
#include "nuut/constellation.h"
#include "nuut/aspects/aspects_engine.h"
#include "nuut/crystals.h"
using namespace daisy;
using namespace daisysp;
DaisySeed hw;
CpuLoadMeter cpuMeter;
Voice voices[14];
NUUTEventVoice eventVoice;
bool eventBellTriggerPending = false;
float eventBellFrequency = 432.0f;
// Shared state for the Comb LP6 breath modulation.
CombLP6BreathState combLP6Breath;
// Global stereo delay used for the main reverb.
DSY_SDRAM_BSS daisysp::ReverbSc reverb;
Pads pads;
Knobs knobs;
Switches switches;
Constellation constellation;
Transit transit;
Crystals crystals;
// ============================================================
// GLOBAL CONTROL STATE
// ============================================================
// Tracks which of the ten planetary pads are currently active.
bool padStates[10] = {false};
// When Hold is enabled, a selected planet remains active
// after its touch pad is released.
bool holdEnabled = false;
float transitPosition = 0.0f;
constexpr int MELODY_LENGTH = 3;
float melodyNotes[MELODY_LENGTH] = {};
int melodyStep = 0;
int melodyLength = 3;
int melodyLastAspect = ASPECT_NONE;
int melodyLastA = -1;
int melodyLastB = -1;
size_t melodySamplesUntilNext = 0;
float melodyCurrentFrequency = 432.0f;
bool melodyActive = false;
int melodyTrillTogglesRemaining = 0;
bool melodyTrillHigh = false;
float melodyTrillBaseFrequency = 432.0f;
bool AnyPadActive();
// ============================================================
// PAD TOUCH CALLBACK
// Handles planet selection and Hold mode.
// ============================================================
void OnPadTouch(uint16_t pad)
{
// Pad 11 toggles Hold mode.
if(pad == 11)
{
holdEnabled = !holdEnabled;
if(holdEnabled)
{
// Indicate that Hold mode is enabled.
hw.SetLed(true);
}
else
{
// Disabling Hold releases every selected planet.
for(uint16_t i = 0; i < 10; i++)
{
padStates[i] = false;
constellation.ReleasePlanet(i);
}
hw.SetLed(false);
}
return;
}
// Pads 0–9 select planets.
if(pad < 10)
{
if(holdEnabled)
{
// In Hold mode, touching a new pad replaces
// the previously selected planet.
for(uint16_t i = 0; i < 10; i++)
{
padStates[i] = false;
constellation.ReleasePlanet(i);
}
}
padStates[pad] = true;
hw.SetLed(true);
// Update the main voices and their constellation.
constellation.AssignPlanet(pad);
}
}
// ============================================================
// PAD RELEASE CALLBACK
// Releases a planet unless Hold mode is active.
// ============================================================
void OnPadRelease(uint16_t pad)
{
if(pad < 10)
{
if(holdEnabled)
{
// Hold mode keeps the selected planet active.
return;
}
padStates[pad] = false;
constellation.ReleasePlanet(pad);
// The LED remains on while any planet is active.
hw.SetLed(AnyPadActive());
}
}
// ============================================================
// ACTIVE PAD CHECK
// Returns true if at least one planetary pad is active.
// ============================================================
bool AnyPadActive()
{
for(uint16_t i = 0; i < 10; i++)
{
if(padStates[i])
return true;
}
return false;
}
float MelodyMidiToFrequency(int midiNote)
{
return 432.0f * powf(
2.0f,
(static_cast<float>(midiNote) - 69.0f) / 12.0f
);
}
// ============================================================
// AUDIO CALLBACK
// Processes controls, voices, transit aspects, reverb,
// crystal effects and stereo output.
// ============================================================
void AudioCallback(
AudioHandle::InputBuffer in,
AudioHandle::OutputBuffer out,
size_t size)
{
cpuMeter.OnBlockStart();
// --------------------------------------------------------
// PRIMARY CONTROLS
// --------------------------------------------------------
float originValue = knobs.s31().Process();
float arcValue = knobs.s33().Process();
// --------------------------------------------------------
// TRANSIT CONTROL
// The center switch position provides manual control.
// The other positions enable automatic orbital movement.
// --------------------------------------------------------
float transitKnob = knobs.s36().Process();
int transitMode = switches.B();
if(transitMode == Switch3::POS_CENTER)
{
// Manual mode: the knob directly sets transit position.
transitPosition = transitKnob;
}
else
{
// Automatic mode starts from the current position.
float speed = 0.00005f;
if(transitMode == Switch3::POS_DOWN)
speed = 0.00015f;
transitPosition += speed;
// Wrap around when the normalized cycle is complete.
if(transitPosition > 1.0f)
transitPosition -= 1.0f;
}
transit.SetPosition(transitPosition);
// --------------------------------------------------------
// CONSTELLATION CONTROL
// --------------------------------------------------------
float constellationValue = knobs.s30().Process();
constellation.SetConstellation(constellationValue);
// --------------------------------------------------------
// ASPECT DETECTION
// Transit finds the first active planetary aspect.
// The aspect engine converts it into sound parameters.
// --------------------------------------------------------
int aspectA = -1;
int aspectB = -1;
float aspectDistance = 0.0f;
int currentAspect = transit.FindAspect(
aspectA,
aspectB,
aspectDistance
);
float aspectProximity = 0.0f;
if(currentAspect != ASPECT_NONE)
{
aspectProximity = transit.GetAspectProximity(
aspectDistance,
currentAspect
);
}
AspectData aspect = BuildAspectData(
currentAspect,
aspectProximity
);
// --------------------------------------------------------
// MELODY GENERATION
// Build a note phrase when the detected aspect changes.
// --------------------------------------------------------
if(currentAspect != ASPECT_NONE && aspectA >= 0 && aspectB >= 0)
{
float melodyFrequencyA = planets[aspectA].frequency;
float melodyFrequencyB = planets[aspectB].frequency;
// Keep both planetary pitches in a useful register.
while(melodyFrequencyA < 220.0f)
melodyFrequencyA *= 2.0f;
while(melodyFrequencyA > 880.0f)
melodyFrequencyA *= 0.5f;
while(melodyFrequencyB < 220.0f)
melodyFrequencyB *= 2.0f;
while(melodyFrequencyB > 880.0f)
melodyFrequencyB *= 0.5f;
if(!melodyActive ||
currentAspect != melodyLastAspect ||
aspectA != melodyLastA ||
aspectB != melodyLastB)
{
// Build a three-note arpeggio using the generator planet as root.
// Preserve the chord intervals when transposing to the planet frequency.
const float rootFrequency = melodyFrequencyA;
switch(currentAspect)
{
case ASPECT_CONJUNCTION: // G-C-E: 0, +5, +9
melodyNotes[0] = rootFrequency;
melodyNotes[1] = rootFrequency * powf(2.0f, 5.0f / 12.0f);
melodyNotes[2] = rootFrequency * powf(2.0f, 9.0f / 12.0f);
melodyLength = 3;
break;
case ASPECT_SEXTILE: // G-C-F: 0, +5, +10
melodyNotes[0] = rootFrequency;
melodyNotes[1] = rootFrequency * powf(2.0f, 5.0f / 12.0f);
melodyNotes[2] = rootFrequency * powf(2.0f, 10.0f / 12.0f);
melodyLength = 3;
break;
case ASPECT_TRINE: // A-C#-E: 0, +4, +7
melodyNotes[0] = rootFrequency;
melodyNotes[1] = rootFrequency * powf(2.0f, 4.0f / 12.0f);
melodyNotes[2] = rootFrequency * powf(2.0f, 7.0f / 12.0f);
melodyLength = 3;
break;
case ASPECT_OPPOSITION: // Bb-C#-E: 0, +4, +7
melodyNotes[0] = rootFrequency;
melodyNotes[1] = rootFrequency * powf(2.0f, 4.0f / 12.0f);
melodyNotes[2] = rootFrequency * powf(2.0f, 7.0f / 12.0f);
melodyLength = 3;
break;
case ASPECT_SQUARE: // A-C#-F: 0, +4, +9
melodyNotes[0] = rootFrequency;
melodyNotes[1] = rootFrequency * powf(2.0f, 4.0f / 12.0f);
melodyNotes[2] = rootFrequency * powf(2.0f, 9.0f / 12.0f);
melodyLength = 3;
break;
default:
melodyLength = 0;
break;
}
if(melodyLength > 0)
{
// Start with the first chord note active.
melodyStep = 1;
melodyCurrentFrequency = melodyNotes[0];
melodySamplesUntilNext = 24000;
// Select the event timbre according to the detected aspect.
switch(currentAspect)
{
case ASPECT_SQUARE:
case ASPECT_OPPOSITION:
SetNUUTEventTimbre(
&eventVoice,
NUUTEventTimbre::Oom);
break;
case ASPECT_CONJUNCTION:
case ASPECT_SEXTILE:
case ASPECT_TRINE:
default:
SetNUUTEventTimbre(
&eventVoice,
NUUTEventTimbre::CrystalBell);
break;
}
// Trigger the bell only when a planet is active.
if(AnyPadActive())
eventBellTriggerPending = true;
melodyActive = true;
}
else
{
melodyActive = false;
}
melodyLastAspect = currentAspect;
melodyLastA = aspectA;
melodyLastB = aspectB;
}
}
else
{
melodyActive = false;
melodyLastAspect = ASPECT_NONE;
melodyLastA = -1;
melodyLastB = -1;
}
// --------------------------------------------------------
// CONSTELLATION VOICE TRIGGERS
// Detect newly activated extra voices so their trigger
// envelopes can be started when they enter the constellation.
// --------------------------------------------------------
bool currentConstellationActive[7] = {false};
for(int v = 3; v < 10; v++)
{
int idx = v - 3;
int planet = constellation.GetPlanet(v);
if(planet != -1 && constellation.IsExtraVoice(v))
{
currentConstellationActive[idx] = true;
}
}
// --------------------------------------------------------
// SOUND CONTROLS
// --------------------------------------------------------
float originSemitones = originValue * 24.0f - 12.0f;
float originRatio = powf(2.0f, originSemitones / 12.0f);
// ALCHEMY
float Alchemy = knobs.s37().Process();
// Crystal effect controls.
float crystalRefraction = knobs.s34().Process();
float crystalRadiance = crystalRefraction;
float crystalWet = knobs.s35().Process();
// KOSMOS
float kosmos = knobs.s32().Process();
// --------------------------------------------------------
// AUDIO PROCESSING
// Generate the planetary voices and constellation voices
// for every sample in the current audio block.
// --------------------------------------------------------
const bool anyPadActive = AnyPadActive();
// EVENT MODULATION
static float eventModPhase = 0.0f;
float eventModRate = 0.0f;
float eventModDepth = 0.0f;
if(anyPadActive)
{
switch(currentAspect)
{
case ASPECT_CONJUNCTION:
eventModRate = 0.18f;
eventModDepth = 0.07f;
break;
case ASPECT_SEXTILE:
eventModRate = 0.45f;
eventModDepth = 0.04f;
break;
case ASPECT_SQUARE:
eventModRate = 2.0f;
eventModDepth = 0.10f;
break;
case ASPECT_TRINE:
eventModRate = 0.12f;
eventModDepth = 0.05f;
break;
case ASPECT_OPPOSITION:
eventModRate = 0.65f;
eventModDepth = 0.08f;
break;
default:
break;
}
}
const float eventModSampleRate = hw.AudioSampleRate();
for(size_t i = 0; i < size; i++)
{
float planetSig = 0.0f;
float constellationSig = 0.0f;
// Crystal event bell and Oom: same aspect-driven trigger.
bool eventTrigger = eventBellTriggerPending && anyPadActive;
float eventSig = ProcessNUUTEventVoice(
&eventVoice,
eventTrigger,
eventBellFrequency);
eventBellTriggerPending = false;
// Gradually add chord notes every 0.5 seconds.
// Previously activated notes remain active.
if(melodyActive && anyPadActive && melodyLength > 0)
{
if(melodyStep < melodyLength)
{
if(melodySamplesUntilNext > 0)
--melodySamplesUntilNext;
if(melodySamplesUntilNext == 0)
{
++melodyStep;
melodySamplesUntilNext = 24000;
}
}
}
for(int v = 0; v < 10; v++)
{
int planet = constellation.GetPlanet(v);
// ------------------------------------------------
// EXTRA CONSTELLATION VOICES
// Voice indices 3–9 use the dedicated constellation
// voice engine rather than the main voice engine.
// ------------------------------------------------
if(v >= 3)
{
int idx = v - 3;
// Extra voices 4, 5 and 6 temporarily form the chord pad.
if(idx >= 4 && idx <= 6 &&
melodyActive && anyPadActive)
{
int noteIndex = idx - 4;
// Activate notes one by one, then keep them together.
constellationVoices[idx].active =
(melodyStep > noteIndex);
constellationVoices[idx].targetFrequency =
melodyNotes[noteIndex] * originRatio;
}
else if(planet != -1 &&
constellation.IsExtraVoice(v) &&
anyPadActive)
{
constellationVoices[idx].active = true;
constellationVoices[idx].targetFrequency =
planets[planet].frequency * originRatio;
}
else
{
constellationVoices[idx].active = false;
}
constellationSig += ProcessPlanetConstellationVoice(
&constellationVoices[idx],
constellationVoices[idx].targetFrequency
);
continue;
}
// ------------------------------------------------
// MAIN PLANETARY VOICES
// The gate follows the state of the assigned pad.
// ------------------------------------------------
bool gate = false;
if(planet != -1)
{
gate = padStates[planet];
}
planetSig += ProcessVoice(
&voices[v],
gate,
0.0f,
&aspect,
originRatio,
arcValue,
Alchemy,
kosmos,
v,
voices[0].frequency,
&combLP6Breath
);
}
// ----------------------------------------------------
// VOICE MIX
// Combine the main and constellation signals.
// ----------------------------------------------------
float sig = planetSig
+ constellationSig * 1.10f
+ eventSig * 0.005f;
// Apply the active aspect modulation.
if(eventModDepth > 0.0f)
{
eventModPhase += eventModRate / eventModSampleRate;
if(eventModPhase >= 1.0f)
eventModPhase -= 1.0f;
const float eventLfo = sinf(eventModPhase * 6.2831853f);
const float eventAmplitude =
1.0f + eventLfo * eventModDepth;
sig *= eventAmplitude;
}
// ----------------------------------------------------
// GLOBAL STEREO REVERB — DaisySP ReverbSc
float reverbOutL = 0.0f;
float reverbOutR = 0.0f;
reverb.Process(
sig,
sig,
&reverbOutL,
&reverbOutR
);
sig = sig * 1.05f
+ reverbOutL * 0.20f
+ reverbOutR * 0.20f;
// ----------------------------------------------------
// CRYSTAL EFFECTS
// Process the signal using the selected crystal mode.
// ----------------------------------------------------
float drySig = sig;
CrystalStereo crystalSig = crystals.Process(
sig,
switches.A(),
crystalRefraction,
crystalRadiance,
anyPadActive
);
// Blend the original signal with the stereo effect.
float outputL =
drySig * (1.0f - crystalWet)
+ crystalSig.left * crystalWet;
float outputR =
drySig * (1.0f - crystalWet)
+ crystalSig.right * crystalWet;
// ----------------------------------------------------
// OUTPUT STAGE
// Apply the existing output gain and soft clipping.
// ----------------------------------------------------
float outputGain = 0.7f;
outputL *= outputGain;
outputR *= outputGain;
outputL = tanhf(outputL);
outputR = tanhf(outputR);
out[0][i] = outputL;
out[1][i] = outputR;
}
// --------------------------------------------------------
// UPDATE CONSTELLATION ACTIVITY HISTORY
// Store the current state for detecting new activations
// during the next audio callback.
// --------------------------------------------------------
for(int i = 0; i < 7; i++)
{
constellationVoiceWasActive[i] =
currentConstellationActive[i];
}
cpuMeter.OnBlockEnd();
}
// ============================================================
// MAIN
// ============================================================
int main()
{
hw.Configure();
hw.Init();
hw.StartLog(false);
hw.PrintLine("NUUT CPU monitor started");
knobs.Init(hw);
hw.SetLed(true);
System::Delay(1000);
hw.SetLed(false);
pads.Init();
switches.Init();
transit.Init();
float sampleRate = hw.AudioSampleRate();
cpuMeter.Init(sampleRate, hw.AudioBlockSize());
InitNUUTEventVoice(&eventVoice, sampleRate);
InitCombLP6Breath(&combLP6Breath, sampleRate);
const int reverbInitStatus = reverb.Init(sampleRate);
if(reverbInitStatus != 0)
{
while(true)
{
hw.SetLed(true);
System::Delay(100);
hw.SetLed(false);
System::Delay(100);
}
}
reverb.SetFeedback(0.85f);
reverb.SetLpFreq(10000.0f);
crystals.Init(sampleRate);
// Initialize all main voices.
for(int i = 0; i < 14; i++)
{
InitVoice(&voices[i], sampleRate);
}
// Initialize the seven extra constellation voices.
for(int i = 0; i < 7; i++)
{
InitPlanetConstellationVoice(
constellationVoices[i],
sampleRate
);
}
// Connect the constellation engine to the voice array
// and the planetary frequency table.
constellation.Init(voices);
pads.SetOnTouch(OnPadTouch);
pads.SetOnRelease(OnPadRelease);
hw.StartAudio(AudioCallback);
while(true)
{
pads.Process();
System::Delay(4);
}
}