2

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

2
1

Ok after a few research, I found out a way out. First, use udevadm monitor before plugging and plug it. Here is the output :

KERNEL[126.555200] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
  KERNEL[126.555888] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
  KERNEL[126.556508] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.1 (usb)
  KERNEL[126.557829] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.2 (usb)
  KERNEL[126.558188] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.3 (usb)
  KERNEL[126.558548] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
  KERNEL[129.399848] add      /module/snd_seq_dummy (module)
  UDEV  [129.406959] add      /module/snd_seq_dummy (module)
  UDEV  [129.493966] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
  UDEV  [129.501782] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.2 (usb)
  UDEV  [129.505609] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.3 (usb)
  KERNEL[129.508606] add      /module/snd_rawmidi (module)
  UDEV  [129.510396] add      /module/snd_rawmidi (module)
  KERNEL[129.513459] add      /module/snd_usbmidi_lib (module)
  UDEV  [129.515605] add      /module/snd_usbmidi_lib (module)
  KERNEL[129.516631] add      /module/snd_hwdep (module)
  UDEV  [129.518617] add      /module/snd_hwdep (module)
  KERNEL[129.535977] add      /module/snd_usb_audio (module)
  UDEV  [129.537881] add      /module/snd_usb_audio (module)
  KERNEL[129.538452] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.1 (usb)
  KERNEL[129.538547] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1 (sound)
  KERNEL[129.539135] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/midiC1D0 (sound)
  KERNEL[129.539260] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq)
  KERNEL[129.539985] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/controlC1 (sound)
  KERNEL[129.541172] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
  UDEV  [129.541345] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.1 (usb)
  KERNEL[129.541428] add      /bus/usb/drivers/snd-usb-audio (drivers)
  UDEV  [129.542776] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
  UDEV  [129.542917] add      /bus/usb/drivers/snd-usb-audio (drivers)
  KERNEL[129.549489] add      /module/snd_seq_midi_event (module)
  UDEV  [129.551648] add      /module/snd_seq_midi_event (module)
  KERNEL[129.552118] add      /module/snd_seq_midi (module)
  KERNEL[129.552213] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq)
  KERNEL[129.552288] add      /bus/snd_seq/drivers/snd_seq_midi (drivers)
  UDEV  [129.554430] add      /module/snd_seq_midi (module)
  UDEV  [129.554572] add      /bus/snd_seq/drivers/snd_seq_midi (drivers)
  UDEV  [129.561871] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
  UDEV  [129.564936] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.1 (usb)
  UDEV  [129.566125] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1 (sound)
  UDEV  [129.568261] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq)
  UDEV  [129.570243] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/midiC1D0 (sound)
  KERNEL[129.573057] change   /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1 (sound)
  UDEV  [129.607147] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/controlC1 (sound)
  UDEV  [129.609881] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
  UDEV  [129.611644] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq)
  UDEV  [129.618931] change   /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1 (sound)

After noticing the last line UDEV [129.611644] bind /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq), I plugged the USB MIDI keyboard, and did :

devadm info -a /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2\:1.0/sound/card1/seq-midi-1-0/> /tmp/udev`

Among the outputs, there is in the first lines :

  looking at device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0':
    KERNEL=="seq-midi-1-0"
    SUBSYSTEM=="snd_seq"
    DRIVER=="snd_seq_midi"

  looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1':
    KERNELS=="card1"
    SUBSYSTEMS=="sound"
    DRIVERS==""
    ATTRS{number}=="1"
    ATTRS{id}=="Impulse"

  looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0':
    KERNELS=="1-1.2:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="snd-usb-audio"
    ATTRS{bInterfaceProtocol}=="00"
    ATTRS{bInterfaceSubClass}=="01"
    ATTRS{interface}=="Novation Impulse"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{authorized}=="1"
    ATTRS{bNumEndpoints}=="00"
    ATTRS{bInterfaceClass}=="01"

The interesting infos are SUBSYSTEM and DRIVERS. So I changed my udev rule to :

ACTION=="bind", SUBSYSTEM=="snd_seq", DRIVERS=="snd-usb-audio", 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"

Here, I changed the ACTION to "bind" because of the first command (udevadm monitor) and matched with SUBSYSTEM and DRIVERS instead of the ids of the product. And it works like a charm ! Hope it helped. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.