Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gallaugher/fcaa90600a13032b1d07f9c91eb51735 to your computer and use it in GitHub Desktop.
Save gallaugher/fcaa90600a13032b1d07f9c91eb51735 to your computer and use it in GitHub Desktop.
just_mp3s_played_over_adalogger_cowbell.py
import board, busio, sdcardio, storage, os, time, digitalio
from audiomp3 import MP3Decoder
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
print("This board does not support audio")
# use speaker location to creat an AudioOut object named audio
audio = AudioOut(board.GP15)
# SPI SD_CS pin
SD_CS = board.GP17
# SPI setup for SD card
spi = busio.SPI(board.GP18, board.GP19, board.GP16)
sdcard = sdcardio.SDCard(spi, 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/robot_sounds/"
# setup the MP#Decoder object first & only once
filename = "0.mp3"
mp3_file = open(path + filename, "rb") # read in data from file
decoder = MP3Decoder(mp3_file)
def play_mp3(filename):
decoder.file = open(path + filename, "rb")
audio.play(decoder)
while audio.playing:
pass # replace with function name if you want concurrent operation
print("🐮 Cowbell Adalogger SD Test")
play_mp3("0.mp3")
play_mp3("1.mp3")
play_mp3("2.mp3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment