Last active
November 15, 2024 03:55
-
-
Save todbot/ac8934d2c75ddcf48805aeaf2ca0ddd7 to your computer and use it in GitHub Desktop.
Play high-quality MP3s on RP2350 Tiny 2350 using the MacroPadSynthPlug as audio interface
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
# mp3-cycler.py -- Play high-quality MP3s on RP2350 Tiny 2350 | |
# using the MacroPadSynthPlug as audio interface | |
# 15 Aug 2024 - @todbot / Tod Kurt | |
import time, os, random | |
import board | |
import audiocore, audiopwmio, audiomixer, audiomp3 | |
import busio | |
import keypad | |
import rainbowio | |
import adafruit_rgbled | |
# Pimoroni Tiny2350 RGB LED has RGB LED on GP18,19,20, active low | |
led = adafruit_rgbled.RGBLED(board.GP18, board.GP19, board.GP20, invert_pwm=True) | |
# Pimoroni Tiny2350 BOOT button is on GP23 with external pullup | |
keys = keypad.Keys( (board.GP23,), value_when_pressed=False, pull=False) | |
# MacroPadSynthPlug : MIDI input : Tiny 2350 SCL = GP13 | |
uart = busio.UART(rx=board.GP13, baudrate=31250) | |
# MacroPadSynthPlug : audio output : Tiny 2350 SDA = GP12 | |
audio = audiopwmio.PWMAudioOut(board.GP12) | |
mixer = audiomixer.Mixer(channel_count=2,voice_count=1,sample_rate=44100, buffer_size=2048) | |
audio.play(mixer) | |
# find all the mp3 files in the given directory | |
mp3_dir = "/mp3" | |
mp3_paths = [] | |
for fname in sorted(os.listdir(mp3_dir)): | |
fname = fname.lower() | |
if fname.endswith(".mp3") and not fname.startswith("."): | |
fullpath = mp3_dir + "/" + fname | |
print('-', fullpath) | |
mp3_paths.append(fullpath) | |
mp3i = 0 # index of which mp3 file to play | |
mp3 = audiomp3.MP3Decoder(mp3_paths[mp3i]) | |
print("playing...", mp3i, mp3_paths[mp3i]) | |
mixer.voice[0].play(mp3) | |
while True: | |
if mixer.voice[0].playing: | |
led.color = rainbowio.colorwheel( time.monotonic() * 400 ) | |
if key := keys.events.get(): | |
#print("key:",key) | |
if key.pressed: | |
mixer.voice[0].stop() | |
mp3i = (mp3i + 1) % len(mp3_paths) | |
mp3 = audiomp3.MP3Decoder(mp3_paths[mp3i]) | |
print("now playing:", mp3i, mp3_paths[mp3i]) | |
mixer.voice[0].play(mp3) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment