Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Connect all MIDI inputs to all MIDI outputs (except themselves). Supports multi-port devices
#!/usr/bin/ruby
t = `aconnect -i -l`
$devices = {}
$device = 0
t.lines.each do |l|
match = /client (\d*)\:((?:(?!client).)*)?/.match(l)
# we skip empty lines and the "Through" port
unless match.nil? || match[1] == '0' || /Through/=~l
$device = match[1]
$devices[$device] = []
end
match = /^\s+(\d+)\s/.match(l)
if !match.nil? && !$devices[$device].nil?
$devices[$device] << match[1]
end
end
$devices.each do |device1, ports1|
ports1.each do |port1|
$devices.each do |device2, ports2|
ports2.each do |port2|
# probably not a good idea to connect a port to itself
unless device1 == device2 && port1 == port2
system "aconnect #{device1}:#{port1} #{device2}:#{port2}"
end
end
end
end
end
@chmanie
Copy link
Author

chmanie commented Jan 26, 2021

@ElJimBob
Copy link

ElJimBob commented Oct 16, 2021

Thankyou for the script - it works perfectly. I'm presuming as it's written will only utilise devices with up to 2 ports. Wondering how I might alter it to use all ports on a 4 port usb midi interface..?

@chmanie
Copy link
Author

chmanie commented Oct 18, 2021

Hey @ElJimBob! I’m using it with a 4 port midi usb device and it works perfectly fine :)

@ElJimBob
Copy link

ElJimBob commented Oct 18, 2021

Interesting. The 3rd and 4th ports of my interface seem to be ignored by the script. Its a Nektar Midiflex. I can still manually connect them to devices via qjackctl though. Perhaps it's a hardware issue..?

@chmanie
Copy link
Author

chmanie commented Oct 20, 2021

Maybe they show up differently in the aconnect list that we are parsing here. Would you mind posting the output of aconnect -i -l?

@ElJimBob
Copy link

ElJimBob commented Oct 20, 2021

Sure, here -
aconnect -i -l
client 0: 'System' [type=kernel]
0 'Timer '
1 'Announce '
client 14: 'Midi Through' [type=kernel]
0 'Midi Through Port-0'
client 16: 'MIDIFlex4' [type=kernel,card=0]
0 'MIDIFlex4 MIDI 1'
Connecting To: 16:1, 20:0, 32:0, 32:1
Connected From: 16:1, 20:0, 32:0, 32:1
1 'MIDIFlex4 MIDI 2'
Connecting To: 16:0, 20:0, 32:0, 32:1
Connected From: 16:0, 20:0, 32:0, 32:1
client 20: 'Craft Synth 2.0' [type=kernel,card=1]
0 'Craft Synth 2.0 MIDI 1'
Connecting To: 16:0, 16:1, 32:0, 32:1
Connected From: 16:0, 16:1, 32:0, 32:1
client 32: 'Launchpad X' [type=kernel,card=4]
0 'Launchpad X MIDI 1'
Connecting To: 16:0, 16:1, 20:0, 32:1
Connected From: 16:0, 16:1, 20:0, 32:1
1 'Launchpad X MIDI 2'
Connecting To: 16:0, 16:1, 20:0, 32:0
Connected From: 16:0, 16:1, 20:0, 32:0

@ElJimBob
Copy link

ElJimBob commented Oct 20, 2021

Screenshot_20211020-202353__01

@chmanie
Copy link
Author

chmanie commented Nov 12, 2021

I think the problem might be that the input ports on the device do not match the number of output ports but I might be mistaken. I'd have to debug that again and try to replicate the issue. Sorry if this is taking a long time

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