2024-09-25 09:11:31 +00:00
|
|
|
# audio.py
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
# Released under the MIT License (MIT). See LICENSE.
|
2024-09-25 09:11:31 +00:00
|
|
|
# Copyright (c) 2021-2024 Peter Hinch
|
2021-09-22 08:01:14 +00:00
|
|
|
|
2024-10-12 09:26:30 +00:00
|
|
|
# Uses nonblocking reads rather than StreamWriter because there is no non-hacky way
|
|
|
|
# to do non-allocating writes: see https://github.com/micropython/micropython/pull/7868
|
|
|
|
# Hack was
|
|
|
|
# swriter.out_buf = wav_samples_mv[:num_read]
|
|
|
|
# await swriter.drain()
|
|
|
|
# WAV files
|
|
|
|
# The proper way is to parse the WAV file as per
|
|
|
|
# https://github.com/miketeachman/micropython-i2s-examples/blob/master/examples/wavplayer.py
|
|
|
|
# Here for simplicity we assume stereo files ripped from CD's.
|
|
|
|
|
2021-09-22 08:01:14 +00:00
|
|
|
import hardware_setup # Create a display instance
|
|
|
|
from gui.core.ugui import Screen, ssd
|
|
|
|
from machine import I2S
|
|
|
|
from machine import Pin
|
|
|
|
import pyb
|
|
|
|
|
2024-10-12 09:26:30 +00:00
|
|
|
root = "/sd/music" # Location of directories containing albums
|
2021-10-23 08:56:24 +00:00
|
|
|
|
2021-09-22 08:01:14 +00:00
|
|
|
# Do allocations early
|
2024-09-25 09:11:31 +00:00
|
|
|
BUFSIZE = 1024 * 20 # 5.8ms/KiB 8KiB occasional dropouts
|
|
|
|
WAVSIZE = 1024 * 2
|
|
|
|
_RFSH_GATE = const(10) # While playing, reduce refresh rate
|
2021-09-22 08:01:14 +00:00
|
|
|
# allocate sample array once
|
2021-10-23 08:56:24 +00:00
|
|
|
wav_samples = bytearray(WAVSIZE)
|
2021-09-22 08:01:14 +00:00
|
|
|
|
2024-10-12 09:26:30 +00:00
|
|
|
# ======= I2S CONFIGURATION =======
|
|
|
|
|
2024-10-08 15:41:38 +00:00
|
|
|
# Pyboard D
|
2024-10-12 09:26:30 +00:00
|
|
|
pyb.Pin("EN_3V3").on() # Pyboard D: provide 3.3V on 3V3 output pin
|
2024-10-08 15:41:38 +00:00
|
|
|
I2S_ID = 1
|
2021-09-22 08:01:14 +00:00
|
|
|
config = {
|
2024-09-25 09:11:31 +00:00
|
|
|
"sck": Pin("W29"),
|
|
|
|
"ws": Pin("W16"),
|
|
|
|
"sd": Pin("Y4"),
|
|
|
|
"mode": I2S.TX,
|
|
|
|
"bits": 16, # Sample size in bits/channel
|
|
|
|
"format": I2S.STEREO,
|
|
|
|
"rate": 44100, # Sample rate in Hz
|
2024-10-12 09:26:30 +00:00
|
|
|
"ibuf": BUFSIZE, # Internal buffer size
|
2024-09-25 09:11:31 +00:00
|
|
|
}
|
2021-09-22 08:01:14 +00:00
|
|
|
|
2024-10-08 15:41:38 +00:00
|
|
|
# RP2 from https://docs.micropython.org/en/latest/rp2/quickref.html#i2s-bus
|
|
|
|
# I2S_ID = 0
|
|
|
|
# config = {
|
|
|
|
# "sck": Pin(16),
|
|
|
|
# "ws": Pin(17),
|
|
|
|
# "sd": Pin(18),
|
|
|
|
# "mode": I2S.TX,
|
|
|
|
# "bits": 16, # Sample size in bits/channel
|
|
|
|
# "format": I2S.STEREO,
|
|
|
|
# "rate": 44100, # Sample rate in Hz
|
|
|
|
# "ibuf": BUFSIZE, # Buffer size
|
|
|
|
# }
|
|
|
|
|
2021-09-22 08:01:14 +00:00
|
|
|
audio_out = I2S(I2S_ID, **config)
|
|
|
|
|
|
|
|
# ======= GUI =======
|
|
|
|
|
2023-04-16 17:24:33 +00:00
|
|
|
from gui.widgets import Button, CloseButton, HorizSlider, Listbox, Label
|
2021-09-22 08:01:14 +00:00
|
|
|
from gui.core.writer import CWriter
|
|
|
|
|
|
|
|
# Font for CWriter
|
|
|
|
import gui.fonts.arial10 as arial10
|
|
|
|
import gui.fonts.icons as icons
|
|
|
|
from gui.core.colors import *
|
|
|
|
|
|
|
|
import os
|
|
|
|
import gc
|
2024-10-12 09:26:30 +00:00
|
|
|
import asyncio
|
2021-09-29 13:20:15 +00:00
|
|
|
import sys
|
|
|
|
|
2022-02-06 12:05:38 +00:00
|
|
|
# Initial check on filesystem
|
2021-09-29 13:20:15 +00:00
|
|
|
try:
|
|
|
|
subdirs = [x[0] for x in os.ilistdir(root) if x[1] == 0x4000]
|
|
|
|
if len(subdirs):
|
|
|
|
subdirs.sort()
|
|
|
|
else:
|
|
|
|
print("No albums found in ", root)
|
|
|
|
sys.exit(1)
|
|
|
|
except OSError:
|
|
|
|
print(f"Expected {root} directory not found.")
|
|
|
|
sys.exit(1)
|
2021-09-22 08:01:14 +00:00
|
|
|
|
2024-09-25 09:11:31 +00:00
|
|
|
|
2021-09-22 08:01:14 +00:00
|
|
|
class SelectScreen(Screen):
|
|
|
|
songs = []
|
|
|
|
album = ""
|
2024-09-25 09:11:31 +00:00
|
|
|
|
2021-09-22 08:01:14 +00:00
|
|
|
def __init__(self, wri):
|
|
|
|
super().__init__()
|
2024-09-25 09:11:31 +00:00
|
|
|
Listbox(wri, 2, 2, elements=subdirs, dlines=8, width=100, callback=self.lbcb)
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def lbcb(self, lb): # sort
|
2024-09-25 09:11:31 +00:00
|
|
|
directory = "".join((root, "/", lb.textvalue()))
|
2021-09-22 08:01:14 +00:00
|
|
|
songs = [x[0] for x in os.ilistdir(directory) if x[1] != 0x4000]
|
|
|
|
songs.sort()
|
2024-09-25 09:11:31 +00:00
|
|
|
SelectScreen.songs = ["".join((directory, "/", x)) for x in songs]
|
2021-09-22 08:01:14 +00:00
|
|
|
SelectScreen.album = lb.textvalue()
|
|
|
|
Screen.back()
|
|
|
|
|
|
|
|
|
2024-09-25 09:11:31 +00:00
|
|
|
class BaseScreen(Screen):
|
2021-09-22 08:01:14 +00:00
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
args = {
|
2024-09-25 09:11:31 +00:00
|
|
|
"bdcolor": RED,
|
|
|
|
"slotcolor": BLUE,
|
|
|
|
"legends": ("-48dB", "-24dB", "0dB"),
|
|
|
|
"value": 0.5,
|
|
|
|
"height": 15,
|
|
|
|
}
|
2021-09-22 08:01:14 +00:00
|
|
|
buttons = {
|
2024-09-25 09:11:31 +00:00
|
|
|
"shape": CIRCLE,
|
|
|
|
"fgcolor": GREEN,
|
|
|
|
}
|
2021-09-22 08:01:14 +00:00
|
|
|
super().__init__()
|
2024-10-12 09:26:30 +00:00
|
|
|
self.mt = asyncio.ThreadSafeFlag()
|
|
|
|
audio_out.irq(self.audiocb)
|
2021-09-22 08:01:14 +00:00
|
|
|
# Audio status
|
|
|
|
self.playing = False # Track is playing
|
|
|
|
self.stop_play = False # Command
|
|
|
|
self.paused = False
|
|
|
|
self.songs = [] # Paths to songs in album
|
|
|
|
self.song_idx = 0 # Current index into .songs
|
|
|
|
self.offset = 0 # Offset into file
|
|
|
|
self.volume = -3
|
|
|
|
|
|
|
|
wri = CWriter(ssd, arial10, GREEN, BLACK, False)
|
|
|
|
wri_icons = CWriter(ssd, icons, WHITE, BLACK, False)
|
2024-09-25 09:11:31 +00:00
|
|
|
Button(wri_icons, 2, 2, text="E", callback=self.new, args=(wri,), **buttons) # New
|
|
|
|
Button(wri_icons, row := 30, col := 2, text="D", callback=self.replay, **buttons) # Replay
|
|
|
|
Button(wri_icons, row, col := col + 25, text="F", callback=self.play_cb, **buttons) # Play
|
|
|
|
Button(wri_icons, row, col := col + 25, text="B", callback=self.pause, **buttons) # Pause
|
|
|
|
Button(wri_icons, row, col := col + 25, text="A", callback=self.stop, **buttons) # Stop
|
|
|
|
Button(wri_icons, row, col + 25, text="C", callback=self.skip, **buttons) # Skip
|
2021-09-22 08:01:14 +00:00
|
|
|
row = 60
|
|
|
|
col = 2
|
2021-09-29 08:08:10 +00:00
|
|
|
self.lbl = Label(wri, row, col, 120)
|
|
|
|
self.lblsong = Label(wri, self.lbl.mrow + 2, col, 120)
|
2021-09-22 08:01:14 +00:00
|
|
|
row = 110
|
|
|
|
col = 14
|
|
|
|
HorizSlider(wri, row, col, callback=self.slider_cb, **args)
|
2021-09-29 13:20:15 +00:00
|
|
|
CloseButton(wri) # Quit the application
|
2021-09-22 08:01:14 +00:00
|
|
|
|
2024-10-12 09:26:30 +00:00
|
|
|
def audiocb(self, i2s): # Audio buffer empty
|
|
|
|
self.mt.set()
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def slider_cb(self, s):
|
|
|
|
self.volume = round(8 * (s.value() - 1))
|
|
|
|
|
|
|
|
def play_cb(self, _):
|
|
|
|
self.play_album()
|
|
|
|
|
|
|
|
def pause(self, _):
|
|
|
|
self.stop_play = True
|
|
|
|
self.paused = True
|
2021-09-29 13:20:15 +00:00
|
|
|
self.show_song()
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def stop(self, _): # Abandon album
|
|
|
|
self.stop_play = True
|
|
|
|
self.paused = False
|
|
|
|
self.song_idx = 0
|
2021-09-29 13:20:15 +00:00
|
|
|
self.show_song()
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def replay(self, _):
|
2021-09-29 13:20:15 +00:00
|
|
|
if self.stop_play:
|
|
|
|
self.song_idx = max(0, self.song_idx - 1)
|
|
|
|
else:
|
|
|
|
self.stop_play = True # Replay from start
|
2021-09-22 08:01:14 +00:00
|
|
|
self.paused = False
|
2021-09-29 13:20:15 +00:00
|
|
|
self.show_song()
|
2024-09-25 09:11:31 +00:00
|
|
|
# self.play_album()
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def skip(self, _):
|
|
|
|
self.stop_play = True
|
|
|
|
self.paused = False
|
2024-09-25 09:11:31 +00:00
|
|
|
self.song_idx = min(self.song_idx + 1, len(self.songs) - 1)
|
2021-09-29 13:20:15 +00:00
|
|
|
self.show_song()
|
2024-09-25 09:11:31 +00:00
|
|
|
# self.play_album()
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def new(self, _, wri):
|
2021-09-29 13:20:15 +00:00
|
|
|
self.stop_play = True
|
|
|
|
self.paused = False
|
2024-09-25 09:11:31 +00:00
|
|
|
Screen.change(
|
|
|
|
SelectScreen,
|
|
|
|
args=[
|
|
|
|
wri,
|
|
|
|
],
|
|
|
|
)
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def play_album(self):
|
2021-09-29 16:39:22 +00:00
|
|
|
if not self.playing:
|
|
|
|
self.reg_task(asyncio.create_task(self.album_task()))
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def after_open(self):
|
|
|
|
self.songs = SelectScreen.songs
|
|
|
|
self.lbl.value(SelectScreen.album)
|
|
|
|
if self.songs:
|
|
|
|
self.song_idx = 0 # Start on track 0
|
2021-09-29 13:20:15 +00:00
|
|
|
self.show_song()
|
2024-09-25 09:11:31 +00:00
|
|
|
# self.play_album()
|
2021-09-22 08:01:14 +00:00
|
|
|
|
2021-10-23 08:56:24 +00:00
|
|
|
def show_song(self): # 13ms
|
2021-09-29 13:20:15 +00:00
|
|
|
song = self.songs[self.song_idx]
|
|
|
|
ns = song.find(SelectScreen.album)
|
2024-09-25 09:11:31 +00:00
|
|
|
ne = song[ns:].find("/") + 1
|
|
|
|
end = song[ns + ne :].find(".wav")
|
|
|
|
self.lblsong.value(song[ns + ne : ns + ne + end])
|
2021-10-23 08:56:24 +00:00
|
|
|
|
2021-09-22 08:01:14 +00:00
|
|
|
async def album_task(self):
|
|
|
|
self.playing = True # Prevent other instances
|
|
|
|
self.stop_play = False
|
|
|
|
# Leave paused status unchanged
|
|
|
|
songs = self.songs[self.song_idx :] # Start from current index
|
|
|
|
for song in songs:
|
2021-09-29 13:20:15 +00:00
|
|
|
self.show_song()
|
2021-09-22 08:01:14 +00:00
|
|
|
await self.play_song(song)
|
|
|
|
if self.stop_play:
|
|
|
|
break # A callback has stopped playback
|
|
|
|
self.song_idx += 1
|
2021-09-29 16:39:22 +00:00
|
|
|
else:
|
|
|
|
self.song_idx = 0 # Played to completion.
|
|
|
|
self.show_song()
|
2021-09-22 08:01:14 +00:00
|
|
|
self.playing = False
|
|
|
|
|
|
|
|
# Open and play a binary wav file
|
|
|
|
async def play_song(self, song):
|
|
|
|
wav_samples_mv = memoryview(wav_samples)
|
|
|
|
size = len(wav_samples)
|
|
|
|
if not self.paused:
|
|
|
|
# advance to first byte of Data section in WAV file. This is not
|
|
|
|
# correct for all WAV files. See link above.
|
|
|
|
self.offset = 44
|
|
|
|
with open(song, "rb") as wav:
|
|
|
|
_ = wav.seek(self.offset)
|
2024-09-25 09:11:31 +00:00
|
|
|
while not self.stop_play:
|
2024-10-12 09:26:30 +00:00
|
|
|
async with Screen.rfsh_lock: # Lock out refresh
|
|
|
|
for n in range(_RFSH_GATE): # for _RFSH_GATE buffers full
|
2024-09-25 09:11:31 +00:00
|
|
|
if not (num_read := wav.readinto(wav_samples_mv)): # Song end
|
2024-10-12 09:26:30 +00:00
|
|
|
return
|
2024-09-25 09:11:31 +00:00
|
|
|
I2S.shift(buf=wav_samples_mv[:num_read], bits=16, shift=self.volume)
|
2024-10-12 09:26:30 +00:00
|
|
|
audio_out.write(wav_samples_mv[:num_read])
|
|
|
|
await self.mt.wait()
|
2024-09-25 09:11:31 +00:00
|
|
|
# wav_samples is now empty. Save offset in case we pause play.
|
|
|
|
self.offset += size
|
|
|
|
await asyncio.sleep_ms(0) # Allow refresh to grab lock
|
|
|
|
|
2021-09-22 08:01:14 +00:00
|
|
|
|
|
|
|
def test():
|
2024-09-25 09:11:31 +00:00
|
|
|
print("Audio demo.")
|
2021-09-29 13:20:15 +00:00
|
|
|
try:
|
|
|
|
Screen.change(BaseScreen) # A class is passed here, not an instance.
|
|
|
|
finally:
|
|
|
|
audio_out.deinit()
|
|
|
|
print("========== CLOSE AUDIO ==========")
|
2021-09-22 08:01:14 +00:00
|
|
|
|
2024-09-25 09:11:31 +00:00
|
|
|
|
2021-09-22 08:01:14 +00:00
|
|
|
test()
|