MIDI DIY interface

You can use the serial interface (UART) of the PI for "talking MIDI" with: The PI's UART can send/receive on TXD0=GPIO14 and RXD0=GPIO15 respectively.
Set the USE_SERIALPORT_MIDI parameter to TRUE to enable it.

See next paragraphs for some tech details regarding the standard midi wiring and the DIN cables.

MIDI Interface Bus

MIDI 5-pin DIN connector Pinout MIDI defines the electrical and physical interface over a 5-pin DIN connector, on a 64 ohm cable. See Philip Rees docs for more tech details.

Pins 1 and 3 are never needed. Pin 2 is used for the shield, in order to ground the cable. That leaves the other two pins to carry an isolated current loop.

MIDI out connectionMIDI in connection
pin 4 = -ve or sinkpin 4 = +ve or source
pin 5 = +ve or sourcepin 5 = -ve or sink
The pins are exchanged because the pins on a male plug are flipped relative to the sockets on a female connection. So, when all is said and done, a positive signal will travel continuously through the positive wire.

This is why all devices (instruments, controllers) have female sockets. And all cables (except those explicitly designed as extension cables) have male plugs. This ensures the correct electrical contacts without having to label ends.

IC Circuitry for the MIDI In, MIDI Thru and MIDI Out Interfaces

The full specs in CA33.pdf of the MIDI association drill down to next more simplified circuits:
MIDI Circuit diagrams

Actual 5-pin DIN chassis socket

Female plug     Female wiring

The TRS connector