|
| 1 | +# SPDX-FileCopyrightText: 2023 John Park for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import os |
| 6 | +import audiocore |
| 7 | +import board |
| 8 | +import audiobusio |
| 9 | +import audiomixer |
| 10 | +from digitalio import DigitalInOut, Pull |
| 11 | +from adafruit_debouncer import Button |
| 12 | + |
| 13 | +button_pins = ( |
| 14 | + board.D2, board.D3, board.D4, board.D5, |
| 15 | + board.D6, board.D7, board.D8, board.D9, |
| 16 | + board.D10, board.MOSI, board.MISO, board.SCK, |
| 17 | +) |
| 18 | +buttons = [] # will hold list of Debouncer button objects |
| 19 | +for pin in button_pins: # set up each pin |
| 20 | + tmp_pin = DigitalInOut(pin) # defaults to input |
| 21 | + tmp_pin.pull = Pull.UP # turn on internal pull-down resistor |
| 22 | + buttons.append(Button(tmp_pin, value_when_pressed=False)) |
| 23 | + |
| 24 | +# get the filenames in aplha order from files in the 'wavs' directory |
| 25 | +sounds = [] |
| 26 | +for filename in sorted(os.listdir("/wavs")): |
| 27 | + filename = filename.lower() |
| 28 | + if filename.endswith(".wav") and not filename.startswith("."): |
| 29 | + sounds.append(filename) |
| 30 | + |
| 31 | +audio = audiobusio.I2SOut(bit_clock=board.A1, word_select=board.A2, data=board.A3) |
| 32 | +mixer = audiomixer.Mixer(voice_count=1, sample_rate=11025, channel_count=1, |
| 33 | + bits_per_sample=16, samples_signed=True) |
| 34 | + |
| 35 | +audio.play(mixer) |
| 36 | +mixer.voice[0].level = 0.5 |
| 37 | + |
| 38 | +def play_sound(sound_number): |
| 39 | + wave_file = open(("wavs/" + sounds[sound_number]), "rb") |
| 40 | + wave = audiocore.WaveFile(wave_file) |
| 41 | + mixer.voice[0].play(wave, loop=False) |
| 42 | + |
| 43 | + |
| 44 | +while True: |
| 45 | + for i in range(len(buttons)): |
| 46 | + buttons[i].update() |
| 47 | + if buttons[i].pressed: |
| 48 | + play_sound(i) |
0 commit comments