You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MP3Decoder: avoid all blocking reads in background
This gets MP3 playback of a soma.fm stream working for up to a minute
at a time, though it's still vulnerable to network glitches.
* the buffer can empty, in which case a single block of audio
plays repeatedly in max headroom stutter fashion
* the server eventually (after 1 to 5 5 minutes) stops getting packets
at all. At this point stream playback stops, with the internal error
indicating a problem MP3 decoding (which doesn't quite make sense):
-9, ERR_MP3_INVALID_HUFFCODES.
* other combinations of audiomixer buffer & mp3 buffer might give
different results
```py
import time
import adafruit_connection_manager
import adafruit_requests
import audiobusio
import audiomixer
import audiomp3
import board
import wifi
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)
# todo: parse PLS files like https://somafm.com/nossl/dronezone.pls
# todo: figure out why https URLs don't work at all (missing select?)
# STREAMING_URL = "http://ice2.somafm.com/dronezone-128-mp3"
STREAMING_URL = "http://ice4.somafm.com/tikitime-128-mp3"
def get_mp3_stream():
if STREAMING_URL.startswith("http:") or STREAMING_URL.startswith("https:"):
return requests.get(STREAMING_URL, headers={"connection": "close"}).socket
return open(STREAMING_URL, "rb")
mixer_buffer_size = 1152 * 2
mp3_buffer = bytearray(32768)
with audiobusio.I2SOut(
bit_clock=board.D12, word_select=board.D13, data=board.D11
) as i2s, get_mp3_stream() as stream, audiomp3.MP3Decoder(
stream, mp3_buffer
) as sample, audiomixer.Mixer(
channel_count=2, sample_rate=44100, buffer_size=mixer_buffer_size
) as m:
v = m.voice[0]
print(sample)
i2s.play(m)
v.play(sample, loop=False)
while v.playing:
time.sleep(0.1)
```
0 commit comments