HOW-TO: DIY Hybrid MIDI Thru box / USB MIDI hub using Raspberry Pi, and MIDI USB to DIN 5P adapters

in #music7 years ago (edited)

Ingredients:

Raspberry Pi, MIDI USB to DIN 5P adapters. Linux knowledge level: medium.

Raspberry Pi mess
Raspberry Pi mess

OS (Raspbian) Installation:

Raspbian is the official supported Raspberry Pi operating system based on Debian. That means you have many available packages which are dead easy to install directly while having working Internet connection. This is done using apt-get command line utility. Follow the installation guides on the official page.

Overcoming the limitations:

The default Raspbian configuration allows only 7 audio USB devices to be connected at the same time.

$ cat /sys/module/snd_usb_audio/parameters/index
-2,-1,-1,-1,-1,-1,-1,-1

This way, you can have 7x7 ins/outs MIDI thru box. Of course, you'll need powered USB hub(s) since the Pi has 4 USB ports. If you want to use the device as a MIDI hub at the same time, the number of available MIDI ports decreases with the number of other connected USB MIDI devices. For example, you can have Akai LPD8 controller, and 6 MIDI USB to DIN 5P adapters connected at the same time. To overcome this, install custom built kernel by following the instructions in this Raspberry Pi forum thread asked, and answered by me :) Anyway, I'll post the solution within this post.

Follow the instructions for building a custom kernel here:
Configuring the kernel
Kernel building

During the configuration, make sure to have this 2 lines in .config (or via menuconfig):

CONFIG_SND_DYNAMIC_MINORS=y
CONFIG_SND_MAX_CARDS=16

This ensures maximum of 15 simultaneously connected audio devices. You can enter higher number like 32. After booting the new kernel, check the maximum number of audio devices that can be connected:

$ sudo modprobe snd-usb-audio
$ cat /sys/module/snd_usb_audio/parameters/index
-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1

Putting it all together:

USB MIDI to DIN 5P adapters look like the one on the picture.

Have in mind limitation about maximum number of audio devices that can be connected. Let's say you use the default Raspbian kernel, and that you have 6 USB to MIDI DIN 5P adapters, and Akai LPD8 MIDI controller. The idea is to interconnect all USB MIDI devices regardless of their type. You need a script that will do this during the boot process. The perl script bellow can be saved within /home/pi/bin/midi-hub-start.pl file. First create the needed directories while logged in as pi user:

$ mkdir bin
$ mkdir tmp

Save the following script in /home/pi/bin directory within midi-hub-start.pl file:

#!/usr/bin/perl -w
 
use strict;
 
my $DIRTMP = '/home/pi/tmp';
system("aconnect -i | egrep \"(CH345|LPD8)\" | grep client > $DIRTMP/midi_clients_out.txt");
system("aconnect -o | egrep \"(CH345|LPD8)\" | grep client > $DIRTMP/midi_clients_in.txt");
 
my @alsa_outs = qw//;
my @alsa_ins = qw//;
 
open(my $fh_out, '<:encoding(UTF-8)', "$DIRTMP/midi_clients_out.txt") or die "Could not open file $DIRTMP/midi_clients_out.txt\n";
my $i = 0;
while (my $line = <$fh_out>) {
    chomp $line;
    my @line = split(' ', $line);
    $alsa_outs[$i] = $line[1];
    $i++;
}
 
open(my $fh_in, '<:encoding(UTF-8)', "$DIRTMP/midi_clients_in.txt") or die "Could not open file $DIRTMP/midi_clients_in.txt\n";
$i = 0;
while (my $line = <$fh_in>) {
    chomp $line;
    my @line = split(' ', $line);
    $alsa_ins[$i] = $line[1];
    $i++;
}
 
foreach my $out (@alsa_outs) {
    my $outconn = $out . '0';
    foreach my $in (@alsa_ins) {
        my $inconn = $in . '0';
        #print "CONNECT: $outconn $inconn\n";
        system("aconnect $outconn $inconn");
    }
}
 
1;

Feel free to replace CH345|LPD8 with the devices you have connected. The hint about device names can be obtained by executing:
$ aconnect -i
For example, CH345|LPD8|LPK25 will interconnect all USB MIDI to DIN 5P adapters (CH345), all Akai LPK25, and all Akai LPD8 controllers connected to the Pi.

Make the script executable:
$ chmod 755 /home/pi/bin/midi-hub-start.pl
Add this line in /etc/rc.local as root user to execute the script during the boot:
su - pi -c "/home/pi/bin/midi-hub-start.pl &"

Reboot the Raspberry Pi device, and enjoy your brand new Hybrid MIDI thru box / USB MIDI host device.

Sort:  

Congratulations @sferamusic! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You made your First Vote
You made your First Comment
You got a First Vote
You got a First Reply
Award for the number of upvotes
Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

The @OriginalWorks bot has determined this post by @sferamusic to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

Congratulations @sferamusic! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.029
BTC 68703.91
ETH 2503.79
USDT 1.00
SBD 2.54