-
Notifications
You must be signed in to change notification settings - Fork 123
/
APP_QQ.ino
1568 lines (1351 loc) · 56.1 KB
/
APP_QQ.ino
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
// Copyright (c) 2015, 2016 Patrick Dowling, Tim Churches
//
// Initial app implementation: Patrick Dowling (pld@gurkenkiste.com)
// Modifications by: Tim Churches (tim.churches@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.
//
// Quad quantizer app, based around the the quantizer/scales implementation from
// from Braids by Olivier Gillet (see braids_quantizer.h/cc et al.). It has since
// grown a little bit...
#include "OC_apps.h"
#include "util/util_logistic_map.h"
#include "util/util_settings.h"
#include "util/util_trigger_delay.h"
#include "util/util_turing.h"
#include "util/util_integer_sequences.h"
#include "peaks_bytebeat.h"
#include "braids_quantizer.h"
#include "braids_quantizer_scales.h"
#include "OC_menus.h"
#include "OC_scales.h"
#include "OC_scale_edit.h"
#include "OC_strings.h"
#ifdef BUCHLA_4U
#define QQ_OFFSET_X 20
#else
#define QQ_OFFSET_X 31
#endif
enum ChannelSetting {
CHANNEL_SETTING_SCALE,
CHANNEL_SETTING_ROOT,
CHANNEL_SETTING_MASK,
CHANNEL_SETTING_SOURCE,
CHANNEL_SETTING_AUX_SOURCE_DEST,
CHANNEL_SETTING_TRIGGER,
CHANNEL_SETTING_CLKDIV,
CHANNEL_SETTING_DELAY,
CHANNEL_SETTING_TRANSPOSE,
CHANNEL_SETTING_OCTAVE,
CHANNEL_SETTING_FINE,
CHANNEL_SETTING_TURING_LENGTH,
CHANNEL_SETTING_TURING_PROB,
CHANNEL_SETTING_TURING_MODULUS,
CHANNEL_SETTING_TURING_RANGE,
CHANNEL_SETTING_TURING_PROB_CV_SOURCE,
CHANNEL_SETTING_TURING_MODULUS_CV_SOURCE,
CHANNEL_SETTING_TURING_RANGE_CV_SOURCE,
CHANNEL_SETTING_LOGISTIC_MAP_R,
CHANNEL_SETTING_LOGISTIC_MAP_RANGE,
CHANNEL_SETTING_LOGISTIC_MAP_R_CV_SOURCE,
CHANNEL_SETTING_LOGISTIC_MAP_RANGE_CV_SOURCE,
CHANNEL_SETTING_BYTEBEAT_EQUATION,
CHANNEL_SETTING_BYTEBEAT_RANGE,
CHANNEL_SETTING_BYTEBEAT_P0,
CHANNEL_SETTING_BYTEBEAT_P1,
CHANNEL_SETTING_BYTEBEAT_P2,
CHANNEL_SETTING_BYTEBEAT_EQUATION_CV_SOURCE,
CHANNEL_SETTING_BYTEBEAT_RANGE_CV_SOURCE,
CHANNEL_SETTING_BYTEBEAT_P0_CV_SOURCE,
CHANNEL_SETTING_BYTEBEAT_P1_CV_SOURCE,
CHANNEL_SETTING_BYTEBEAT_P2_CV_SOURCE,
CHANNEL_SETTING_INT_SEQ_INDEX,
CHANNEL_SETTING_INT_SEQ_MODULUS,
CHANNEL_SETTING_INT_SEQ_RANGE,
CHANNEL_SETTING_INT_SEQ_DIRECTION,
CHANNEL_SETTING_INT_SEQ_BROWNIAN_PROB,
CHANNEL_SETTING_INT_SEQ_LOOP_START,
CHANNEL_SETTING_INT_SEQ_LOOP_LENGTH,
CHANNEL_SETTING_INT_SEQ_FRAME_SHIFT_PROB,
CHANNEL_SETTING_INT_SEQ_FRAME_SHIFT_RANGE,
CHANNEL_SETTING_INT_SEQ_STRIDE,
CHANNEL_SETTING_INT_SEQ_INDEX_CV_SOURCE,
CHANNEL_SETTING_INT_SEQ_MODULUS_CV_SOURCE,
CHANNEL_SETTING_INT_SEQ_RANGE_CV_SOURCE,
CHANNEL_SETTING_INT_SEQ_STRIDE_CV_SOURCE,
CHANNEL_SETTING_INT_SEQ_RESET_TRIGGER,
CHANNEL_SETTING_LAST
};
enum ChannelTriggerSource {
CHANNEL_TRIGGER_TR1,
CHANNEL_TRIGGER_TR2,
CHANNEL_TRIGGER_TR3,
CHANNEL_TRIGGER_TR4,
CHANNEL_TRIGGER_CONTINUOUS_UP,
CHANNEL_TRIGGER_CONTINUOUS_DOWN,
CHANNEL_TRIGGER_LAST
};
enum ChannelSource {
CHANNEL_SOURCE_CV1,
CHANNEL_SOURCE_CV2,
CHANNEL_SOURCE_CV3,
CHANNEL_SOURCE_CV4,
CHANNEL_SOURCE_TURING,
CHANNEL_SOURCE_LOGISTIC_MAP,
CHANNEL_SOURCE_BYTEBEAT,
CHANNEL_SOURCE_INT_SEQ,
CHANNEL_SOURCE_LAST
};
enum QQ_CV_DEST {
QQ_DEST_NONE,
QQ_DEST_ROOT,
QQ_DEST_OCTAVE,
QQ_DEST_TRANSPOSE,
QQ_DEST_MASK,
QQ_DEST_LAST
};
class QuantizerChannel : public settings::SettingsBase<QuantizerChannel, CHANNEL_SETTING_LAST> {
public:
int get_scale(uint8_t dummy) const {
return values_[CHANNEL_SETTING_SCALE];
}
void set_scale(int scale) {
if (scale != get_scale(DUMMY)) {
const OC::Scale &scale_def = OC::Scales::GetScale(scale);
uint16_t mask = get_mask();
if (0 == (mask & ~(0xffff << scale_def.num_notes)))
mask |= 0x1;
apply_value(CHANNEL_SETTING_MASK, mask);
apply_value(CHANNEL_SETTING_SCALE, scale);
}
}
// dummy
int get_scale_select() const {
return 0;
}
// dummy
void set_scale_at_slot(int scale, uint16_t mask, int root, int transpose, uint8_t scale_slot) {
}
// dummy
int get_transpose(uint8_t DUMMY) const {
return 0;
}
int get_root() const {
return values_[CHANNEL_SETTING_ROOT];
}
int get_root(uint8_t DUMMY) const {
return 0x0;
}
uint16_t get_mask() const {
return values_[CHANNEL_SETTING_MASK];
}
uint16_t get_rotated_scale_mask() const {
return last_mask_;
}
ChannelSource get_source() const {
return static_cast<ChannelSource>(values_[CHANNEL_SETTING_SOURCE]);
}
ChannelTriggerSource get_trigger_source() const {
return static_cast<ChannelTriggerSource>(values_[CHANNEL_SETTING_TRIGGER]);
}
uint8_t get_channel_index() const {
return channel_index_;
}
uint8_t get_clkdiv() const {
return values_[CHANNEL_SETTING_CLKDIV];
}
uint16_t get_trigger_delay() const {
return values_[CHANNEL_SETTING_DELAY];
}
int get_transpose() const {
return values_[CHANNEL_SETTING_TRANSPOSE];
}
int get_octave() const {
return values_[CHANNEL_SETTING_OCTAVE];
}
int get_fine() const {
return values_[CHANNEL_SETTING_FINE];
}
uint8_t get_aux_cv_dest() const {
return values_[CHANNEL_SETTING_AUX_SOURCE_DEST];
}
uint8_t get_turing_length() const {
return values_[CHANNEL_SETTING_TURING_LENGTH];
}
uint8_t get_turing_prob() const {
return values_[CHANNEL_SETTING_TURING_PROB];
}
uint8_t get_turing_modulus() const {
return values_[CHANNEL_SETTING_TURING_MODULUS];
}
uint8_t get_turing_range() const {
return values_[CHANNEL_SETTING_TURING_RANGE];
}
uint8_t get_turing_prob_cv_source() const {
return values_[CHANNEL_SETTING_TURING_PROB_CV_SOURCE];
}
uint8_t get_turing_modulus_cv_source() const {
return values_[CHANNEL_SETTING_TURING_MODULUS_CV_SOURCE];
}
uint8_t get_turing_range_cv_source() const {
return values_[CHANNEL_SETTING_TURING_RANGE_CV_SOURCE];
}
uint8_t get_logistic_map_r() const {
return values_[CHANNEL_SETTING_LOGISTIC_MAP_R];
}
uint8_t get_logistic_map_range() const {
return values_[CHANNEL_SETTING_LOGISTIC_MAP_RANGE];
}
uint8_t get_logistic_map_r_cv_source() const {
return values_[CHANNEL_SETTING_LOGISTIC_MAP_R_CV_SOURCE];
}
uint8_t get_logistic_map_range_cv_source() const {
return values_[CHANNEL_SETTING_LOGISTIC_MAP_RANGE_CV_SOURCE];
}
uint8_t get_bytebeat_equation() const {
return values_[CHANNEL_SETTING_BYTEBEAT_EQUATION];
}
uint8_t get_bytebeat_range() const {
return values_[CHANNEL_SETTING_BYTEBEAT_RANGE];
}
uint8_t get_bytebeat_p0() const {
return values_[CHANNEL_SETTING_BYTEBEAT_P0];
}
uint8_t get_bytebeat_p1() const {
return values_[CHANNEL_SETTING_BYTEBEAT_P1];
}
uint8_t get_bytebeat_p2() const {
return values_[CHANNEL_SETTING_BYTEBEAT_P2];
}
uint8_t get_bytebeat_equation_cv_source() const {
return values_[CHANNEL_SETTING_BYTEBEAT_EQUATION_CV_SOURCE];
}
uint8_t get_bytebeat_range_cv_source() const {
return values_[CHANNEL_SETTING_BYTEBEAT_RANGE_CV_SOURCE];
}
uint8_t get_bytebeat_p0_cv_source() const {
return values_[CHANNEL_SETTING_BYTEBEAT_P0_CV_SOURCE];
}
uint8_t get_bytebeat_p1_cv_source() const {
return values_[CHANNEL_SETTING_BYTEBEAT_P1_CV_SOURCE];
}
uint8_t get_bytebeat_p2_cv_source() const {
return values_[CHANNEL_SETTING_BYTEBEAT_P2_CV_SOURCE];
}
uint8_t get_int_seq_index() const {
return values_[CHANNEL_SETTING_INT_SEQ_INDEX];
}
uint8_t get_int_seq_modulus() const {
return values_[CHANNEL_SETTING_INT_SEQ_MODULUS];
}
uint8_t get_int_seq_range() const {
return values_[CHANNEL_SETTING_INT_SEQ_RANGE];
}
int16_t get_int_seq_start() const {
return static_cast<int16_t>(values_[CHANNEL_SETTING_INT_SEQ_LOOP_START]);
}
void set_int_seq_start(uint8_t start_pos) {
values_[CHANNEL_SETTING_INT_SEQ_LOOP_START] = start_pos;
}
int16_t get_int_seq_length() const {
return static_cast<int16_t>(values_[CHANNEL_SETTING_INT_SEQ_LOOP_LENGTH] - 1);
}
bool get_int_seq_dir() const {
return static_cast<bool>(values_[CHANNEL_SETTING_INT_SEQ_DIRECTION]);
}
int16_t get_int_seq_brownian_prob() const {
return static_cast<int16_t>(values_[CHANNEL_SETTING_INT_SEQ_BROWNIAN_PROB]);
}
uint8_t get_int_seq_index_cv_source() const {
return values_[CHANNEL_SETTING_INT_SEQ_INDEX_CV_SOURCE];
}
uint8_t get_int_seq_modulus_cv_source() const {
return values_[CHANNEL_SETTING_INT_SEQ_MODULUS_CV_SOURCE];
}
uint8_t get_int_seq_range_cv_source() const {
return values_[CHANNEL_SETTING_INT_SEQ_RANGE_CV_SOURCE];
}
uint8_t get_int_seq_frame_shift_prob() const {
return values_[CHANNEL_SETTING_INT_SEQ_FRAME_SHIFT_PROB];
}
uint8_t get_int_seq_frame_shift_range() const {
return values_[CHANNEL_SETTING_INT_SEQ_FRAME_SHIFT_RANGE];
}
uint8_t get_int_seq_stride() const {
return values_[CHANNEL_SETTING_INT_SEQ_STRIDE];
}
uint8_t get_int_seq_stride_cv_source() const {
return values_[CHANNEL_SETTING_INT_SEQ_STRIDE_CV_SOURCE];
}
ChannelTriggerSource get_int_seq_reset_trigger_source() const {
return static_cast<ChannelTriggerSource>(values_[CHANNEL_SETTING_INT_SEQ_RESET_TRIGGER]);
}
void clear_dest() {
// ...
schedule_mask_rotate_ = 0x0;
continuous_offset_ = 0x0;
prev_transpose_cv_ = 0x0;
prev_transpose_cv_ = 0x0;
prev_root_cv_ = 0x0;
}
void Init(ChannelSource source, ChannelTriggerSource trigger_source) {
InitDefaults();
apply_value(CHANNEL_SETTING_SOURCE, source);
apply_value(CHANNEL_SETTING_TRIGGER, trigger_source);
channel_index_ = source;
force_update_ = true;
instant_update_ = false;
last_scale_ = -1;
last_mask_ = 0;
last_sample_ = 0;
clock_ = 0;
int_seq_reset_ = false;
continuous_offset_ = false;
schedule_mask_rotate_ = false;
prev_octave_cv_ = 0;
prev_transpose_cv_ = 0;
prev_root_cv_ = 0;
prev_destination_ = 0;
trigger_delay_.Init();
turing_machine_.Init();
logistic_map_.Init();
bytebeat_.Init();
int_seq_.Init(get_int_seq_start(), get_int_seq_length());
quantizer_.Init();
update_scale(true, false);
trigger_display_.Init();
update_enabled_settings();
scrolling_history_.Init(OC::DAC::kOctaveZero * 12 << 7);
}
void force_update() {
force_update_ = true;
}
void instant_update() {
instant_update_ = (~instant_update_) & 1u;
}
inline void Update(uint32_t triggers, DAC_CHANNEL dac_channel) {
uint8_t index = channel_index_;
ChannelSource source = get_source();
ChannelTriggerSource trigger_source = get_trigger_source();
bool continuous = CHANNEL_TRIGGER_CONTINUOUS_UP == trigger_source || CHANNEL_TRIGGER_CONTINUOUS_DOWN == trigger_source;
bool triggered = !continuous &&
(triggers & DIGITAL_INPUT_MASK(trigger_source - CHANNEL_TRIGGER_TR1));
if (source == CHANNEL_SOURCE_INT_SEQ) {
ChannelTriggerSource int_seq_reset_trigger_source = get_int_seq_reset_trigger_source() ;
int_seq_reset_ = (triggers & DIGITAL_INPUT_MASK(int_seq_reset_trigger_source - 1));
}
trigger_delay_.Update();
if (triggered)
trigger_delay_.Push(OC::trigger_delay_ticks[get_trigger_delay()]);
triggered = trigger_delay_.triggered();
if (triggered) {
++clock_;
if (clock_ >= get_clkdiv()) {
clock_ = 0;
} else {
triggered = false;
}
}
bool update = continuous || triggered;
if (update)
update_scale(force_update_, schedule_mask_rotate_);
int32_t sample = last_sample_;
int32_t temp_sample = 0;
int32_t history_sample = 0;
switch (source) {
case CHANNEL_SOURCE_TURING: {
// this doesn't make sense when continuously quantizing; should be hidden via the menu ...
if (continuous)
break;
turing_machine_.set_length(get_turing_length());
int32_t probability = get_turing_prob();
if (get_turing_prob_cv_source()) {
probability += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_turing_prob_cv_source() - 1)) + 7) >> 4;
CONSTRAIN(probability, 0, 255);
}
turing_machine_.set_probability(probability);
if (triggered) {
uint32_t shift_register = turing_machine_.Clock();
uint8_t range = get_turing_range();
if (get_turing_range_cv_source()) {
range += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_turing_range_cv_source() - 1)) + 15) >> 5;
CONSTRAIN(range, 1, 120);
}
if (quantizer_.enabled()) {
uint8_t modulus = get_turing_modulus();
if (get_turing_modulus_cv_source()) {
modulus += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_turing_modulus_cv_source() - 1)) + 15) >> 5;
CONSTRAIN(modulus, 2, 121);
}
// Since our range is limited anyway, just grab the last byte for lengths > 8,
// otherwise scale to use bits. And apply the modulus
uint32_t shift = turing_machine_.length();
uint32_t scaled = (shift_register & 0xff) * range;
scaled = (scaled >> (shift > 7 ? 8 : shift)) % modulus;
// The quantizer uses a lookup codebook with 128 entries centered
// about 0, so we use the range/scaled output to lookup a note
// directly instead of changing to pitch first.
int32_t pitch =
quantizer_.Lookup(64 + range / 2 - scaled + get_transpose()) + (get_root() << 7);
sample = OC::DAC::pitch_to_scaled_voltage_dac(dac_channel, pitch, get_octave(), OC::DAC::get_voltage_scaling(dac_channel));
history_sample = pitch + ((OC::DAC::kOctaveZero + get_octave()) * 12 << 7);
} else {
// Scale range by 128, so 12 steps = 1V
// We dont' need a calibrated value here, really.
uint32_t scaled = multiply_u32xu32_rshift(range << 7, shift_register, get_turing_length());
scaled += get_transpose() << 7;
sample = OC::DAC::pitch_to_scaled_voltage_dac(dac_channel, scaled, get_octave(), OC::DAC::get_voltage_scaling(dac_channel));
history_sample = scaled + ((OC::DAC::kOctaveZero + get_octave()) * 12 << 7);
}
}
}
break;
case CHANNEL_SOURCE_BYTEBEAT: {
// this doesn't make sense when continuously quantizing; should be hidden via the menu ...
if (continuous)
break;
int32_t bytebeat_eqn = get_bytebeat_equation() << 12;
if (get_bytebeat_equation_cv_source()) {
bytebeat_eqn += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_bytebeat_equation_cv_source() - 1)) << 4);
bytebeat_eqn = USAT16(bytebeat_eqn);
}
bytebeat_.set_equation(bytebeat_eqn);
int32_t bytebeat_p0 = get_bytebeat_p0() << 8;
if (get_bytebeat_p0_cv_source()) {
bytebeat_p0 += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_bytebeat_p0_cv_source() - 1)) << 4);
bytebeat_p0 = USAT16(bytebeat_p0);
}
bytebeat_.set_p0(bytebeat_p0);
int32_t bytebeat_p1 = get_bytebeat_p1() << 8;
if (get_bytebeat_p1_cv_source()) {
bytebeat_p1 += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_bytebeat_p1_cv_source() - 1)) << 4);
bytebeat_p1 = USAT16(bytebeat_p1);
}
bytebeat_.set_p1(bytebeat_p1);
int32_t bytebeat_p2 = get_bytebeat_p2() << 8;
if (get_bytebeat_p2_cv_source()) {
bytebeat_p2 += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_bytebeat_p2_cv_source() - 1)) << 4);
bytebeat_p2 = USAT16(bytebeat_p2);
}
bytebeat_.set_p2(bytebeat_p2);
if (triggered) {
uint32_t bb = bytebeat_.Clock();
uint8_t range = get_bytebeat_range();
if (get_bytebeat_range_cv_source()) {
range += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_bytebeat_range_cv_source() - 1)) + 15) >> 5;
CONSTRAIN(range, 1, 120);
}
if (quantizer_.enabled()) {
// Since our range is limited anyway, just grab the last byte
uint32_t scaled = ((bb >> 8) * range) >> 8;
// The quantizer uses a lookup codebook with 128 entries centered
// about 0, so we use the range/scaled output to lookup a note
// directly instead of changing to pitch first.
int32_t pitch =
quantizer_.Lookup(64 + range / 2 - scaled + get_transpose()) + (get_root() << 7);
sample = OC::DAC::pitch_to_scaled_voltage_dac(dac_channel, pitch, get_octave(), OC::DAC::get_voltage_scaling(dac_channel));
history_sample = pitch + ((OC::DAC::kOctaveZero + get_octave()) * 12 << 7);
} else {
// We dont' need a calibrated value here, really
int octave = get_octave();
CONSTRAIN(octave, 0, 6);
sample = OC::DAC::get_octave_offset(dac_channel, octave) + (get_transpose() << 7);
// range is actually 120 (10 oct) but 65535 / 128 is close enough
sample += multiply_u32xu32_rshift32((static_cast<uint32_t>(range) * 65535U) >> 7, bb << 16);
sample = USAT16(sample);
history_sample = sample;
}
}
}
break;
case CHANNEL_SOURCE_LOGISTIC_MAP: {
// this doesn't make sense when continuously quantizing; should be hidden via the menu ...
if (continuous)
break;
logistic_map_.set_seed(123);
int32_t logistic_map_r = get_logistic_map_r();
if (get_logistic_map_r_cv_source()) {
logistic_map_r += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_logistic_map_r_cv_source() - 1)) + 7) >> 4;
CONSTRAIN(logistic_map_r, 0, 255);
}
logistic_map_.set_r(logistic_map_r);
if (triggered) {
int64_t logistic_map_x = logistic_map_.Clock();
uint8_t range = get_logistic_map_range();
if (get_logistic_map_range_cv_source()) {
range += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_logistic_map_range_cv_source() - 1)) + 15) >> 5;
CONSTRAIN(range, 1, 120);
}
if (quantizer_.enabled()) {
uint32_t logistic_scaled = (logistic_map_x * range) >> 24;
// See above, may need tweaking
int32_t pitch =
quantizer_.Lookup(64 + range / 2 - logistic_scaled + get_transpose()) + (get_root() << 7);
sample = OC::DAC::pitch_to_scaled_voltage_dac(dac_channel, pitch, get_octave(), OC::DAC::get_voltage_scaling(dac_channel));
history_sample = pitch + ((OC::DAC::kOctaveZero + get_octave()) * 12 << 7);
} else {
int octave = get_octave();
CONSTRAIN(octave, 0, 6);
sample = OC::DAC::get_octave_offset(dac_channel, octave) + (get_transpose() << 7);
sample += multiply_u32xu32_rshift24((static_cast<uint32_t>(range) * 65535U) >> 7, logistic_map_x);
sample = USAT16(sample);
history_sample = sample;
}
}
}
break;
case CHANNEL_SOURCE_INT_SEQ: {
// this doesn't make sense when continuously quantizing; should be hidden via the menu ...
if (continuous)
break;
int_seq_.set_loop_direction(get_int_seq_dir());
int_seq_.set_brownian_prob(get_int_seq_brownian_prob());
int16_t int_seq_index = get_int_seq_index();
int16_t int_seq_stride = get_int_seq_stride();
if (get_int_seq_index_cv_source()) {
int_seq_index += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_int_seq_index_cv_source() - 1)) + 127) >> 8;
}
if (int_seq_index < 0) int_seq_index = 0;
if (int_seq_index > 8) int_seq_index = 8;
int_seq_.set_int_seq(int_seq_index);
int16_t int_seq_modulus_ = get_int_seq_modulus();
if (get_int_seq_modulus_cv_source()) {
int_seq_modulus_ += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_int_seq_modulus_cv_source() - 1)) + 31) >> 6;
CONSTRAIN(int_seq_modulus_, 2, 121);
}
int_seq_.set_int_seq_modulus(int_seq_modulus_);
if (get_int_seq_stride_cv_source()) {
int_seq_stride += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_int_seq_stride_cv_source() - 1)) + 31) >> 6;
}
if (int_seq_stride < 1) int_seq_stride = 1;
if (int_seq_stride > kIntSeqLen - 1) int_seq_stride = kIntSeqLen - 1;
int_seq_.set_fractal_stride(int_seq_stride);
int_seq_.set_loop_start(get_int_seq_start());
int_seq_.set_loop_length(get_int_seq_length());
if (int_seq_reset_) {
int_seq_.reset_loop();
int_seq_reset_ = false;
}
if (triggered) {
// uint32_t is = int_seq_.Clock();
// check whether frame should be shifted and if so, by how much.
if (get_int_seq_pass_go()) {
// OK, we're at the start of a loop or at one end of a pendulum swing
uint8_t fs_prob = get_int_seq_frame_shift_prob();
uint8_t fs_range = get_int_seq_frame_shift_range();
// Serial.print("fs_prob=");
// Serial.println(fs_prob);
// Serial.print("fs_range=");
// Serial.println(fs_range);
uint8_t fs_rand = static_cast<uint8_t>(random(0,256)) ;
// Serial.print("fs_rand=");
// Serial.println(fs_rand);
// Serial.println("---");
if (fs_rand < fs_prob) {
// OK, move the frame!
int16_t frame_shift = random(-fs_range, fs_range + 1) ;
// Serial.print("frame_shift=");
// Serial.println(frame_shift);
// Serial.print("current start pos=");
// Serial.println(get_int_seq_start());
int16_t new_start_pos = get_int_seq_start() + frame_shift ;
// Serial.print("new_start_pos=");
// Serial.println(new_start_pos);
// Serial.println("===");
if (new_start_pos < 0) new_start_pos = 0;
if (new_start_pos > kIntSeqLen - 2) new_start_pos = kIntSeqLen - 2;
set_int_seq_start(static_cast<uint8_t>(new_start_pos)) ;
int_seq_.set_loop_start(get_int_seq_start());
}
}
uint32_t is = int_seq_.Clock();
int16_t range_ = get_int_seq_range();
if (get_int_seq_range_cv_source()) {
range_ += (OC::ADC::value(static_cast<ADC_CHANNEL>(get_int_seq_range_cv_source() - 1)) + 31) >> 6;
CONSTRAIN(range_, 1, 120);
}
if (quantizer_.enabled()) {
// Since our range is limited anyway, just grab the last byte
uint32_t scaled = ((is >> 4) * range_) >> 8;
// The quantizer uses a lookup codebook with 128 entries centered
// about 0, so we use the range/scaled output to lookup a note
// directly instead of changing to pitch first.
int32_t pitch =
quantizer_.Lookup(64 + range_ / 2 - scaled + get_transpose()) + (get_root() << 7);
sample = OC::DAC::pitch_to_scaled_voltage_dac(dac_channel, pitch, get_octave(), OC::DAC::get_voltage_scaling(dac_channel));
history_sample = pitch + ((OC::DAC::kOctaveZero + get_octave()) * 12 << 7);
} else {
// We dont' need a calibrated value here, really
int octave = get_octave();
CONSTRAIN(octave, 0, 6);
sample = OC::DAC::get_octave_offset(dac_channel, octave) + (get_transpose() << 7);
// range is actually 120 (10 oct) but 65535 / 128 is close enough
sample += multiply_u32xu32_rshift32((static_cast<uint32_t>(range_) * 65535U) >> 7, is << 20);
sample = USAT16(sample);
history_sample = sample;
}
}
}
break;
default: {
if (update) {
int32_t transpose = get_transpose() + prev_transpose_cv_;
int octave = get_octave() + prev_octave_cv_;
int root = get_root() + prev_root_cv_;
int32_t pitch = quantizer_.enabled()
? OC::ADC::raw_pitch_value(static_cast<ADC_CHANNEL>(source))
: OC::ADC::pitch_value(static_cast<ADC_CHANNEL>(source));
// repurpose channel CV input? --
uint8_t _aux_cv_destination = get_aux_cv_dest();
if (_aux_cv_destination != prev_destination_)
clear_dest();
prev_destination_ = _aux_cv_destination;
if (!continuous && index != source) {
// this doesn't really work all that well for continuous quantizing...
// see below
switch(_aux_cv_destination) {
case QQ_DEST_NONE:
break;
case QQ_DEST_TRANSPOSE:
transpose += (OC::ADC::value(static_cast<ADC_CHANNEL>(index)) + 63) >> 7;
break;
case QQ_DEST_ROOT:
root += (OC::ADC::value(static_cast<ADC_CHANNEL>(index)) + 127) >> 8;
break;
case QQ_DEST_OCTAVE:
octave += (OC::ADC::value(static_cast<ADC_CHANNEL>(index)) + 255) >> 9;
break;
case QQ_DEST_MASK:
update_scale(false, (OC::ADC::value(static_cast<ADC_CHANNEL>(index)) + 127) >> 8);
break;
default:
break;
}
}
// limit:
CONSTRAIN(octave, -4, 4);
CONSTRAIN(root, 0, 11);
CONSTRAIN(transpose, -12, 12);
int32_t quantized = quantizer_.Process(pitch, root << 7, transpose);
sample = temp_sample = OC::DAC::pitch_to_scaled_voltage_dac(dac_channel, quantized, octave + continuous_offset_, OC::DAC::get_voltage_scaling(dac_channel));
// continuous mode needs special treatment to give useful results.
// basically, update on note change only
if (continuous && last_sample_ != sample) {
bool _re_quantize = false;
int _aux_cv = 0;
if (index != source) {
switch(_aux_cv_destination) {
case QQ_DEST_NONE:
break;
case QQ_DEST_TRANSPOSE:
_aux_cv = (OC::ADC::value(static_cast<ADC_CHANNEL>(index)) + 63) >> 7;
if (_aux_cv != prev_transpose_cv_) {
transpose = get_transpose() + _aux_cv;
CONSTRAIN(transpose, -12, 12);
prev_transpose_cv_ = _aux_cv;
_re_quantize = true;
}
break;
case QQ_DEST_ROOT:
_aux_cv = (OC::ADC::value(static_cast<ADC_CHANNEL>(index)) + 127) >> 8;
if (_aux_cv != prev_root_cv_) {
root = get_root() + _aux_cv;
CONSTRAIN(root, 0, 11);
prev_root_cv_ = _aux_cv;
_re_quantize = true;
}
break;
case QQ_DEST_OCTAVE:
_aux_cv = (OC::ADC::value(static_cast<ADC_CHANNEL>(index)) + 255) >> 9;
if (_aux_cv != prev_octave_cv_) {
octave = get_octave() + _aux_cv;
CONSTRAIN(octave, -4, 4);
prev_octave_cv_ = _aux_cv;
_re_quantize = true;
}
break;
case QQ_DEST_MASK:
schedule_mask_rotate_ = (OC::ADC::value(static_cast<ADC_CHANNEL>(index)) + 127) >> 8;
update_scale(force_update_, schedule_mask_rotate_);
break;
default:
break;
}
// end switch
}
// offset when TR source = continuous ?
int8_t _trigger_offset = 0;
bool _trigger_update = false;
if (OC::DigitalInputs::read_immediate(static_cast<OC::DigitalInput>(index))) {
_trigger_offset = (trigger_source == CHANNEL_TRIGGER_CONTINUOUS_UP) ? 1 : -1;
}
if (_trigger_offset != continuous_offset_)
_trigger_update = true;
continuous_offset_ = _trigger_offset;
// run quantizer again -- presumably could be made more efficient...
if (_re_quantize)
quantized = quantizer_.Process(pitch, root << 7, transpose);
if (_re_quantize || _trigger_update)
sample = OC::DAC::pitch_to_scaled_voltage_dac(dac_channel, quantized, octave + continuous_offset_, OC::DAC::get_voltage_scaling(dac_channel));
}
// end special treatment
history_sample = quantized + ((OC::DAC::kOctaveZero + octave + continuous_offset_) * 12 << 7);
}
}
} // end switch
bool changed = continuous ? (last_sample_ != temp_sample) : (last_sample_ != sample);
if (changed) {
MENU_REDRAW = 1;
last_sample_ = continuous ? temp_sample : sample;
}
OC::DAC::set(dac_channel, sample + get_fine());
if (triggered || (continuous && changed)) {
scrolling_history_.Push(history_sample);
trigger_display_.Update(1, true);
} else {
trigger_display_.Update(1, false);
}
scrolling_history_.Update();
}
// Wrappers for ScaleEdit
void scale_changed() {
force_update_ = true;
}
uint16_t get_scale_mask(uint8_t scale_select) const {
return get_mask();
}
void update_scale_mask(uint16_t mask, uint16_t dummy) {
apply_value(CHANNEL_SETTING_MASK, mask); // Should automatically be updated
last_mask_ = mask;
force_update_ = true;
}
//
uint8_t getTriggerState() const {
return trigger_display_.getState();
}
uint32_t get_shift_register() const {
return turing_machine_.get_shift_register();
}
uint32_t get_logistic_map_register() const {
return logistic_map_.get_register();
}
uint32_t get_bytebeat_register() const {
return bytebeat_.get_last_sample();
}
uint32_t get_int_seq_register() const {
return int_seq_.get_register();
}
int16_t get_int_seq_k() const {
return int_seq_.get_k();
}
int16_t get_int_seq_l() const {
return int_seq_.get_l();
}
int16_t get_int_seq_i() const {
return int_seq_.get_i();
}
int16_t get_int_seq_j() const {
return int_seq_.get_j();
}
int16_t get_int_seq_n() const {
return int_seq_.get_n();
}
int16_t get_int_seq_x() const {
return int_seq_.get_x();
}
bool get_int_seq_pass_go() const {
return int_seq_.get_pass_go();
}
// Maintain an internal list of currently available settings, since some are
// dependent on others. It's kind of brute force, but eh, works :) If other
// apps have a similar need, it can be moved to a common wrapper
int num_enabled_settings() const {
return num_enabled_settings_;
}
ChannelSetting enabled_setting_at(int index) const {
return enabled_settings_[index];
}
void update_enabled_settings() {
ChannelSetting *settings = enabled_settings_;
*settings++ = CHANNEL_SETTING_SCALE;
if (OC::Scales::SCALE_NONE != get_scale(DUMMY)) {
*settings++ = CHANNEL_SETTING_ROOT;
*settings++ = CHANNEL_SETTING_MASK;
}
*settings++ = CHANNEL_SETTING_SOURCE;
switch (get_source()) {
case CHANNEL_SOURCE_CV1:
case CHANNEL_SOURCE_CV2:
case CHANNEL_SOURCE_CV3:
case CHANNEL_SOURCE_CV4:
if (get_source() != get_channel_index())
*settings++ = CHANNEL_SETTING_AUX_SOURCE_DEST;
break;
case CHANNEL_SOURCE_TURING:
*settings++ = CHANNEL_SETTING_TURING_LENGTH;
if (OC::Scales::SCALE_NONE != get_scale(DUMMY))
*settings++ = CHANNEL_SETTING_TURING_MODULUS;
*settings++ = CHANNEL_SETTING_TURING_RANGE;
*settings++ = CHANNEL_SETTING_TURING_PROB;
if (OC::Scales::SCALE_NONE != get_scale(DUMMY))
*settings++ = CHANNEL_SETTING_TURING_MODULUS_CV_SOURCE;
*settings++ = CHANNEL_SETTING_TURING_RANGE_CV_SOURCE;
*settings++ = CHANNEL_SETTING_TURING_PROB_CV_SOURCE;
break;
case CHANNEL_SOURCE_LOGISTIC_MAP:
*settings++ = CHANNEL_SETTING_LOGISTIC_MAP_R;
*settings++ = CHANNEL_SETTING_LOGISTIC_MAP_RANGE;
*settings++ = CHANNEL_SETTING_LOGISTIC_MAP_R_CV_SOURCE;
*settings++ = CHANNEL_SETTING_LOGISTIC_MAP_RANGE_CV_SOURCE;
break;
case CHANNEL_SOURCE_BYTEBEAT:
*settings++ = CHANNEL_SETTING_BYTEBEAT_EQUATION;
*settings++ = CHANNEL_SETTING_BYTEBEAT_RANGE;
*settings++ = CHANNEL_SETTING_BYTEBEAT_P0;
*settings++ = CHANNEL_SETTING_BYTEBEAT_P1;
*settings++ = CHANNEL_SETTING_BYTEBEAT_P2;
*settings++ = CHANNEL_SETTING_BYTEBEAT_EQUATION_CV_SOURCE;
*settings++ = CHANNEL_SETTING_BYTEBEAT_RANGE_CV_SOURCE;
*settings++ = CHANNEL_SETTING_BYTEBEAT_P0_CV_SOURCE;
*settings++ = CHANNEL_SETTING_BYTEBEAT_P1_CV_SOURCE;
*settings++ = CHANNEL_SETTING_BYTEBEAT_P2_CV_SOURCE;
break;
case CHANNEL_SOURCE_INT_SEQ:
*settings++ = CHANNEL_SETTING_INT_SEQ_INDEX;
*settings++ = CHANNEL_SETTING_INT_SEQ_MODULUS;
*settings++ = CHANNEL_SETTING_INT_SEQ_RANGE;
*settings++ = CHANNEL_SETTING_INT_SEQ_DIRECTION;
*settings++ = CHANNEL_SETTING_INT_SEQ_BROWNIAN_PROB;
*settings++ = CHANNEL_SETTING_INT_SEQ_LOOP_START;
*settings++ = CHANNEL_SETTING_INT_SEQ_LOOP_LENGTH;
*settings++ = CHANNEL_SETTING_INT_SEQ_STRIDE;
*settings++ = CHANNEL_SETTING_INT_SEQ_STRIDE_CV_SOURCE;
*settings++ = CHANNEL_SETTING_INT_SEQ_FRAME_SHIFT_PROB;
*settings++ = CHANNEL_SETTING_INT_SEQ_FRAME_SHIFT_RANGE;
*settings++ = CHANNEL_SETTING_INT_SEQ_INDEX_CV_SOURCE;
*settings++ = CHANNEL_SETTING_INT_SEQ_MODULUS_CV_SOURCE;
*settings++ = CHANNEL_SETTING_INT_SEQ_RANGE_CV_SOURCE;
*settings++ = CHANNEL_SETTING_INT_SEQ_RESET_TRIGGER;
break;
default:
break;
}
*settings++ = CHANNEL_SETTING_TRIGGER;
if (get_trigger_source() < CHANNEL_TRIGGER_CONTINUOUS_UP) {
*settings++ = CHANNEL_SETTING_CLKDIV;