Skip to content

Instantly share code, notes, and snippets.

@albedozero
Last active August 3, 2022 01:58
Show Gist options
  • Save albedozero/2dec9bcd7e2f68287cb6ace7485875bc to your computer and use it in GitHub Desktop.
Save albedozero/2dec9bcd7e2f68287cb6ace7485875bc to your computer and use it in GitHub Desktop.
Setting up a headless Raspberry Pi synth

A complete setup with many options can be performed by running the script at git.io/squishbox with the command

curl -L git.io/squishbox | bash

To perform the minimal steps to set up, first install the required software: Connect to your Pi via ssh or plug in a keyboard and monitor and type in the following commands to install the required software:

sudo apt install git python3-pip libfluidsynth1 fluid-soundfont-gm
sudo pip3 install oyaml RPLCD RPi.GPIO
git clone https://github.com/albedozero/fluidpatcher.git
cp -r fluidpatcher/* /home/pi
ln -s /usr/share/sounds/sf2/FluidR3_GM.sf2 SquishBox/sf2/FluidR3_GM.sf2

Edit the headlesspi.py script and modify the values of PATCH_SELECT_CHANNEL, DEC_CC, and INC_CC to correspond to buttons on your MIDI controller - or reprogram two of the buttons on your controller to send momentary CC messages 27 and 28. If you instead want to select the patch using a knob/slider, uncomment the line containing SELECT_CC and modify the value as needed.

Edit the file /etc/rc.local as root (e.g. sudo vi /etc/rc.local) and add the following above the exit 0 line:

sudo python3 /home/pi/headlesspi.py &

Finally, run alsamixer and make sure your volume is turned up. Now, unplug the monitor and keyboard (if using), connect your headphones and MIDI controller, and reboot!


If you want to use a USB sound card instead of the Raspberry Pi's headphone jack:

Edit /boot/config.txt and comment (#) out the following line:

dtparam=audio=on

Edit Squishbox/squishboxconf.yaml and add the following line under the fluidsettings: line:

  audio.alsa.device: hw:0
@clendee
Copy link

clendee commented Oct 14, 2021

Hi, I'm really enjoying your project. Thanks for the work you have done on this, it is exactly what I am looking for.

I notice the volume is very low through my speaker, is there a way to increase the gain in fluidsynth?
I have turned the gain up to maximum in the bank1.yaml file with:

fluidsettings: synth.gain: 1

however the overall gain is still very low.
I have also run alsamixer and increased the output to maximum.
image

I was trying a different process outlined here:
https://medium.com/@rreinold/how-to-use-a-raspberry-pi-3-to-turn-midi-piano-to-into-stand-alone-powered-piano-4aeb79e309ce

and they suggest to increase the fluidsynth gain using the following command:
fluidsynth --audio-driver=alsa --gain 5 /usr/share/sounds/sf2/FluidR3_GM.sf2

Is there a way to incorportate tthe equivalent of his approach into your program?

I'd welcome any thoughts on this.

@albedozero
Copy link
Author

albedozero commented Oct 15, 2021

Glad you like the project! Your addition to the bank file isn't quite right. If you put everything on one line like that, you have to show what's being nested, like this:

fluidsettings: {synth.gain: 1.0}

Or, you can put things on separate lines and use indentation to show the nesting:

fluidsettings:
  synth.gain: 1.0

The script couldn't parse your setting, so it was ignoring it and using the default value of 0.2. Once you set fluidsynth's gain above 1.0 the sound will begin to distort, so fair warning. The loudness of the sound output can also vary from speaker to speaker - it's meant to be a headphone jack, so it doesn't have a huge power output, and some speakers are harder to drive than others.

Setting synth.gain this way does the same thing as setting gain on the command line, just in case that's not clear.

@albedozero
Copy link
Author

I actually prefer to put the synth.gain setting in SquishBox/squishboxconf.yaml - that way it's the same for all my bank files.

@clendee
Copy link

clendee commented Oct 15, 2021

Hi @albedozero ,

Thank you for your speedy response.

Yes, that did work, its much better when I set the gain to 1.0, I did try 2.0 but it was too distorted.
It was also a good tip to put it in the squishboxconf.yaml file.

I have ordered this little USB audio card for the Pi, hopefully that'll improve audio output levels, apparently its good with latency as well, but I have to say you did an amazing job as the latency is very low with your code.

I'd love to try and build the squishbox, but Im unsure if its within my capabilities.
Is it possible to buy just the PCB as I'd have some of the parts lying about [from other unifinished projects :| ]

I presume you are familiar with the PiSound project, would this audio card work with your project?

Should I be concerned when my Pi red power light is flashing? I thought that meant the Pi isnt getting enough power to the board, but I'm using an original Pi3B+ power supply, or have you done some jiggery-pokery there also?

@clendee
Copy link

clendee commented Oct 15, 2021

Hi,

I wanted to bring this to your attention

image

This is my Pi-Hole server, it has rate limited the SquishBox raspberry pi because it was making over 1000 DNS queries per minute. The purple lines are the IP address of the Raspberry Pi making DNS requests. All the other colours, much lower in the graph are other hosts on my network, they are not making so many DNS requests. It there some random unresolving DNS stuff happening on your code? Im just bringing this to your attention if it is useful information to you.

@albedozero
Copy link
Author

I have ordered this little USB audio card for the Pi, hopefully that'll improve audio output levels, apparently its good with latency as well, but I have to say you did an amazing job as the latency is very low with your code.

I have one of those and can confirm it does improve the latency and has more robust output.

I'd love to try and build the squishbox, but Im unsure if its within my capabilities. Is it possible to buy just the PCB as I'd have some of the parts lying about [from other unifinished projects :| ]

I'm just about to order a batch of new PCBs as all my current stock are going into fully assembled SquishBoxes for the Tindie store. This is a good idea and I'll offer an option to buy just the PCB in addition to the kits - thanks :)

I presume you are familiar with the PiSound project, would this audio card work with your project?

I'm familiar - another user brought it up in an issue. I think it should work - probably the simplest way is to run their setup first, then run my setup script with curl -L git.io/squishbox | bash and choose the Blokas card for audio output.

Should I be concerned when my Pi red power light is flashing? I thought that meant the Pi isnt getting enough power to the board, but I'm using an original Pi3B+ power supply, or have you done some jiggery-pokery there also?

My script flashes the LED if there's an error or when shutting down (by holding one of the patch change buttons ~7sec). If you think there's an error, you can get more info by running from the command line.

@albedozero
Copy link
Author

albedozero commented Oct 15, 2021

This is my Pi-Hole server, it has rate limited the SquishBox raspberry pi because it was making over 1000 DNS queries per minute. The purple lines are the IP address of the Raspberry Pi making DNS requests. All the other colours, much lower in the graph are other hosts on my network, they are not making so many DNS requests. It there some random unresolving DNS stuff happening on your code? Im just bringing this to your attention if it is useful information to you.

Yikes! I don't think anything in my code should be doing that - I used to have it set up a socket and listen for incoming connections for remote bank editing, but that wouldn't generate DNS requests and I've removed the feature. FluidSynth does have server-client capability - maybe I need to look into whether this should be disabled. If you have a chance to see if the DNS spamming still occurs without running my script that would be interesting to know. Thanks for the info!

@clendee
Copy link

clendee commented Oct 15, 2021

There's really something weird going on the the DNS requests on port 53 on the squishBox
image

I'll re-image the micro sd card tomorrow and just run your install script (piping to bash) and see if the problem persists.

@clendee
Copy link

clendee commented Oct 16, 2021

Hi @albedozero ,

Feel free to delete my last few messages here if you wish to not clutter up your page with my stuff.

I did a complete re-image of the sd card and only ran your script and the DNS issues seem to have subsided. Im not really sure what was going on there, I didn't add much stuff to the image. Hmmm.... computers....

@albedozero
Copy link
Author

I put up a few PCBs for sale if you're still interested - the BOM and instructions are here.

@softelli
Copy link

softelli commented Aug 1, 2022

Hi all,
I have problems with the LADSPA library.
Anyone can help me in this case?

Selected patch 24/26: SFX
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: Unable to load LADSPA library '/usr/lib/ladspa/patchcord': /usr/lib/ladspa/patchcord.so: cannot open shared object file: No such file or directory
fluidsynth: error: LADSPA check failed, unable to activate effects

@albedozero
Copy link
Author

Is the file /usr/lib/ladspa/patchcord.so actually there? If not, go to the directory where you installed everything (probably your home directory) and enter the command

gcc -shared assets/patchcord.c -o patchcord.so && sudo mv -f patchcord.so /usr/lib/ladspa/

If the file is there you may be having some kind of disk access problem. Maybe corrupt SD card?

This error is a little odd since that patch doesn't use LADSPA effects. What happens if you select patch 5 (Tonewheel) or patch 8 (Shred Guitar)?

@softelli
Copy link

softelli commented Aug 3, 2022

Thanks, albedozero!
This code "gcc -shared..." works for me.
God bless you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment