-
Notifications
You must be signed in to change notification settings - Fork 0
/
squishbox
399 lines (361 loc) · 13.3 KB
/
squishbox
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/bash
installdir=""
logfile="install-log.txt"
UPDATED=false
UPGRADE=false
PYTHON_PKG=""
ASK_TO_REBOOT=false
promptorno() {
read -r -p "$1 (y/[n]) " response < /dev/tty
if [[ $response =~ ^(yes|y|Y)$ ]]; then
true
else
false
fi
}
promptoryes() {
read -r -p "$1 ([y]/n) " response < /dev/tty
if [[ $response =~ ^(no|n|N)$ ]]; then
false
else
true
fi
}
query() {
read -r -p "$1 [$2] " response < /dev/tty
if [[ $response == "" ]]; then
response=$2
fi
}
success() {
echo -e "$(tput setaf 2)$1$(tput sgr0)"
}
inform() {
echo -e "$(tput setaf 6)$1$(tput sgr0)"
}
warning() {
echo -e "$(tput setaf 1)$1$(tput sgr0)"
}
sysupdate() {
if ! $UPDATED; then
echo "Updating apt indexes..."
sudo apt-get -qq update || { warning "Failed to update apt indexes!" && exit 1; }
sleep 3
UPDATED=true
if $UPGRADE; then
echo "Upgrading your system..."
sudo apt-get -qqy upgrade --with-new-pkgs &>> $logfile
sudo apt-get clean && sudo apt-get autoclean
sudo apt-get -qqy autoremove
UPGRADE=false
fi
fi
}
apt_pkg_install() {
APT_CHK=$(dpkg-query -W -f='${Status}\n' "$1" 2> /dev/null | grep "install ok installed")
if [[ $APT_CHK == "" ]]; then
echo "Apt is installing $1..."
sudo apt-get --no-install-recommends --yes install "$1" &>> $logfile || { warning "Apt failed to install $1!" && exit 1; }
fi
}
pip_install() {
if [[ $PYTHON_PKG == "" ]]; then
PYTHON_PKG=$(pip3 list 2> /dev/null)
fi
if ! [[ $PYTHON_PKG =~ "$1" ]]; then
echo "Pip is installing $1..."
sudo -H pip3 install "$1" &>> $logfile || { warning "Pip failed to install $1!" && exit 1; }
fi
}
sleep 2
rm -f $logfile
## get options from the user
inform "\nThis script installs/updates software and optonal extras"
inform "for the SquishBox or headless Raspberry Pi synth."
warning "Always be careful when running scripts and commands copied"
warning "from the internet. Ensure they are from a trusted source."
echo "If you want to see what this script does before running it,"
echo "hit ctrl-C and enter 'curl https://geekfunklabs.com/squishbox | more'"
echo "Logging errors and technical output to $logfile"
echo -e "Report issues with this script at https://github.com/albedozero/fluidpatcher\n"
inform "Core software update/install and system settings:"
query "Install location" `echo ~`; installdir=$response
if test -f "$installdir/patcher/__init__.py"; then
FP_VER=`sed -n '/^VERSION/s|[^0-9\.]*||gp' $installdir/patcher/__init__.py`
echo "Installed FluidPatcher is version $FP_VER"
fi
NEW_FP_VER=`curl -s https://raw.githubusercontent.com/albedozero/fluidpatcher/master/patcher/__init__.py | sed -n '/^VERSION/s|[^0-9\.]*||gp'`
if promptoryes "Install/update FluidPatcher version $NEW_FP_VER?"; then
update="yes"
if promptorno "Overwrite existing banks/settings?"; then
overwrite="yes"
fi
fi
echo "Which audio output would you like to use?"
echo " 1. Add-on DAC (SquishBox)"
echo " 2. Headphone jack (headless pi)"
echo " 3. USB sound card"
echo " 0. No change"
query "Choose" "0"; audiosetup=$response
echo "What script should be run on startup?"
echo " 1. squishbox.py"
echo " 2. headlesspi.py"
echo " 3. Nothing"
echo " 0. No change"
query "Choose" "0"; startup=$response
if [[ $startup == 2 ]]; then
echo -e "Set up controls for headless pi:"
query " MIDI channel for controls" "use default"; ctrls_channel=$response
query " Next patch button CC" "use default"; incpatch=$response
query " Previous patch button CC" "use default"; decpatch=$response
query " Patch select knob CC" "use default"; selectpatch=$response
query " Bank change button CC" "use default"; bankinc=$response
fi
if promptorno "OK to upgrade your system (if necessary)?"; then
UPGRADE=true
fi
inform "\nOptional tasks/add-ons:"
if command -v fluidsynth > /dev/null; then
INST_VER=`fluidsynth --version | sed -n '/runtime version/s|[^0-9\.]*||gp'`
echo "Installed FluidSynth is version $INST_VER"
else
PKG_VER=`apt-cache policy fluidsynth | sed -n '/Candidate:/s/ Candidate: //p'`
echo "FluidSynth version $PKG_VER will be installed"
fi
BUILD_VER=`curl -s https://github.com/FluidSynth/fluidsynth/releases/latest | sed -e 's|.*tag/v||' -e 's|">redirected.*||'`
if promptorno "Compile and install FluidSynth $BUILD_VER from source?"; then
compile="yes"
fi
if promptorno "Set up web-based file manager?"; then
filemgr="yes"
echo " Please create a user name and password."
read -r -p " username: " fmgr_user < /dev/tty
read -r -p " password: " password < /dev/tty
fmgr_hash=`wget -qO- geekfunklabs.com/passhash.php?password=$password`
fi
if promptorno "Download and install ~400MB of additional soundfonts?"; then
soundfonts="yes"
fi
echo ""
if ! promptoryes "All options chosen. OK to proceed?"; then
exit 1
fi
warning "\nThis may take some time ... go make some coffee.\n"
## do things
umask 002 # friendly permissions for web file manager
if [[ $update == "yes" ]]; then
inform "Installing/Updating FluidPatcher and any required packages..."
# get dependencies
sysupdate
apt_pkg_install "git"
apt_pkg_install "python3-pip"
apt_pkg_install "python3-rtmidi"
apt_pkg_install "libfluidsynth2"
apt_pkg_install "fluid-soundfont-gm"
pip_install "oyaml"
pip_install "mido"
# pip_install "RPi.GPIO"
# pip_install "RPLCD"
echo "Installing/Updating FluidPatcher version $NEW_FP_VER ..."
rm -rf fluidpatcher
git clone https://github.com/albedozero/fluidpatcher &>> $logfile
cd fluidpatcher
if [[ $overwrite == "yes" ]]; then
find . -type d -exec mkdir -p $installdir/{} \; &>> ../$logfile
find . -type f ! -name "hw_overlay.py" -exec cp -f {} $installdir/{} \; &>> ../$logfile
find . -type f -name "hw_overlay.py" -exec cp -n {} $installdir/{} \; &>> ../$logfile
else
find . -type d -exec mkdir -p $installdir/{} \; &>> $logfile
find . -type f ! -name "*.yaml" ! -name "hw_overlay.py" -exec cp -f {} $installdir/{} \; &>> ../$logfile
find . -type f -name "hw_overlay.py" -exec cp -n {} $installdir/{} \; &>> ../$logfile
find . -type f -name "*.yaml" -exec cp -n {} $installdir/{} \; &>> ../$logfile
fi
cd ..
rm -rf fluidpatcher
ln -s /usr/share/sounds/sf2/FluidR3_GM.sf2 $installdir/SquishBox/sf2/ &>> $logfile
# real-time audio tweaks
sudo usermod -a -G audio pi
AUDIOCONF="/etc/security/limits.d/audio.conf"
if test -f "$AUDIOCONF"; then
if ! grep -q "^@audio - rtprio 80" $AUDIOCONF; then
echo "@audio - rtprio 80" | sudo tee -a $AUDIOCONF &>> $logfile
fi
if ! grep -q "^@audio - memlock unlimited" $AUDIOCONF; then
echo "@audio - memlock unlimited" | sudo tee -a $AUDIOCONF &>> $logfile
fi
else
echo -e "@audio - rtprio 80\n@audio - memlock unlimited" | sudo tee -a $AUDIOCONF &>> $logfile
fi
fi
# set up audio
CONFIG="/boot/config.txt"
sudo mv /etc/xdg/autostart/piwiz.desktop /etc/xdg/autostart/piwiz.disabled &>> $logfile
if [[ $audiosetup =~ ^(1|2|3)$ ]]; then
sudo sed -i "/dtparam=audio=on/d" $CONFIG
sudo sed -i "/dtoverlay=hifiberry-dac/d" $CONFIG
fi
if [[ $audiosetup == "1" ]]; then
echo "Activating DAC sound..."
echo "dtoverlay=hifiberry-dac" | sudo tee -a $CONFIG &>> $logfile
ASK_TO_REBOOT=true
elif [[ $audiosetup == "2" ]]; then
echo "Activating headphone audio..."
echo "dtparam=audio=on" | sudo tee -a $CONFIG &>> $logfile
sudo amixer -q sset 'Headphone' 400
ASK_TO_REBOOT=true
elif [[ $audiosetup == "3" ]]; then
echo "Activating USB audio..."
# nothing needed
ASK_TO_REBOOT=true
fi
# set up services
if [[ $startup == "1" ]]; then
echo "Enabling startup service..."
chmod a+x $installdir/squishbox.py
cat <<EOF | sudo tee /etc/systemd/system/squishbox.service &>> $logfile
[Unit]
Description=SquishBox
After=local-fs.target
[Service]
Type=simple
ExecStart=$installdir/squishbox.py
User=pi
WorkingDirectory=$installdir
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable squishbox.service &>> $logfile
ASK_TO_REBOOT=true
elif [[ $startup == "2" ]]; then
echo "Enabling startup service..."
chmod a+x $installdir/headlesspi.py
cat <<EOF | sudo tee /etc/systemd/system/squishbox.service &>> $logfile
[Unit]
Description=Headless Pi Synth
After=local-fs.target
[Service]
Type=simple
ExecStart=$installdir/headlesspi.py
User=pi
WorkingDirectory=$installdir
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable squishbox.service &>> $logfile
if ! [[ $decpatch == "use default" ]]; then
sed -i "/^DEC_PATCH/s|[0-9]\+|$decpatch|" $installdir/headlesspi.py; fi
if ! [[ $incpatch == "use default" ]]; then
sed -i "/^INC_PATCH/s|[0-9]\+|$incpatch|" $installdir/headlesspi.py; fi
if ! [[ $bankinc == "use default" ]]; then
sed -i "/^BANK_INC/s|[0-9]\+|$bankinc|" $installdir/headlesspi.py; fi
if ! [[ $selectpatch == "use default" ]]; then
sed -i "/^SELECT_PATCH/s|[0-9]\+|$selectpatch|" $installdir/headlesspi.py; fi
if ! [[ $ctrls_channel == "use default" ]]; then
sed -i "/^CTRLS_MIDI_CHANNEL/s|[0-9]\+|$ctrls_channel|" $installdir/headlesspi.py; fi
ASK_TO_REBOOT=true
elif [[ $startup == "3" ]]; then
echo "Disabling startup service..."
sudo systemctl disable squishbox.service &>> $logfile
ASK_TO_REBOOT=true
fi
if [[ $compile == "yes" ]]; then
# compile latest FluidSynth
inform "Compiling latest FluidSynth from source..."
if grep -q "#deb-src" /etc/apt/sources.list; then
sudo sed -i "/^#deb-src/s|#||" /etc/apt/sources.list
fi
UPDATED=false
sysupdate
apt_pkg_install "tap-plugins"
echo "Getting build dependencies..."
sudo apt-get build-dep fluidsynth --no-install-recommends --yes &>> $logfile
rm -rf fluidsynth
git clone https://github.com/FluidSynth/fluidsynth &>> $logfile
mkdir fluidsynth/build
cd fluidsynth/build
echo "Configuring..."
cmake .. &>> $logfile
echo "Compiling..."
make &>> $logfile
sudo make install &>> $logfile
sudo ldconfig &>> $logfile
cd ../..
rm -rf fluidsynth
fi
if [[ $filemgr == "yes" ]]; then
# set up web server, install tinyfilemanager
inform "Setting up web-based file manager..."
sysupdate
apt_pkg_install "nginx"
apt_pkg_install "php-fpm"
# enable php in nginx
cat <<'EOF' | sudo tee /etc/nginx/sites-available/default &>> $logfile
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
}
EOF
# some tweaks to allow uploading bigger files
if grep -q "client_max_body_size" /etc/nginx/nginx.conf; then
sudo sed -i "/client_max_body_size/cclient_max_body_size 900M;" /etc/nginx/nginx.conf
else
sudo sed -i "/^http {/aclient_max_body_size 900M;" /etc/nginx/nginx.conf
fi
sudo sed -i "/upload_max_filesize/cupload_max_filesize = 900M" /etc/php/7.3/fpm/php.ini
sudo sed -i "/post_max_size/cpost_max_size = 999M" /etc/php/7.3/fpm/php.ini
# set permissions and umask to avoid permissions problems
sudo usermod -a -G pi www-data
sudo chmod -R g+rw $installdir/SquishBox
if grep -q "UMask" /lib/systemd/system/php7.3-fpm.service; then
sudo sed -i "/UMask/cUMask=0002" /lib/systemd/system/php7.3-fpm.service
else
sudo sed -i "/\[Service\]/aUMask=0002" /lib/systemd/system/php7.3-fpm.service
fi
# install and configure [tinyfilemanager](https://tinyfilemanager.github.io)
echo "Configuring file manager..."
wget -q raw.githubusercontent.com/prasathmani/tinyfilemanager/master/tinyfilemanager.php
sudo sed -i "/define('APP_TITLE'/cdefine('APP_TITLE', 'SquishBox Manager');" tinyfilemanager.php
sudo sed -i "/'admin' =>/d;/'user' =>/d" tinyfilemanager.php
sudo sed -i "/\$auth_users =/a '$fmgr_user' => '$fmgr_hash'" tinyfilemanager.php
sudo sed -i "/\$theme =/c\$theme = 'dark';" tinyfilemanager.php
sudo sed -i "0,/root_path =/s|root_path = .*|root_path = '$installdir/SquishBox';|" tinyfilemanager.php
sudo sed -i "0,/favicon_path =/s|favicon_path = .*|favicon_path = 'gfl_logo.png';|" tinyfilemanager.php
sudo sed -i '/aceMode/s|,"yaml":"YAML"||' tinyfilemanager.php
sudo sed -i 's|"aceMode":{|&"yaml":"YAML",|' tinyfilemanager.php
sudo mv -f tinyfilemanager.php /var/www/html/index.php
wget -q geekfunklabs.com/gfl_logo.png
sudo mv -f gfl_logo.png /var/www/html/
ASK_TO_REBOOT=true
fi
if [[ $soundfonts == "yes" ]]; then
inform "Downloading free soundfonts..."
apt_pkg_install "unzip"
apt_pkg_install "tap-plugins"
apt_pkg_install "wah-plugins"
wget -nv --show-progress geekfunklabs.com/squishbox_soundfonts.zip
unzip -na squishbox_soundfonts.zip -d $installdir/SquishBox
rm squishbox_soundfonts.zip
fi
sudo echo defaults.pcm.card 0 >> /etc/asound.conf
sudo echo defaults.ctl.card 0 >> /etc/asound.conf
success "Tasks complete!"
if $ASK_TO_REBOOT; then
warning "\nSome changes made to your system require"
warning "your computer to reboot to take effect."
echo
if promptoryes "Would you like to reboot now?"; then
sync && sudo reboot
fi
fi