Created
July 30, 2024 23:54
-
-
Save gallaugher/dc77f2610664b6a3692576fa102eac5b to your computer and use it in GitHub Desktop.
audiomixer-drum-macahine.py
This file contains hidden or 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
# audiomixer-drum-machine.py | |
import board, time, digitalio, touchio | |
import audiomixer, audiocore | |
try: | |
from audioio import AudioOut | |
except ImportError: | |
try: | |
from audiopwmio import PWMAudioOut as AudioOut | |
except ImportError: | |
print("This board does not support audio") | |
from adafruit_led_animation.color import RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, \ | |
BLUE, PURPLE, MAGENTA, GOLD, PINK, AQUA, JADE, AMBER, OLD_LACE, WHITE, BLACK | |
# Speaker setup for the CircuitPlayground boards (not for Pico) | |
speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE) | |
speaker.direction = digitalio.Direction.OUTPUT | |
speaker.value = True | |
# use speaker location to create an AudioOut object named audio | |
audio = AudioOut(board.SPEAKER) # use board.GP# for pin number for audio out on a Pico | |
# set path where audio files can be found on device | |
path = "drumSounds/" | |
drumsamples = ["bass_hit_c.wav", "bd_tek.wav", "bd_zome.wav", "drum_cowbell.wav", \ | |
"elec_cymbal.wav", "elec_hi_snare.wav", "scratch.wav", "splat.wav"] | |
# create Mixer and attach to audio playback | |
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1, bits_per_sample=16, samples_signed=True) | |
audio.play(mixer) | |
# setup touchpads | |
pads = [board.A1, board.A2, board.A3, board.A4, board.A5, board.A6, board.TX] | |
# create an empty list of touchpads | |
touchpads = [] | |
# loop through all pads | |
for pad in pads: | |
touchpads.append(touchio.TouchIn(pad)) | |
def play_voice(filename): | |
wave = audiocore.WaveFile(open(path+filename,"rb")) | |
mixer.voice[0].play(wave) | |
# mixer.voice[0].level = 1.0 | |
while mixer.voice[0].playing: | |
pass | |
while True: | |
while True: | |
for i in range(len(touchpads)): | |
if touchpads[i].value: # if this pad was touched | |
play_voice(drumsamples[i]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment