|
| 1 | +# SPDX-FileCopyrightText: 2022 John Park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +# Breakbeat Breadboard based on: |
| 5 | +# @todbot / Tod Kurt - https://github.com/todbot/plinkykeeb |
| 6 | +# Convert files to appropriate WAV format (mono, 22050 Hz, 16-bit signed) with command: |
| 7 | +# sox loop.mp3 -b 16 -c 1 -r 22050 loop.wav |
| 8 | +# put samples in "/wav" folder |
| 9 | + |
| 10 | +import time |
| 11 | +import board |
| 12 | +import keypad |
| 13 | +import audiocore |
| 14 | +import audiomixer |
| 15 | +from audiopwmio import PWMAudioOut as AudioOut |
| 16 | + |
| 17 | +# wait a little bit so USB can stabilize and not glitch audio |
| 18 | +time.sleep(3) |
| 19 | + |
| 20 | +# list of (samples to play, mixer gain level) |
| 21 | +wav_files = ( |
| 22 | + ('wav/amen_22k16b_160bpm.wav', 1.0), |
| 23 | + ('wav/dnb21580_22k16b_160bpm.wav', 0.9), |
| 24 | + ('wav/drumloopA_22k16b_160bpm.wav', 1.0), |
| 25 | + ('wav/femvoc_330662_22k16b_160bpm.wav', 0.8), |
| 26 | + ('wav/scratch.wav', 0.5), |
| 27 | + ('wav/pt_limor_modem_vox_01.wav', 0.4), |
| 28 | + ('wav/snowpeaks_22k_s16.wav', 0.8), |
| 29 | + ('wav/dnb21580_22k16b_160bpm_rev.wav', 1.0) |
| 30 | +) |
| 31 | + |
| 32 | +# pins used by keyboard |
| 33 | +KEY_PINS = ( |
| 34 | + board.RX, board.D2, board.D3, board.D4, |
| 35 | + board.D5, board.D6, board.D7, board.D8 |
| 36 | +) |
| 37 | + |
| 38 | +km = keypad.Keys( KEY_PINS, value_when_pressed=False, pull=True) |
| 39 | + |
| 40 | +audio = AudioOut( board.D10 ) # RP2040 PWM, use RC filter on breadboard |
| 41 | +mixer = audiomixer.Mixer(voice_count=len(wav_files), sample_rate=22050, channel_count=1, |
| 42 | + bits_per_sample=16, samples_signed=True) |
| 43 | +audio.play(mixer) # attach mixer to audio playback |
| 44 | + |
| 45 | +for i in range(len(wav_files)): # start all samples at once for use w handle_mixer |
| 46 | + wave = audiocore.WaveFile(open(wav_files[i][0],"rb")) |
| 47 | + mixer.voice[i].play(wave, loop=True) |
| 48 | + mixer.voice[i].level = 0 |
| 49 | + |
| 50 | +def handle_mixer(num, pressed): |
| 51 | + voice = mixer.voice[num] # get mixer voice |
| 52 | + if pressed: |
| 53 | + voice.level = wav_files[num][1] # play at level in wav_file list |
| 54 | + else: # released |
| 55 | + voice.level = 0 # mute it |
| 56 | + |
| 57 | +keys_pressed = [] # list of keys currently being pressed down |
| 58 | + |
| 59 | + |
| 60 | +while True: |
| 61 | + event = km.events.get() |
| 62 | + if event: |
| 63 | + if event.key_number < len(wav_files): |
| 64 | + if event.pressed: |
| 65 | + handle_mixer(event.key_number, True) |
| 66 | + keys_pressed.append( event.key_number ) |
| 67 | + |
| 68 | + if event.released: |
| 69 | + handle_mixer( event.key_number, False ) |
| 70 | + keys_pressed.remove( event.key_number ) |
0 commit comments