Skip to content

Commit 726c835

Browse files
authored
Merge pull request #2590 from jedgarpark/ambient-machine
adjusted buffers
2 parents 16497b2 + f9495e0 commit 726c835

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

Ambient_Machine/code.py

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: MIT
44
# Ambient Machine inspired by Yuri Suzuki https://www.yurisuzuki.com/projects/the-ambient-machine
55
import os
6+
import gc
67
import board
78
import busio
89
import audiocore
@@ -26,8 +27,6 @@
2627
pin.switch_to_input(pull=Pull.UP)
2728
switches.append(Debouncer(pin))
2829

29-
switch_states = [False] * 20 # list of switch states
30-
3130
wav_files = []
3231

3332
for filename in os.listdir('/samples/'): # on board flash
@@ -36,62 +35,57 @@
3635
print(filename)
3736

3837
wav_files.sort() # put in alphabetical/numberical order
39-
38+
gc.collect()
4039
# Metro M7 pins for the I2S amp:
4140
lck_pin, bck_pin, dat_pin = board.D9, board.D10, board.D12
4241

4342
audio = audiobusio.I2SOut(bit_clock=bck_pin, word_select=lck_pin, data=dat_pin)
4443
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)
4645
audio.play(mixer)
4746

4847
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))
5049
mixer.voice[i].play(wave, loop=True)
5150
mixer.voice[i].level = 0.0
5251

52+
LOW_VOL = 0.2
53+
HIGH_VOL = 0.5
5354

5455
while True:
5556
for i in range(len(switches)):
5657
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
5860
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
6163
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
6465
if switches[i].rose:
6566
mixer.voice[i].level = 0.0
66-
switch_states[i] = not switch_states[i]
6767

68-
elif 4 < i < 10: # second row adjusts volume of first row
68+
if switch_row == 1: # second row adjusts volume of first row
6969
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
7372
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
7775

78-
elif 9 < i < 15: # third row plays five different samples
76+
if switch_row == 2: # third row plays five different samples
7977
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
8280
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
8582
if switches[i].rose:
8683
mixer.voice[i-5].level = 0.0
87-
switch_states[i] = not switch_states[i]
8884

89-
elif 14 < i < 20: # fourth row adjust volumes of third row
85+
if switch_row == 3: # fourth row adjust volumes of third row
9086
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
9489
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

Comments
 (0)