-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxEuclidConfig.py
1783 lines (1500 loc) · 81 KB
/
lxEuclidConfig.py
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
from _thread import allocate_lock
from random import randint
from micropython import const
from utime import ticks_ms, sleep
from ucollections import OrderedDict
from cvManager import CvAction, CvChannel, LOW_PERCENTAGE_RISING_THRESHOLD, percent_to_exp_percent
T_CLK_LED_ON_MS = const(10)
T_GATE_ON_MS = const(10)
MAX_BEATS = const(32)
MAJOR_E_ADDR = const(0)
MINOR_E_ADDR = const(1)
FIX_E_ADDR = const(2)
CV_PAGE_MAX = const(2)
PRESET_PAGE_MAX = const(3)
PADS_PAGE_MAX = const(2)
CHANNEL_PAGE_MAX = const(4)
MENU_PAGE_MAX = const(2)
PRESCALER_LIST = [1, 2, 3, 4, 6, 8, 16]
# pass from 360° (in capacitive circle referential) to 0..steps
def angle_to_index(angle, steps, offset_45=False):
angle = 180 - angle
if offset_45 == True:
angle = angle + 45
step_angle = 360/steps
return int((int(((angle+(step_angle/2)) % 360)/step_angle)) % steps)
class EuclideanRhythmParameters:
def __init__(self, beats, pulses, offset, pulses_probability, prescaler_index=0, gate_length_ms=T_GATE_ON_MS, randomize_gate_length=False, algo_index=0):
self.set_parameters(beats, pulses, offset, pulses_probability,
prescaler_index, gate_length_ms, randomize_gate_length, algo_index)
def set_parameters_from_rhythm(self, euclideanRhythmParameters):
self.set_parameters(euclideanRhythmParameters.beats, euclideanRhythmParameters.pulses, euclideanRhythmParameters.offset, euclideanRhythmParameters.pulses_probability,
euclideanRhythmParameters.prescaler_index, euclideanRhythmParameters.gate_length_ms, euclideanRhythmParameters.randomize_gate_length, euclideanRhythmParameters.algo_index)
def set_parameters(self, beats, pulses, offset, pulses_probability, prescaler_index, gate_length_ms, randomize_gate_length, algo_index):
self._prescaler_index = prescaler_index
self.prescaler = PRESCALER_LIST[prescaler_index]
self.beats = beats
self.pulses_probability = pulses_probability
if pulses > beats:
self.pulses = beats
else:
self.pulses = pulses
if offset > beats:
self.offset = beats
else:
self.offset = offset
if self.beats < 1:
self.beats = 1
if self.pulses < 0:
self.pulses = 0
if self.pulses <= 1:
self.pulses_set_0_1 = True
else:
self.pulses_set_0_1 = False
self.__pulses_ratio = self.pulses / self.beats
self.clear_gate_needed = False
self.gate_length_ms = gate_length_ms
self.randomize_gate_length = randomize_gate_length
self.randomized_gate_length_ms = gate_length_ms
self.algo_index = algo_index
@property
def prescaler_index(self):
return self._prescaler_index
@prescaler_index.setter
def prescaler_index(self, prescaler_index):
self._prescaler_index = prescaler_index
class EuclideanRhythm(EuclideanRhythmParameters):
def __init__(self, beats, pulses, offset, pulses_probability, prescaler_index=0):
EuclideanRhythmParameters.__init__(
self, beats, pulses, offset, pulses_probability, prescaler_index)
self.current_step = 0
self.prescaler = PRESCALER_LIST[prescaler_index]
self.prescaler_rhythm_counter = 0
# var used in get_current_step function. Since it's called in interrupt we can't create memory
# so we create those buffer before
self.get_current_step_offset = 0
self.get_current_step_beats = 0
self.global_cv_offset = 0
self.global_cv_probability = 0
# when we start, it's like we are in a reset step and we wait for the 1st
# clock to play the first beat
self.reset_step_occure = True
# CV linked attributes
self.has_cv_beat = False
self.has_cv_pulse = False
self.has_cv_offset = False
self.has_cv_prob = False
self.cv_percent_beat = int(0)
self.cv_percent_pulse = int(0)
self.cv_percent_offset = int(0)
self.cv_percent_prob = int(0)
# this var is used to know if we need to keep the pulses stable to 0 and 1 even if
# it's supposed to change by cv or pads
self.pulses_set_0_1 = False
if self.pulses <= 1:
self.pulses_set_0_1 = True
self.is_mute = False
self.is_fill = False
# is used to clear mute and fill only if macro or only if cv
self.mute_by_macro = False
self.fill_by_macro = False
self.rhythm = []
self.set_rhythm()
@property
def prescaler_index(self):
return self._prescaler_index
@prescaler_index.setter
def prescaler_index(self, prescaler_index):
self._prescaler_index = prescaler_index
self.prescaler = PRESCALER_LIST[self._prescaler_index]
def mute(self, mute_by_macro=False):
self.is_mute = True
self.mute_by_macro = mute_by_macro
self.set_rhythm()
def unmute(self):
self.is_mute = False
self.set_rhythm()
def invert_mute(self, mute_by_macro=False):
self.is_mute = not self.is_mute
if self.is_mute:
self.mute_by_macro = mute_by_macro
self.set_rhythm()
def fill(self, fill_by_macro=False):
self.is_fill = True
self.fill_by_macro = fill_by_macro
self.set_rhythm()
def unfill(self):
self.is_fill = False
self.set_rhythm()
def invert_fill(self, fill_by_macro=False):
self.is_fill = not self.is_fill
if self.is_fill:
self.fill_by_macro = fill_by_macro
self.set_rhythm()
def set_offset(self, offset):
self.offset = offset % self.beats
def incr_offset(self):
self.offset = (self.offset + 1) % self.beats
def decr_offset(self,):
self.offset = (self.offset - 1) % self.beats
def set_cv_percent_offset(self, percent):
self.cv_percent_offset = percent
# compute direcctly the global offset for later use in the interrupt function
self.global_cv_offset = self.offset + \
int(len(self.rhythm)*self.cv_percent_offset/100)
def set_cv_percent_probability(self, percent):
self.cv_percent_prob = percent
# compute direcctly the global probability for later use in the interrupt function
probability = self.pulses_probability+self.cv_percent_prob
self.global_cv_probability = min(100, (max(0, probability)))
def incr_beats(self):
if self.beats != MAX_BEATS:
self.beats = self.beats + 1
if not self.pulses_set_0_1:
self.set_pulses_per_ratio()
self.set_rhythm()
def decr_beats(self):
self.beats = self.beats - 1
if self.beats == 0:
self.beats = 1
if self.pulses > self.beats:
self.pulses = self.beats
if self.offset > self.beats:
self.offset = self.beats
if not self.pulses_set_0_1:
self.set_pulses_per_ratio()
self.set_rhythm()
def set_cv_percent_beat(self, percent):
self.cv_percent_beat = percent
if not self.pulses_set_0_1:
self.set_pulses_per_ratio()
self.set_rhythm()
def __compute_pulses_per_ratio(self, local_beat):
computed_pulses_per_ratio = round(local_beat*self.__pulses_ratio)
return max(1, (min(local_beat, computed_pulses_per_ratio)))
def set_pulses_per_ratio(self):
self.pulses = self.__compute_pulses_per_ratio(self.beats)
def set_cv_percent_pulse(self, percent):
self.cv_percent_pulse = percent
self.set_rhythm()
def incr_pulses(self):
self.pulses = self.pulses + 1
if self.pulses > self.beats:
self.pulses = self.beats
if self.pulses <= 1:
self.pulses_set_0_1 = True
else:
self.pulses_set_0_1 = False
self.__pulses_ratio = self.pulses / self.beats
self.set_rhythm()
def decr_pulses(self):
self.pulses = self.pulses - 1
if self.pulses < 0:
self.pulses = 0
if self.pulses <= 1:
self.pulses_set_0_1 = True
else:
self.pulses_set_0_1 = False
self.__pulses_ratio = self.pulses / self.beats
self.set_rhythm()
def incr_pulses_probability(self):
if self.pulses_probability != 100:
self.pulses_probability = self.pulses_probability + 5
def decr_pulses_probability(self):
if self.pulses_probability != 0:
self.pulses_probability = self.pulses_probability - 5
def incr_step(self):
to_return = False
if self.prescaler_rhythm_counter == 0:
self.current_step = self.current_step + 1
beat_limit = len(self.rhythm)-1
if self.current_step > beat_limit:
self.current_step = 0
to_return = True
self.prescaler_rhythm_counter = self.prescaler_rhythm_counter+1
if self.prescaler_rhythm_counter >= self.prescaler:
self.prescaler_rhythm_counter = 0
return to_return
def incr_gate_length(self):
if (self.gate_length_ms+10) < 250:
self.gate_length_ms = self.gate_length_ms + 10
else:
self.gate_length_ms = 250
def decr_gate_length(self):
if (self.gate_length_ms-10) > 10:
self.gate_length_ms = self.gate_length_ms - 10
else:
self.gate_length_ms = 10
# this function can be called by an interrupt, this is why it cannot allocate any memory
def reset_step(self):
self.reset_step_occure = True
def get_current_step(self):
try:
self.get_current_step_offset = self.offset
self.get_current_step_beats = len(self.rhythm)
if self.has_cv_offset:
self.get_current_step_offset = self.global_cv_offset
to_return = self.rhythm[(
self.current_step-self.get_current_step_offset) % self.get_current_step_beats]
if to_return == 0:
return 0
else:
if self.has_cv_prob:
if self.global_cv_probability == 100:
return to_return
elif randint(0, 100) < self.global_cv_probability:
return to_return
else:
return 0
else:
if self.pulses_probability == 100:
return to_return
elif randint(0, 100) < self.pulses_probability:
return to_return
else:
return 0
except Exception as e:
print(e, "x")
def set_rhythm(self):
local_beats = self.beats
local_pulse = self.pulses
if self.has_cv_beat:
local_beats = local_beats+int(MAX_BEATS*self.cv_percent_beat/100)
if local_beats > MAX_BEATS:
local_beats = MAX_BEATS
elif local_beats <= 0:
local_beats = 1
if not self.pulses_set_0_1:
local_pulse = self.__compute_pulses_per_ratio(local_beats)
if self.has_cv_pulse:
local_pulse = local_pulse + \
int(local_beats*self.cv_percent_pulse/100)
# range back beats from 1 to MAX_BEATS
if local_beats > MAX_BEATS:
local_beats = MAX_BEATS
elif local_beats <= 0:
local_beats = 1
# range back from 0 to current beat number
if local_pulse > local_beats:
local_pulse = local_beats
elif local_pulse < 0:
local_pulse = 0
if self.is_mute:
self.rhythm = [0]*local_beats
elif self.is_fill:
self.rhythm = [1]*local_beats
elif local_pulse == 0:
self.rhythm = [0]*local_beats
elif local_pulse == 1:
self.rhythm = [1]*1+[0]*(local_beats-1)
elif local_beats == local_pulse:
self.rhythm = [1]*local_beats
else:
if self.algo_index == 0:
self.rhythm = self.__set_rhythm_bjorklund(
local_beats, local_pulse)
elif self.algo_index == 1:
self.rhythm = self.__exponential_rhythm(
local_beats, local_pulse)
elif self.algo_index == 2:
self.rhythm = self.__exponential_rhythm(
local_beats, local_pulse, True)
else:
self.rhythm = self.__symmetric_exponential(
local_beats, local_pulse)
# from https://github.com/brianhouse/bjorklund/tree/master
def __set_rhythm_bjorklund(self, beats, pulses):
pattern = []
counts = []
remainders = []
divisor = beats - pulses
remainders.append(pulses)
level = 0
while True:
counts.append(divisor // remainders[level])
remainders.append(divisor % remainders[level])
divisor = remainders[level]
level = level + 1
if remainders[level] <= 1:
break
counts.append(divisor)
def build(level):
if level == -1:
pattern.append(0)
elif level == -2:
pattern.append(1)
else:
for _ in range(0, counts[level]):
build(level - 1)
if remainders[level] != 0:
build(level - 2)
build(level)
i = pattern.index(1)
pattern = pattern[i:] + pattern[0:i]
return pattern
def __exponential_rhythm(self, beats, pulses, reverse=False):
if pulses == 0:
return [0]*beats
elif pulses == 1:
return [1]*1+[0]*(beats-1)
else:
alpha = 1.4
# Calculate the exponential positions
positions = [round((i / (pulses - 1))**alpha * (beats - 1))
for i in range(pulses)]
# Make sure all positions are unique
positions = list(set(positions))
# If fewer unique positions than k, fill in the gaps
while len(positions) < pulses:
for i in range(1, beats):
if i not in positions:
positions.append(i)
if len(positions) >= pulses:
break
# Create the rhythm array
rhythm = [0] * beats
for pos in positions:
rhythm[pos] = 1
if reverse:
return list(reversed(rhythm))
else:
return rhythm
def __symmetric_exponential(self, beats, pulses):
if beats % 2 == 1:
rhythm0_n = int(beats/2)
rhythm1_n = rhythm0_n+1
else:
rhythm0_n = int(beats/2)
rhythm1_n = rhythm0_n
if pulses % 2 == 1:
rhythm0_k = int(pulses/2)
rhythm1_k = rhythm0_k+1
else:
rhythm0_k = int(pulses/2)
rhythm1_k = rhythm0_k
r_0 = self.__exponential_rhythm(rhythm0_n, rhythm0_k)
r_1 = self.__exponential_rhythm(rhythm1_n, rhythm1_k, True)
return r_1+r_0
class LxEuclidConstant:
TAP_MODE = const(0)
CLK_IN = const(1)
MAX_BPM = const(250)
MIN_BPM = const(8)
# tap is in 4/4 so time is 4x delay time
MIN_TAP_DELAY_MS = int(((60/MAX_BPM)*1000))
# equivalent to ~2s (rhythm 4/4) (Max would be --> 2**16/10/1000 = 6.5536 s)
MAX_TAP_DELAY_MS = int(((60/MIN_BPM)*1000))
CIRCLE_ACTION_NONE = const(0)
CIRCLE_ACTION_RESET = const(1)
CIRCLE_ACTION_BEATS = const(2)
CIRCLE_ACTION_PULSES = const(3)
CIRCLE_ACTION_ROTATE = const(4)
CIRCLE_ACTION_PROB = const(5)
CIRCLE_ACTION_FILL = const(6)
CIRCLE_ACTION_MUTE = const(7)
CIRCLE_RHYTHM_1 = const(0)
CIRCLE_RHYTHM_2 = const(1)
CIRCLE_RHYTHM_3 = const(2)
CIRCLE_RHYTHM_4 = const(3)
CIRCLE_RHYTHM_ALL = const(4)
STATE_INIT = const(0)
STATE_LIVE = const(1)
STATE_MENU_SELECT = const(2)
STATE_PARAM_MENU = const(3)
STATE_PARAM_MENU_SELECTION = const(4)
STATE_RHYTHM_PARAM_INNER_BEAT_PULSE = const(5)
STATE_RHYTHM_PARAM_INNER_OFFSET_PROBABILITY = const(6)
STATE_PARAM_PRESETS = const(7)
STATE_PARAM_PADS_SELECTION = const(8)
STATE_PARAM_PADS = const(9)
STATE_CHANNEL_CONFIG = const(10)
STATE_CHANNEL_CONFIG_SELECTION = const(11)
STATE_TEST= const(100)
EVENT_INIT = const(0)
EVENT_MENU_BTN = const(1)
EVENT_MENU_BTN_LONG = const(2)
EVENT_TAP_BTN = const(3)
EVENT_TAP_BTN_LONG = const(4)
EVENT_INNER_CIRCLE_INCR = const(5)
EVENT_INNER_CIRCLE_DECR = const(6)
EVENT_OUTER_CIRCLE_INCR = const(7)
EVENT_OUTER_CIRCLE_DECR = const(8)
EVENT_INNER_CIRCLE_TOUCH = const(9)
EVENT_OUTER_CIRCLE_TOUCH = const(10)
EVENT_INNER_CIRCLE_TAP = const(11)
EVENT_OUTER_CIRCLE_TAP = const(12)
EVENT_BTN_SWITCHES = const(13)
PRESET_RECALL_DIRECT_W_RESET = const(0)
PRESET_EXTERNAL_RESET = const(1)
PRESET_RECALL_DIRECT_WO_RESET = const(2)
PRESET_INTERNAL_RESET = const(3)
MAX_CIRCLE_DISPLAY_TIME_MS = const(500)
class LxEuclidConfig:
def __init__(self, lx_hardware, LCD, software_version):
self.v_major = software_version[0]
self.v_minor = software_version[1]
self.v_fix = software_version[2]
self.lx_hardware = lx_hardware
self.LCD = LCD
self._flip = False
self.flip_lock = allocate_lock()
self.LCD.set_config(self)
self.euclidean_rhythms = []
self.euclidean_rhythms.append(EuclideanRhythm(16, 4, 0, 100))
self.euclidean_rhythms.append(EuclideanRhythm(8, 1, 4, 100))
self.euclidean_rhythms.append(EuclideanRhythm(4, 1, 2, 100))
self.euclidean_rhythms.append(EuclideanRhythm(9, 5, 0, 100))
self.presets = []
self.presets.append([EuclideanRhythmParameters(8, 3, 1, 100), EuclideanRhythmParameters(
16, 4, 0, 100, algo_index=1), EuclideanRhythmParameters(16, 5, 10, 100), EuclideanRhythmParameters(8, 1, 1, 100)])
self.presets.append([EuclideanRhythmParameters(16, 4, 10, 100), EuclideanRhythmParameters(
12, 7, 0, 100), EuclideanRhythmParameters(16, 4, 12, 100), EuclideanRhythmParameters(4, 2, 0, 100)])
self.presets.append([EuclideanRhythmParameters(12, 1, 7, 100), EuclideanRhythmParameters(
6, 5, 0, 100), EuclideanRhythmParameters(3, 1, 2, 100), EuclideanRhythmParameters(6, 1, 5, 100)])
self.presets.append([EuclideanRhythmParameters(16, 4, 0, 100), EuclideanRhythmParameters(
8, 1, 4, 100), EuclideanRhythmParameters(4, 1, 2, 100), EuclideanRhythmParameters(9, 5, 0, 100)])
self.presets.append([EuclideanRhythmParameters(16, 4, 0, 100), EuclideanRhythmParameters(
8, 1, 4, 100), EuclideanRhythmParameters(4, 1, 2, 100), EuclideanRhythmParameters(9, 5, 0, 100)])
self.presets.append([EuclideanRhythmParameters(16, 4, 0, 100), EuclideanRhythmParameters(
8, 1, 4, 100), EuclideanRhythmParameters(4, 1, 2, 100), EuclideanRhythmParameters(9, 5, 0, 100)])
self.presets.append([EuclideanRhythmParameters(16, 4, 0, 100), EuclideanRhythmParameters(
8, 1, 4, 100), EuclideanRhythmParameters(4, 1, 2, 100), EuclideanRhythmParameters(9, 5, 0, 100)])
self.presets.append([EuclideanRhythmParameters(16, 4, 0, 100), EuclideanRhythmParameters(
8, 1, 4, 100), EuclideanRhythmParameters(4, 1, 2, 100), EuclideanRhythmParameters(9, 5, 0, 100)])
self.preset_recall_mode = LxEuclidConstant.PRESET_RECALL_DIRECT_W_RESET
self.preset_recall_int_reset = False
self.preset_recall_ext_reset = False
self.rhythm_lock = allocate_lock()
self.menu_lock = allocate_lock()
self.state_lock = allocate_lock()
self.save_data_lock = allocate_lock()
self.need_save_data_in_file = False
self.state = LxEuclidConstant.STATE_INIT
self.on_event(LxEuclidConstant.EVENT_INIT)
self.sm_rhythm_param_counter = 0
self.clk_mode = LxEuclidConstant.CLK_IN
self._save_preset_index = 0
self._load_preset_index = 0
self.inner_rotate_action = LxEuclidConstant.CIRCLE_ACTION_NONE
self.inner_action_rhythm = 0
self.outer_rotate_action = LxEuclidConstant.CIRCLE_ACTION_NONE
self.outer_action_rhythm = 0
self._need_circle_action_display = False
self.last_set_need_circle_action_display_ms = ticks_ms()
self.last_gate_led_event = ticks_ms()
self.clear_led_needed = False
self.action_display_index = 0
self.action_display_info = ""
self.param_cvs_index = 0 # used when doing CVs parameters selection
self.param_cvs_page = 0
self.param_presets_page = 0
self.param_pads_page = 0
self.param_pads_inner_outer_page = 0
self.param_channel_config_page = 0
self.param_channel_config_cv_page = 0
self.param_channel_config_action_index = 0
self.param_menu_page = 0
self.computation_index_incr_step = 0 # used in interrupt function that can't create memory
self._tap_delay_ms = 125 # default tap tempo 120bmp 125ms for 16th note
self.tap_delay_ms_lock = allocate_lock()
# list used to test if data changed and needs to be stocked in memory
self.previous_dict_data_list = []
# used in create_memory_dict, put it as attribute so it doesn't create memory in loop
self.dict_data = OrderedDict()
self.load_data()
self.reload_rhythms()
self.lx_hardware.capacitives_circles.flip = self._flip
if self._flip == True:
# flip the display at boot if needed
self.LCD.fill(self.LCD.black)
self.LCD.show()
self.LCD.init_display(self._flip)
@property
def flip(self):
to_return = 0
self.flip_lock.acquire()
to_return = self._flip
self.flip_lock.release()
return to_return
@flip.setter
def flip(self, flip):
self.flip_lock.acquire()
if flip != self._flip:
self.LCD.set_need_flip()
self._flip = flip
self.lx_hardware.capacitives_circles.flip = self._flip
self.flip_lock.release()
@property
def need_circle_action_display(self):
if ticks_ms() - self.last_set_need_circle_action_display_ms > LxEuclidConstant.MAX_CIRCLE_DISPLAY_TIME_MS:
self._need_circle_action_display = False
return self._need_circle_action_display
@need_circle_action_display.setter
def need_circle_action_display(self, need_circle_action_display):
self._need_circle_action_display = need_circle_action_display
if need_circle_action_display:
self.last_set_need_circle_action_display_ms = ticks_ms()
@property
def save_preset_index(self):
return self._save_preset_index
@save_preset_index.setter
def save_preset_index(self, save_preset_index):
self._save_preset_index = save_preset_index
index = 0
for preset_euclidean_rhythm in self.presets[self._save_preset_index]:
preset_euclidean_rhythm.set_parameters_from_rhythm(
self.euclidean_rhythms[index])
index = index + 1
self.save_data()
@property
def load_preset_index(self):
return self._load_preset_index
@load_preset_index.setter
def load_preset_index(self, load_preset_index):
self._load_preset_index = load_preset_index
if self.preset_recall_mode in [LxEuclidConstant.PRESET_RECALL_DIRECT_W_RESET,
LxEuclidConstant.PRESET_RECALL_DIRECT_WO_RESET]:
# if previous reset recall were launched, clear them, only one preset load can be in queue
self.preset_recall_int_reset = False
self.preset_recall_ext_reset = False
# only load preset if we are in a "direct" mode
self.delegate_load_preset()
elif self.preset_recall_mode is LxEuclidConstant.PRESET_INTERNAL_RESET:
self.preset_recall_int_reset = True
# if previous reset recall were launched, clear them, only one preset load can be in queue
self.preset_recall_ext_reset = False
elif self.preset_recall_mode is LxEuclidConstant.PRESET_EXTERNAL_RESET:
self.preset_recall_ext_reset = True
# if previous reset recall were launched, clear them, only one preset load can be in queue
self.preset_recall_int_reset = False
def delegate_load_preset(self):
for index, euclidean_rhythm in enumerate(self.euclidean_rhythms):
euclidean_rhythm.set_parameters_from_rhythm(
self.presets[self._load_preset_index][index])
euclidean_rhythm.set_rhythm()
# if current recall mode is direct wo reset or, we called previously a preset_recall_ext_reset
if self.preset_recall_mode is not LxEuclidConstant.PRESET_RECALL_DIRECT_WO_RESET and self.preset_recall_ext_reset is False:
self.reset_steps()
@property
def tap_delay_ms(self):
to_return = 0
self.tap_delay_ms_lock.acquire()
to_return = self._tap_delay_ms
self.tap_delay_ms_lock.release()
return to_return
@tap_delay_ms.setter
def tap_delay_ms(self, tap_delay_ms):
self.tap_delay_ms_lock.acquire()
self._tap_delay_ms = tap_delay_ms
self.tap_delay_ms_lock.release()
def get_int_bpm(self):
return round((60/(self.tap_delay_ms/1000))/4)
def incr_bpm(self, incr):
old_delay_ms = self.tap_delay_ms
bpm = min(LxEuclidConstant.MAX_BPM,self.get_int_bpm()+incr)
delay_ms = round((60/(bpm*4))*1000)
# incr of bpm mean exp incr of delay ms
# this mean with high bpm decr, the delta ms of 1 bpm can be smaller than 1ms
# if so --> force decr of 1ms
if bpm != LxEuclidConstant.MAX_BPM and old_delay_ms-delay_ms == 0:
delay_ms = delay_ms-1
self.tap_delay_ms = delay_ms
def decr_bpm(self, decr):
old_delay_ms = self.tap_delay_ms
bpm = max(LxEuclidConstant.MIN_BPM,self.get_int_bpm()-decr)
delay_ms = round((60/(bpm*4))*1000)
# incr of bpm mean exp incr of delay ms
# this mean with high bpm decr, the delta ms of 1 bpm can be smaller than 1ms
# if so --> force decr of 1ms
if bpm != LxEuclidConstant.MIN_BPM and old_delay_ms-delay_ms == 0:
delay_ms = delay_ms+1
self.tap_delay_ms = delay_ms
def on_event(self, event, data=None):
self.state_lock.acquire()
local_state = self.state
self.state_lock.release()
if local_state == LxEuclidConstant.STATE_INIT:
if event == LxEuclidConstant.EVENT_INIT:
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_LIVE
self.state_lock.release()
elif local_state == LxEuclidConstant.STATE_LIVE:
# START STATE LIVE
if event == LxEuclidConstant.EVENT_MENU_BTN:
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_MENU_SELECT
self.lx_hardware.set_tap_led()
self.state_lock.release()
self.sm_rhythm_param_counter = 0
if event == LxEuclidConstant.EVENT_MENU_BTN_LONG:
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_PARAM_PRESETS
self.state_lock.release()
self.sm_rhythm_param_counter = 0
self.lx_hardware.set_tap_led()
self.lx_hardware.set_menu_led()
self.param_presets_page = 0
elif event == LxEuclidConstant.EVENT_TAP_BTN_LONG:
if self.preset_recall_int_reset:
self.preset_recall_int_reset = False
self.delegate_load_preset()
self.reset_steps()
elif event == LxEuclidConstant.EVENT_BTN_SWITCHES:
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_RHYTHM_PARAM_INNER_BEAT_PULSE
self.state_lock.release()
self.lx_hardware.set_sw_leds(data)
self.lx_hardware.set_tap_led()
self.lx_hardware.set_menu_led()
self.menu_lock.acquire()
self.sm_rhythm_param_counter = data
self.menu_lock.release()
elif event in [LxEuclidConstant.EVENT_INNER_CIRCLE_TAP, LxEuclidConstant.EVENT_OUTER_CIRCLE_TAP]:
if event == LxEuclidConstant.EVENT_INNER_CIRCLE_TAP:
rotate_action = self.inner_rotate_action
action_rhythm = self.inner_action_rhythm
angle = self.lx_hardware.capacitives_circles.inner_circle_angle
else:
rotate_action = self.outer_rotate_action
action_rhythm = self.outer_action_rhythm
angle = self.lx_hardware.capacitives_circles.outer_circle_angle
if rotate_action in [LxEuclidConstant.CIRCLE_ACTION_RESET, LxEuclidConstant.CIRCLE_ACTION_FILL, LxEuclidConstant.CIRCLE_ACTION_MUTE]:
menu_selection_index = angle_to_index(angle, 4)
if rotate_action == LxEuclidConstant.CIRCLE_ACTION_RESET:
self.euclidean_rhythms[menu_selection_index].reset_step(
)
elif rotate_action == LxEuclidConstant.CIRCLE_ACTION_FILL:
self.euclidean_rhythms[menu_selection_index].invert_fill(
fill_by_macro=True)
elif rotate_action == LxEuclidConstant.CIRCLE_ACTION_MUTE:
self.euclidean_rhythms[menu_selection_index].invert_mute(
mute_by_macro=True)
self.action_display_index = menu_selection_index
self.action_display_info = "~"
self.need_circle_action_display = True
elif event in [LxEuclidConstant.EVENT_INNER_CIRCLE_DECR, LxEuclidConstant.EVENT_INNER_CIRCLE_INCR, LxEuclidConstant.EVENT_OUTER_CIRCLE_DECR, LxEuclidConstant.EVENT_OUTER_CIRCLE_INCR]:
if event in [LxEuclidConstant.EVENT_INNER_CIRCLE_DECR, LxEuclidConstant.EVENT_INNER_CIRCLE_INCR]:
rotate_action = self.inner_rotate_action
action_rhythm = self.inner_action_rhythm
incr_event = LxEuclidConstant.EVENT_INNER_CIRCLE_INCR
decr_event = LxEuclidConstant.EVENT_INNER_CIRCLE_DECR
else:
rotate_action = self.outer_rotate_action
action_rhythm = self.outer_action_rhythm
incr_event = LxEuclidConstant.EVENT_OUTER_CIRCLE_INCR
decr_event = LxEuclidConstant.EVENT_OUTER_CIRCLE_DECR
if rotate_action in [LxEuclidConstant.CIRCLE_ACTION_BEATS, LxEuclidConstant.CIRCLE_ACTION_PULSES, LxEuclidConstant.CIRCLE_ACTION_ROTATE, LxEuclidConstant.CIRCLE_ACTION_PROB] and action_rhythm != 0:
for euclidean_rhythm_index in range(0, 4):
if action_rhythm & (1 << euclidean_rhythm_index) != 0:
if event == incr_event:
if rotate_action == LxEuclidConstant.CIRCLE_ACTION_BEATS:
self.euclidean_rhythms[euclidean_rhythm_index].incr_beats(
)
self.euclidean_rhythms[euclidean_rhythm_index].set_pulses_per_ratio(
)
self.euclidean_rhythms[euclidean_rhythm_index].set_rhythm(
)
elif rotate_action == LxEuclidConstant.CIRCLE_ACTION_PULSES:
self.euclidean_rhythms[euclidean_rhythm_index].incr_pulses(
)
elif rotate_action == LxEuclidConstant.CIRCLE_ACTION_ROTATE:
self.euclidean_rhythms[euclidean_rhythm_index].incr_offset(
)
elif rotate_action == LxEuclidConstant.CIRCLE_ACTION_PROB:
self.euclidean_rhythms[euclidean_rhythm_index].incr_pulses_probability(
)
self.action_display_info = "+"
elif event == decr_event:
if rotate_action == LxEuclidConstant.CIRCLE_ACTION_BEATS:
self.euclidean_rhythms[euclidean_rhythm_index].decr_beats(
)
self.euclidean_rhythms[euclidean_rhythm_index].set_pulses_per_ratio(
)
self.euclidean_rhythms[euclidean_rhythm_index].set_rhythm(
)
elif rotate_action == LxEuclidConstant.CIRCLE_ACTION_PULSES:
self.euclidean_rhythms[euclidean_rhythm_index].decr_pulses(
)
elif rotate_action == LxEuclidConstant.CIRCLE_ACTION_ROTATE:
self.euclidean_rhythms[euclidean_rhythm_index].decr_offset(
)
elif rotate_action == LxEuclidConstant.CIRCLE_ACTION_PROB:
self.euclidean_rhythms[euclidean_rhythm_index].decr_pulses_probability(
)
self.action_display_info = "-"
self.need_circle_action_display = True
if action_rhythm == 1: # circle action only affect one rhythm
self.action_display_index = 0
elif action_rhythm == 2:
self.action_display_index = 1
elif action_rhythm == 4:
self.action_display_index = 2
elif action_rhythm == 8:
self.action_display_index = 3
else: # circle action only affect multiple rhythm --> color will be white
self.action_display_index = 4
# END STATE LIVE
elif self.state == LxEuclidConstant.STATE_MENU_SELECT:
if event == LxEuclidConstant.EVENT_INNER_CIRCLE_TAP:
angle_inner = self.lx_hardware.capacitives_circles.inner_circle_angle
menu_selection_index = angle_to_index(angle_inner, 3)
if menu_selection_index == 0: # Preset
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_PARAM_PRESETS
self.state_lock.release()
self.lx_hardware.set_menu_led()
self.param_presets_page = 0
elif menu_selection_index == 1: # Pads
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_PARAM_PADS_SELECTION
self.state_lock.release()
self.lx_hardware.set_menu_led()
self.param_pads_page = 0
self.param_pads_inner_outer_page = 0
elif menu_selection_index == 2: # Other
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_PARAM_MENU_SELECTION
self.state_lock.release()
self.lx_hardware.set_menu_led()
elif event == LxEuclidConstant.EVENT_TAP_BTN:
self.save_data()
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_LIVE
self.state_lock.release()
self.lx_hardware.clear_tap_led()
self.lx_hardware.clear_menu_led()
elif self.state == LxEuclidConstant.STATE_PARAM_PADS_SELECTION:
if event == LxEuclidConstant.EVENT_INNER_CIRCLE_TAP:
angle_inner = self.lx_hardware.capacitives_circles.inner_circle_angle
pad_selection = angle_to_index(angle_inner, 2)
self.param_pads_inner_outer_page = pad_selection
self.param_pads_page = 0
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_PARAM_PADS
self.state_lock.release()
elif event == LxEuclidConstant.EVENT_TAP_BTN:
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_LIVE
self.state_lock.release()
self.lx_hardware.clear_tap_led()
self.lx_hardware.clear_menu_led()
self.lx_hardware.clear_sw_leds()
elif event == LxEuclidConstant.EVENT_MENU_BTN:
self.state_lock.acquire()
self.state = LxEuclidConstant.STATE_MENU_SELECT
self.state_lock.release()
elif self.state == LxEuclidConstant.STATE_PARAM_PADS:
if event == LxEuclidConstant.EVENT_INNER_CIRCLE_TAP:
angle_inner = self.lx_hardware.capacitives_circles.inner_circle_angle
if self.param_pads_page == 0: # action
rotate_action_index = angle_to_index(angle_inner, 8)
if self.param_pads_inner_outer_page == 0: # inner
previous_rotate_action = self.inner_rotate_action
action_rhythm = self.inner_action_rhythm
self.inner_rotate_action = rotate_action_index
else: # outer
previous_rotate_action = self.outer_rotate_action
action_rhythm = self.outer_action_rhythm
self.outer_rotate_action = rotate_action_index
# make sure to reset fill and mute if we remove it from a rotate action
if previous_rotate_action == LxEuclidConstant.CIRCLE_ACTION_FILL:
for euclidean_rhythm_index in range(0, 4):
if self.euclidean_rhythms[euclidean_rhythm_index].fill_by_macro and self.euclidean_rhythms[euclidean_rhythm_index].is_fill:
self.euclidean_rhythms[euclidean_rhythm_index].unfill(
)
elif previous_rotate_action == LxEuclidConstant.CIRCLE_ACTION_MUTE:
for euclidean_rhythm_index in range(0, 4):
if self.euclidean_rhythms[euclidean_rhythm_index].mute_by_macro and self.euclidean_rhythms[euclidean_rhythm_index].is_mute:
self.euclidean_rhythms[euclidean_rhythm_index].unmute(
)
# go to next macro page if we select any parameter except listed ones
if rotate_action_index not in [LxEuclidConstant.CIRCLE_ACTION_NONE, LxEuclidConstant.CIRCLE_ACTION_RESET, LxEuclidConstant.CIRCLE_ACTION_MUTE, LxEuclidConstant.CIRCLE_ACTION_FILL]:
self.param_pads_page = 1
elif self.param_pads_page == 1: # output
out_index = angle_to_index(angle_inner, 4)
if self.param_pads_inner_outer_page == 0: # inner
previous_inner_action_rhythm = self.inner_action_rhythm
self.inner_action_rhythm = self.inner_action_rhythm ^ (
1 << out_index)
else: # outer