-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathpams.py
1579 lines (1357 loc) · 50.9 KB
/
pams.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
#!/usr/bin/env python3
# Copyright 2023 Allen Synthesis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A EuroPi clone of ALM's Pamela's NEW Workout
@author Chris Iverach-Brereton <[email protected]>
@year 2023
See pams.md for complete feature list
"""
from europi import *
from europi_script import EuroPiScript
from configuration import *
from experimental.euclid import generate_euclidean_pattern
from experimental.knobs import KnobBank
from experimental.quantizer import CommonScales, Quantizer, SEMITONES_PER_OCTAVE
from experimental.screensaver import OledWithScreensaver
from experimental.settings_menu import *
from machine import Timer
import gc
import math
import time
import random
## Screensaver-enabled display
ssoled = OledWithScreensaver()
## Lockable knob bank for K2 to make menu navigation a little easier
#
# Note that this does mean _sometimes_ you'll need to sweep the knob all the way left/right
# to unlock it
k2_bank = (
KnobBank.builder(k2)
.with_unlocked_knob("main_menu")
.with_locked_knob("submenu", initial_percentage_value=0)
.with_locked_knob("choice", initial_percentage_value=0)
.build()
)
## The scales that each PamsOutput can quantize to
QUANTIZER_NAMES = [
"None",
"Chromatic",
# Major scales
"Nat Maj",
"Har Maj",
"Maj 135",
"Maj 1356",
"Maj 1357",
# Minor scales
"Nat Min",
"Har Min",
"Min 135",
"Min 1356",
"Min 1357",
# Blues scales
"Maj Blues",
"Min Blues",
# Misc
"Whole",
"Penta",
"Dom 7",
]
QUANTIZERS = {
"None" : None,
"Chromatic" : CommonScales.Chromatic,
# Major scales
"Nat Maj" : CommonScales.NatMajor,
"Har Maj" : CommonScales.HarMajor,
"Maj 135" : CommonScales.Major135,
"Maj 1356" : CommonScales.Major1356,
"Maj 1357" : CommonScales.Major1357,
# Minor scales
"Nat Min" : CommonScales.NatMinor,
"Har Min" : CommonScales.HarMinor,
"Min 135" : CommonScales.Minor135,
"Min 1356" : CommonScales.Minor1356,
"Min 1357" : CommonScales.Minor1357,
# Blues scales
"Maj Blues" : CommonScales.MajorBlues,
"Min Blues" : CommonScales.MinorBlues,
# Misc
"Whole" : CommonScales.WholeTone,
"Penta" : CommonScales.Pentatonic,
"Dom 7" : CommonScales.Dominant7,
}
SEMITONE_LABELS = {
0: "C",
1: "C#",
2: "D",
3: "D#",
4: "E",
5: "F",
6: "F#",
7: "G",
8: "G#",
9: "A",
10: "A#",
11: "B",
}
## Always-on gate when the clock is running
CLOCK_MOD_RUN = 100
## Short trigger on clock start
CLOCK_MOD_START = 102
## Short trigger on clock stop
CLOCK_MOD_RESET = 103
## Available clock modifiers
CLOCK_MOD_NAMES = [
"/16",
"/12",
"/8",
"/6",
"/4",
"/3",
"/2",
"x1",
"x2",
"x3",
"x4",
"x6",
"x8",
"x12",
"x16",
"Run",
"Start",
"Reset",
]
CLOCK_MULTIPLIERS = {
"/16": 1/16.0,
"/12": 1/12.0,
"/8" : 1/8.0,
"/6" : 1/6.0,
"/4" : 1/4.0,
"/3" : 1/3.0,
"/2" : 1/2.0,
"x1" : 1.0,
"x2" : 2.0,
"x3" : 3.0,
"x4" : 4.0,
"x6" : 6.0,
"x8" : 8.0,
"x12": 12.0,
"x16": 16.0,
"Run": CLOCK_MOD_RUN,
"Start": CLOCK_MOD_START,
"Reset": CLOCK_MOD_RESET,
}
## Some clock mods have graphics
CLOCK_MOD_IMGS = {
"Run": bytearray(b'\xff\xf0\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00'), # run gate
"Start": bytearray(b'\xe0\x00\xa0\x00\xa0\x00\xa0\x00\xa0\x00\xa0\x00\xa0\x00\xa0\x00\xa0\x00\xa0\x00\xa0\x00\xbf\xf0'), # start trigger
"Reset": bytearray(b'\x03\xf0\x02\x00\x02\x00\x02\x00\x02\x00\x02\x00\x02\x00\x02\x00\x02\x00\x02\x00\x02\x00\xfe\x00'), # reset trigger
}
## Standard pulse/square wave with PWM
WAVE_SQUARE = 0
## Triangle wave
#
# - When width is 50 this is a symmetrical triangle /\
# - When width is < 50 we become more saw-like |\
# - When sidth is > 50 we become more ramp-like /|
WAVE_TRIANGLE = 1
## Sine wave
#
# Width is ignored
WAVE_SIN = 2
## A configurable ADSR envelope
WAVE_ADSR = 3
## Random wave
#
# Width is ignored
WAVE_RANDOM = 4
## Use raw AIN as the direct input
#
# This lets you effectively use Pam's as a quantizer for
# the AIN signal
WAVE_AIN = 5
## Using K1 as the direct input
#
# This lets you "play" K1 as a manual LFO, flat voltage,
# etc...
WAVE_KNOB = 6
## Turing machine shift register
#
# Requires a sub-setting for either gate or CV mode
WAVE_TURING = 7
## Available wave shapes
#
# These must be placed in the desired order
WAVE_SHAPES = [
WAVE_SQUARE,
WAVE_TRIANGLE,
WAVE_SIN,
WAVE_ADSR,
WAVE_TURING,
WAVE_RANDOM,
WAVE_AIN,
WAVE_KNOB,
]
## Labels for the wave shape chooser menu
WAVE_SHAPE_LABELS = {
WAVE_SQUARE: "Square",
WAVE_TRIANGLE: "Triangle",
WAVE_SIN: "Sine",
WAVE_ADSR: "ADSR",
WAVE_TURING: "Turing",
WAVE_RANDOM: "Random",
WAVE_AIN: "AIN (S&H)",
WAVE_KNOB: "KNOB (S&H)",
}
# Turing machine modes of operation
#
# We can either output the gate pulses OR we can
# output the semi-random CV
MODE_TURING_GATE = 0
MODE_TURING_CV = 1
TURING_MODES = [
MODE_TURING_GATE,
MODE_TURING_CV,
]
TURING_MODE_LABELS = {
MODE_TURING_GATE: "Gate",
MODE_TURING_CV: "CV",
}
## Images of the wave shapes
#
# These are 12x12 bitmaps. See:
# - https://github.com/Allen-Synthesis/EuroPi/blob/main/software/oled_tips.md
# - https://github.com/novaspirit/img2bytearray
WAVE_SHAPE_IMGS = {
WAVE_SQUARE: bytearray(b'\xfe\x00\x82\x00\x82\x00\x82\x00\x82\x00\x82\x00\x82\x00\x82\x00\x82\x00\x82\x00\x82\x00\x83\xf0'),
WAVE_TRIANGLE: bytearray(b'\x06\x00\x06\x00\t\x00\t\x00\x10\x80\x10\x80 @ @@ @ \x80\x10\x80\x10'),
WAVE_SIN: bytearray(b'\x10\x00(\x00D\x00D\x00\x82\x00\x82\x00\x82\x10\x82\x10\x01\x10\x01\x10\x00\xa0\x00@'),
WAVE_ADSR: bytearray(b' \x00 \x000\x000\x00H\x00H\x00G\xc0@@\x80 \x80 \x80\x10\x80\x10'),
WAVE_TURING: bytearray(b'\xff\xf0\x04\x00\xf8\x00\x00\x00\xff\xf0\x04\x00\xf8\x00\x00\x00\xff\xf0\x04\x00\xf8\x00\x00\x00'),
WAVE_RANDOM: bytearray(b'\x00\x00\x08\x00\x08\x00\x14\x00\x16\x80\x16\xa0\x11\xa0Q\xf0Pp`P@\x10\x80\x00'),
WAVE_AIN: bytearray(b'\x00\x00|\x00|\x00d\x00d\x00g\x80a\x80\xe1\xb0\xe1\xb0\x01\xf0\x00\x00\x00\x00'),
WAVE_KNOB: bytearray(b'\x06\x00\x19\x80 @@ @ \x80\x10\x82\x10A @\xa0 @\x19\x80\x06\x00'),
}
STATUS_IMG_PLAY = bytearray(b'\x00\x00\x18\x00\x18\x00\x1c\x00\x1c\x00\x1e\x00\x1f\x80\x1e\x00\x1e\x00\x1c\x00\x18\x00\x18\x00')
STATUS_IMG_PAUSE = bytearray(b'\x00\x00y\xc0y\xc0y\xc0y\xc0y\xc0y\xc0y\xc0y\xc0y\xc0y\xc0y\xc0')
STATUS_IMG_WIDTH = 12
STATUS_IMG_HEIGHT = 12
## Do we use gate input on din to turn the module on/off
DIN_MODE_GATE = 'Gate'
## Do we toggle the module on/off with a trigger on din?
DIN_MODE_TRIGGER = 'Trig'
## Reset on a rising edge, but don't start/stop the clock
DIN_MODE_RESET = 'Reset'
## Sorted list of DIN modes for display
DIN_MODES = [
DIN_MODE_GATE,
DIN_MODE_TRIGGER,
DIN_MODE_RESET
]
## True/False labels for yes/no settings (e.g. mute)
OK_CANCEL_LABELS = {
False: "Cancel",
True: "OK",
}
YES_NO_LABELS = {
False: "N",
True: "Y",
}
ON_OFF_LABELS = {
False: "Off",
True: "On",
}
## IDs for the load/save banks
#
# Banks are shared across all channels
# The -1 index is used to indicate "cancel"
BANK_IDs = list(range(-1, 6))
## Labels for the banks
BANK_LABELS = [
"Cancel",
"1",
"2",
"3",
"4",
"5",
"6"
]
class BufferedAnalogueReader(AnalogueReader):
"""A wrapper for basic AnalogueReader instances that read the ADC hardware on-demand
This is useful if the reader is going to be using `.choice(...)` for multiple things,
as normally every call to .percent, .choice, .voltage, etc... re-reads the ADC.
Call .update() to re-sample from the ADC
"""
def __init__(self, cv_in: AnalogueReader, label: str):
"""
Create the buffered reader
@param cv_in The base reader we're buffering
@param label A label used to stringify this object
"""
self.reverse_percentage = type(cv_in) is Knob
self.gain = SettingMenuItem(
config_point = IntegerConfigPoint(
f"{label.lower()}_gain",
0,
200,
100
),
prefix = label,
title = "Gain"
)
self.precision = SettingMenuItem(
config_point = ChoiceConfigPoint(
f"{label.lower()}_precision",
["Low", "Med", "High"],
"Med"
),
prefix = label,
title = "Precision",
value_map = {
"Low": DEFAULT_SAMPLES / 2,
"Med": DEFAULT_SAMPLES,
"High": DEFAULT_SAMPLES * 2
}
)
self._last_sample = 0
super().__init__(cv_in.pin_id)
self.label = label
def percent(self, samples=None, deadzone=None):
"""
Apply our gain control to the base percentage
Note that even though the gain goes up to 200%, this returns a value in the range [0, 1].
"""
p = super().percent(samples, deadzone)
if self.gain:
p = p * self.gain.value / 100.0
p = clamp(p, 0.0, 1.0)
if self.reverse_percentage:
return 1.0 - p
return p
def _sample_adc(self, samples=None):
"""
Override the default _sample_adc to just return the last sample
"""
return self._last_sample
def update(self):
"""
Re-read the ADC and store the sample value
"""
self._last_sample = super()._sample_adc(samples=self.precision.mapped_value)
def __str__(self):
return self.label
## Wrapped copies of all CV inputs so we can iterate through them to update them
CV_INS = {
"KNOB": BufferedAnalogueReader(k1, "Knob"),
"AIN": BufferedAnalogueReader(ain, "AIN"),
}
class MasterClock:
"""The main clock that ticks and runs the outputs
"""
## The clock actually runs faster than its maximum BPM to allow
# clock divisions to work nicely
#
# Use 48 internal clock pulses per quarter note. This is slow enough
# that we won't choke the CPU with interrupts, but smooth enough that we
# should be able to approximate complex waves. Must be a multiple of
# 3 to properly support triplets and a multiple of 16 to allow easy
# semi-quavers
PPQN = 48
## The absolute slowest the clock can go
MIN_BPM = 1
## The absolute fastest the clock can go
MAX_BPM = 240
def __init__(self, bpm):
"""Create the main clock to run at a given bpm
@param bpm The initial BPM to run the clock at
"""
self.channels = []
self.is_running = False
self.bpm = SettingMenuItem(
config_point = IntegerConfigPoint(
"bpm",
self.MIN_BPM,
self.MAX_BPM,
60
),
prefix="Clk",
title = "BPM",
callback = self.recalculate_timer_hz,
autoselect_knob = True,
autoselect_cv = True,
)
self.reset_on_start = SettingMenuItem(
config_point = BooleanConfigPoint(
"reset_on_start",
True
),
prefix = "Clk",
title="Stop-Rst",
labels=ON_OFF_LABELS,
)
self.tick_hz = 1.0
self.timer = Timer()
self.recalculate_timer_hz()
self.elapsed_pulses = 0
self.start_time = 0
def add_channels(self, channels):
"""Add the CV channels that this clock is (indirectly) controlling
@param channels A list of PamsOutput objects corresponding to the
output channels
"""
for ch in channels:
self.channels.append(ch)
def on_tick(self, timer):
"""Callback function for the timer's tick
"""
if self.is_running:
for ch in self.channels:
ch.tick()
self.elapsed_pulses = self.elapsed_pulses + 1
for ch in self.channels:
ch.apply()
def start(self):
"""Start the timer
"""
if not self.is_running:
self.is_running = True
self.start_time = time.ticks_ms()
if self.reset_on_start.value:
self.elapsed_pulses = 0
for ch in self.channels:
ch.reset()
self.timer.init(freq=self.tick_hz, mode=Timer.PERIODIC, callback=self.on_tick)
def stop(self):
"""Stop the timer
"""
if self.is_running:
self.is_running = False
self.timer.deinit()
# Fire a reset trigger on any channels that have the CLOCK_MOD_RESET mode set
# This trigger lasts 10ms
# Turn all other channels off so we don't leave hot wires
for ch in self.channels:
if ch.clock_mod.value == CLOCK_MOD_RESET:
ch.cv_out.voltage(MAX_OUTPUT_VOLTAGE * ch.amplitude.value / 100.0)
else:
ch.cv_out.off()
time.sleep(0.01) # time.sleep works in SECONDS not ms
for ch in self.channels:
if ch.clock_mod.value == CLOCK_MOD_RESET:
ch.cv_out.off()
def running_time(self):
"""Return how long the clock has been running
"""
if self.is_running:
now = time.ticks_ms()
return time.ticks_diff(now, self.start_time)
else:
return 0
def recalculate_timer_hz(self, new_value=None, old_value=None, config_point=None, arg=None):
"""Callback function for when the BPM changes
If the timer is currently running deinitialize it and reset it to use the correct BPM
"""
self.tick_hz = self.bpm.value / 60.0 * self.PPQN
if self.is_running:
self.timer.deinit()
self.timer.init(freq=self.tick_hz, mode=Timer.PERIODIC, callback=self.on_tick)
class PamsOutput:
"""Controls a single output jack
"""
## The maximum length of a Euclidean pattern we allow
#
# The maximum is somewhat arbitrary, but depends more on the UI since the knob
# resolution is only so good.
MAX_EUCLID_LENGTH = 64
## Minimum duration of a CLOCK_MOD_START trigger
#
# The actual length depends on clock rate and PPQN, and may be longer than this
TRIGGER_LENGTH_MS = 10
def __init__(self, cv_out, clock, n):
"""Create a new output to control a single cv output
@param cv_out One of the six output jacks
@param clock The MasterClock that controls the timing of this output
@param n The CV number 1-6
"""
self.cv_n = n
self.out_volts = 0.0
self.cv_out = cv_out
self.clock = clock
# 16-bit integer, initially random
self.turing_register = random.randint(0, 65535)
## What quantization are we using?
#
# See contrib.pams.QUANTIZERS
self.quantizer = SettingMenuItem(
config_point = ChoiceConfigPoint(
f"cv{n}_quantizer",
QUANTIZER_NAMES,
"None"
),
prefix=f"CV{n}",
title="Quant.",
callback = self.update_menu_visibility,
value_map=QUANTIZERS,
autoselect_knob = True,
autoselect_cv = True,
)
## The root of the quantized scale (ignored if quantizer is None)
self.root = SettingMenuItem(
config_point = ChoiceConfigPoint(
f"cv{n}_root",
list(range(SEMITONES_PER_OCTAVE)),
0
),
prefix = f"CV{n}",
title = "Q Root",
labels = SEMITONE_LABELS,
autoselect_knob = True,
autoselect_cv = True,
)
## The clock modifier for this channel
#
# - 1.0 is the same as the main clock's BPM
# - <1.0 will tick slower than the BPM (e.g. 0.5 will tick once every 2 beats)
# - >1.0 will tick faster than the BPM (e.g. 3.0 will tick 3 times per beat)
self.clock_mod = SettingMenuItem(
config_point = ChoiceConfigPoint(
f"cv{n}_mod",
CLOCK_MOD_NAMES,
"x1"
),
prefix = f"CV{n}",
title = "Mod",
value_map = CLOCK_MULTIPLIERS,
callback = self.request_clock_mod,
graphics = CLOCK_MOD_IMGS,
autoselect_knob = True,
autoselect_cv = True,
)
## To prevent phase misalignment we use this as the active clock modifier
#
# If clock_mod is changed, we apply it to this when it is safe to do so
self.real_clock_mod = self.clock_mod.mapped_value
## Indicates if clock_mod and real_clock_mod are the same or not
self.clock_mod_dirty = False
## What shape of wave are we generating?
#
# For now, stick to square waves for triggers & gates
self.wave_shape = SettingMenuItem(
config_point = ChoiceConfigPoint(
f"cv{n}_wave",
WAVE_SHAPES,
WAVE_SQUARE,
),
prefix = f"CV{n}",
title = "Wave",
labels = WAVE_SHAPE_LABELS,
graphics = WAVE_SHAPE_IMGS,
callback = self.update_menu_visibility,
)
## The phase offset of the output as a [0, 100] percentage
self.phase = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_phase",
0,
100,
0
),
prefix = f"CV{n}",
title = "Phase",
autoselect_knob = True,
autoselect_cv = True,
)
## The amplitude of the output as a [0, 100] percentage
self.amplitude = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_amplitude",
0,
100,
50
),
prefix = f"CV{n}",
title = "Ampl",
autoselect_knob = True,
autoselect_cv = True,
)
## Wave width
self.width = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_width",
0,
100,
50
),
prefix = f"CV{n}",
title = "Width",
autoselect_knob = True,
autoselect_cv = True,
)
## Euclidean -- number of steps in the pattern (0 = disabled)
self.e_step = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_e_step",
0,
self.MAX_EUCLID_LENGTH,
0
),
prefix = f"CV{n}",
title = "EStep",
callback = self.change_e_length,
autoselect_knob = True,
autoselect_cv = True,
)
## Euclidean -- number of triggers in the pattern
self.e_trig = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_e_trig",
0,
self.MAX_EUCLID_LENGTH,
0
),
prefix = f"CV{n}",
title = "ETrig",
callback = self.recalculate_e_pattern,
autoselect_knob = True,
autoselect_cv = True,
)
## Euclidean -- rotation of the pattern
self.e_rot = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_e_rot",
0,
self.MAX_EUCLID_LENGTH,
0
),
prefix = f"CV{n}",
title = "ERot",
callback = self.recalculate_e_pattern,
autoselect_knob = True,
autoselect_cv = True,
)
## Probability that we skip an output [0-100]
self.skip = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_skip",
0,
100,
0
),
prefix = f"CV{n}",
title = "Skip%",
autoselect_knob = True,
autoselect_cv = True,
)
# ADSR settings
self.attack = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_attack",
0,
100,
10
),
prefix = f"CV{n}",
title = "Attack",
autoselect_knob = True,
autoselect_cv = True,
)
self.decay = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_decay",
0,
100,
10
),
prefix = f"CV{n}",
title = "Decay",
autoselect_knob = True,
autoselect_cv = True,
)
self.sustain = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_sustsain",
0,
100,
50
),
prefix = f"CV{n}",
title = "Sustain",
autoselect_knob = True,
autoselect_cv = True,
)
self.release = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_release",
0,
100,
50
),
prefix = f"CV{n}",
title = "Release",
autoselect_knob = True,
autoselect_cv = True,
)
## Swing percentage
#
# 50% -> even, no swing
# <50% -> short-long-short-long-...
# >50% -> long-short-long-short-...
self.swing = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_swing",
0,
100,
50
),
prefix = f"CV{n}",
title = "Swing%",
autoselect_knob = True,
autoselect_cv = True,
)
## Allows muting a channel during runtime
#
# A muted channel can still be edited
self.mute = SettingMenuItem(
config_point = BooleanConfigPoint(
f"cv{n}_mute",
False
),
prefix = f"CV{n}",
title="Mute",
labels = YES_NO_LABELS,
autoselect_knob = True,
autoselect_cv = True,
)
# Turing machine settings
self.t_length = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_t_len",
2,
16,
8
),
prefix = f"CV{n}",
title = "TLen",
autoselect_knob = True,
autoselect_cv = True,
)
self.t_lock = SettingMenuItem(
config_point = IntegerConfigPoint(
f"cv{n}_t_lock",
-100,
100,
0
),
prefix = f"CV{n}",
title = "TLock",
autoselect_knob = True,
autoselect_cv = True,
)
self.t_mode = SettingMenuItem(
config_point = ChoiceConfigPoint(
f"cv{n}_t_mode",
TURING_MODES,
MODE_TURING_GATE,
),
prefix = f"CV{n}",
title = "TMode",
labels = TURING_MODE_LABELS,
)
## All settings in an array so we can iterate through them in reset_settings(self)
self.all_settings = [
self.quantizer,
self.root,
self.clock_mod,
self.wave_shape,
self.phase,
self.amplitude,
self.width,
self.e_step,
self.e_trig,
self.e_rot,
self.t_length,
self.t_lock,
self.t_mode,
self.skip,
self.swing,
self.mute,
self.attack,
self.decay,
self.sustain,
self.release
]
## Counter that increases every time we finish a full wave form
self.wave_counter = 0
## The euclidean pattern we step through
self.e_pattern = [1]
## Our current position within the euclidean pattern
self.e_position = 0
## If we change patterns while playing store the next one here and
# change when the current pattern ends
#
# This helps ensure all outputs stay synchronized. The down-side is
# that a slow pattern may take a long time to reset
self.next_e_pattern = None
## The previous sample we played back
self.previous_wave_sample = 0
## Used during the tick() function to store whether or not we're skipping
# the current step
self.skip_this_step = False
self.change_e_length()
self.update_menu_visibility()
def __str__(self):
return f"out_cv{self.cv_n}"
def update_menu_visibility(self, new_value=None, old_value=None, config_point=None, arg=None):
"""Callback function for changing the visibility of menu items
@param sender The Setting object that called this function
@param args The callback arguments passed from the Setting
"""
# hide the ADSR settings if we're not in ADSR mode
wave_shape = self.wave_shape.value
show_adsr = wave_shape == WAVE_ADSR
self.attack.is_visible = show_adsr
self.decay.is_visible = show_adsr
self.sustain.is_visible = show_adsr
self.release.is_visible = show_adsr
# hide the quantization root if we're not quantizing
show_root = self.quantizer.mapped_value is not None
self.root.is_visible = show_root
# hide the width parameter if we're reading from AIN or KNOB, or outputting a sine wave
show_width = wave_shape != WAVE_AIN and wave_shape != WAVE_KNOB and wave_shape != WAVE_SIN
self.width.is_visible = show_width
# hide the turing machine settings if we're not in Turing mode
show_turing = wave_shape == WAVE_TURING
self.t_length.is_visible = show_turing
self.t_lock.is_visible = show_turing
self.t_mode.is_visible = show_turing
def change_e_length(self, new_value=None, old_value=None, config_point=None, arg=None):
self.e_trig.modify_choices(list(range(self.e_step.value+1)), self.e_step.value)
self.e_rot.modify_choices(list(range(self.e_step.value+1)), self.e_step.value)
self.recalculate_e_pattern()
def recalculate_e_pattern(self, new_value=None, old_value=None, config_point=None, arg=None):
"""Recalulate the euclidean pattern this channel outputs
"""
# always assume we're doing some kind of euclidean pattern
e_pattern = [1]
if self.e_step.value > 0:
e_pattern = generate_euclidean_pattern(self.e_step.value, self.e_trig.value, self.e_rot.value)
self.next_e_pattern = e_pattern
def request_clock_mod(self, new_value=None, old_value=None, config_point=None, arg=None):
self.clock_mod_dirty = True
def change_clock_mod(self):
self.real_clock_mod = self.clock_mod.mapped_value
self.clock_mod_dirty = False
def square_wave(self, tick, n_ticks):
"""Calculate the [0, 1] value of a square wave with PWM
@param tick The current tick, in the range [0, n_ticks)
@param n_ticks The number of ticks in which the wave must complete
@return A value in the range [0, 1] indicating the height of the wave at this tick
"""
# the first part of the square wave is on, the last part is off
# cutoff depends on the duty-cycle/pulse width
duty_cycle = n_ticks * self.width.value / 100.0
# because of phase offset the wave _can_ start at e.g. 75% of the ticks and end at the following window's 25%
start_tick = self.phase.value * n_ticks / 100.0
end_tick = (start_tick + duty_cycle) % n_ticks
if (