-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.c
1463 lines (1159 loc) · 42.3 KB
/
params.c
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
/*
* params.c - Parameters
*
* Author: Dan Green (danngreen1@gmail.com), Hugo Paris (hugoplho@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 "globals.h"
#include "params.h"
#include "dig_inouts.h"
#include "exp_1voct.h"
#include "system_mode.h"
#include "rotary.h"
#include "rotation.h"
#include "math.h"
#include "leds.h"
#include "user_scales.h"
#include "lpf.h"
extern float exp_4096[4096];
extern __IO uint16_t adc_buffer[NUM_ADCS];
extern __IO uint16_t potadc_buffer[NUM_ADC3S];
enum Filter_Types filter_type=MAXQ;
enum Filter_Modes filter_mode=TWOPASS;
extern enum UI_Modes ui_mode;
extern uint8_t editscale_notelocked;
extern uint8_t editscale_tracklocked;
extern uint8_t editscale_voctlocked;
uint16_t old_adc_buffer[NUM_ADCS];
uint16_t old_potadc_buffer[NUM_ADC3S];
extern uint32_t rotary_state;
float trackcomp[2]={1.0,1.0};
int16_t trackoffset[2]={0,0};
//FREQ NUDGE/LOCK JACKS
float freq_nudge[NUM_CHANNELS]={1.0,1.0};
uint8_t ongoing_coarse_tuning[2]; // keeps track of ongoing coarse tunings
uint8_t ongoing_fine_tuning[2]; // keeps track of ongoing fine tunings
uint32_t fine_tuning_timeout[2]={0,0}; // timer to clear fine tuning when freq knob isn't being adjusted
float coarse_adj_led[NUM_CHANNELS];
float coarse_adj[NUM_CHANNELS]={1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
float freq_shift[NUM_CHANNELS];
uint16_t mod_mode_135;
uint16_t mod_mode_246;
uint8_t fine_timer[2]={1,1}; // flag for fine tune display soft release 1: soft release. 0: immediate release
float t_fo, t_fe; // buffers for freq nudge knob readouts
float f_nudge_odds=1, f_nudge_evens=1;
o_analog freq_jack_conditioning[2]; //LPF and bracketing for freq jacks
extern uint8_t do_LOCK135;
extern uint8_t do_LOCK246;
// ROTARY BUTTON
uint16_t rotary_mon;
uint16_t rotary_toggle;
uint16_t rotsw_up, rotsw_down;
uint32_t rotary_button_hold_ctr;
uint8_t user_turned_rotary=0;
//LOCK BUTTONS
uint8_t lock[NUM_CHANNELS]; // LATCH 0: channel unlocked, 1: channel locked
uint8_t lock_pressed[NUM_CHANNELS]; // MOMENTARY 1 while button pressed. 0 otherwise
uint8_t lock_up[NUM_CHANNELS]; // MOMENTARY 1 while button pressed. 0 otherwise
uint32_t lock_down[NUM_CHANNELS]; // COUNTER starts at 1 and goes up while button's pressed
uint8_t num_clear_coarse_staged = 0; // number of coarse tunings staged to be cleared
uint8_t already_handled_lock_release[NUM_CHANNELS];
//ENV OUTS
uint32_t env_prepost_mode;
// BINARY CONVENTION USED
// left-most bit is the sign of the coarse adjustment (+/- X semitone, 1 is going down semitones) rest is LED ON=1 OFF=0 is display order
int saved_envled_state[NUM_CHANNELS]={0b0001100,0b0001100,0b0001100,0b0001100,0b0001100,0b0001100};
int cur_envled_state=0b0000000; // envled state for coarse tuning
int fine_envled=0b000000; // envled state for fine tuning
enum Env_Out_Modes env_track_mode;
float envspeed_attack, envspeed_decay;
//ROTATE SCALE
uint16_t rotate_to_next_scale;
//FREQ BLOCKS
uint32_t freqblock = 0b00000000000000000000; // 20 freq positions
int num_freq_blocked=0;
int rotary_switch_b = 0;
//CHANNEL LEVELS/SLEW
float channel_level[NUM_CHANNELS]={0,0,0,0,0,0};
float CHANNEL_LEVEL_LPF=0;
//Q POT AND CV
uint32_t qval[NUM_CHANNELS];
float qval_goal[NUM_CHANNELS] = {0.0,0.0,0.0,0.0,0.0,0.0};
uint32_t qvalcv, qvalpot;
uint8_t q_locked[NUM_CHANNELS]={0,0,0,0,0,0};
uint8_t user_turned_Q_pot=0;
//Filters
uint8_t note[NUM_CHANNELS];
uint8_t scale[NUM_CHANNELS];
uint8_t scale_bank[NUM_CHANNELS];
//Motion
extern int8_t motion_spread_dest[NUM_CHANNELS];
extern int8_t motion_spread_dir[NUM_CHANNELS];
extern int8_t motion_scale_dest[NUM_CHANNELS];
extern int8_t motion_fadeto_note[NUM_CHANNELS];
extern int8_t motion_fadeto_scale[NUM_CHANNELS];
extern int8_t motion_rotate;
extern int8_t motion_scalecv_overage[NUM_CHANNELS];
extern int8_t motion_notejump;
extern float motion_morphpos[NUM_CHANNELS];
extern float rot_dir[6];
extern int8_t spread;
//Scale CV
uint8_t scale_cv[NUM_CHANNELS];
//Scale bank
uint8_t hover_scale_bank=0;
int16_t change_scale_mode=0;
uint8_t just_switched_to_change_scale_mode=0;
extern uint8_t cur_param_bank;
extern uint8_t cur_colsch;
uint32_t diff(uint32_t a, uint32_t b)
{
return (a>b)?(a-b):(b-a);
}
void set_default_param_values(void){
uint8_t i;
//Set default parameter values
for (i=0;i<NUM_CHANNELS;i++){
note[i]=i+5;
scale[i]=6;
motion_fadeto_scale[i]=scale[i];
motion_scale_dest[i]=scale[i];
scale_bank[i]=0;
rot_dir[i]=0;
motion_spread_dir[i]=0;
motion_spread_dest[i]=note[i];
motion_fadeto_note[i]=note[i];
motion_morphpos[i]=0;
freq_shift[i]=0;
motion_scalecv_overage[i]=0;
}
motion_notejump=0;
motion_rotate=0;
filter_type=MAXQ;
filter_mode=TWOPASS;
trackcomp[0]=1.0;
trackcomp[1]=1.0;
trackoffset[0]=0;
trackoffset[1]=0;
}
void param_read_freq(void){
uint8_t i,j,k;
static uint8_t sleep_range_saved =0, first_run[2]={1,1};
static uint8_t old_switch_state[2]; // previous 135/246 switch state
static uint8_t fknob_lock[2]={0,0}; // ==1 disables nudge knob for odds/evens
static uint8_t coarse_lock[NUM_CHANNELS]={1,1,1,1,1,1}; // same as fknoblock, for the coarse tuning
static float sleep_range=800; // number of counts that won't wake up nudge knob after module is turned on
static float wakeup_evens[2], wakeup_odds[2]; // range at which f_nudge knob wakes up after module turns on: low end of range is [0], high end is [1].
static float old_f_nudge_odds=1, old_f_nudge_evens=1; // keeps track of freq knob rotation
static float f_nudge_odds_buf, f_nudge_evens_buf;
static float f_nudge_buf[2];
static float f_shift_odds=1, f_shift_evens=1;
static float saved_coarse_adj[NUM_CHANNELS]={1.0,1.0,1.0,1.0,1.0,1.0}; // value to cross in order to unlock coarse tuning
static uint8_t clear_coarse_staged[NUM_CHANNELS]={0,0,0,0,0,0};
int odds[3] ={0, 2, 4}; // ch 1, 3, 5
int evens[3]={1, 3, 5}; // ch 2, 4, 6
int32_t freq_jack_cv;
// FREQ SHIFT
//With the Maxq filter, the Freq Nudge pot alone adjusts the "nudge", and the CV jack is 1V/oct shift
//With the BpRe filter, the Freq Nudge pot plus CV jack adjusts the "nudge", and there is no 1V/oct shift
if (filter_type==MAXQ)
{
// Read buffer knob and normalize input: 0-1
t_fo=(float)(adc_buffer[FREQNUDGE1_ADC]);
t_fe=(float)(adc_buffer[FREQNUDGE6_ADC]);
// after turning on the module
// keep freq nudge knobs output at 0 as long as knobs are within sleep range
if (!sleep_range_saved){
wakeup_odds[0] =t_fo - sleep_range;
wakeup_odds[1] =t_fo + sleep_range;
wakeup_evens[0]=t_fe - sleep_range;
wakeup_evens[1]=t_fe + sleep_range;
sleep_range_saved = 1; // ensures this only gets computed once
}
if (first_run[0]){
if ((t_fo > wakeup_odds[0]) && (t_fo < wakeup_odds[1])) {t_fo=0.0;}
else{first_run[0]=0;}
}
if (first_run[1]){
if ((t_fe > wakeup_evens[0]) && (t_fe < wakeup_evens[1])) {t_fe=0.0;}
else{first_run[1]=0;}
}
// Freq shift odds
// is odds cv input Low-passed and adjusted for 1V/Oct
if (trackcomp[0]<0.5 || trackcomp[0]>2.0) trackcomp[0]=1.0; //sanity check
freq_jack_cv = (adc_buffer[FREQCV1_ADC] + trackoffset[0] + BASE_TRACKOFFSET) * trackcomp[0];
if (freq_jack_cv<0) freq_jack_cv=0;
if (freq_jack_cv>4095) freq_jack_cv=4095;
freq_jack_conditioning[0].raw_val = freq_jack_cv;
apply_fir_lpf(&(freq_jack_conditioning[0]));
apply_bracket(&(freq_jack_conditioning[0]));
f_shift_odds = exp_1voct[(uint32_t)freq_jack_conditioning[0].bracketed_val];
// Freq shift evens
// is odds cv input Low-passed and adjusted for 1V/Oct
if (trackcomp[1]<0.5 || trackcomp[1]>2.0) trackcomp[1]=1.0; //sanity check
freq_jack_cv = (adc_buffer[FREQCV6_ADC] + trackoffset[1] + BASE_TRACKOFFSET) * trackcomp[1];
if (freq_jack_cv<0) freq_jack_cv=0;
if (freq_jack_cv>4095) freq_jack_cv=4095;
freq_jack_conditioning[1].raw_val = freq_jack_cv;
apply_fir_lpf(&(freq_jack_conditioning[1]));
apply_bracket(&(freq_jack_conditioning[1]));
f_shift_evens = exp_1voct[(uint32_t)freq_jack_conditioning[1].bracketed_val];
// FREQ NUDGE
// SEMITONE FINE TUNE
f_nudge_odds = 1 + t_fo/55000.0; // goes beyond semitone
f_nudge_evens = 1 + t_fe/55000.0;
// 12-SEMITONE COARSE TUNE
// [freq nudge knob] + [lock button] -> coarse tune
// ODDS
if (fabsf(f_nudge_odds - old_f_nudge_odds) > NUDGEPOT_MIN_CHANGE){
// inform led_ring_c that fine tuning is ongoing
ongoing_fine_tuning[0]=1;
// update buffers
f_nudge_odds_buf = old_f_nudge_odds;
old_f_nudge_odds = f_nudge_odds;
// enable fine tuning timer for slow display release
fine_timer[0] = 1;
// for each odd channel
for (i=0;i<3;i++){
j = odds[i];
//update fine tuning env led
fine_envled = fine_envled | (1<<(5-j));
// inform led_ring_c that fine tuning is ongoing on odd chan(s)
ongoing_fine_tuning[0] = 1;
fine_tuning_timeout[0] = 1; // stage timeout counter
// If lock button pressed, disable fine tuning and process coarse tuning
if (lock_pressed[j]){
// inform led_ring_c that coarse tuning is ongoing
ongoing_coarse_tuning[0]=1;
// disable fine tuning timer for instantaneous display release
fine_timer[0] = 0;
// coarse tuning superseds fine tuning
ongoing_fine_tuning[0] = 0;
// disable fine adjustments when setting coarse tuning
if (fknob_lock[0]==0){
f_nudge_buf[0] = f_nudge_odds_buf;
fknob_lock[0] = 1;
}
// Compute and apply coarse tuning
// - 6 semitones
if (t_fo < 1.0*4095.0/13.0){coarse_adj[j]=1.0/1.41421356237;cur_envled_state=0b1111111;}
else if ((t_fo >= 1.0*4095.0/13.0) && (t_fo < 2.0*4095.0/13.0)){coarse_adj[j]=1.0/1.33483985417;cur_envled_state=0b1011111;}
else if ((t_fo >= 2.0*4095.0/13.0) && (t_fo < 3.0*4095.0/13.0)){coarse_adj[j]=1.0/1.25992104989;cur_envled_state=0b1001111;}
else if ((t_fo >= 3.0*4095.0/13.0) && (t_fo < 4.0*4095.0/13.0)){coarse_adj[j]=1.0/1.189207115; cur_envled_state=0b1000111;}
else if ((t_fo >= 4.0*4095.0/13.0) && (t_fo < 5.0*4095.0/13.0)){coarse_adj[j]=1.0/1.12246204831;cur_envled_state=0b1000011;}
else if ((t_fo >= 5.0*4095.0/13.0) && (t_fo < 6.0*4095.0/13.0)){coarse_adj[j]=1.0/1.05946309436;cur_envled_state=0b1000001;}
//Tuned
else if ((t_fo >= 6.0*4095.0/13.0) && (t_fo < 7.0*4095.0/13.0)){coarse_adj[j]=1.0;cur_envled_state=0b0001100;}
// + 6 semitones
else if ((t_fo >= 7.0*4095.0/13.0) && (t_fo < 8.0*4095.0/13.0)){coarse_adj[j]=1.05946309436; cur_envled_state=0b0100000;}
else if ((t_fo >= 8.0*4095.0/13.0) && (t_fo < 9.0*4095.0/13.0)){coarse_adj[j]=1.12246204831; cur_envled_state=0b0110000;}
else if ((t_fo >= 9.0*4095.0/13.0) && (t_fo < 10.0*4095.0/13.0)){coarse_adj[j]=1.189207115; cur_envled_state=0b0111000;}
else if ((t_fo >= 10.0*4095.0/13.0) && (t_fo < 11.0*4095.0/13.0)){coarse_adj[j]=1.25992104989; cur_envled_state=0b0111100;}
else if ((t_fo >= 11.0*4095.0/13.0) && (t_fo < 12.0*4095.0/13.0)){coarse_adj[j]=1.33483985417; cur_envled_state=0b0111110;}
else if ((t_fo >= 12.0*4095.0/13.0) && (t_fo < 13.0*4095.0/13.0)){coarse_adj[j]=1.41421356237; cur_envled_state=0b0111111;}
// apply/clear coarse lock and corresponding envled_state as needed
if ( fabsf(coarse_adj[j] - saved_coarse_adj[j])< 0.001){coarse_lock[j]=0;}
if(coarse_lock[j]){coarse_adj[j] = saved_coarse_adj[j];cur_envled_state=saved_envled_state[j];}
else{saved_coarse_adj[j]=coarse_adj[j];saved_envled_state[j]=cur_envled_state;}
// apply coarse adjustment (in case channel is locked)
freq_nudge[j] = f_nudge_buf[0] * coarse_adj[j];
already_handled_lock_release[j] = 1; //set this flag so that we don't do anything when the button is released
}else{coarse_lock[j]=1;}
}
}
// EVENS
if (fabsf(f_nudge_evens - old_f_nudge_evens) > NUDGEPOT_MIN_CHANGE){
// inform led_ring_c that fine tuning is ongoing
ongoing_fine_tuning[1]=1;
// update buffers
f_nudge_evens_buf = old_f_nudge_evens;
old_f_nudge_evens=f_nudge_evens;
// enable fine tuning timer for slow display release
fine_timer[1] = 1;
// for each even channel
for (i=0;i<3;i++){
j = evens[i];
//update fine tuning env led
fine_envled = fine_envled | (1<<(5-j));
// inform led_ring_c that fine tuning is ongoing on even chan(s)
ongoing_fine_tuning[1] = 1;
fine_tuning_timeout[1] = 1; // stage timeout counter
// If lock button pressed, disable fine tuning and process coarse tuning
if (lock_pressed[j]){
// inform led_ring_c that coarse tuning is ongoing
ongoing_coarse_tuning[1]=1;
// coarse tuning superseds fine tuning
ongoing_fine_tuning[1] = 0;
// disable fine tuning timer for instantaneous display release
fine_timer[1] = 0;
// disable fine adjustments when setting coarse tuning
if (fknob_lock[1]==0){
f_nudge_buf[1] = f_nudge_evens_buf;
fknob_lock[1] = 1;
}
// Compute and apply coarse tuning
// - 6 semitones
if (t_fe < 1.0*4095.0/13.0){coarse_adj[j]=1.0/1.41421356237;cur_envled_state=0b1111111;}
else if ((t_fe >= 1.0*4095.0/13.0) && (t_fe < 2.0*4095.0/13.0)){coarse_adj[j]=1.0/1.33483985417;cur_envled_state=0b1011111;}
else if ((t_fe >= 2.0*4095.0/13.0) && (t_fe < 3.0*4095.0/13.0)){coarse_adj[j]=1.0/1.25992104989;cur_envled_state=0b1001111;}
else if ((t_fe >= 3.0*4095.0/13.0) && (t_fe < 4.0*4095.0/13.0)){coarse_adj[j]=1.0/1.189207115; cur_envled_state=0b1000111;}
else if ((t_fe >= 4.0*4095.0/13.0) && (t_fe < 5.0*4095.0/13.0)){coarse_adj[j]=1.0/1.12246204831;cur_envled_state=0b1000011;}
else if ((t_fe >= 5.0*4095.0/13.0) && (t_fe < 6.0*4095.0/13.0)){coarse_adj[j]=1.0/1.05946309436;cur_envled_state=0b1000001;}
//Tuned
else if ((t_fe >= 6.0*4095.0/13.0) && (t_fe < 7.0*4095.0/13.0)){coarse_adj[j]=1.0;cur_envled_state=0b0001100;}
// + 6 semitones
else if ((t_fe >= 7.0*4095.0/13.0) && (t_fe < 8.0*4095.0/13.0)){coarse_adj[j]=1.05946309436; cur_envled_state=0b0100000;}
else if ((t_fe >= 8.0*4095.0/13.0) && (t_fe < 9.0*4095.0/13.0)){coarse_adj[j]=1.12246204831; cur_envled_state=0b0110000;}
else if ((t_fe >= 9.0*4095.0/13.0) && (t_fe < 10.0*4095.0/13.0)){coarse_adj[j]=1.189207115; cur_envled_state=0b0111000;}
else if ((t_fe >= 10.0*4095.0/13.0) && (t_fe < 11.0*4095.0/13.0)){coarse_adj[j]=1.25992104989; cur_envled_state=0b0111100;}
else if ((t_fe >= 11.0*4095.0/13.0) && (t_fe < 12.0*4095.0/13.0)){coarse_adj[j]=1.33483985417; cur_envled_state=0b0111110;}
else if ((t_fe >= 12.0*4095.0/13.0) && (t_fe < 13.0*4095.0/13.0)){coarse_adj[j]=1.41421356237; cur_envled_state=0b0111111;}
// apply/clear coarse lock and corresponding envled_state as needed
if ( fabsf(coarse_adj[j] - saved_coarse_adj[j])< 0.001){coarse_lock[j]=0;}
if(coarse_lock[j]){coarse_adj[j] = saved_coarse_adj[j];cur_envled_state=saved_envled_state[j];}
else{saved_coarse_adj[j]=coarse_adj[j];saved_envled_state[j]=cur_envled_state;}
// apply coarse adjustment (in case channel is locked)
freq_nudge[j] = f_nudge_buf[1] * coarse_adj[j];
already_handled_lock_release[j] = 1;
}else{coarse_lock[j]=1;}
}
}
// MORE FINE/COARSE TUNE DISPLAY CASES
// stop displaying fine tune if fine tune knob is not being adjusted but leave display on for timed duration
// special case: both fine tunes are ajusted at once. Leave more time to facilitate tuning odds/even together
else if ((ongoing_fine_tuning[0]==1) && (ongoing_fine_tuning[1]==1)){
fine_tuning_timeout[0]+=1;
fine_tuning_timeout[1]+=1;
if((fine_tuning_timeout[0]> (DISPLAYTIME_NUDGEBOTHSIDES * fine_timer[0])) && (fine_tuning_timeout[1]> (DISPLAYTIME_NUDGEBOTHSIDES * fine_timer[1])) ){
ongoing_fine_tuning[0]=0;
ongoing_fine_tuning[1]=0;
fine_envled = fine_envled & 0b000000; // turn off all env led
fine_tuning_timeout[0]=0;
fine_tuning_timeout[1]=0;
}
// turn off odds fine tune display after short time
} else if ((ongoing_fine_tuning[0]==1) && (ongoing_fine_tuning[1]==0)){
fine_tuning_timeout[0]+=1;
if(fine_tuning_timeout[0]> (DISPLAYTIME_NUDGEONESIDE * fine_timer[0])){
ongoing_fine_tuning[0]=0;
fine_envled = fine_envled & 0b010101; // turn off odds env led
fine_tuning_timeout[0]=0;
}
// turn off evens fine tune display after short time
} else if ((ongoing_fine_tuning[1]==1) && (ongoing_fine_tuning[0]==0)){
fine_tuning_timeout[1]+=1;
if(fine_tuning_timeout[1]> (DISPLAYTIME_NUDGEONESIDE * fine_timer[1])){
ongoing_fine_tuning[1]=0;
fine_envled = fine_envled & 0b101010; // turn off even env led
fine_tuning_timeout[1]=0;
}
}
// display fine tune when locked button is held down
if (lock_pressed[0] || lock_pressed[2] || lock_pressed[4]) {
fine_envled=fine_envled | 0b101010;
ongoing_fine_tuning[0]=1;
fine_tuning_timeout[0]=1;
if (!ongoing_coarse_tuning[0]) fine_timer[0] = 1; //enable soft release
}
if (lock_pressed[1] || lock_pressed[3] || lock_pressed[5]) {
fine_envled=fine_envled | 0b010101;
ongoing_fine_tuning[1]=1;
fine_tuning_timeout[1]=1;
if (!ongoing_coarse_tuning[1]) fine_timer[1] = 1; //enable soft release
}
// display fine tune when lock switch state is changed
// switch 135
if (mod_mode_135!=old_switch_state[0]){
old_switch_state[0] = mod_mode_135;
fine_envled=fine_envled | 0b101010;
ongoing_fine_tuning[0]=1;
fine_tuning_timeout[0]=1; //reset the timer so that we get the full soft-release period
fine_timer[0] = 1; //enable soft release
}
// switch 246
if (mod_mode_246!=old_switch_state[1]){
old_switch_state[1] = mod_mode_246;
fine_envled=fine_envled | 0b010101;
ongoing_fine_tuning[1]=1;
fine_tuning_timeout[1]=1; //reset the timer so that we get the full soft-release period
fine_timer[1] = 1; //enable soft release
}
// exit coarse tuning display as needed
if(ongoing_coarse_tuning[0]){
if (!lock_pressed[0] && !lock_pressed[2] && !lock_pressed[4]){
ongoing_coarse_tuning[0]=0;
}
}
if(ongoing_coarse_tuning[1]){
if (!lock_pressed[1] && !lock_pressed[3] && !lock_pressed[5]){
ongoing_coarse_tuning[1]=0;
}
}
// LOCK SWITCHES
// nudge and shift always enabled on 1 and 6
// ... and enabled on 3,5,2 and 4 based on the lock toggles
// ODDS
// enable freq nudge and shift for "135 mode"
if (!lock[0]){
// Prevent fknob from adjusting freq_nudge when it's fknob_lock(ed)
if (fknob_lock[0]==1){ // if freq knob is locked
// during coarse adj
if((fabsf(f_nudge_odds - f_nudge_buf[0]) < 0.001) && !lock_pressed[0]){fknob_lock[0]=0;} // unlock odds/evens freq knob if it crosses the value it was locked on and lock button isn't pressed
else {freq_nudge[0]=f_nudge_buf[0] * coarse_adj[0];} // use buffered value otherwise
}
if (fknob_lock[0]==0){freq_nudge[0]=f_nudge_odds * coarse_adj[0];}; // if freq knob is unlocked, apply coarse adjustment to current f_knob fine tune
}
freq_shift[0]=f_shift_odds; // apply freq CV in
if (mod_mode_135==135){
if (!lock[2]){
if (fknob_lock[0]==1){
if((fabsf(f_nudge_odds - f_nudge_buf[0]) < 0.001) && !lock_pressed[2]){fknob_lock[0]=0;}
else {freq_nudge[2]=f_nudge_buf[0] * coarse_adj[2];} //
}
if (fknob_lock[0]==0){freq_nudge[2]=f_nudge_odds * coarse_adj[2];};
}
freq_shift[2]=f_shift_odds;
if (!lock[4]){
if (fknob_lock[0]==1){
if((fabsf(f_nudge_odds - f_nudge_buf[0]) < 0.001) && !lock_pressed[4]){fknob_lock[0]=0;}
else {freq_nudge[4]=f_nudge_buf[0] * coarse_adj[4];} //
}
if (fknob_lock[0]==0){freq_nudge[4]=f_nudge_odds * coarse_adj[4];};
}
freq_shift[4]=f_shift_odds;
}
// disable freq nudge and shift on channel 3 and 5 when in "1 mode"
else {
if (!lock[2]){
freq_nudge[2]= coarse_adj[2];
}
freq_shift[2]=1.0;
if (!lock[4]){
freq_nudge[4]= coarse_adj[4];
}
freq_shift[4]=1.0;
}
//EVENS
if (!lock[5]){
if (fknob_lock[1]==1){
if((fabsf(f_nudge_evens - f_nudge_buf[1]) < 0.001) && !lock_pressed[5]){fknob_lock[1]=0;}
else {freq_nudge[5]=f_nudge_buf[1] * coarse_adj[5];} //
}
if (fknob_lock[1]==0){freq_nudge[5]=f_nudge_evens * coarse_adj[5];};
}
freq_shift[5]=f_shift_evens;
if (mod_mode_246==246){
if (!lock[1]){
if (fknob_lock[1]==1){
if((fabsf(f_nudge_evens - f_nudge_buf[1]) < 0.001) && !lock_pressed[1]){fknob_lock[1]=0;}
else {freq_nudge[1]=f_nudge_buf[1] * coarse_adj[1];} //
}
if (fknob_lock[1]==0){freq_nudge[1]=f_nudge_evens * coarse_adj[1];};
}
freq_shift[1]=f_shift_evens;
if (!lock[3]){
if (fknob_lock[1]==1){
if((fabsf(f_nudge_evens - f_nudge_buf[1]) < 0.001) && !lock_pressed[3]){fknob_lock[1]=0;}
else {freq_nudge[3]=f_nudge_buf[1] * coarse_adj[3];} //
}
if (fknob_lock[1]==0){freq_nudge[3]=f_nudge_evens * coarse_adj[3];};
}
freq_shift[3]=f_shift_evens;
}
// disable freq nudge and shift on channel 2 and 4 when in "6 mode"
else {
if (!lock[3]){
freq_nudge[3]= coarse_adj[3];
}
freq_shift[3]=1.0;
if (!lock[1]){
freq_nudge[1]= coarse_adj[1];
}
freq_shift[1]=1.0;
}
// CLEAR COARSE TUNING AT BUTTON RELEASE AFTER 6 BUTTON PRESS (~3s)
// if locked buttons have all been pressed for +3s
if (lock_down[0] > 36000 &&
lock_down[1] > 36000 &&
lock_down[2] > 36000 &&
lock_down[3] > 36000 &&
lock_down[4] > 36000 &&
lock_down[5] > 36000 ){
clear_coarse_staged[0] = 1;
clear_coarse_staged[1] = 1;
clear_coarse_staged[2] = 1;
clear_coarse_staged[3] = 1;
clear_coarse_staged[4] = 1;
clear_coarse_staged[5] = 1;
num_clear_coarse_staged=6;
} else{
coarse_adj_led[0]=coarse_adj[0];
coarse_adj_led[1]=coarse_adj[1];
coarse_adj_led[2]=coarse_adj[2];
coarse_adj_led[3]=coarse_adj[3];
coarse_adj_led[4]=coarse_adj[4];
coarse_adj_led[5]=coarse_adj[5];
}
if (num_clear_coarse_staged !=0){
for (k=0;k<6;k++){
if (clear_coarse_staged[k]){
// do not change lock states upon button release
already_handled_lock_release[k]=1;
if (!lock_pressed[k]){
// clear coarse adjustment
coarse_adj[k]=1.0;
//update saved coarse adjustement + env led state & lock to it
saved_coarse_adj[k]=coarse_adj[k];
saved_envled_state[k]=0b0001100;
coarse_lock[k]=1;
// inform led.c that this coarse tuning was cleared
coarse_adj_led[k] = coarse_adj[k];
// unstage coarse adjustment
clear_coarse_staged[k]=0;
// remove channel from list of adjustments to be cleared
num_clear_coarse_staged-=1;
//
already_handled_lock_release[k]=0;
}
}
}
}
}else{
t_fo=(float)(adc_buffer[FREQNUDGE1_ADC]+adc_buffer[FREQCV1_ADC])/4096.0;
if (t_fo>1.0) t_fo=1.0;
t_fe=(float)(adc_buffer[FREQNUDGE6_ADC]+adc_buffer[FREQCV6_ADC])/4096.0;
if (t_fe>1.0) t_fe=1.0;
f_shift_odds=1.0;
f_shift_evens=1.0;
f_nudge_odds *= FREQNUDGE_LPF;
f_nudge_odds += (1.0f-FREQNUDGE_LPF)*t_fo;
f_nudge_evens *= FREQNUDGE_LPF;
f_nudge_evens += (1.0f-FREQNUDGE_LPF)*t_fe;
if (!lock[0]){
freq_nudge[0]=f_nudge_odds;
}
freq_shift[0]=f_shift_odds;
if (mod_mode_135==135){
if (!lock[2]){
freq_nudge[2]=f_nudge_odds;
}
freq_shift[2]=f_shift_odds;
if (!lock[4]){
freq_nudge[4]=f_nudge_odds;
}
freq_shift[4]=f_shift_odds;
} else {
if (!lock[2]){
freq_nudge[2]=0.0;
}
freq_shift[2]=1.0;
if (!lock[4]){
freq_nudge[4]=0.0;
}
freq_shift[4]=1.0;
}
if (!lock[5]){
freq_nudge[5]=f_nudge_evens;
}
freq_shift[5]=f_shift_evens;
if (mod_mode_246==246){
if (!lock[1]){
freq_nudge[1]=f_nudge_evens;
}
freq_shift[1]=f_shift_evens;
if (!lock[3]){
freq_nudge[3]=f_nudge_evens;
}
freq_shift[3]=f_shift_evens;
} else {
if (!lock[1]){
freq_nudge[1]=0.0;
}
freq_shift[1]=1.0;
if (!lock[3]){
freq_nudge[3]=0.0;
}
freq_shift[3]=1.0;
}
}
}
void param_read_q(void){
int32_t t, i, num_locked;
static float qpot_lpf=0;
static float old_qpot_lpf=0xFFFF;
static uint32_t poll_ctr=0;
static float prev_qval[NUM_CHANNELS] = {0.0,0.0,0.0,0.0,0.0,0.0};
if (poll_ctr++>15){ // UPDATE RATE
poll_ctr=0;
//Check jack + LPF
t=adc_buffer[QVAL_ADC];
if (diff(t,old_adc_buffer[QVAL_ADC])>15)
{
old_adc_buffer[QVAL_ADC]=adc_buffer[QVAL_ADC];
t=adc_buffer[QVAL_ADC];
qvalcv *= QCV_LPF;
qvalcv += (1.0f-QCV_LPF)*t;
}
//Check pot + LPF
t = potadc_buffer[QPOT_ADC];
qpot_lpf *= QPOT_LPF;
qpot_lpf += (1.0f-QPOT_LPF)*t;
num_locked=0;
if (fabsf(qpot_lpf - old_qpot_lpf) > QPOT_MIN_CHANGE){
old_qpot_lpf=qpot_lpf;
if (ui_mode==PLAY){
for (i=0;i<6;i++){
if (lock_pressed[i]){ //if lock button is being held down, then q_lock the channel and assign its qval
q_locked[i]=1;
user_turned_Q_pot=1;
prev_qval[i]=qval_goal[i];
qval_goal[i]=qpot_lpf;
num_locked++;
}
}
}
//otherwise, if no lock buttons were held down, then change the qvalpot (which effects all non-q_locked channels)
}
if (!num_locked) qvalpot=qpot_lpf;
for (i=0;i<NUM_CHANNELS;i++){
if (!q_locked[i]){
prev_qval[i]=qval_goal[i];
qval_goal[i]=qvalcv + qvalpot;
if (qval_goal[i]>4095) qval_goal[i]=4095;
}
}
}
// SMOOTH OUT DATA BETWEEN ADC READS
for (i=0;i<NUM_CHANNELS;i++){
// smooth output of channels that are not q-locked
if (!q_locked[i]){
qval[i] = (uint32_t)(prev_qval[i] + (poll_ctr * (qval_goal[i]-prev_qval[i])/15.0));
// use locked qval for q-locked channels
}else{
qval[i] = qval_goal[i];
}
}
}
void param_read_one_q(int16_t i){
int32_t t, num_locked;
static float qpot_lpf = 0.0;
static float old_qpot_lpf = 0.0;
//static poll_ctr=0;
//if (poll_ctr++>10){poll_ctr=0;
//Check jack
t=adc_buffer[QVAL_ADC];
if (diff(t,old_adc_buffer[QVAL_ADC])>15){
old_adc_buffer[QVAL_ADC]=adc_buffer[QVAL_ADC];
qvalcv=adc_buffer[QVAL_ADC];
}
// LPF qpot
t = potadc_buffer[QPOT_ADC];
qpot_lpf *= QPOT_LPF;
qpot_lpf += (1.0f-QPOT_LPF)*t;
num_locked=0;
if (fabsf(qpot_lpf - old_qpot_lpf) > QPOT_MIN_CHANGE){
old_qpot_lpf=qpot_lpf;
if (ui_mode==PLAY){
if (lock_pressed[i]){ //if lock button is being held down, then q_lock the channel and assign its qval
q_locked[i]=1;
user_turned_Q_pot=1;
qval[i]=qpot_lpf;
num_locked++;
}
}
}
if (!num_locked) qvalpot=qpot_lpf;
if (!q_locked[i]){
qval[i]=qvalcv + qvalpot;
if (qval[i]>4095) qval[i]=4095;
}
//}
}
void param_read_switches(void)
{
static uint32_t old_cvlag=0xFFFF;
uint32_t lag_val;
float t_LEVEL_LPF_DECAY, t_LEVEL_LPF_ATTACK;
/*** Read Switches ***/
//PRE|POST switch
if (ENV_MODE){
env_prepost_mode=PRE;
} else {
env_prepost_mode=POST;
}
if (MOD246){
mod_mode_246=6;
} else {
mod_mode_246=246;
}
if (MOD135){
mod_mode_135=1;
} else {
mod_mode_135=135;
}
if (RANGE_MODE){
rotate_to_next_scale=1;
} else {
rotate_to_next_scale=0;
}
//float ga = exp(-1.0f/(SampleRate*AttackTimeInSecond));
if (ENVSPEEDFAST)
{
if (env_track_mode != ENV_VOLTOCT && env_track_mode!=ENV_FAST) //changed state
{
if (ROTARY_SW)
env_track_mode=ENV_VOLTOCT;
else
env_track_mode=ENV_FAST;
envspeed_attack=0.9990;
envspeed_decay=0.9991;
}
} else {
if (ENVSPEEDSLOW){
env_track_mode=ENV_SLOW;
envspeed_attack=0.9995;
envspeed_decay=0.9999;
} else { //trigger
env_track_mode=ENV_TRIG;
envspeed_attack=0.0;
envspeed_decay=0.0;
}
}
lag_val=CVLAG;
if (old_cvlag!=lag_val){
old_cvlag=lag_val;
if (lag_val){ //CVLAG switch is flipped on, latch the current Morph adc value and use that to calculate LPF coefficients
//Read from morph pot, and scale to 137..4095
lag_val = (adc_buffer[MORPH_ADC]/2) + 137;
if (lag_val>4095) lag_val=4095;
CHANNEL_LEVEL_LPF = 1.0 - exp_4096[lag_val];
//if (lag_val<2000) lag_val=2000; //force some amount of CV Slew even if Morph knob is all the way down
//if (lag_val>4095) lag_val=4095;
//t_LEVEL_LPF_ATTACK= 1.0 - (1.0/((lag_val)*0.10));
//t_LEVEL_LPF_DECAY= 1.0 - (1.0/((lag_val)*0.25));
//if (t_LEVEL_LPF_ATTACK<0 || t_LEVEL_LPF_ATTACK>=1) LEVEL_LPF_ATTACK=LAG_ATTACK_MIN_LPF;
//else LEVEL_LPF_ATTACK = t_LEVEL_LPF_ATTACK;
//if (t_LEVEL_LPF_DECAY<0 || t_LEVEL_LPF_DECAY>=1) LEVEL_LPF_DECAY=LAG_DECAY_MIN_LPF;
//else LEVEL_LPF_DECAY = t_LEVEL_LPF_DECAY;
}else{
//LEVEL_LPF_ATTACK= 0.778; //0.995 ^50 lag_val = 2000
//LEVEL_LPF_DECAY= 0.904; //lag_val = 2000
CHANNEL_LEVEL_LPF = CHANNEL_LEVEL_MIN_LPF;
}
}
}
uint8_t num_locks_pressed(void){
uint8_t i,j;
for (i=0,j=0;i<NUM_CHANNELS;i++){
if (lock_pressed[i]) j++;
}
return j;
}
void process_lock_buttons(void)
{
uint8_t i;
for (i=0;i<6;i++){
if (LOCKBUTTON(i)){
lock_up[i]=1;
if (lock_down[i]!=0
&& lock_down[i]!=0xFFFFFFFF) //don't wrap our counter!
lock_down[i]++;
if (lock_down[i]==5){ //first time we notice lock button is solidly down...
lock_pressed[i]=1;
user_turned_Q_pot=0;
already_handled_lock_release[i]=0;
}
if (ui_mode==PLAY){
//check to see if it's been held down for a while, and the user hasn't turned the Q pot
//if so, then we should unlock immediately, but not unlock the q_lock
if (lock_down[i]>LOCK_BUTTON_QUNLOCK_HOLD_TIME && lock[i] && !user_turned_Q_pot && q_locked[i]) {
lock[i]=0;
LOCKLED_OFF(i);
already_handled_lock_release[i]=1; //set this flag so that we don't do anything when the button is released
lock_down[i]=0; //stop checking this button until it's released
}
}
if (ui_mode==SELECT_PARAMS){
if (lock_down[i]>LOCK_BUTTON_LONG_HOLD_TIME){
if (num_locks_pressed() == 6 && ROTARY_SW){
factory_reset();
exit_system_mode(0); //do not restore lock[] because factory reset clears them
while (ROTARY_SW) {;}
already_handled_lock_release[0]=1;already_handled_lock_release[1]=1;already_handled_lock_release[2]=1;
already_handled_lock_release[3]=1;already_handled_lock_release[4]=1;already_handled_lock_release[5]=1;
} else {
exit_system_mode(1); //restore lock[] so that it gets saved
save_param_bank(i);
already_handled_lock_release[i]=1;
lock_down[i]=0; //stop checking this button until it's released
}
}
}