The TiMidity Howto - Using TiMidity as the ALSA sequencer client

Document written by Takashi Iwai <tiwai_at_suse.de>
Document prepared by Patrick Shirkey <pshirkey_at_boosthardware.com>

What is TiMidity?

TiMidity allows you to use MIDI via the ALSA sequencer system without a midi soundcard. The ALSA sequencer interface is the interface of TiMidity which lets you create ALSA sequencer ports with real-time response.

Notes:
The ALSA sequencer patch was already merged to TiMidity++ main tree. Get the latest source from here.

Getting it working

Compiling
For enabling this interface, configure TiMidity with

./configure --enable-audio=alsa --enable-alsaseq --enable-gtk;make;make install

Installing Sound fonts
To use TiMidity you need a working timidity.cfg config file and some sound fonts. Copy this compressed file into the directory you intend to store all your midi soundfonts. For example /usr/local/share/

Now uncompress the file.

        % tar -zxf  timidity.tar.gz 
If you have put the file in a different directory to the default you will need to change this line in ~/timidity/timidity.h then recompile TiMidity.

	/* You could specify a complete path, e.g. "/etc/timidity.cfg", and
  	then specify the library directory in the configuration file. */
	#define CONFIG_FILE "/etc/timidity.cfg" 
	#ifndef CONFIG_FILE

For example:

	/* You could specify a complete path, e.g. "/etc/timidity.cfg", and
   	then specify the library directory in the configuration file. */
	#define CONFIG_FILE "/art/1music/midi/instruments/timidity.cfg" 
	#ifndef CONFIG_FILE

If you use the eawpats soundfonts provided with this guide then you will also need to edit timidity.cfg file to point to the correct location.

Change:

	##############################################################################
	# If you've moved things around since compiling, uncomment and edit this to
	# point to the library directory: 
	#
	dir c:\timidity

To:

	##############################################################################
	# If you've moved things around since compiling, uncomment and edit this to
	# point to the library directory: 
	#
	dir dir-where-you-unpacked-the-eawpats-files/instruments/

Starting
The ALSA seq interface is invoked when -iA option is specified to timidity. Eventually the -Bx,y option is required for reduction of latency, where x is the fragments and y is the bits of audio buffer size. In both cases, smaller numbers result in faster real-time response. Typically, -B2,8 is enough. This specifies two fragments with 256 samples.

        % timidity -iA -B2,8 -Os -EFreverb=0 
In this example, the reverb effect is disabled, because it eats lots of CPU power. You may a may also turn off chrous effect via -EFchorus=0 if less CPlso turn off chrous effect via -EFchorus=0 if less CPU usage is preferred.

Then you'll get an output like this:

        Timidity starting in ALSA server mode
        Opening sequencer port 128:0 128:1
This shows that timidity created ALSA sequencer ports 128:0 and 128:1.

To load with a gtk gui

	% timidity -iA -B2,8 -Os -EFreverb=0 -ig
Playing music
For hearing MIDI music, run pmidi to send events to this port:

        % pmidi -p128:0 foo.mid
Yes, it's an overkill :-) (Originally, you can listen to MIDI music directly via timidity, of course.)

	% timidity -iA -B2,8 -Os -EFreverb=0 -ig  ~/*.mid 
Using a Keyboard
For connecting MIDI keyboard input to timidity,

        % aconnect 64:0 128:0
This interface tries to reset process priority as SCHED_FIFO and as high as possible. This is allowed only for root. Thus you need to invoke timidity as root or install the timidity binary with setuid root. The SCHED_FIFO'd program shows much better real-time response.

Timidity loads instruments dynamically at each time a PRM_CHANGE event is received. This causes sometimes pauses during playback. It occurs often in the playback via pmidi. Furthermore, timidity resets the loaded instruments when the all subscriptions are disconnected. Thus for keeping all loaded instruments also after playback is finished, you need to connect a dummy port (e.g. midi input port) to timidity port via aconnect:

        % aconnect 64:0 128:0
If you prefer a bit more fancy visual output, use my tiny program, aseqview (see below).

        % aseqview -p2 &
Then connect two ports to timidity ports:

        % aconnect 129:0 128:0
        % aconnect 129:1 128:1
The outputs ought to be redirected to 129:0,1 instead of 128:0,1.