Yet Another FluidSynth Piano. Python3 script to use a TV Box as a headless software midi synthesizer for a USB Midi keyboard
There are many people that have created scripts to use a Raspberry Pi as a software synth to use it as a standalone device into which connect a midi keyboard. Among others, the SquishBox, Zynthian, the Sampler box, the Zbox or Pipiano / original Pipiano. Most of them are based on FluidSynth. This is just one more of those projects, but with the twist of using the hardware buttons of the USB soundcard for additional control, which is easier than using the edit button of the midi keyboard (if it has one). Besides, it has been actually tested only on a TV Box runing Armbian. This is much less ambitius than the Sonaremin project, which is also targeted towards TV Boxes.
Although this script has been tested on a QPlus TV Box, it might work with other devices with Armbian (similar ones are the T95 and T95Max, if H6-based). The QPlus TV Box is based on the Allwinner H6 chipset, which is the same chipset of several Orange Pi models, but at a competitive price since it comes with 2GB-4GB (although only 3GB available) RAM, 32GB/64GB emmc memory, a case, and a power plug. Check Amazon or AliExpress for current prices. Unfortunately, this low price comes with some limitations: the unofficial Armbian version that can be installed on these devices often do not support their WiFi, their GPU, their remote or their internal audio DAC, which might be OK for certain projects such as this one. Once configured, the box takes less than 40 seconds to be ready to play from the moment you plug it in.
The python script might actually work on any linux box, but it has not been tested.
Besides the TV Box, you also need a USB soundcard (preferably with 4 buttons: mute mic, mute volume, volume up and volume down). The buttons allow changing the instrument and the bank. You can use a USB Soundcard without buttons, but you will just be able to play one instrument. Obviously, you should also have a USB Midi keyboard to connect to the box. The idea of the project is to be headless (without monitor), so you just switch on the box, wait a few seconds to boot, and start playing without the need of a keyboard, mouse or monitor.
Ir you need more advanced functionality you can try the HeadlessPi from the author of the SquishBox
There are many places to download soundfonts. For piano soundfonts, try Soundfonts4U
The included sounfont contains a piano from studioax.com and other instruments from the NiceKeys soundfont of Soundfonts4U
If you have a piano like the Casio CDP-S100, which has a nice keybed but a mediocre piano sound, this little box can really improve the factory sound.
There are many USB Soundcards out there. Cheap ones tend to sound bad. Some inexpensive and compatible chipsets include CM108, HS100B and PCM2704. The quality of the sound will also depend on the additional circuitry surrounding the main chipset, but PCM2704-based cards seem to tend to sound good, although it is difficult to find them with a case. I have used successfully a LogiLink UA0078 card (CM108 based):
and a generic HS100B card which included some (not very good) headphones
The LogiLink needs a USB extension cable, since it does not fit directly in the USB connector of the QPlus. You can look for USB Headsets in Amazon and choose one that has a 3.5mm Jack just to be able to to use the DAC with a different headset. I would not be surprised that the cheap ones use a HS100B chipset, but there is not guarantee that they will actually work with Armbian if they use an exotic chipset.
Installation is not difficult if you follow the steps carefully. You should connect a computer keyboard, mouse, monitor and ethernet cable with internet access to the box in order to configure it. You should also have an empty SD Card (minimum 8GB and Class 1 -'A1' recommended).
This is done on a separate computer (in my case I have Windows). These instructions are for a compatible TV Box (basically a QPlus, a T95 or a T95Max) (see https://forum.armbian.com/topic/16859-allwinner-h6/) but might work with other boxes using the apropriate Armbian image.
Download the latest armbian buster desktop release for the H6 chipset from https://users.armbian.com/balbes150/aw-h6-tv/ . At the time of writing it is
Armbian_21.08.0-trunk_Aw-h6-tv_buster_current_5.10.47_xfce_desktop.img.xz
The official Armbian documentation recommends the follwing steps and tools:
- Format first the card using the SD Memory Card Formatter
- Eject the card, insert it again.
- Use USB Imager to load the image into the SD Card, directly selecting the compressed .xz image. Be careful to select the drive with the SD Card in the dropdown list. Although the Windows drive cannot be selected, if you have additional disks they will appear in there.
One of the good things of these H6-based TV Boxes is that they can boot from the SD Card directly. Once the image is burned in the SD Card, just insert the card in the TV Box, plug it and Armbian will start loading. The original Android system will stay in the internal storage and you will be able to boot normally to Android if the SD Card is not inserted. There is an option to actually replace Android with Armbian, but it is not required, unless you want to forget about Android and just use Armbian. Armbian comes with a tool just to do that.
Upon first boot, Armbian will typically ask several questions:
- Set the root password. Use letters and numbers. You will have to re-enter it for confirmation. If for some strange reason you get the login prompt and not the request to set the root password, just login with user
root
and password1234
. It will then prompt you to set the root password as well as the following questions. - Default command shell. I use bash (press '1')
- Default user name and password with confirmation
- Your 'real name'. Just leave the default, which is the user capitalized.
- If you want to set your user language based on your location. Since I prefer using English for the interface (it is easier to google problems and instructions if the messages are in English), I press 'n'
If all goes well, it should show an x-windows interface. On the top left corner go to Applications-Settings-Armbian config. You will be asked for your password. Enter it. Once in the tool, select Personal. If needed, configure your keyboard layout for your language and then your timezone. I recommend using the Windows logo key for the Compose key. It can be useful when using the nano editor.
Click on your user name on the top right corner and select Restart.
You will restart to the command line. Enter your user and password. You can go back to the desktop environment using
startx
There you can use the webbrowser to access these instructions and then open a Terminal Emulator and copy and paste in the terminal window (pasting is done with CTRL+SHIFT+V).
To make the system login automatically to the x-windows environment, we need to perform some additional steps. Type the following commands, entering your password when asked and confirming the actions if need to. Note that when entering paths, you can start writing, use TAB to partially autocomplete, keep writing and use TAB again to autocomplete.
Download this repository:
git clone https://github.com/fizban99/yafspiano.git
Add your user in the configuration file:
cd yafspiano
nano install_assets/22-armbian-autologin.conf
The second line will present you with an editor. Go to the line with autologin-user=
and enter your user name in there, replacing the placeholder. Press CTRL+X, confirm with 'y' and ENTER to save.
If you press CTRL+Z by mistake, you can just type fg
to return back to the editor.
Copy the configuration to the apropriate system path:
sudo cp install_assets/22-armbian-autologin.conf /etc/lightdm/lightdm.conf.d/
Reboot the system to verify that the autologin works with:
sudo reboot
The default FluidSynth package is version 1.1.1, but the latest one is 2.2.2. It comes with some useful functionality such as autodetection of a midi-keyboard. So, we will compile the latest version. Open a Terminal emulator and type or copy/paste the following (better line by line):
wget https://github.com/FluidSynth/fluidsynth/archive/refs/tags/v2.2.2.tar.gz
tar -xvzf v2.2.2.tar.gz
cd fluidsynth-2.2.2
mkdir build
cd build
sudo sed -i~orig -e 's/#deb-src/deb-src/' /etc/apt/sources.list
sudo apt update
sudo apt-get build-dep fluidsynth --no-install-recommends
cmake ..
sudo make install
sudo nano /etc/ld.so.conf.d/libc.conf
In the nano editor, add one line with /usr/local/lib64
. Press CTRL+X, confirm with 'y' and ENTER to save.
Finally type the following in the terminal for the change to take effect:
sudo ldconfig
Check that fluidsynth launches:
fluidsynth
Although fluidsynth will complain about not finding a soundfont, you should get a >
prompt. Press CTRL+C to exit fluidsynth.
We will also install a component to reduce the latency of the sound. With jack, the latency can be much lower than any latency you can have in Android. The drawback is that only one application can take a hold of the audio device, which for us is fine (just remember not to have Firefox on when running the yafspiano):
sudo apt install jackd2
It wil ask you if you want to enable realtime. Select Yes.
The default configuration assumes you USB Soundcard is called "Device". To confirm it, connect your USB Soundcard and type:
cat /proc/asound/cards
Note the name within []. If it is not "Device", edit the jackd configuration file with nano and replace "Device" with the corresponding name in your system, then press CTRL+X, confirm with 'y' and Enter to save:
nano ~/yafspiano/install_assets/.jackdrc
Copy the default jack configuration to your home:
cp ~/yafspiano/install_assets/.jackdrc ~/
Reboot for the changes to take effect with:
sudo reboot
Since the yafspiano script is in python3, we need to install some requirements:
sudo apt install python3-pip
pip3 install pexpect setuptools
sudo apt install python3-dev
pip3 install wheel
pip3 install pynput
Close Firefox and test the script (you need at least a usb sound card connected, so either you use a USB hub or disconnect the mouse and connect the usb soundcard before running it):
python3 yafspiano/yafspiano.py
After a few seconds you should hear a chord, indicating that the synth is ready. If you are not using a USB hub, you can now disconnect your computer keyboard and connect a midi keyboard. Fluidsynth should autodetect it and you should be able to play. You can press CTRL+C to abort (reconnect your computer keyboard if needed).
Since this is going to be headless, we want the script to autostart upon boot.
cp ~/yafspiano/install_assets/yafspiano.desktop ~/.config/autostart/yafspiano.desktop
That's it. Make sure you close Firefox and reboot to see if it works:
sudo reboot
When running the box without a monitor (headless), to powerdown the system, press volume up and down at the same time for a few seconds. You should hear a sound and if the soundcard has a led, it should turn off. This would signal that you can unplug the tvbox.
You can download the GM Soundfont that typically is used with fluidsynth and thwn create a symlink in the soundfonts directory:
sudo apt install -y fluid-soundfont-gm
ln -s /usr/share/sounds/sf2/FluidR3_GM.sf2 ~/yafspiano/soundfonts/
Out of the 4 buttons only 3 are clickable: volume up, volume down and volume mute. The mic mute does not generate any signal.
The volume mute changes the functionality of the volume up and down. By default, volume up and down navigate the instruments from the current bank. By pressing once on the volume mute, the volume keys navigate through the banks. Once more and you can navigate through the soundfonts and a third one to toggle reverb. One more press and you can navigate through the instruments of the current bank again. If you do a long press on volume up, you will go to the first element of the current mode (first instrument, bank or sounfont or reverb off).
Additionaly, you can use the default functionality of FluidSynth if you have an Edit button in your Keyboard (e.g. KeyStation49 or KeyRig49):