Akai EWI USB and Linux, part 3

In part 1 and part 2 of my Akai odyssey, I've described getting the EWI to produce sound and configuring fluidsynth (plus QSynth) for optimum performance without latency. I was happy with my setup but after a while I started craving a little convenience.

Setting up a playing session is a silly affair involving the instrument itself, my notebook, a pair of headphones, 2 cables and 4 connectors. The same goes for winding things down after playing. It used to be even more obnoxious since I had to manually start and exit the QSynth software. I knew that Linux can react to USB hot-plug events and this reaction can be configured; so I decided to make QSynth start and stop automagically.

The system-du-jour for reacting to hotplug events in Linux is currently udev. Debian's wiki happens to have a very nice udev page which explains that all one needs to do is to put a line in a file under /etc/udev/rules.d. One can either use one of the existing files under that directory or create a new one, which is what I did just to keep things tidy.

Entries in udev rule files rely on an IF-THEN syntax where the IF part indicates in what circumstances the rule should trigger and THEN stipulates what should be done. The syntax is quite restrictive in that each rule has to fit on a line - i.e. no fancy procedural tricks. The IF part offers a lot of ways to pick up an event, though. One can specify the BUS on which the event happens, the NAME of the device, the ACTION that the event represents, expected environment variable values etc. I found the options quite extensive, making it easy to specify any event.

Having to run sudo udevadm control --reload-rules after each rule update was a bit annoying but what really held me up was figuring out which events the rule should react to. I found out that plugging the EWI into and out of the USB port caused entire cascades of events with different characteristics. I eventually settled on the following rules:

ATTR{idVendor}=="09e8", ATTR{idProduct}=="006d", ACTION=="add", RUN+="/usr/local/bin/ewi"
ENV{DEVNAME}=="/dev/snd/midiC1D0", ACTION=="remove" RUN+="/usr/local/bin/ewi"

The first rule fires when I plug the device in, the second one upon unplugging. Both rules launch the same shell script when invoked. The shell script itself looks like this:

if [ $ACTION = "add" ] ; then
 set -x 
 xhost local:username
 export DISPLAY=:0.0
 su username -c qsynth &
elif [ $ACTION = "remove" ] ; then
 killall qsynth
fi

The "add" section contains incantations needed to properly launch a GUI program from a shell script. I picked them up in some forum and have unfortunately lost the link. Kudos to the original poster, anyway.

With this bit of scripting, my EWI is the very definition of Plug-and-Play, as well as Stop-Playing-and-Unplug ;-) . All is not perfect, though; one problem remains and it's completely unrelated to udev. Since I use legacy OSS emulation due to latency issues, I cannot run any other sound-producing application while QSynth is active (OSS doesn't support software mixing). I don't mind that just yet but as I get my fingerings in order and advance my playing I will need to resolve it. The saga continues...

Proudly powered by Pelican, which takes great advantage of Python.