-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·176 lines (147 loc) · 5.93 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·176 lines (147 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# ============================================================
# MIDI File Player - Raspberry Pi Setup Script
# Run on Pi Zero 2 WH with Pirate Audio Line Out HAT
# Usage: chmod +x setup.sh && sudo ./setup.sh
# ============================================================
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
step() { echo -e "\n${GREEN}[STEP]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
fail() { echo -e "${RED}[FAIL]${NC} $1"; exit 1; }
ok() { echo -e "${GREEN}[OK]${NC} $1"; }
# --- Preflight checks ---
if [ "$(id -u)" -ne 0 ]; then
fail "This script must be run as root (sudo ./setup.sh)"
fi
ACTUAL_USER="${SUDO_USER:-pi}"
ACTUAL_HOME=$(eval echo "~${ACTUAL_USER}")
echo "============================================"
echo " MIDI File Player - Pi Setup"
echo " User: ${ACTUAL_USER}"
echo " Home: ${ACTUAL_HOME}"
echo "============================================"
# --- Phase 2: System Update & Interface Config ---
step "Updating system packages..."
apt update && apt upgrade -y
step "Enabling SPI and I2C..."
raspi-config nonint do_spi 0
raspi-config nonint do_i2c 0
ok "SPI and I2C enabled"
step "Configuring boot config for HiFiBerry DAC..."
CONFIG="/boot/firmware/config.txt"
if ! grep -q "dtoverlay=hifiberry-dac" "$CONFIG"; then
echo "dtoverlay=hifiberry-dac" >> "$CONFIG"
ok "Added hifiberry-dac overlay"
else
ok "hifiberry-dac overlay already present"
fi
if ! grep -q "gpio=25=op,dh" "$CONFIG"; then
echo "gpio=25=op,dh" >> "$CONFIG"
ok "Added GPIO25 power config"
else
ok "GPIO25 config already present"
fi
# Disable onboard audio if it's set to on
if grep -q "dtparam=audio=on" "$CONFIG"; then
sed -i 's/dtparam=audio=on/dtparam=audio=off/' "$CONFIG"
ok "Disabled onboard audio (was on)"
fi
# --- Phase 3: Software Installation ---
step "Installing system packages..."
apt install -y fluidsynth bluez alsa-utils git \
python3-gpiozero python3-spidev python3-pip \
python3-pil python3-numpy libopenblas-dev
step "Disabling FluidSynth auto-start service..."
systemctl stop fluidsynth.service 2>/dev/null || true
systemctl disable fluidsynth.service 2>/dev/null || true
ok "FluidSynth service disabled"
step "Installing Python packages..."
pip3 install st7789 --break-system-packages
pip3 install pidi-display-st7789 --break-system-packages
pip3 install gitpython --break-system-packages
pip3 install pyfluidsynth --break-system-packages
pip3 install python-rtmidi --break-system-packages
pip3 install mido --break-system-packages
ok "Python packages installed"
step "Cloning ST7789 display driver examples..."
if [ ! -d "${ACTUAL_HOME}/st7789-python" ]; then
sudo -u "${ACTUAL_USER}" git clone https://github.com/pimoroni/st7789-python "${ACTUAL_HOME}/st7789-python"
ok "ST7789 driver cloned"
else
ok "ST7789 driver already present"
fi
step "Enabling Bluetooth MIDI (experimental mode)..."
BT_SERVICE="/lib/systemd/system/bluetooth.service"
if grep -q "bluetoothd$" "$BT_SERVICE"; then
sed -i 's|ExecStart=/usr/sbin/bluetoothd|ExecStart=/usr/sbin/bluetoothd --experimental|' "$BT_SERVICE"
systemctl daemon-reload
systemctl restart bluetooth
ok "Bluetooth MIDI enabled"
elif grep -q "bluetoothd --experimental" "$BT_SERVICE"; then
ok "Bluetooth MIDI already enabled"
fi
step "Setting audio priority in limits.conf..."
LIMITS="/etc/security/limits.conf"
if ! grep -q "@audio - rtprio 90" "$LIMITS"; then
echo "@audio - rtprio 90" >> "$LIMITS"
echo "@audio - memlock unlimited" >> "$LIMITS"
ok "Audio priority set"
else
ok "Audio priority already configured"
fi
# --- Phase 4: Sound Fonts & Directory Structure ---
step "Creating directory structure..."
sudo -u "${ACTUAL_USER}" mkdir -p "${ACTUAL_HOME}/midifiles"
ok "Created ~/midifiles/"
if [ ! -L "${ACTUAL_HOME}/sf2" ]; then
sudo -u "${ACTUAL_USER}" ln -s /usr/share/sounds/sf2 "${ACTUAL_HOME}/sf2"
ok "Created ~/sf2 symlink"
else
ok "~/sf2 symlink already exists"
fi
step "Downloading sound fonts..."
SF2_DIR="/usr/share/sounds/sf2"
mkdir -p "$SF2_DIR"
if [ ! -f "${SF2_DIR}/General_MIDI_64_1.6.sf2" ]; then
wget -q --show-progress -O "${SF2_DIR}/General_MIDI_64_1.6.sf2" \
"https://musical-artifacts.com/artifacts/923/General_MIDI_64_1.6.sf2" || \
warn "Failed to download General_MIDI_64_1.6.sf2 - you may need to download manually"
else
ok "General_MIDI_64_1.6.sf2 already present"
fi
if [ ! -f "${SF2_DIR}/32MbGMStereo.sf2" ]; then
wget -q --show-progress -O "${SF2_DIR}/32MbGMStereo.sf2" \
"http://ntonyx.com/soft/32MbGMStereo.sf2" || \
warn "Failed to download 32MbGMStereo.sf2 - you may need to download manually"
else
ok "32MbGMStereo.sf2 already present"
fi
# --- Verification ---
echo ""
echo "============================================"
echo " Setup Complete - Verification"
echo "============================================"
echo -n "SPI devices: "
ls /dev/spidev* 2>/dev/null && ok "SPI OK" || warn "SPI not detected (may need reboot)"
echo -n "Python imports: "
python3 -c "import fluidsynth, rtmidi, mido; print('OK')" 2>/dev/null && ok "Core imports OK" || warn "Some imports failed"
echo -n "Sound fonts: "
ls "${SF2_DIR}"/*.sf2 2>/dev/null && ok "Sound fonts OK" || warn "No sound fonts found"
echo -n "Midifiles dir: "
[ -d "${ACTUAL_HOME}/midifiles" ] && ok "~/midifiles/ exists" || warn "~/midifiles/ missing"
echo -n "SF2 symlink: "
[ -L "${ACTUAL_HOME}/sf2" ] && ok "~/sf2 symlink OK" || warn "~/sf2 symlink missing"
echo ""
echo "============================================"
echo -e "${GREEN} REBOOT REQUIRED${NC}"
echo " Run: sudo reboot"
echo ""
echo " After reboot, test these in order:"
echo " 1. Display: cd ~/st7789-python/examples && python3 scrolling-text.py"
echo " 2. Audio: fluidsynth -a alsa -n -i /usr/share/sounds/sf2/General_MIDI_64_1.6.sf2 ~/midifiles/test.mid"
echo " 3. Script: cd ~/midifileplayer && sudo python3 midiplayer.py"
echo "============================================"