Skip to content

Instantly share code, notes, and snippets.

@jepler
Created May 8, 2024 23:34
Show Gist options
  • Save jepler/330afe6002f8cc9484080f8e79a8d9f5 to your computer and use it in GitHub Desktop.
Save jepler/330afe6002f8cc9484080f8e79a8d9f5 to your computer and use it in GitHub Desktop.
from math import sin
import sdcardio
import storage
import audiobusio
import audiocore
import audiomixer
import audiomp3
import board
import time
import math
# Mount SD card
bus = board.SPI()
sd_cs = board.SD_CS
sdcard = sdcardio.SDCard(bus, sd_cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
# Open a couple of MP3s
sample1 = audiomp3.MP3Decoder("/sd/bartlebeats - Frequency - 01 Idea3.mp3")
sample2 = audiomp3.MP3Decoder("/sd/bartlebeats - Frequency - 14 Tall Bird.mp3")
# Engage the sound hardware
with audiobusio.I2SOut(bit_clock=board.D12, word_select=board.D13, data=board.D11) as i2s:
# Start up a mixer
m = audiomixer.Mixer(channel_count=2, sample_rate=44100, buffer_size=8192)
i2s.play(m)
# And associate an MP3 file with each voice
v0, v1 = m.voice
v0.level = 1
v1.level = 0
v0.play(sample1, loop=True)
v1.play(sample2, loop=True)
# Now, show off the mixer by switching back and forth between the two songs
# .. in a sinusoidal fashion
t = 0
ov = .2
while True:
t = t + .01
l = sin(t) * .5 + .5
v0.level = l * ov
v1.level = (1-l) * ov
time.sleep(.01)
# At some point (end of track?) I get static & USB disconnect so there's a bug somewhere :-/
# maybe looping MP3s is broken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment