-
Notifications
You must be signed in to change notification settings - Fork 786
/
Copy pathcode.py
740 lines (688 loc) · 31.2 KB
/
code.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
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT
from random import randint
import ulab.numpy as np
import board
import audiobusio
import audiomixer
import synthio
import simpleio
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
from adafruit_ht16k33 import segments
from adafruit_ht16k33.matrix import Matrix8x8x2
from adafruit_seesaw import seesaw, rotaryio, digitalio
SAMPLE_RATE = 44100
SAMPLE_SIZE = 256
VOLUME = 5000
# waveforms, envelopes and synth setup
square = np.concatenate((np.ones(SAMPLE_SIZE//2, dtype=np.int16)*VOLUME,np.ones(SAMPLE_SIZE//2,
dtype=np.int16)*-VOLUME))
sine = np.array(np.sin(np.linspace(0, 4*np.pi, SAMPLE_SIZE, endpoint=False)) * VOLUME,
dtype=np.int16)
saw = np.linspace(VOLUME, -VOLUME, num=SAMPLE_SIZE, dtype=np.int16)
noise = np.array([randint(-VOLUME, VOLUME) for i in range(SAMPLE_SIZE)], dtype=np.int16)
lfo = synthio.LFO(rate = .5, waveform = sine)
amp_env0 = synthio.Envelope(attack_time=0.1, decay_time = 0.1, release_time=0.1,
attack_level=1, sustain_level=0.05)
amp_env1 = synthio.Envelope(attack_time=0.05, decay_time = 0.1, release_time=0.1,
attack_level=1, sustain_level=0.05)
# synth plays the notes
synth = synthio.Synthesizer(sample_rate=SAMPLE_RATE)
# these are the notes
synth0 = synthio.Note(frequency = 0.0, envelope=amp_env0, waveform=square, ring_frequency = 0,
ring_bend = lfo, ring_waveform = sine)
synth1 = synthio.Note(frequency = 0.0, envelope=amp_env1, waveform=sine, ring_frequency = 0,
ring_bend = lfo, ring_waveform = sine)
synth2 = synthio.Note(frequency = 0.0, envelope=amp_env0, waveform=square, ring_frequency = 0,
ring_bend = lfo, ring_waveform = sine)
synth3 = synthio.Note(frequency = 0.0, envelope=amp_env1, waveform=sine, ring_frequency = 0,
ring_bend = lfo, ring_waveform = sine)
synths = [synth0, synth1, synth2, synth3]
wave_names = ["SQUR", "SINE", "SAW ", "NOIZ"]
waveforms = [square, sine, saw, noise]
synth0_wave = 0
synth1_wave = 1
synth2_wave = 0
synth3_wave = 1
# i2s amp setup
audio = audiobusio.I2SOut(bit_clock=board.D10, word_select=board.D11, data=board.D9)
mixer = audiomixer.Mixer(voice_count=4, sample_rate=SAMPLE_RATE, channel_count=1,
bits_per_sample=16, samples_signed=True, buffer_size=2048 )
audio.play(mixer)
vol_val = 2
mixer.voice[0].play(synth)
mixer.voice[0].level = 0.3
# these are the triads, all major
c_tones = [130.81, 164.81, 196.00]
g_tones = [196.00, 246.94, 293.66]
d_tones = [146.83, 185.00, 220.00]
a_tones = [220.00, 277.18, 329.63]
e_tones = [164.81, 207.65, 246.94]
b_tones = [246.94, 311.13, 369.99]
fsharp_tones = [185.00, 233.08, 277.18]
csharp_tones = [138.59, 174.61, 207.65]
aflat_tones = [207.65, 261.63, 311.13]
eflat_tones = [155.56, 196.00, 233.08]
bflat_tones = [233.08, 293.66, 349.23]
f_tones = [174.61, 220.00, 261.63]
# names for the alphanumeric displays
chord_names = ["Cmaj", "Gmaj", "Dmaj", "Amaj", "Emaj", "Bmaj",
"F#ma", "C#ma", "Abma", "Ebma", "Bbma", "Fmaj"]
chords = [c_tones, g_tones, d_tones, a_tones, e_tones, b_tones, fsharp_tones, csharp_tones,
aflat_tones, eflat_tones, bflat_tones, f_tones]
# i2c setup
i2c = board.I2C()
# the encoders
seesaw0 = seesaw.Seesaw(i2c, addr=0x49)
seesaw1 = seesaw.Seesaw(i2c, addr=0x4A)
seesaw2 = seesaw.Seesaw(i2c, addr=0x4B)
seesaw3 = seesaw.Seesaw(i2c, addr=0x4C)
menu_seesaw = seesaw.Seesaw(i2c, addr=0x4D)
# the alphanumeric displays
display0 = segments.Seg14x4(i2c, address=0x70)
display1 = segments.Seg14x4(i2c, address=0x71)
display2 = segments.Seg14x4(i2c, address=0x72)
display3 = segments.Seg14x4(i2c, address=0x73)
menu_display = segments.Seg14x4(i2c, address=0x74)
# the matrix
matrix0 = Matrix8x8x2(i2c, address=0x75)
seesaws = [seesaw0, seesaw1, seesaw2, seesaw3, menu_seesaw]
buttons0 = []
buttons1 = []
buttons2 = []
buttons3 = []
menu_buttons = []
button0_states = []
button1_states = []
button2_states = []
button3_states = []
menu_states = []
button0_names = ["Select", "Up", "Left", "Down", "Right"]
# setup the buttons on all of the encoders
for i in range(1, 6):
seesaw0.pin_mode(i, seesaw0.INPUT_PULLUP)
seesaw1.pin_mode(i, seesaw1.INPUT_PULLUP)
seesaw2.pin_mode(i, seesaw2.INPUT_PULLUP)
seesaw3.pin_mode(i, seesaw3.INPUT_PULLUP)
menu_seesaw.pin_mode(i, menu_seesaw.INPUT_PULLUP)
buttons0.append(digitalio.DigitalIO(seesaw0, i))
buttons1.append(digitalio.DigitalIO(seesaw1, i))
buttons2.append(digitalio.DigitalIO(seesaw2, i))
buttons3.append(digitalio.DigitalIO(seesaw3, i))
menu_buttons.append(digitalio.DigitalIO(menu_seesaw, i))
button0_states.append(False)
button1_states.append(False)
button2_states.append(False)
button3_states.append(False)
menu_states.append(False)
# make all of the encoders
encoder0 = rotaryio.IncrementalEncoder(seesaw0)
last_position0 = 0
encoder1 = rotaryio.IncrementalEncoder(seesaw1)
last_position1 = 0
encoder2 = rotaryio.IncrementalEncoder(seesaw2)
last_position2 = 0
encoder3 = rotaryio.IncrementalEncoder(seesaw3)
last_position3 = 0
menu_enc = rotaryio.IncrementalEncoder(menu_seesaw)
last_menuPosition = 0
# Python Implementation of Björklund's Algorithm by Brian House
# MIT License 2011
# https://github.com/brianhouse/bjorklund
def bjorklund(steps, pulses):
steps = int(steps)
pulses = int(pulses)
if pulses > steps:
raise ValueError
pattern = []
counts = []
remainders = []
divisor = steps - 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)
p = pattern.index(1)
pattern = pattern[p:] + pattern[0:p]
return pattern
# using ticks for time tracking
clock = ticks_ms()
# default BPM
bpm = 120
# beat divison
beat_div = [15, 30, 60, 120, 240]
beat_index = 2
beat_names = ["1/16", "1/8 ", "1/4 ", "1/2 ", "HOLE"]
delay = int((beat_div[beat_index] / bpm) * 1000)
# variables for euclidean
c0 = 0
c1 = 0
c2 = 0
c3 = 0
r0 = 0
r1 = 0
r2 = 0
r3 = 0
last_r0 = 0
last_r1 = 0
last_r2 = 0
last_r3 = 0
euclid0_steps = 8
euclid0_pulses = 4
euclid1_steps = 8
euclid1_pulses = 4
euclid2_steps = 8
euclid2_pulses = 4
euclid3_steps = 8
euclid3_pulses = 4
rhythm0 = bjorklund(euclid0_steps, euclid0_pulses)
rhythm1 = bjorklund(euclid1_steps, euclid1_pulses)
rhythm2 = bjorklund(euclid2_steps, euclid2_pulses)
rhythm3 = bjorklund(euclid3_steps, euclid3_pulses)
# read buttons to update Euclidean rhythms
# pylint: disable=too-many-branches
def read_buttons(button_array, button_states, euc, e_step, e_pulse, the_step):
for b in range(5):
if not button_array[b].value and button_states[b] is False:
button_states[b] = True
if button0_names[b] == "Select":
e_step = 8
e_pulse = 4
if the_step >= e_step:
the_step = 0
elif button0_names[b] == "Up":
if e_step > 16:
e_step = 16
else:
e_step += 1
elif button0_names[b] == "Down":
if e_step < 1:
e_step = 1
else:
e_step -= 1
if the_step >= e_step:
the_step = 0
elif button0_names[b] == "Left":
e_pulse -= 1
e_pulse = max(e_pulse, 1)
else:
e_pulse += 1
e_pulse = min(e_pulse, e_step)
euc = bjorklund(e_step, e_pulse)
if button_array[b].value and button_states[b] is True:
button_states[b] = False
if button0_names[b] in ("Select", "Up", "Down"):
matrix0.fill(matrix0.LED_OFF)
draw_steps(euclid0_steps, 0)
draw_steps(euclid1_steps, 2)
draw_steps(euclid2_steps, 4)
draw_steps(euclid3_steps, 6)
return euc, e_step, e_pulse, the_step
# play euclidean rhythms and update matrix
def play_euclidean(this_synth, n, the_rhythm, rhythm_count, last_count, c, matrix_slot):
if last_count <= 7:
matrix0[matrix_slot, last_count] = matrix0.LED_GREEN
else:
c -= 1
matrix0[matrix_slot + 1, (last_count - last_count) + c] = matrix0.LED_GREEN
c += 1
if the_rhythm[rhythm_count] == 1:
this_synth.frequency = n[randint(0, 2)]
synth.press(this_synth)
if rhythm_count <= 7:
matrix0[matrix_slot, rhythm_count] = matrix0.LED_RED
else:
matrix0[matrix_slot + 1, (rhythm_count - rhythm_count) + c] = matrix0.LED_RED
c += 1
else:
synth.release(this_synth)
if rhythm_count > 7:
c += 1
last_count = rhythm_count
rhythm_count += 1
if rhythm_count >= len(the_rhythm):
rhythm_count = 0
if rhythm_count == 1:
c = 0
return rhythm_count, last_count, c
# initial matrix draw
def draw_steps(euc_steps, col):
dif = 0
for m in range(euc_steps):
if m <= 7:
matrix0[col, m] = matrix0.LED_GREEN
else:
matrix0[col + 1, (m - m) + dif] = matrix0.LED_GREEN
dif += 1
draw_steps(euclid0_steps, 0)
draw_steps(euclid1_steps, 2)
draw_steps(euclid2_steps, 4)
draw_steps(euclid3_steps, 6)
# clocks for playing euclidean and reading menu encoder
enc_clock = ticks_ms()
menu_clock = ticks_ms()
# the modes menu
modes = ["PLAY", "EUC ", "BPM ", "BEAT", "ADSR", "WAVE", "RING", "LFO ", "VOL "]
mode_index = 0
mode = modes[mode_index]
menu_display.print(f" {mode}")
# default chords
chord0_sel = 0
chord1_sel = 1
chord2_sel = 0
chord3_sel = 1
display0.print(chord_names[chord0_sel])
display1.print(chord_names[chord1_sel])
display2.print(chord_names[chord2_sel])
display3.print(chord_names[chord3_sel])
# arrays of individual buttons
select_buttons = [buttons0[0], buttons1[0], buttons2[0], buttons3[0]]
left_buttons = [buttons0[2], buttons1[2], buttons2[2], buttons3[2]]
right_buttons = [buttons0[4], buttons1[4], buttons2[4], buttons3[4]]
select_states = [button0_states[0], button1_states[0], button2_states[0], button3_states[0]]
left_states = [button0_states[2], button1_states[2], button2_states[2], button3_states[2]]
right_states = [button0_states[4], button1_states[4], button2_states[4], button3_states[4]]
select_index = 0
left_index = 0
right_index = 0
# adsr mode
adsr_names = ["A", "D", "S", "R"]
synth_adsr_indexes = [0, 0, 0, 0]
adsr_properties = [0, 1, 4, 2]
adsr0_values = [amp_env0.attack_time, amp_env0.decay_time,
amp_env0.sustain_level, amp_env0.release_time]
adsr1_values = [amp_env1.attack_time, amp_env1.decay_time,
amp_env1.sustain_level, amp_env1.release_time]
adsr2_values = [amp_env0.attack_time, amp_env0.decay_time,
amp_env0.sustain_level, amp_env0.release_time]
adsr3_values = [amp_env1.attack_time, amp_env1.decay_time,
amp_env1.sustain_level, amp_env1.release_time]
all_adsr_values = [adsr0_values, adsr1_values, adsr2_values, adsr3_values]
adsr0_val = int(simpleio.map_range(amp_env0.attack_time, 0.0, 1.0, 0, 19))
adsr1_val = int(simpleio.map_range(amp_env0.decay_time, 0.0, 1.0, 0, 19))
adsr2_val = int(simpleio.map_range(amp_env0.sustain_level, 0.0, 1.0, 0, 19))
adsr3_val = int(simpleio.map_range(amp_env0.release_time, 0.0, 1.0, 0, 19))
clock_stretch = False
ring0_val = 0
ring1_val = 0
ring2_val = 0
ring3_val = 0
lfo_val = 0
# used to play/pause
play_states = [True, True, True, True]
while True:
# rotary encoder reading
if ticks_diff(ticks_ms(), enc_clock) >= 100:
position0 = encoder0.position
position1 = encoder1.position
position2 = encoder2.position
position3 = encoder3.position
menuPosition = menu_enc.position
# menu changes mode
if menuPosition != last_menuPosition:
if menuPosition > last_menuPosition:
mode_index = (mode_index + 1) % len(modes)
else:
mode_index = (mode_index - 1) % len(modes)
if mode in ("EUC ", "ADSR"):
clock_stretch = True
if mode in ("PLAY", "BPM ", "BEAT", "WAVE") and clock_stretch:
clock = ticks_ms()
clock_stretch = False
mode = modes[mode_index]
menu_display.print(f" {mode}")
last_menuPosition = menuPosition
# encoder functionality depends on mode
# encoder 0 has most functionality
if position0 != last_position0:
if position0 > last_position0:
if mode == "PLAY":
chord0_sel = (chord0_sel + 1) % len(chords)
display0.print(chord_names[chord0_sel])
elif mode == "BEAT":
beat_index = (beat_index + 1) % 5
delay = int((beat_div[beat_index] / bpm) * 1000)
display0.print(f" {beat_names[beat_index]}")
elif mode == "BPM ":
bpm += 1
delay = int((beat_div[beat_index] / bpm) * 1000)
display0.print(f" {bpm}")
elif mode == "ADSR":
adsr0_val = (adsr0_val + 1) % 20
mapped_val = simpleio.map_range(adsr0_val, 0, 19, 0.0, 1.0)
all_adsr_values[0][synth_adsr_indexes[0]] = mapped_val
the_env = synthio.Envelope(attack_time=all_adsr_values[0][0],
decay_time = all_adsr_values[0][1],
release_time=all_adsr_values[0][3],
attack_level=1, sustain_level=all_adsr_values[0][2])
synth0.envelope = the_env
elif mode == "WAVE":
synth0_wave = (synth0_wave + 1) % len(wave_names)
synth0.waveform = waveforms[synth0_wave]
elif mode == "RING":
ring0_val = (ring0_val + 1) % 25
mapped_val = simpleio.map_range(ring0_val, 0, 24, 0.0, 220.0)
synth0.ring_frequency = mapped_val
elif mode == "LFO ":
lfo_val = (lfo_val + 1) % 10
mapped_val = simpleio.map_range(lfo_val, 0, 9, 0.0, 5.0)
lfo.rate = mapped_val
elif mode == "VOL ":
vol_val = (vol_val + 1) % 10
mapped_val = simpleio.map_range(vol_val, 0, 9, 0.0, 1.0)
mixer.voice[0].level = mapped_val
else:
if mode == "PLAY":
chord0_sel = (chord0_sel - 1) % len(chords)
display0.print(chord_names[chord0_sel])
elif mode == "BEAT":
beat_index = (beat_index - 1) % 5
delay = int((beat_div[beat_index] / bpm) * 1000)
display0.print(f" {beat_names[beat_index]}")
elif mode == "BPM ":
bpm -= 1
display0.print(f" {bpm}")
elif mode == "ADSR":
adsr0_val = (adsr0_val - 1) % 20
mapped_val = simpleio.map_range(adsr0_val, 0, 19, 0.0, 1.0)
all_adsr_values[0][synth_adsr_indexes[0]] = mapped_val
the_env = synthio.Envelope(attack_time=all_adsr_values[0][0],
decay_time = all_adsr_values[0][1],
release_time=all_adsr_values[0][3],
attack_level=1, sustain_level=all_adsr_values[0][2])
synth0.envelope = the_env
elif mode == "WAVE":
synth0_wave = (synth0_wave - 1) % len(wave_names)
synth0.waveform = waveforms[synth0_wave]
elif mode == "RING":
ring0_val = (ring0_val - 1) % 25
mapped_val = simpleio.map_range(ring0_val, 0, 24, 0.0, 220.0)
synth0.ring_frequency = mapped_val
elif mode == "LFO ":
lfo_val = (lfo_val - 1) % 10
mapped_val = simpleio.map_range(lfo_val, 0, 9, 0.0, 5.0)
lfo.rate = mapped_val
elif mode == "VOL ":
vol_val = (vol_val - 1) % 10
mapped_val = simpleio.map_range(vol_val, 0, 9, 0.0, 1.0)
mixer.voice[0].level = mapped_val
last_position0 = position0
if position1 != last_position1:
if position1 > last_position1:
if mode == "PLAY":
chord1_sel = (chord1_sel + 1) % len(chords)
display1.print(chord_names[chord1_sel])
elif mode == "ADSR":
adsr1_val = (adsr1_val + 1) % 20
mapped_val = simpleio.map_range(adsr1_val, 0, 19, 0.0, 1.0)
all_adsr_values[1][synth_adsr_indexes[1]] = mapped_val
the_env = synthio.Envelope(attack_time=all_adsr_values[1][0],
decay_time = all_adsr_values[1][1],
release_time=all_adsr_values[1][3],
attack_level=1, sustain_level=all_adsr_values[1][2])
synth1.envelope = the_env
elif mode == "WAVE":
synth1_wave = (synth1_wave + 1) % len(wave_names)
synth1.waveform = waveforms[synth1_wave]
elif mode == "RING":
ring1_val = (ring1_val + 1) % 25
mapped_val = simpleio.map_range(ring1_val, 0, 24, 0.0, 220.0)
synth1.ring_frequency = mapped_val
else:
if mode == "PLAY":
chord1_sel = (chord1_sel - 1) % len(chords)
display1.print(chord_names[chord1_sel])
elif mode == "ADSR":
adsr1_val = (adsr1_val - 1) % 20
mapped_val = simpleio.map_range(adsr1_val, 0, 19, 0.0, 1.0)
all_adsr_values[1][synth_adsr_indexes[1]] = mapped_val
the_env = synthio.Envelope(attack_time=all_adsr_values[1][0],
decay_time = all_adsr_values[1][1],
release_time=all_adsr_values[1][3],
attack_level=1, sustain_level=all_adsr_values[1][2])
synth1.envelope = the_env
elif mode == "WAVE":
synth1_wave = (synth1_wave - 1) % len(wave_names)
synth1.waveform = waveforms[synth1_wave]
elif mode == "RING":
ring1_val = (ring1_val - 1) % 25
mapped_val = simpleio.map_range(ring1_val, 0, 24, 0.0, 220.0)
synth1.ring_frequency = mapped_val
last_position1 = position1
if position2 != last_position2:
if position2 > last_position2:
if mode == "PLAY":
chord2_sel = (chord2_sel + 1) % len(chords)
elif mode == "ADSR":
adsr2_val = (adsr2_val + 1) % 20
mapped_val = simpleio.map_range(adsr2_val, 0, 19, 0.0, 1.0)
all_adsr_values[2][synth_adsr_indexes[2]] = mapped_val
the_env = synthio.Envelope(attack_time=all_adsr_values[2][0],
decay_time = all_adsr_values[2][1],
release_time=all_adsr_values[2][3],
attack_level=1, sustain_level=all_adsr_values[2][2])
synth2.envelope = the_env
elif mode == "WAVE":
synth2_wave = (synth2_wave + 1) % len(wave_names)
synth2.waveform = waveforms[synth2_wave]
elif mode == "RING":
ring2_val = (ring2_val + 1) % 25
mapped_val = simpleio.map_range(ring2_val, 0, 24, 0.0, 220.0)
synth2.ring_frequency = mapped_val
else:
if mode == "PLAY":
chord2_sel = (chord2_sel - 1) % len(chords)
display2.print(chord_names[chord2_sel])
elif mode == "ADSR":
adsr2_val = (adsr2_val - 1) % 20
mapped_val = simpleio.map_range(adsr2_val, 0, 19, 0.0, 1.0)
all_adsr_values[2][synth_adsr_indexes[2]] = mapped_val
the_env = synthio.Envelope(attack_time=all_adsr_values[2][0],
decay_time = all_adsr_values[2][1],
release_time=all_adsr_values[2][3],
attack_level=1, sustain_level=all_adsr_values[2][2])
synth2.envelope = the_env
elif mode == "WAVE":
synth2_wave = (synth2_wave - 1) % len(wave_names)
synth2.waveform = waveforms[synth2_wave]
elif mode == "RING":
ring2_val = (ring2_val - 1) % 25
mapped_val = simpleio.map_range(ring2_val, 0, 24, 0.0, 220.0)
synth2.ring_frequency = mapped_val
last_position2 = position2
if position3 != last_position3:
if position3 > last_position3:
if mode == "PLAY":
chord3_sel = (chord3_sel + 1) % len(chords)
display3.print(chord_names[chord3_sel])
elif mode == "ADSR":
adsr3_val = (adsr3_val + 1) % 20
mapped_val = simpleio.map_range(adsr3_val, 0, 19, 0.0, 1.0)
all_adsr_values[3][synth_adsr_indexes[3]] = mapped_val
the_env = synthio.Envelope(attack_time=all_adsr_values[3][0],
decay_time = all_adsr_values[3][1],
release_time=all_adsr_values[3][3],
attack_level=1, sustain_level=all_adsr_values[3][2])
synth3.envelope = the_env
elif mode == "WAVE":
synth3_wave = (synth3_wave + 1) % len(wave_names)
synth3.waveform = waveforms[synth3_wave]
elif mode == "RING":
ring3_val = (ring3_val + 1) % 25
mapped_val = simpleio.map_range(ring3_val, 0, 24, 0.0, 220.0)
synth3.ring_frequency = mapped_val
else:
if mode == "PLAY":
chord3_sel = (chord3_sel - 1) % len(chords)
display3.print(chord_names[chord3_sel])
elif mode == "ADSR":
adsr3_val = (adsr3_val - 1) % 20
mapped_val = simpleio.map_range(adsr3_val, 0, 19, 0.0, 1.0)
all_adsr_values[3][synth_adsr_indexes[3]] = mapped_val
the_env = synthio.Envelope(attack_time=all_adsr_values[3][0],
decay_time = all_adsr_values[3][1],
release_time=all_adsr_values[3][3],
attack_level=1, sustain_level=all_adsr_values[3][2])
synth3.envelope = the_env
elif mode == "WAVE":
synth3_wave = (synth3_wave - 1) % len(wave_names)
synth3.waveform = waveforms[synth3_wave]
elif mode == "RING":
ring3_val = (ring3_val - 1) % 25
mapped_val = simpleio.map_range(ring3_val, 0, 24, 0.0, 220.0)
synth3.ring_frequency = mapped_val
last_position3 = position3
enc_clock = ticks_add(enc_clock, 100)
# synth plays based on ticks timing
if ticks_diff(ticks_ms(), clock) >= delay:
if play_states[0] is True:
r0, last_r0, c0 = play_euclidean(synth0, chords[chord0_sel],
rhythm0, r0, last_r0, c0, 0)
if play_states[1] is True:
r1, last_r1, c1 = play_euclidean(synth1, chords[chord1_sel],
rhythm1, r1, last_r1, c1, 2)
if play_states[2] is True:
r2, last_r2, c2 = play_euclidean(synth2, chords[chord2_sel],
rhythm2, r2, last_r2, c2, 4)
if play_states[3] is True:
r3, last_r3, c3 = play_euclidean(synth3, chords[chord3_sel],
rhythm3, r3, last_r3, c3, 6)
clock = ticks_add(clock, delay)
# in PLAY select button controls play/pause
if mode == "PLAY":
for i in range(4):
if not select_buttons[i].value and select_states[i] is False:
select_states[i] = True
if play_states[i] is True:
synth.release(synths[i])
play_states[i] = False
else:
play_states[i] = True
if select_buttons[i].value and select_states[i] is True:
select_states[i] = False
display0.print(chord_names[chord0_sel])
display1.print(chord_names[chord1_sel])
display2.print(chord_names[chord2_sel])
display3.print(chord_names[chord3_sel])
# EUC menu select resets cycle count
elif mode == "EUC ":
if not menu_buttons[0].value and menu_states[0] is False:
r0 = 0
r1 = 0
r2 = 0
r3 = 0
menu_states[0] = True
if menu_buttons[0].value and menu_states[0] is True:
menu_states[0] = False
rhythm0, euclid0_steps, euclid0_pulses, r0 = read_buttons(buttons0, button0_states,
rhythm0, euclid0_steps,
euclid0_pulses, r0)
rhythm1, euclid1_steps, euclid1_pulses, r1 = read_buttons(buttons1, button1_states,
rhythm1, euclid1_steps,
euclid1_pulses, r1)
rhythm2, euclid2_steps, euclid2_pulses, r2 = read_buttons(buttons2, button2_states,
rhythm2, euclid2_steps,
euclid2_pulses, r2)
rhythm3, euclid3_steps, euclid3_pulses, r3 = read_buttons(buttons3, button3_states,
rhythm3, euclid3_steps,
euclid3_pulses, r3)
display0.print(f" {euclid0_pulses}")
display1.print(f" {euclid1_pulses}")
display2.print(f" {euclid2_pulses}")
display3.print(f" {euclid3_pulses}")
# BPM is adjusted
elif mode == "BPM ":
if not select_buttons[0].value and select_states[0] is False:
bpm = 120
select_states[0] = True
if select_buttons[0].value and select_states[0] is True:
select_states[0] = False
display0.print(f" {bpm}")
display1.print(" ")
display2.print(" ")
display3.print(" ")
# beat division is changed
elif mode == "BEAT":
if not select_buttons[0].value and select_states[0] is False:
beat_names[beat_index] = 2
select_states[0] = True
if select_buttons[0].value and select_states[0] is True:
select_states[0] = False
display0.print(f" {beat_names[beat_index]}")
display1.print(" ")
display2.print(" ")
display3.print(" ")
# adsr for each voice
elif mode == "ADSR":
for i in range(4):
if not left_buttons[i].value and left_states[i] is False:
synth_adsr_indexes[i] = (synth_adsr_indexes[i] - 1) % 4
left_states[i] = True
the_synth = synths[i]
if left_buttons[i].value and left_states[i] is True:
left_states[i] = False
if not right_buttons[i].value and right_states[i] is False:
synth_adsr_indexes[i] = (synth_adsr_indexes[i] + 1) % 4
right_states[i] = True
if right_buttons[i].value and right_states[i] is True:
right_states[i] = False
if not select_buttons[i].value and select_states[i] is False:
the_synth = synths[i]
all_adsr_values[i][0] = 0.1
all_adsr_values[i][1] = 0.1
all_adsr_values[i][3] = 0.1
all_adsr_values[i][2] = 0.05
the_env = synthio.Envelope(attack_time=all_adsr_values[i][0],
decay_time = all_adsr_values[i][1],
release_time=all_adsr_values[i][3],
attack_level=1, sustain_level=all_adsr_values[i][2])
the_synth.envelope = the_env
select_states[i] = True
if select_buttons[i].value and select_states[i] is True:
select_states[i] = False
# pylint: disable=line-too-long
display0.print(f"{adsr_names[synth_adsr_indexes[0]]}{synth0.envelope[adsr_properties[synth_adsr_indexes[0]]]:.2f}")
display1.print(f"{adsr_names[synth_adsr_indexes[1]]}{synth1.envelope[adsr_properties[synth_adsr_indexes[1]]]:.2f}")
display2.print(f"{adsr_names[synth_adsr_indexes[2]]}{synth2.envelope[adsr_properties[synth_adsr_indexes[2]]]:.2f}")
display3.print(f"{adsr_names[synth_adsr_indexes[3]]}{synth3.envelope[adsr_properties[synth_adsr_indexes[3]]]:.2f}")
# change waveform
elif mode == "WAVE":
display0.print(f" {wave_names[synth0_wave]}")
display1.print(f" {wave_names[synth1_wave]}")
display2.print(f" {wave_names[synth2_wave]}")
display3.print(f" {wave_names[synth3_wave]}")
# adjust ring modulation
elif mode == "RING":
display0.print(f" {synth0.ring_frequency:.1f}")
display1.print(f" {synth1.ring_frequency:.1f}")
display2.print(f" {synth2.ring_frequency:.1f}")
display3.print(f" {synth3.ring_frequency:.1f}")
# adjust lfo rate used for ring modulation
elif mode == "LFO ":
display0.print("RATE")
display1.print(f" {lfo.rate:.1f}")
display2.print(" ")
display3.print(" ")
# overall volume 0.0 - 1.0
elif mode == "VOL ":
display0.print(f" {mixer.voice[0].level:.1f}")
display1.print(" ")
display2.print(" ")
display3.print(" ")