adjusted buffers

This commit is contained in:
John Park 2023-08-12 20:58:46 -07:00
parent 16497b25c0
commit f9495e0736

View file

@ -3,6 +3,7 @@
# SPDX-License-Identifier: MIT
# Ambient Machine inspired by Yuri Suzuki https://www.yurisuzuki.com/projects/the-ambient-machine
import os
import gc
import board
import busio
import audiocore
@ -26,8 +27,6 @@ for p in (8,9,10,11,12,4,3,2,1,0):
pin.switch_to_input(pull=Pull.UP)
switches.append(Debouncer(pin))
switch_states = [False] * 20 # list of switch states
wav_files = []
for filename in os.listdir('/samples/'): # on board flash
@ -36,62 +35,57 @@ for filename in os.listdir('/samples/'): # on board flash
print(filename)
wav_files.sort() # put in alphabetical/numberical order
gc.collect()
# Metro M7 pins for the I2S amp:
lck_pin, bck_pin, dat_pin = board.D9, board.D10, board.D12
audio = audiobusio.I2SOut(bit_clock=bck_pin, word_select=lck_pin, data=dat_pin)
mixer = audiomixer.Mixer(voice_count=len(wav_files), sample_rate=22050, channel_count=1,
bits_per_sample=16, samples_signed=True, buffer_size=8192)
bits_per_sample=16, samples_signed=True, buffer_size=2048)
audio.play(mixer)
for i in range(10): # start playing all wavs on loop w levels down
wave = audiocore.WaveFile(open(wav_files[i], "rb"))
wave = audiocore.WaveFile(wav_files[i], bytearray(1024))
mixer.voice[i].play(wave, loop=True)
mixer.voice[i].level = 0.0
LOW_VOL = 0.2
HIGH_VOL = 0.5
while True:
for i in range(len(switches)):
switches[i].update()
if i < 5: # first row plays five samples
switch_row = i // 5
if switch_row == 0: # first row plays five samples
if switches[i].fell:
if switch_states[i+5] is True: # check volume switch
mixer.voice[i].level = 0.4 # if up
if switches[i+5].value is False: # check vol switch (pull-down, so 'False' is 'on')
mixer.voice[i].level = HIGH_VOL # if up
else:
mixer.voice[i].level = 0.2 # if down
switch_states[i] = not switch_states[i]
mixer.voice[i].level = LOW_VOL # if down
if switches[i].rose:
mixer.voice[i].level = 0.0
switch_states[i] = not switch_states[i]
elif 4 < i < 10: # second row adjusts volume of first row
if switch_row == 1: # second row adjusts volume of first row
if switches[i].fell:
if switch_states[i-5] is True: # raise volume if it is on
mixer.voice[i-5].level = 0.4
switch_states[i] = not switch_states[i]
if switches[i-5].value is False: # raise volume if it is on
mixer.voice[i-5].level = HIGH_VOL
if switches[i].rose:
if switch_states[i-5] is True: # lower volume if it is on
mixer.voice[i-5].level = 0.2
switch_states[i] = not switch_states[i]
if switches[i-5].value is False: # lower volume if it is on
mixer.voice[i-5].level = LOW_VOL
elif 9 < i < 15: # third row plays five different samples
if switch_row == 2: # third row plays five different samples
if switches[i].fell:
if switch_states[i+5] is True:
mixer.voice[i-5].level = 0.4
if switches[i+5].value is False:
mixer.voice[i-5].level = HIGH_VOL
else:
mixer.voice[i-5].level = 0.2
switch_states[i] = not switch_states[i]
mixer.voice[i-5].level = LOW_VOL
if switches[i].rose:
mixer.voice[i-5].level = 0.0
switch_states[i] = not switch_states[i]
elif 14 < i < 20: # fourth row adjust volumes of third row
if switch_row == 3: # fourth row adjust volumes of third row
if switches[i].fell:
if switch_states[i-5] is True:
mixer.voice[i-10].level = 0.4
switch_states[i] = not switch_states[i]
if switches[i-5].value is False:
mixer.voice[i-10].level = HIGH_VOL
if switches[i].rose:
if switch_states[i-5] is True:
mixer.voice[i-10].level = 0.2
switch_states[i] = not switch_states[i]
if switches[i-5].value is False:
mixer.voice[i-10].level = LOW_VOL