Connect your MIDI hardware to your computer
To connect a musical instrument to your computer using its MIDI port, your computer will need a MIDI port as well. Several sound cards have such a port. These have MIDI support in their driver module and should work without much hassle.
Alternatively, you can buy a cheap MIDI-USB adapter; this is a Y-shaped cable with a USB connector on one end, to be plugged into your computer, and two MIDI connectors (MIDI IN and MIDI OUT) to be plugged into your musical instrument(s). Any modern kernel has support for such usb-midi interface.
Build your own MIDI port
A MIDI port is very simple, so you can build it yourself easily. You do need to solder a few components together.
There is a schematic at [http://www.midi.org/techspecs/electrispec.php]. If you only have one instrument, you will not need the "MIDI-THRU" part. If you only want to record your instrument, you will not need the "MIDI OUT" part. Doing things the "normal" way means you use a female 5-pin DIN connector and a standard midi cable (which connects all wires between two 5-pin mail connectors). However, if you only have one instrument, you can as well include the cable in your design and only use one male 5-pin DIN connector at the end, which plugs into your instrument's port.
So for a port to record from your instrument only, you can use:
5-pin male DIN connector, connects to a two-wire cable (preferably with twisted wires and a shield, but any other cable will also work). The shield must not be connected at the connector, the wires connect to pins 4 and 5 (note the strange numbering though). The cable is connected (through a 220 ohm resistor) to pins 2 and 3 of a 6n137 (this is not what's in the image, but it works as well). A diode is also connected to those pins.
On the other side, pins 7 and 8 are connected to a 5V power line; pin 5 is connected to the power ground and pin 6 is the signal. The signal is pulled up by connecting it with a resisor to the 5 V power. As output on the computer side, you have three wires: the 5V line, the signal line and the power ground.
The signal line must be connected to a ttl serial port (note that you need a convertor if you want to connect to a "real" rs232 serial port). The easiest way to get one is by using a usb to ttl serial convertor, such as the ones from ftdi. The good thing about those is that they also provide a 5V power output, so you don't need a power supply for that. You connect the power and ground, to the MIDI interface, and the signal line to the serial data input.
Setting up the serial port
MIDI uses 31.25 kbit per second. This is a non-standard baud-rate, and not all serial controllers may be able to use it. The ftdi port can, but you need to do things in the right order, or it will not work. Make sure you have the setserial package installed. Then run the following commands (assuming that the port you want to use is /dev/ttyUSB0, which it will be if you have no other usb serial ports connected):
setserial /dev/ttyUSB0 spd_cust setserial /dev/ttyUSB0 divisor 0 stty -F /dev/ttyUSB0 38400 setserial /dev/ttyUSB0 divisor 768
Now the port should be ready to receive data. You can test this by running cat -A /dev/ttyUSB0 and playing some notes. If everything is right, some (non-understandable) symbols should appear.
Using the MIDI data
To really use the data that comes out of the port, the system must know that it's MIDI data (as opposed to just some bytes it doesn't understand). For this, we use a trick: we shall write the data to a MIDI port, as if we want to play it (instead of record). The port we use for writing is a virtual midi port, which is internally connected to another port. For this, the snd-virmidi kernel module must be loaded (modprobe snd-virmidi as root, or add it to /etc/modules if you want to load it at system boot).
For connecting the ports, you use:
This gives you a list of all writable ports. You see that port 20 is a virtual midi port, as is 21. Those are read and write. We use them. Connect them with
aconnect 20 21
Then write the serial port data to the midi port:
cat /dev/ttyUSB0 > /dev/midiC1D0
(The 1 may be a different number on your system.)
Now you should be able to use virtual midi port 1 as your midi input port.