|
| 1 | +# SPDX-FileCopyrightText: 2022 John Park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +# put samples in "/wav" folder |
| 5 | + |
| 6 | +import time |
| 7 | +import board |
| 8 | +import keypad |
| 9 | +import audiocore |
| 10 | +import audiomixer |
| 11 | +import audiobusio |
| 12 | + |
| 13 | +# wait a little bit so USB can stabilize and not glitch audio |
| 14 | +time.sleep(3) |
| 15 | + |
| 16 | +# list of (samples to play, mixer gain level) |
| 17 | +wav_files = ( |
| 18 | + ('wav/airhorn.wav', 1.0), |
| 19 | + ('wav/bike-horn.wav', 1.0), |
| 20 | + ('wav/chime.wav', 1.0) |
| 21 | +) |
| 22 | + |
| 23 | +# pins used by keyboard |
| 24 | +KEY_PINS = ( |
| 25 | + board.D5, board.D6, board.D12 |
| 26 | +) |
| 27 | + |
| 28 | +km = keypad.Keys( KEY_PINS, value_when_pressed=False, pull=True) |
| 29 | + |
| 30 | +audio = audiobusio.I2SOut(board.D1, board.D0, board.D9) |
| 31 | + |
| 32 | +mixer = audiomixer.Mixer(voice_count=len(wav_files), sample_rate=22050, channel_count=1, |
| 33 | + bits_per_sample=16, samples_signed=True) |
| 34 | +audio.play(mixer) # attach mixer to audio playback |
| 35 | + |
| 36 | +for i in range(len(wav_files)): # start all samples at once for use w handle_mixer |
| 37 | + wave = audiocore.WaveFile(open(wav_files[i][0],"rb")) |
| 38 | + mixer.voice[i].play(wave, loop=True) |
| 39 | + mixer.voice[i].level = 0 |
| 40 | + |
| 41 | +def handle_mixer(num, pressed): |
| 42 | + voice = mixer.voice[num] # get mixer voice |
| 43 | + if pressed: |
| 44 | + voice.level = wav_files[num][1] # play at level in wav_file list |
| 45 | + else: # released |
| 46 | + voice.level = 0 # mute it |
| 47 | + |
| 48 | + |
| 49 | +while True: |
| 50 | + event = km.events.get() |
| 51 | + if event: |
| 52 | + if event.key_number < len(wav_files): |
| 53 | + if event.pressed: |
| 54 | + handle_mixer(event.key_number, True) |
| 55 | + |
| 56 | + if event.released: |
| 57 | + handle_mixer( event.key_number, False ) |
0 commit comments