Hi, I’m trying to write a Python MIDI file player using the Pirate Audio board.
So far, I’ve done the following
Installed 32 bit os lite, bookworm or bullseye
ssh to raspberrypi
sudo apt update
sudo apt upgrade
sudo apt install fluidsynth
sudo raspi-config - turn on spi and i2c
sudo nano /boot/firmware/config.txt, add
dtoverlay=hifiberry-dac
gpio=25=op,dh
sudo apt-get install python3-rpi.gpio python3-spidev python3-pip python3-pil python3-numpy
sudo pip3 install pidi-display-st7789
sudo pip3 install pyfluidsynth
sudo apt install git
git clone https // github . com/pimoroni/st7789-python
cd st7789-python/examples/
sudo apt install libopenblas-dev
python3 scrolling-text.py
→ this works!
sudo nano /etc/security/limits.conf
@audio - rtprio 90
@audio - memlock unlimited
sudo reboot
cd /usr/share/sounds/sf2
sudo wget http // ntonyx.com/soft/32MbGMStereo.sf2
sudo wget https // musical-artifacts.com/artifacts/923/General_MIDI_64_1.6.sf2
(Download any MIDI file)
fluidsynth -a alsa -n -i /usr/share/sounds/sf2/FluidR3_GM.sf2 midifile.mid
→ this works, MIDI file is played back
I then use this code
#!/usr/bin/env python3
import sys
import time
import os
import fluidsynth
from gpiozero import Button, DigitalOutputDevice
from PIL import Image, ImageDraw, ImageFont
import st7789
MESSAGE = “”
directory = ‘/home/pi’
file_extension = ‘.mid’
button1=Button(5)
button2=Button(6)
button3=Button(16)
button4=Button(24)
pathes=
files=
selectedindex=0
def handle_button(bt):
global selectedindex
global files
global pathes
if str(bt.pin)==“GPIO16”:
selectedindex-=1
if str(bt.pin)==“GPIO24”:
selectedindex+=1
if selectedindex<0:
selectedindex=0
if selectedindex>len(files)-1:
selectedindex=len(files)-1
if str(bt.pin)==“GPIO5”:
fs = fluidsynth.Synth()
fs.start(driver=“alsa”)
sfid=fs.sfload(“/usr/share/sounds/sf2/General_MIDI_64_1.6.sf2”)
fs.play_midi_file(pathes[selectedindex])
input(“Press Enter to stop playback…”)
fs.play_midi_stop()
fs.delete()
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
if filename.endswith(file_extension):
pathes.append(dirpath+“/”+filename)
files.append(filename.replace(“.mid”,“”).replace(“_”," "))
button1.when_pressed = handle_button
button2.when_pressed = handle_button
button3.when_pressed = handle_button
button4.when_pressed = handle_button
try:
display_type = sys.argv[2]
except IndexError:
display_type = “square”
Create ST7789 LCD display class.
after this, sound is MUTED
if display_type in (“square”, “rect”, “round”):
disp = st7789.ST7789(
height=135 if display_type == “rect” else 240,
rotation=0 if display_type == “rect” else 90,
port=0,
cs=st7789.BG_SPI_CS_FRONT, # BG_SPI_CS_BACK or BG_SPI_CS_FRONT
dc=9,
backlight=19, # 18 for back BG slot, 19 for front BG slot.
spi_speed_hz=80 * 1000 * 1000,
offset_left=0 if display_type == “square” else 40,
offset_top=53 if display_type == “rect” else 0,
)
elif display_type == “dhmini”:
disp = st7789.ST7789(
height=240,
width=320,
rotation=180,
port=0,
cs=1,
dc=9,
backlight=13,
spi_speed_hz=60 * 1000 * 1000,
offset_left=0,
offset_top=0,
)
else:
print(“Invalid display type!”)
Initialize display.
disp.begin()
WIDTH = disp.width
HEIGHT = disp.height
img = Image.new(“RGB”, (WIDTH, HEIGHT), color=(0, 0, 0))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(“/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf”, 20)
size_x, size_y = draw.textsize(MESSAGE, font)
while True:
time.sleep(0.1)
draw.rectangle((0, 0, disp.width, disp.height), (0, 0, 0))
for i, line in enumerate(files): # Highlight the specific line by inverting its colors
if i == selectedindex:
draw.rectangle([10, 10 + (i * 30), 230, 40 + (i * 30)], fill=(255, 255, 255))
draw.text((10, 10 + (i * 30)), line, font=font, fill=(0, 0, 0))
else:
draw.text((10, 10 + (i * 30)), line, font=font, fill=(255, 255, 255))
disp.display(img)
After running this, sound is muted system-wide. Even after quitting the python script, anything audio related won’t play any sounds, for example aplay xyz.wav
Any idea?