-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.cpp
1594 lines (1494 loc) · 48.7 KB
/
main.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
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// c++ include
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
// pico files
#include "hardware/adc.h" // adc_read
#include "hardware/clocks.h"
#include "hardware/flash.h" // flash memory
#include "hardware/irq.h" // interrupts
#include "hardware/pwm.h" // pwm
#include "hardware/sync.h" // wait for interrupt
#include "pico/stdlib.h" // stdlib
// pikocore files
#include "doth/audio2h.h"
#include "doth/button.h"
#include "doth/delay.h"
#include "doth/easing.h"
#include "doth/filter.h"
#include "doth/flash_target_offset.h"
#include "doth/knob.h"
#include "doth/led.h"
#include "doth/ledarray.h"
#include "doth/onewiremidi.h"
#include "doth/runningavg.h"
#include "doth/sequencer.h"
#include "doth/trigger_out.h"
// constants
#define CLOCK_RATE 264000
#define NUM_BUTTONS 8
#define NUM_KNOBS 3
#define NUM_LEDS 8
#define DISTORTION_MAX 30
#define VOLUME_REDUCE_MAX 30
#define HEAD_SHIFT 10 // crossfade time in samples (2^HEAD_SHIFT)
#define AUDIO_PIN 20 // audio out
#ifdef PICO_DEFAULT_LED_PIN
#define LED_PIN PICO_DEFAULT_LED_PIN
#endif
#define CLOCK_PIN 22 // clock in pin
#define TRIGO_PIN 21 // trigger out pin
#define MAIN_LOOP_HZ 4
#define MAIN_LOOP_DELAY 50
#if WS2812_ENABLED == 1
#include "doth/WS2812.hpp"
#endif
/*
* GLOBAL VARIABLES
*/
// flash
// https://github.com/raspberrypi/pico-examples/blob/master/flash/program/flash_program.c
// https://kevinboone.me/picoflash.html
#define SAVE_VOLUME 0 // needs two bytes
#define SAVE_BPM 2 // needs two bytes
#define SAVE_FILTER 4 // needs one byte
#define SAVE_SAMPLE 5 // needs one byte
#define SAVE_GATE 6 // needs two bytes
#define SAVE_PROB_DIRECTION 8
#define SAVE_PROB_RETRIG 9
#define SAVE_PROB_JUMP 10
#define SAVE_PROB_GATE 11
#define SAVE_PROB_TUNNEL 12
const uint8_t *flash_target_contents =
(const uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET);
// inputs
Button input_button[NUM_BUTTONS];
Knob input_knob[NUM_KNOBS];
// outputs
LEDArray ledarray;
TriggerOut output_trigger;
// audio tracking
uint8_t audio_now = 0;
uint8_t audio_clk = 0;
uint8_t audio_clk_thresh = 48;
bool do_mute = false;
uint8_t do_mute_debounce = 0;
// sample tracking
uint16_t sample = 0;
uint16_t sample_beats = 8;
uint16_t sample_change = 0;
uint16_t sample_add = 0;
uint16_t sample_set = 0;
uint32_t phase_sample[] = {0, 0};
uint32_t phase_retrig = 0;
bool phase_head = 0;
uint32_t phase_xfade = 0;
// beat tracking
uint16_t select_beat = 0;
uint16_t select_beat_freeze = 0;
bool direction[] = {1, 1}; // 0 = reverse, 1 = forward
bool base_direction = 1; // 0 = reverse, 1 == forward
uint8_t volume_mod = 0;
// volume/distortion/filter/bitcrush
uint8_t distortion = 0;
uint8_t volume_reduce = 0;
uint8_t filter_fc = LPF_MAX + 10;
uint8_t hpf_fc = 0;
uint8_t filter_q = 0;
uint8_t bitcrush = 0;
uint8_t stretch_change = 0;
bool do_lock_clock = false;
// beat tracking (beat = eighth-note)
uint32_t beat_counter = 0;
uint16_t bpm_set = 79;
uint32_t beat_thresh = 21120000;
uint32_t beat_num_total = 0;
bool beat_onset = false;
bool beat_led = 0;
bool btn_reset = 0;
bool soft_sync = 0;
bool is_syncing = false;
bool do_sync_play = false;
// probabilities
uint8_t probability_jump = 0;
uint8_t probability_direction = 0;
uint8_t probability_retrig = 0;
uint8_t probability_gate = 0;
uint8_t probability_tunnel = 0; // jumps between samples
// retriggering / fx
bool fx_retrig = false;
bool btn_retrig = 0;
uint8_t retrig_sel = 4;
uint8_t retrig_count = 0;
uint8_t retrig_max = 2;
uint8_t retrig_filter = 0;
uint8_t retrig_filter_change = 0;
int8_t retrig_pitch_change = 0;
uint8_t retrig_volume_reduce = 0;
uint8_t button_on = NUM_BUTTONS;
uint8_t button_on2 = 3;
uint8_t button_filter = 0;
bool button_filter_on = false;
uint8_t retrig_volume_reduce_change = 0;
bool retrig_pitch_up = false;
bool retrig_pitch_down = false;
uint8_t syncing_clicks = 0;
// bpm configuring
bool flag_half_time = 0; // specifies quarter note or not
// noise gate
uint16_t noise_gate_val;
uint16_t noise_gate_thresh;
uint16_t noise_gate_thresh_use;
uint8_t noise_gate_fade = 0;
// buttons
bool button_trigger[8] = {false, false, false, false,
false, false, false, false};
// sequencer
Sequencer sequencer;
#if MIDI_IN_ENABLED == 1
// midi handler
Onewiremidi *onewiremidi;
#endif
int8_t midi_button1 = -1;
int8_t midi_button2 = -1;
/*
* HELPER FUNCTIONS
* knobs / clock in can set these inputs
*
*/
void param_set_break(uint16_t knob_val, uint8_t &filter_fc_,
uint8_t &distortion_, uint8_t &probability_jump_,
uint8_t &probability_retrig_, uint8_t &probability_gate_,
uint8_t &probability_direction_,
uint8_t &probability_tunnel_,
uint8_t save_data_[FLASH_PAGE_SIZE]) {
if (knob_val < 50) {
// turn it all off
distortion_ = 0;
probability_jump_ = 0;
probability_retrig_ = 0;
probability_gate_ = 0;
probability_direction_ = 0;
probability_tunnel_ = 0;
} else {
distortion_ = ease_distortion(knob_val) * DISTORTION_MAX / 255;
probability_jump_ = ease_probability_jump(knob_val);
probability_retrig_ = ease_probability_retrig(knob_val);
probability_gate_ = ease_probability_gate(knob_val);
probability_direction_ = ease_probability_direction(knob_val);
probability_tunnel_ = ease_probability_tunnel(knob_val);
}
save_data_[SAVE_PROB_JUMP] = probability_jump_;
save_data_[SAVE_PROB_DIRECTION] = probability_direction_;
save_data_[SAVE_PROB_RETRIG] = probability_jump_;
save_data_[SAVE_PROB_GATE] = probability_direction_;
save_data_[SAVE_PROB_TUNNEL] = probability_tunnel_;
save_data_[SAVE_VOLUME] =
(uint8_t)((distortion_ * 1095 / DISTORTION_MAX + 3000) >> 8);
save_data_[SAVE_VOLUME + 1] =
(uint8_t)(distortion_ * 1095 / DISTORTION_MAX + 3000);
}
void param_set_bpm(uint16_t bpm, uint16_t &bpm_set_, uint32_t &beat_thresh_,
uint8_t &audio_clk_thresh_) {
if (bpm > 360) {
return;
}
// set default bpm
bpm_set_ = bpm;
// double bpm_fudge = ((double)bpm);
// calibrated
double bpm_fudge = ((double)bpm) * 1.054234344 - 1.420118655;
// beat_thresh_ = round(((SAMPLE_RATE * 960.0) << flag_half_time) /
// bpm_fudge);
beat_thresh_ = round(((SAMPLE_RATE * 960.0)) / bpm_fudge);
audio_clk_thresh = round(CLOCK_RATE * BPM_SAMPLED / 250.0 /
(SAMPLE_RATE / 1000.0) / bpm_fudge);
#ifdef DEBUG_BPM
printf("new bpm: %d\n", bpm_set_);
printf("new bpm fudge: %2.3f\n", bpm_fudge);
#endif
}
void param_set_volume(uint16_t knobval, uint8_t &distortion_,
uint8_t &volume_reduce_) {
if (knobval < 2000) {
distortion_ = 0;
volume_reduce_ = (2000 - knobval) * (VOLUME_REDUCE_MAX + 3) / 2000;
} else if (knobval > 3000) {
volume_reduce_ = 0;
distortion = (knobval - 3000) * DISTORTION_MAX / (4095 - 3000);
} else {
volume_reduce_ = 0;
distortion = 0;
}
}
// randint returns value
int randint(int min, int max) {
int MaxValue = max - min;
int random_value =
(int)((1.0 + MaxValue) * rand() /
(RAND_MAX + 1.0)); // Scale rand()'s return value against RAND_MAX
// using doubles instead of a pure modulus to
// have a more distributed result.
return (random_value + min);
}
/*
* PWM INTERRUPT LOGIC (main audio thread)
*/
void pwm_interrupt_handler() {
pwm_clear_irq(pwm_gpio_to_slice_num(AUDIO_PIN));
if ((!do_sync_play && is_syncing) || do_mute) {
pwm_set_gpio_level(AUDIO_PIN, 128);
return;
// bool do_manual_hit = false;
// if (do_mute) {
// if (input_button[1].ChangedHigh(true) ||
// input_button[2].ChangedHigh(true) ||
// input_button[5].ChangedHigh(true) ||
// input_button[6].ChangedHigh(true)) {
// soft_sync = true;
// do_manual_hit = true;
// do_mute_debounce = 0;
// }
// }
// if (!do_manual_hit) {
// pwm_set_gpio_level(AUDIO_PIN, 128);
// return;
// }
}
// clocking when to change beats
beat_counter++;
if ((!is_syncing && beat_counter >= beat_thresh) || btn_reset || soft_sync) {
#ifdef DEBUG_CLOCK
if (soft_sync) {
printf("softsync; beat_counter: %d, beat_thresh: %d\n", beat_counter,
beat_thresh);
}
#endif
soft_sync = false;
beat_num_total++;
beat_counter = 0;
beat_onset = true;
beat_led = 1 - beat_led;
noise_gate_val = 0;
if (btn_reset) {
beat_led = 1;
beat_num_total = 0;
}
gpio_put(LED_PIN, beat_led);
output_trigger.Trigger();
if (do_mute_debounce > 0) {
do_mute_debounce--;
}
// check button 1
if (button_on < NUM_BUTTONS) {
if (!input_button[button_on].On()) {
// button is off
button_on = NUM_BUTTONS;
button_on2 = NUM_BUTTONS;
select_beat_freeze = 0;
button_filter_on = false;
// hm
retrig_volume_reduce = 0;
retrig_volume_reduce_change = 0; // reset
if (btn_reset) {
retrig_count = retrig_max;
}
}
} else if (do_mute_debounce == 0) {
for (uint8_t i = 0; i < NUM_BUTTONS; i++) {
if (input_button[i].On()) {
if (button_on >= NUM_BUTTONS) {
select_beat_freeze = (select_beat / NUM_BUTTONS) * NUM_BUTTONS;
}
button_on = i;
// select new beat
#ifdef DEBUG_BUTTONS
printf("%d on\n", button_on);
#endif
break;
}
}
}
// check button 2
if (button_on2 < NUM_BUTTONS) {
if (!input_button[button_on2].On()) {
button_on2 = NUM_BUTTONS;
button_filter_on = false;
}
}
if (!btn_retrig) {
// check button 2
if (button_on < NUM_BUTTONS && do_mute_debounce == 0) {
// 1st button pressed, check for second button
for (uint8_t i = 0; i < NUM_BUTTONS; i++) {
if (i == button_on) {
continue;
}
if (input_button[i].On()) {
#ifdef DEBUG_BUTTONS
printf("%d + %d\n", button_on, i);
#endif
btn_retrig = true;
button_on2 = i;
}
}
} else {
if (randint(0, 254) < probability_retrig) {
btn_retrig = true;
}
}
} else if (btn_retrig) {
// turn off retrig if one of the buttons is released
if (button_on == NUM_BUTTONS || button_on2 == NUM_BUTTONS) {
retrig_count = retrig_max;
}
}
if (!fx_retrig) {
#ifdef DEBUG_PWM
printf("[%d bpm / %d thresh / beat_num: %d] ", bpm_set, beat_thresh,
beat_num_total);
#endif
// check for fx
if (btn_retrig && !fx_retrig) {
#ifdef DEBUG_PWM
printf("\n");
#endif
fx_retrig = true;
uint8_t r1 = randint(0, 100);
uint8_t r2 = randint(0, 100);
uint8_t r3 = randint(0, 100);
uint8_t r4 = randint(0, 100);
retrig_count = 0;
// retrig_sel = randint(0, 11);
// if (retrig_sel == 4) {
// retrig_sel = 5;
// }
if (button_on2 >= NUM_BUTTONS) {
retrig_sel = randint(2, 16);
} else {
switch (button_on2) {
case 0:
retrig_sel = randint(0, 2);
break;
case 1:
retrig_sel = randint(2, 4);
break;
case 2:
retrig_sel = randint(4, 6);
break;
case 3:
retrig_sel = randint(6, 8);
break;
case 4:
retrig_sel = randint(8, 10);
break;
case 5:
retrig_sel = randint(10, 12);
break;
case 6:
retrig_sel = randint(12, 14);
break;
case 7:
retrig_sel = randint(14, 16);
break;
}
}
retrig_max = randint(3, 16);
if (retrig_sel < 6) {
retrig_max = retrig_max / 2;
} else if (retrig_sel > 11) {
retrig_max = retrig_max * 2;
}
if (r1 <= 15) {
retrig_pitch_up = true;
} else if (r2 <= 15) {
retrig_pitch_down = true;
}
if (r3 < 30) {
retrig_filter = retrig_max;
retrig_filter_change = (LPF_MAX - 10) / retrig_max;
}
if (r4 < 20 && retrig_sel > 6) {
retrig_volume_reduce = retrig_max;
if (retrig_volume_reduce > 5) {
retrig_volume_reduce = 5;
}
retrig_volume_reduce_change = 1; // volume increases
if (randint(1, 100) < 30) {
// delay fx
retrig_volume_reduce_change = 2; // volume decreases
retrig_volume_reduce = 1;
}
}
audio_clk = audio_clk_thresh - 1;
phase_retrig = (retrigs[retrig_sel] << flag_half_time) - 1;
}
}
}
// disable beat interrupts during fx
if (fx_retrig) {
beat_onset = false;
}
// clocking when to change samples
audio_clk++;
if (audio_clk == (audio_clk_thresh + retrig_pitch_change + stretch_change) ||
beat_onset) {
audio_clk = 0;
// beat onset causes next sample
if (beat_onset && fx_retrig == false) {
bool do_switch_heads = true;
if (probability_tunnel > 0) {
if (randint(0, 255) < probability_tunnel) {
sample_add = randint(0, NUM_SAMPLES);
} else {
sample_add = 0;
}
} else {
sample_add = 0;
}
if (sample_set != sample_change) {
sample_set = sample_change;
}
sample = (sample_set + sample_add) % NUM_SAMPLES;
sample_beats = raw_beats(sample);
beat_onset = false;
if (do_lock_clock) {
select_beat = beat_num_total % sample_beats;
} else {
select_beat++;
}
if (flag_half_time) {
select_beat++;
if (select_beat % 2 > 0)
select_beat++; // make sure for halftime mode its only on the even
// beats
}
if (select_beat < 0) {
do_switch_heads = false;
select_beat = sample_beats - 1;
}
if (select_beat >= sample_beats) {
do_switch_heads = false;
select_beat = 0;
}
// random jumps
if (probability_jump > 0) {
if (randint(0, 255) < probability_jump) {
select_beat = randint(0, sample_beats - 1);
}
}
// random gate
if (probability_gate > 0) {
if (randint(0, 255) < probability_gate) {
noise_gate_thresh_use = SAMPLES_PER_BEAT * randint(800, 1000) / 1000;
} else {
noise_gate_thresh_use = noise_gate_thresh;
}
} else {
noise_gate_thresh_use = noise_gate_thresh;
}
// reset
if (btn_reset) {
btn_reset = 0;
select_beat = 0;
}
// printf("button_on: %d\n", button_on);
// printf("button_on2: %d\n", button_on2);
// printf("select_beat: %d\n", select_beat);
// get beat from sequencer
if (sequencer.IsPlaying()) {
select_beat = sequencer.Next(beat_num_total);
#ifdef DEBUG_SEQUENCER
printf("sequencer: [%d] %d\n", sequencer.NextI(beat_num_total),
select_beat);
#endif
}
// hold button down to play that beat
if (button_on < NUM_BUTTONS) {
select_beat = (button_on + select_beat_freeze) % sample_beats;
// record the current beat
sequencer.Record(select_beat);
}
#ifdef DEBUG_PWM
printf("select_beat:%d for %d samples\n", select_beat,
retrigs[retrig_sel] << flag_half_time);
#endif
if (do_switch_heads) {
phase_head = 1 - phase_head; // switch heads
phase_xfade = 1 << HEAD_SHIFT;
}
phase_sample[phase_head] =
select_beat * (SAMPLES_PER_BEAT << flag_half_time);
// random direction for the new head
if (probability_direction > 0) {
uint8_t r1 = randint(0, 255);
if (direction[phase_head] == base_direction) {
if (r1 < probability_direction) {
direction[phase_head] = 1 - base_direction;
}
} else {
if (r1 > probability_direction) {
direction[phase_head] = base_direction;
}
}
} else {
direction[phase_head] = base_direction;
}
} else {
// update the sample
noise_gate_val++;
if (noise_gate_val<10 & noise_gate_fade> 0) {
// noise gate fade in
noise_gate_fade--;
} else if (noise_gate_val > noise_gate_thresh_use) {
// noise gate fade out
if (noise_gate_val % 100 == 0) {
if (noise_gate_fade < 8) {
noise_gate_fade++;
}
}
}
for (uint8_t i = 0; i < 2; i++) {
if (direction[i]) {
phase_sample[i]++;
if (phase_sample[i] == raw_len(sample) - 1) {
phase_sample[i] = 0;
}
} else {
if (phase_sample[i] == 0) {
phase_sample[i] = raw_len(sample) - 2;
} else {
phase_sample[i]--;
}
}
}
}
if (fx_retrig) {
phase_retrig++;
// prevent noise gating?
noise_gate_fade = 0;
noise_gate_val = 0;
if (phase_retrig % (retrigs[retrig_sel] << flag_half_time) == 0) {
retrig_count++;
if (retrig_filter > 0) {
retrig_filter--;
}
// printf("retrig_volume_reduce_change: %d\n",
// retrig_volume_reduce_change);
// printf("retrig_volume_reduce: %d\n", retrig_volume_reduce);
if (retrig_volume_reduce_change == 1 && retrig_volume_reduce > 0) {
if (retrig_sel > 11) {
if (retrig_count % 2 == 0) {
retrig_volume_reduce--;
}
} else {
retrig_volume_reduce--;
}
} else if (retrig_volume_reduce_change == 2 &&
retrig_volume_reduce < 8 && retrig_count % 2 == 0) {
if (retrig_sel > 11) {
if (retrig_count % 4 == 0) {
retrig_volume_reduce++;
}
} else {
retrig_volume_reduce++;
}
}
if (retrig_pitch_up) {
retrig_pitch_change++;
} else if (retrig_pitch_down) {
retrig_pitch_change--;
}
if (retrig_count >= retrig_max) {
// reset retrig stuff
retrig_filter = 0;
retrig_pitch_up = false;
retrig_pitch_down = false;
retrig_pitch_change = 0;
retrig_volume_reduce = 0;
retrig_volume_reduce_change = 0;
button_filter_on = false;
fx_retrig = false;
btn_retrig = false;
}
#ifdef DEBUG_PWM
printf(
"[retrig %d/%d] select_beat:%d for %d samples, beat_counter: %d, "
"\n\tphase_sample[phase_head]: %d%%%d==0\n",
retrig_count, retrig_max, select_beat,
retrigs[retrig_sel] << flag_half_time, beat_counter,
phase_sample[phase_head], (retrigs[retrig_sel] << flag_half_time));
#endif
// setup
phase_head = 1 - phase_head; // switch heads
phase_xfade = 1 << HEAD_SHIFT;
phase_sample[phase_head] =
select_beat * (SAMPLES_PER_BEAT << flag_half_time);
phase_retrig = 0;
}
}
// determine sample
if (phase_xfade == 0) {
audio_now = raw_val(sample, phase_sample[phase_head]);
} else {
phase_xfade--;
// new head
uint32_t u = (uint32_t)raw_val(sample, phase_sample[phase_head]);
u = u * ((1 << HEAD_SHIFT) - phase_xfade); // fade it in
// old head
uint32_t v = (uint32_t)raw_val(sample, phase_sample[1 - phase_head]);
v = v * phase_xfade; // fade it out
// combine
u = (u + v) >> HEAD_SHIFT;
// set to audio now
audio_now = (uint8_t)u;
// if (phase_xfade == 1 << HEAD_SHIFT - 1) {
// // printf("\nphase_head: %d; audio_now=%d\n", phase_head, u);
// }
}
// <volume>
if (volume_reduce >= VOLUME_REDUCE_MAX) audio_now = 128;
if (audio_now != 128) {
// distortion / wave-folding
if (distortion > 0) {
if (audio_now > 128) {
if (audio_now < (255 - distortion)) {
audio_now += distortion;
} else {
audio_now = 255 - distortion;
}
audio_now = 128 + ((audio_now - 128) / ((distortion >> 4) + 1));
} else {
if (audio_now > distortion) {
audio_now -= distortion;
} else {
audio_now = distortion - audio_now;
}
audio_now = 128 - ((128 - audio_now) / ((distortion >> 4) + 1));
}
}
// reduce volume
if (volume_reduce > 0) {
if (audio_now > 128) {
audio_now = audio_now - (volume_reduce);
if (audio_now < 128) audio_now = 128;
} else {
audio_now = audio_now + (volume_reduce);
if (audio_now > 128) audio_now = 128;
}
}
if ((volume_mod + retrig_volume_reduce + noise_gate_fade) > 0 &&
audio_now != 128) {
if (audio_now > 128) {
audio_now = ((audio_now - 128) >>
(volume_mod + retrig_volume_reduce + noise_gate_fade)) +
128;
} else {
audio_now =
128 - ((128 - audio_now) >>
(volume_mod + retrig_volume_reduce + noise_gate_fade));
}
}
} // </volume>
// <bitcrush>
// if (bitcrush > 0) {
// if (audio_now > 128) {
// audio_now = 128 + (((audio_now - 128) >> bitcrush) << bitcrush);
// } else if (audio_now < 128) {
// audio_now = 128 - (((128 - audio_now) >> bitcrush) << bitcrush);
// }
// }
// </bitcrush>
// <filter>
if ((filter_fc - (retrig_filter * retrig_filter_change) - button_filter) <=
LPF_MAX) {
audio_now = (uint8_t)filter_lpf(
(int64_t)audio_now,
(filter_fc - (retrig_filter * retrig_filter_change) - button_filter),
filter_q);
// } else {
// audio_now = (uint8_t)filter_lpf((int64_t)audio_now, LPF_MAX, filter_q);
}
// </filter>
// <delay>
// audio_now = delay.Update(audio_now);
// </delay>
// <dither>
// audio_now = ditherer.Update(audio_now);
// </dither>
}
pwm_set_gpio_level(AUDIO_PIN, audio_now);
}
void print_buf(const uint8_t *buf, size_t len) {
for (size_t i = 0; i < len; ++i) {
printf("%02x", buf[i]);
if (i % 24 == 23)
printf("\n");
else
printf(" ");
}
}
void do_stop_everything() { do_mute = true; }
void do_start_everything() {
// reset syncing
is_syncing = false;
syncing_clicks = 0;
do_mute_debounce = 8;
button_on = NUM_BUTTONS;
button_on2 = NUM_BUTTONS;
btn_reset = true;
// reset everything
// reset retrig stuff
retrig_filter = 0;
retrig_pitch_up = false;
retrig_pitch_down = false;
retrig_pitch_change = 0;
retrig_volume_reduce = 0;
retrig_volume_reduce_change = 0;
button_filter_on = false;
fx_retrig = false;
btn_retrig = false;
do_mute = false;
}
#if MIDI_IN_ENABLED == 1
uint32_t current_time() { return to_ms_since_boot(get_absolute_time()); }
uint16_t *sort_int32_t(uint32_t array[], int n) {
// C
// uint16_t *indexes = malloc(sizeof(uint16_t) * n);
// C++
uint16_t *indexes = new uint16_t[n];
for (int i = 0; i < n; i++) {
indexes[i] = i;
}
// Sort the auxiliary array based on the values in the original array.
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (array[indexes[i]] > array[indexes[j]]) {
// Swap the elements at indexes[i] and indexes[j].
int temp = indexes[i];
indexes[i] = indexes[j];
indexes[j] = temp;
}
}
}
return indexes;
}
#define MIDI_MAX_NOTES 128
#define MIDI_MAX_TIME_ON 10000 // 10 seconds
uint32_t note_hit[MIDI_MAX_NOTES];
bool note_on[MIDI_MAX_NOTES];
uint32_t midi_last_time = 0;
uint32_t midi_delta_sum = 0;
uint32_t midi_delta_count = 0;
#define MIDI_DELTA_COUNT_MAX 32
uint32_t midi_timing_count = 0;
const uint8_t midi_timing_modulus = 24;
void midi_note_off(uint8_t note) {
#ifdef DEBUG_MIDI
printf("note_off: %d\n", note);
#endif
#if MIDI_NOTE_KEY == 1
input_button[note % NUM_BUTTONS].Set(false);
if (midi_button2 > -1) {
midi_button2 = -1;
} else {
midi_button1 = -1;
}
#endif
}
void midi_note_on(uint8_t note, uint8_t velocity) {
#ifdef DEBUG_MIDI
printf("note_on: %d\n", note);
#endif
#if MIDI_NOTE_KEY == 1
if (midi_button1 > -1) {
midi_button2 = note % NUM_BUTTONS;
} else {
midi_button1 = note % NUM_BUTTONS;
}
input_button[note % NUM_BUTTONS].Set(true);
#endif
}
void midi_start() {
do_start_everything();
soft_sync = false;
btn_reset = false;
midi_timing_count = 24 * MIDI_RESET_EVERY_BEAT - 1;
}
void midi_continue() {
do_start_everything();
soft_sync = false;
btn_reset = false;
midi_timing_count = 24 * MIDI_RESET_EVERY_BEAT - 1;
}
void midi_stop() {
do_stop_everything();
soft_sync = false;
btn_reset = false;
midi_timing_count = 24 * MIDI_RESET_EVERY_BEAT - 1;
}
void midi_timing() {
midi_timing_count++;
if (midi_timing_count % (24 * MIDI_RESET_EVERY_BEAT) == 0) {
btn_reset = true;
#ifdef DEBUG_MIDI
printf("midi resetting");
#endif
} else if (midi_timing_count %
(midi_timing_modulus / MIDI_CLOCK_MULTIPLIER) ==
0) {
soft_sync = true;
}
uint32_t now_time = time_us_32();
if (midi_last_time > 0) {
midi_delta_sum += now_time - midi_last_time;
midi_delta_count++;
if (midi_delta_count == MIDI_DELTA_COUNT_MAX) {
uint32_t bpm_input =
(int)round(1250000.0 * MIDI_CLOCK_MULTIPLIER * MIDI_DELTA_COUNT_MAX /
(float)(midi_delta_sum));
#ifdef DEBUG_MIDI
printf("midi bpm\t%d\n", bpm_input);
#endif
if (bpm_input - 7 != bpm_set) {
// #ifdef DEBUG_CLOCK
// printf("%d, %d\n", clock_sync_ms, bpm_input);
// #endif
// REDUCE THE BPM INPUT TO ELIMINATE OVERSTEPPING
param_set_bpm(bpm_input - 7, bpm_set, beat_thresh, audio_clk_thresh);
}
midi_delta_count = 0;
midi_delta_sum = 0;
}
}
midi_last_time = now_time;
}
#endif
int main(void) {
stdio_init_all();
// sleep needed to make sure it can start on battery
// not sure why
sleep_ms(100);
// initialize bpm
param_set_bpm(BPM_SAMPLED, bpm_set, beat_thresh, audio_clk_thresh);
// initialize leds
ledarray.Init();
// initialize clocking and PWM interrupts
// overclock at a multiple of sampling rate
set_sys_clock_khz(CLOCK_RATE, true);
gpio_set_function(AUDIO_PIN, GPIO_FUNC_PWM);
int audio_pin_slice = pwm_gpio_to_slice_num(AUDIO_PIN);
pwm_clear_irq(audio_pin_slice);
pwm_set_irq_enabled(audio_pin_slice, true);
irq_set_exclusive_handler(PWM_IRQ_WRAP, pwm_interrupt_handler);
irq_set_enabled(PWM_IRQ_WRAP, true);
pwm_config config = pwm_get_default_config();
/*
* clock fires 176 Mhz
* with wrap set to 250
* at clock division of 1
* = 704 kHz
* so 22 kHz sampled audio needs a new sample
* every 32 pulses
*/
pwm_config_set_clkdiv(&config, 1.0f);
pwm_config_set_wrap(&config, 250);
pwm_init(audio_pin_slice, &config, true);
pwm_set_gpio_level(AUDIO_PIN, 0);
// setup gpio pins
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_init(CLOCK_PIN);
gpio_set_dir(CLOCK_PIN, GPIO_IN);
gpio_pull_down(CLOCK_PIN);
gpio_init(23);
gpio_pull_up(23);
gpio_set_dir(23, GPIO_OUT);
gpio_put(23, 0);
// initialize buttons
for (uint8_t i = 0; i < NUM_BUTTONS; i++) {
input_button[i].Init(i + 4, 10); // GPIO 4 through 11 are buttons
}
// initialize knobs
adc_init();
for (uint8_t i = 0; i < NUM_KNOBS; i++) {
input_knob[i].Init(i, 50);
}
// initialize sequencer
sequencer.Init();
// initialize save data
uint8_t save_data[FLASH_PAGE_SIZE];