Created
March 3, 2023 00:26
-
-
Save gallaugher/1b454a82d7ccd3388caf127214419eba to your computer and use it in GitHub Desktop.
DJ Board with SD Card
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Idea based on original code by @todbot | |
# Modified (probably poorly) by Prof. John Gallaugher to include | |
# This version tested with CircuitPython v.8 | |
import board, adafruit_mpr121, time, audiomixer, audiocore | |
import mount_sd | |
from audiocore import WaveFile | |
from audiopwmio import PWMAudioOut as AudioOut | |
# Setup i2c on the Pico w/STEMMA_QT wired as shown in the diagram - GP4 (SDA/Blue), GP5 (SCL/Yellow, Power 3.3v(Out) Red | |
i2c = board.STEMMA_I2C() # Same for any board w/built-in STEMMA_QT port | |
touch_pad = adafruit_mpr121.MPR121(i2c) | |
# Coment out below if using the CircuitPlayground Bluefruit | |
# and be sure the board's pin is set to pin attached to tip of your audio plug. | |
audio = AudioOut(board.GP15) # I'm using GP15 for audio in. CHANGE if you use a different pin. | |
# Will be changed for microSD card lesson where we use larger files in a differently named folder/location | |
path = "/sd/slim_beats/" # This MUST be the name of the folder on CIRCUITPY, otherwise change as needed. | |
# MUST have 12 beats if using MPR121 touchpad breakout. Also make sure these 12 names match | |
# the files in your folder, named in the path, above. Otherwise modify the names. | |
beats = ["amen6.wav", | |
"amenfull.wav", | |
"ohohoh2.wav", | |
"drum_loop.wav", | |
"snappy_beats.wav", | |
"hi_guitar.wav", | |
"low_guitar.wav", | |
"drum_cowbell.wav", | |
"duck-scratch.wav", | |
"freakie-freak.wav", | |
"watch-this.wav", | |
"yo.wav"] | |
# audiomixer code: | |
num_voices = len(beats) | |
mixer = audiomixer.Mixer(voice_count=num_voices, sample_rate=8000, channel_count=1, bits_per_sample=16, samples_signed=True) | |
audio.play(mixer) | |
print("About to load in beats") | |
for i in range(len(beats)): | |
print(f"loading: {path+beats[i]}") | |
wave = audiocore.WaveFile(open(path+beats[i],"rb")) | |
mixer.voice[i].play(wave, loop=True ) | |
mixer.voice[i].level = 0.0 | |
time.sleep(1.0) # let drums play a bit | |
print("beats loaded") | |
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