Skip to content

Instantly share code, notes, and snippets.

@gallaugher
Created February 23, 2023 15:31
Show Gist options
  • Save gallaugher/4622c9bb3feb885c0c78ec12c4157da2 to your computer and use it in GitHub Desktop.
Save gallaugher/4622c9bb3feb885c0c78ec12c4157da2 to your computer and use it in GitHub Desktop.
Trying to play some mp3 files using audiomixer in CircuitPython.
# audiomixer_demo.py -- show how to fade up and down playing loops
import time, board, audiocore, audiomixer, adafruit_mpr121, digitalio
from audiomp3 import MP3Decoder
# import PWMAudioOut & name it AudioOut
from audiopwmio import PWMAudioOut as AudioOut
# set up I2C
i2c = board.STEMMA_I2C()
# set up touchpads
touch_pad = adafruit_mpr121.MPR121(i2c)
audio = AudioOut(board.GP14)
beats = ["amenfull_22k_s16.mp3",
"amen6_22k_s16.mp3",
"amen7_22k_s16.mp3",
"ohohoh2.mp3",
"bass_hit_c.mp3",
"bd_tek.mp3",
"bd_zome.mp3",
"drum_cowbell.mp3"]
# "duck-scratch.mp3",
# "yo.mp3",
# "watch-this.mp3",
# "freakie-freak.mp3"]
path = "mp3-beats/"
num_voices = len(beats)
mp3 = open(path + beats[0], "rb")
decoder = MP3Decoder(mp3)
mixer = audiomixer.Mixer(voice_count=num_voices, sample_rate=22050, channel_count=1,
bits_per_sample=16, samples_signed=True)
# attach mixer to audio playback
audio.play(mixer)
for i in range(len(beats)):
decoder.file = open(path + beats[i], "rb")
audio.play(decoder)
print(f"file = {beats[i]}")
mixer.voice[i].play(decoder, loop=True )
mixer.voice[i].level = 0.0
time.sleep(1.0) # let last play a bit, just to make sure it plays
while True:
for i in range(len(beats)):
if touch_pad[i].value:
print(f"You touched pad # {i}!")
mixer.voice[i].level = 1.0
else:
mixer.voice[i].level = 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment