|
| 1 | +# SPDX-FileCopyrightText: 2023 John Park |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# Ambient Machine inspired by Yuri Suzuki https://www.yurisuzuki.com/projects/the-ambient-machine |
| 5 | +import os |
| 6 | +import board |
| 7 | +import busio |
| 8 | +import audiocore |
| 9 | +import audiobusio |
| 10 | +import audiomixer |
| 11 | +from digitalio import Pull |
| 12 | +from adafruit_debouncer import Debouncer |
| 13 | +from adafruit_mcp230xx.mcp23017 import MCP23017 |
| 14 | + |
| 15 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 16 | +mcp_a = MCP23017(i2c, address=0x20) # default address |
| 17 | +mcp_b = MCP23017(i2c, address=0x21) # MCP23017 w/ address 0 jumper set |
| 18 | + |
| 19 | +switches = [] # set up all the switch pins on two MCP23017 boards as debouncer switches |
| 20 | +for p in (8,9,10,11,12,4,3,2,1,0): |
| 21 | + pin = mcp_a.get_pin(p) |
| 22 | + pin.switch_to_input(pull=Pull.UP) |
| 23 | + switches.append(Debouncer(pin)) |
| 24 | +for p in (8,9,10,11,12,4,3,2,1,0): |
| 25 | + pin = mcp_b.get_pin(p) |
| 26 | + pin.switch_to_input(pull=Pull.UP) |
| 27 | + switches.append(Debouncer(pin)) |
| 28 | + |
| 29 | +switch_states = [False] * 20 # list of switch states |
| 30 | + |
| 31 | +wav_files = [] |
| 32 | + |
| 33 | +for filename in os.listdir('/samples/'): # on board flash |
| 34 | + if filename.lower().endswith('.wav') and not filename.startswith('.'): |
| 35 | + wav_files.append("/samples/"+filename) |
| 36 | + print(filename) |
| 37 | + |
| 38 | +wav_files.sort() # put in alphabetical/numberical order |
| 39 | + |
| 40 | +# Metro M7 pins for the I2S amp: |
| 41 | +lck_pin, bck_pin, dat_pin = board.D9, board.D10, board.D12 |
| 42 | + |
| 43 | +audio = audiobusio.I2SOut(bit_clock=bck_pin, word_select=lck_pin, data=dat_pin) |
| 44 | +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) |
| 46 | +audio.play(mixer) |
| 47 | + |
| 48 | +for i in range(10): # start playing all wavs on loop w levels down |
| 49 | + wave = audiocore.WaveFile(open(wav_files[i], "rb")) |
| 50 | + mixer.voice[i].play(wave, loop=True) |
| 51 | + mixer.voice[i].level = 0.0 |
| 52 | + |
| 53 | + |
| 54 | +while True: |
| 55 | + for i in range(len(switches)): |
| 56 | + switches[i].update() |
| 57 | + if i < 5: # first row plays five samples |
| 58 | + 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 | + else: |
| 62 | + mixer.voice[i].level = 0.2 # if down |
| 63 | + switch_states[i] = not switch_states[i] |
| 64 | + if switches[i].rose: |
| 65 | + mixer.voice[i].level = 0.0 |
| 66 | + switch_states[i] = not switch_states[i] |
| 67 | + |
| 68 | + elif 4 < i < 10: # second row adjusts volume of first row |
| 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] |
| 73 | + 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] |
| 77 | + |
| 78 | + elif 9 < i < 15: # third row plays five different samples |
| 79 | + if switches[i].fell: |
| 80 | + if switch_states[i+5] is True: |
| 81 | + mixer.voice[i-5].level = 0.4 |
| 82 | + else: |
| 83 | + mixer.voice[i-5].level = 0.2 |
| 84 | + switch_states[i] = not switch_states[i] |
| 85 | + if switches[i].rose: |
| 86 | + mixer.voice[i-5].level = 0.0 |
| 87 | + switch_states[i] = not switch_states[i] |
| 88 | + |
| 89 | + elif 14 < i < 20: # fourth row adjust volumes of third row |
| 90 | + 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] |
| 94 | + 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] |
0 commit comments