|
3 | 3 | # SPDX-License-Identifier: MIT |
4 | 4 | # Ambient Machine inspired by Yuri Suzuki https://www.yurisuzuki.com/projects/the-ambient-machine |
5 | 5 | import os |
| 6 | +import gc |
6 | 7 | import board |
7 | 8 | import busio |
8 | 9 | import audiocore |
|
26 | 27 | pin.switch_to_input(pull=Pull.UP) |
27 | 28 | switches.append(Debouncer(pin)) |
28 | 29 |
|
29 | | -switch_states = [False] * 20 # list of switch states |
30 | | - |
31 | 30 | wav_files = [] |
32 | 31 |
|
33 | 32 | for filename in os.listdir('/samples/'): # on board flash |
|
36 | 35 | print(filename) |
37 | 36 |
|
38 | 37 | wav_files.sort() # put in alphabetical/numberical order |
39 | | - |
| 38 | +gc.collect() |
40 | 39 | # Metro M7 pins for the I2S amp: |
41 | 40 | lck_pin, bck_pin, dat_pin = board.D9, board.D10, board.D12 |
42 | 41 |
|
43 | 42 | audio = audiobusio.I2SOut(bit_clock=bck_pin, word_select=lck_pin, data=dat_pin) |
44 | 43 | mixer = audiomixer.Mixer(voice_count=len(wav_files), sample_rate=22050, channel_count=1, |
45 | | - bits_per_sample=16, samples_signed=True, buffer_size=8192) |
| 44 | + bits_per_sample=16, samples_signed=True, buffer_size=2048) |
46 | 45 | audio.play(mixer) |
47 | 46 |
|
48 | 47 | for i in range(10): # start playing all wavs on loop w levels down |
49 | | - wave = audiocore.WaveFile(open(wav_files[i], "rb")) |
| 48 | + wave = audiocore.WaveFile(wav_files[i], bytearray(1024)) |
50 | 49 | mixer.voice[i].play(wave, loop=True) |
51 | 50 | mixer.voice[i].level = 0.0 |
52 | 51 |
|
| 52 | +LOW_VOL = 0.2 |
| 53 | +HIGH_VOL = 0.5 |
53 | 54 |
|
54 | 55 | while True: |
55 | 56 | for i in range(len(switches)): |
56 | 57 | switches[i].update() |
57 | | - if i < 5: # first row plays five samples |
| 58 | + switch_row = i // 5 |
| 59 | + if switch_row == 0: # first row plays five samples |
58 | 60 | if switches[i].fell: |
59 | | - if switch_states[i+5] is True: # check volume switch |
60 | | - mixer.voice[i].level = 0.4 # if up |
| 61 | + if switches[i+5].value is False: # check vol switch (pull-down, so 'False' is 'on') |
| 62 | + mixer.voice[i].level = HIGH_VOL # if up |
61 | 63 | else: |
62 | | - mixer.voice[i].level = 0.2 # if down |
63 | | - switch_states[i] = not switch_states[i] |
| 64 | + mixer.voice[i].level = LOW_VOL # if down |
64 | 65 | if switches[i].rose: |
65 | 66 | mixer.voice[i].level = 0.0 |
66 | | - switch_states[i] = not switch_states[i] |
67 | 67 |
|
68 | | - elif 4 < i < 10: # second row adjusts volume of first row |
| 68 | + if switch_row == 1: # second row adjusts volume of first row |
69 | 69 | if switches[i].fell: |
70 | | - if switch_states[i-5] is True: # raise volume if it is on |
71 | | - mixer.voice[i-5].level = 0.4 |
72 | | - switch_states[i] = not switch_states[i] |
| 70 | + if switches[i-5].value is False: # raise volume if it is on |
| 71 | + mixer.voice[i-5].level = HIGH_VOL |
73 | 72 | if switches[i].rose: |
74 | | - if switch_states[i-5] is True: # lower volume if it is on |
75 | | - mixer.voice[i-5].level = 0.2 |
76 | | - switch_states[i] = not switch_states[i] |
| 73 | + if switches[i-5].value is False: # lower volume if it is on |
| 74 | + mixer.voice[i-5].level = LOW_VOL |
77 | 75 |
|
78 | | - elif 9 < i < 15: # third row plays five different samples |
| 76 | + if switch_row == 2: # third row plays five different samples |
79 | 77 | if switches[i].fell: |
80 | | - if switch_states[i+5] is True: |
81 | | - mixer.voice[i-5].level = 0.4 |
| 78 | + if switches[i+5].value is False: |
| 79 | + mixer.voice[i-5].level = HIGH_VOL |
82 | 80 | else: |
83 | | - mixer.voice[i-5].level = 0.2 |
84 | | - switch_states[i] = not switch_states[i] |
| 81 | + mixer.voice[i-5].level = LOW_VOL |
85 | 82 | if switches[i].rose: |
86 | 83 | mixer.voice[i-5].level = 0.0 |
87 | | - switch_states[i] = not switch_states[i] |
88 | 84 |
|
89 | | - elif 14 < i < 20: # fourth row adjust volumes of third row |
| 85 | + if switch_row == 3: # fourth row adjust volumes of third row |
90 | 86 | if switches[i].fell: |
91 | | - if switch_states[i-5] is True: |
92 | | - mixer.voice[i-10].level = 0.4 |
93 | | - switch_states[i] = not switch_states[i] |
| 87 | + if switches[i-5].value is False: |
| 88 | + mixer.voice[i-10].level = HIGH_VOL |
94 | 89 | if switches[i].rose: |
95 | | - if switch_states[i-5] is True: |
96 | | - mixer.voice[i-10].level = 0.2 |
97 | | - switch_states[i] = not switch_states[i] |
| 90 | + if switches[i-5].value is False: |
| 91 | + mixer.voice[i-10].level = LOW_VOL |
0 commit comments