I have a MIDI keyboard "Impulse", and a raspi3. I want to connect the keyboard to the raspi, and having sounds without touching anything.
So I use fluidsynth with jack as audio driver. Fluidsynth is launched by systemd service. There is no problem with it.
So I made a script, where I "jack_connect" the ports, and "aconnect" the midi port of my keyboard to the synthesizer fluidsynth in this way :
impulseport=$(aconnect -i|grep -i "IMPULSE" | cut -d ' ' -f 2)0
synthport=$(aconnect -o |grep -i "FLUID" | cut -d ' ' -f 2)0
# some verifications of existence and exit if one port is missing
aconnect ${impulseport} ${synthport}
The thing is it does not really work as I wanted. My udev rule that exec this script is fired before the loading of "snd-usb-audio" interface driver. As a consequence, the variable $impulseport is empty (actually =0 with the concatenation at the end).
Here is my udev rule :
ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="1235", ATTRS{idProduct}=="001a", RUN+="/bin/su -c /home/pi/piano_connect - pi"
ACTION=="remove", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="1235", ATTRS{idProduct}=="001a", RUN+="/usr/bin/aconnect -x"
In the syslog :
1 systemd[410]: Started Sound Service.
2 fluidsynth[423]: fluidsynth: Jack sample rate mismatch, adjusting. (synth.sample-rate=44100, jackd=48000)
3 kernel: [162.772916] usb 1-1.2: new full-speed USB device number 4 using dwc_otg
4 kernel: [162.905473] usb 1-1.2: New USB device found, idVendor=1235, idProduct=001a, bcdDevice= 0.00
5 kernel: [162.905491] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
6 kernel: [162.905500] usb 1-1.2: Product: Impulse
7 kernel: [162.905509] usb 1-1.2: Manufacturer: Focusrite A.E. Ltd
8 mtp-probe: checking bus 1, device 4: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2"
9 mtp-probe: bus: 1, device: 4 was not an MTP device
10 systemd[1]: Started Session c2 of user pi.
11 pi: [Piano] Jackd : connecting system to fluidsynth ports
12 pi: [Piano] Error: Port Impulse unknown, exiting
13 systemd-udevd[473]: Process '/bin/su -c /home/pi/piano_connect - pi' failed with exit code 1.
14 systemd[1]: session-c2.scope: Succeeded.15
15 kernel: [165.876060] usbcore: registered new interface driver snd-usb-audio
16 mtp-probe: checking bus 1, device 4: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2"
17 mtp-probe: bus: 1, device: 4 was not an MTP device
The problem is at the line 12 : $impulseport is empty ! So it makes the script exiting (line 13). And you can see line 15 that the driver is loaded AFTERWARDS.. So in my script I made a dirty thing like a while loop where I check if the directory /sys/bus/usb/drivers/snd-usb-audio exists with a sleep but it hangs the system and the module never loads.. Do you guys have any idea about a way out ?
NB: If I unplug the USB MIDI keyboard, and plug it again it works well, but I'd like it to work correctly on the first plug after boot... Or even more to work after boot while the keyboard is already plugged :D