Skip to content

Instantly share code, notes, and snippets.

@gallaugher
Created October 8, 2025 18:40
Show Gist options
  • Save gallaugher/d87edf18c50af2e98093c45124748795 to your computer and use it in GitHub Desktop.
Save gallaugher/d87edf18c50af2e98093c45124748795 to your computer and use it in GitHub Desktop.
cap-touch-dj.py
# Uses adafruit_mpr121, an Adalogger Cowbell, PCM5102 DAC, & PAM8302 AMP
import board, busio, sdcardio, storage, os, time, digitalio, adafruit_mpr121
import audiomixer, audiocore, audiobusio # use this instead of audiopwmio
from audiocore import WaveFile
touch_pad = adafruit_mpr121.MPR121(board.STEMMA_I2C())
# Create an I2S audio output w/our pins
audio = audiobusio.I2SOut(
bit_clock=board.GP10,
word_select=board.GP9,
data=board.GP11
)
# SPI SD_CS pin for SD card on Adalogger Cowbell
SD_CS = board.GP17
# SPI0 setup for SD card
spi0 = busio.SPI(board.GP18, board.GP19, board.GP16)
sdcard = sdcardio.SDCard(spi0, SD_CS)
vfs = storage.VfsFat(sdcard)
try:
storage.mount(vfs, "/sd")
print("😎 sd card mounted")
except ValueError:
print("❌ no SD card")
# Setup path & filenames
path = "/sd/beats_22k/"
beats = ["amen6.wav",
"amen7.wav",
"drum_cowbell.wav",
"drum_loop.wav",
"duck-scratch.wav",
"freakie-freak.wav",
"hi_guitar.wav",
"low_guitar.wav",
"ohohoh2.wav",
"snappy_beats.wav",
"watch-this.wav",
"yo.wav"]
# Should be 12 voices
num_voices = len(beats)
# Create Mixer and attach to audio playback
# IMPORTANT: most wave sounds are sampled at sample_rate=22050.
# If you change .wav quality (e.g. kHz setting in Audacity), make sure you adjust the sample rate for any new sample rate.
mixer = audiomixer.Mixer(voice_count=num_voices, sample_rate=22050, channel_count=1, bits_per_sample=16, samples_signed=True)
audio.play(mixer)
# read in all beats & simultaneously play them at audio sound .level = 0 (no volume)
for i in range(len(beats)):
print(f"Reading: {beats[i]}")
wave = WaveFile(open(path+beats[i],"rb"))
mixer.voice[i].play(wave, loop=True )
mixer.voice[i].level = 0.0
time.sleep(0.05) # short pause usually needed
print("Now playing DJ board")
while True:
for i in range(len(beats)):
if touch_pad[i].value:
mixer.voice[i].level = 1.0 # When touched, increase to full volume
else:
mixer.voice[i].level = 0.0 # No touch, then set volume back to zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment