Using TiMidity++ with ALSA raw MIDI
From Allegro Wiki
This information is very Linux-specific, so if you are not using Linux it doesn't apply.
There is a software synthesiser called TiMidity++ which convert MIDI's to PCM data. It is useful if your sound card has no (good) hardware support for MIDIs, or the hardware is there but is not supported by the ALSA driver. TiMidity++ can hook up to the ALSA sequencer API so that any program which is able to play MIDIs using the ALSA sequencer API will then play through TiMidity++.
Allegro has something like TiMidity++ built in, called the DIGMID driver, but it is not very good. We would like to use TiMidity++ to play MIDIs from Allegro programs. The Unix port of Allegro has a MIDI driver which uses the ALSA "Raw MIDI" API, which is not the same as the sequencer API. However, it is possible to connect the Raw MIDI API to the sequencer API, and then you can have Allegro playing MIDIs through TiMidity++.
Here's how. I will assume you have TiMidity++ installed on your system, with an appropriate patch set (soundfont). Have a look around on the net if you have not got that far.
First we make sure the appropriate kernel modules are loaded. As root:
modprobe snd-seq snd-virmidi
Next we start timidity. You may want to use different parameters, but the important thing is the
This will launch timidity has an ALSA sequencer client.
timidity -iAqqq -c /usr/share/soundfonts/Unison.cfg -Os1S -s 44100 &
The previous command will open a few sequencer ports, e.g. 128:0. Let's check that it works.
aplaymidi -p 128:0 file.mid
Now find the first Virtual Raw MIDI port:
On my system this gives the following, so I want
72:0. The other ports would also work.
client 0: 'System' [type=kernel] 0 'Timer ' 1 'Announce ' client 62: 'Midi Through' [type=kernel] 0 'Midi Through Port-0' client 64: 'MPU-401 MIDI 0-0' [type=kernel] 0 'MPU-401 MIDI 0-0' client 72: 'Virtual Raw MIDI 1-0' [type=kernel] 0 'VirMIDI 1-0 ' client 73: 'Virtual Raw MIDI 1-1' [type=kernel] 0 'VirMIDI 1-1 ' client 74: 'Virtual Raw MIDI 1-2' [type=kernel] 0 'VirMIDI 1-2 ' client 75: 'Virtual Raw MIDI 1-3' [type=kernel] 0 'VirMIDI 1-3 '
Connect the Virtual Raw MIDI port to the Timidity port
aconnect 72:0 128:0
Find the device name of the Virtual Raw MIDI port. Since I went with the first Raw MIDI port, the device name I want is
Device Name hw:0,0 MPU-401 MIDI 0-0 hw:1,0 Virtual Raw MIDI (16 subdevices) hw:1,1 Virtual Raw MIDI (16 subdevices) hw:1,2 Virtual Raw MIDI (16 subdevices) hw:1,3 Virtual Raw MIDI (16 subdevices)
Finally we tell Allegro to use that device in
[sound] ... alsa_rawmidi_device = hw:1,0
Do NOT put "quotes" around
hw:1,0. You may also want to check the the ALSA Raw MIDI driver has been compiled into your copy of Allegro and perhaps forcebly select it in the config file. With a bit of luck (or perseverence),
exmidi should play MIDIs through TiMidity. The only problem left is the latency...