Enabling Note Track playback on Linux

From Audacity Wiki
Jump to: navigation, search

MIDI playback on Linux with TiMidmidity++ software synthesizer

TiMidity++ is a software MIDI player and synthesizer, that can be used by Audacity to render MIDI output as audio.

This guide has instructions for Ubuntu 16.04, though the necessary steps for other distributions will often be very similar.

Warning icon These instructions assume that the audio system uses PulseAudio. These instructions must not be used if you use Jack Audio System because running TiMidity++ as described here will interfere with Jack server and may prevent the Jack server starting at all. If using Jack Audio System, QSynth is likely to be a better option than TiMidity++.


Install TiMidity++ (MIDI player/synth), freepats (Sound bank) and pmidi (command line MIDI utility).

sudo apt-get install timidity freepats pmidi

Setting Up

By default, TiMidity will launch on log-in and run as a service (daemon), but because it is launched by root, it tries to access ALSA directly, which will conflict with PulseAudio. We need TiMidity to run with user permissions so that it can use PulseAudio (the default sound system on most Linux distributions). PulseAudio allows multiple applications to share the same audio device, which is what we need so that both Audacity and TiMidity can play audio.

First we need to disable TiMidity-daemon from starting:

For Ubuntu 16.04: Open /etc/default/timidity in a text editor with root permissions. For example (assuming that gedit text editor is installed):

sudo gedit /etc/default/timidity

and change:




Then, to launch TiMidity automatically after log-in:

  1. Launch "Startup Applications" (Ubuntu) or "Session and Startup" (Xubuntu)
  2. Create a new Application Autostart item containing the command:
timidity -iA

Then reboot.

Bulb icon "Session and Startup" for the XFCE desktop only handles startup commands. It does not provide a means to end scripts on logout. This means that if you log out and back in again, there will be two instances of Timodity because it is started on login but not closed on logout. The simplest solution is to manually kill Timidity before logging out, which may be done at the command line with:
  killall timidity

See below for a way to automatically stop Timidity when logging out from XFCE.


Open a terminal window and enter:

pmidi -l

The output should look similar to:

 Port     Client name     Port name
 14:0     Midi Through    Midi Through Port-0
128:0     TiMidity        TiMidity port 0
128:1     TiMidity        TiMidity port 1
128:2     TiMidity        TiMidity port 2
128:3     TiMidity        TiMidity port 3

Navigate to a folder that contains a MIDI file and enter:

pmidi -p 128:0 "name_of_midi_file.mid"

(Ctrl+C to quit)

While TiMidity is playing, launch PulseAudio Volume Control (pavucontrol) and ensure that TiMidity is playing through PulseAudio (the Playback tab should list "ALSA plug-in [timidity]:")

Warning icon There may be crackles when MIDI playback starts. This is due to a bug in TiMidity 3.13.

For more information, see: https://bugs.launchpad.net/ubuntu/+source/timidity/+bug/1587215 A fix is available here: http://martinwguy.co.uk/ubuntu/ which may be included when Ubuntu / Debian next update TiMidity++

Playing a MIDI file with Audacity

  1. Launch Audacity
  2. Import a MIDI (.MID) file
  3. Open Preferences and ensure that one of the TiMidity ports is selected for playback.
  4. Close preferences.
  5. Start playback.

Optional steps for better MIDI sounds

The "freepat" sound bank is a fairly small, free sound bank. Most Linux distributions also have a much larger GM (general midi) sound bank available called "Fluid3_GM". To install:

sudo apt-get install fluid-soundfont-gm

To configure TiMidity to use Fluid3_GM

sudo gedit /etc/timidity/timidity.cfg

Scroll down near the bottom of the file and change:

source /etc/timidity/freepats.cfg


# source /etc/timidity/freepats.cfg

and from:

#source /etc/timidity/fluidr3_gm.cfg


source /etc/timidity/fluidr3_gm.cfg

Save the changes, then log out and back in again.

Warning icon Note that "better" is subjective. For some MIDI files, the freepat sound bank may sound better than Fluid GM sound bank.

Timidity with XFCE

The XFCE application "Session and Startup" is a convenient way to start Timidity (or other applications) automatically on logging in, but at time of writing (XFCE 4.12) it does not provide a means to stop the application or script on logout. For some applications (including Timidity), this has the downside that logging out of the XFCE session and back in again will cause a new instance of the application to be launched in addition to the original instance.

To avoid starting multiple instances of Timidity, the application may simply be killed before logging out with the command line:

killall timidity

A more convenient way to stop Timidity on logging out, is to add a script to override the default xfce4-session behavior. To do this we create a bash script called /usr/local/bin/xfce4-session which will run when logging into XFCE, and the script will launch the standard /usr/bin/xfce4-session application, allowing us to add additional commands before starting or after ending the XFCE session.

Assuming MousePad is the text editor (if you use a different text editor, substitute the name of your editor in place of "mousepad")

sudo mousepad /usr/local/bin/xfce4-session

Then in the editor, add the following:


### Uncomment the next line to test that this script runs on logon.
### To uncomment, remove the '#' from the beginning of the line.
#echo "Login  : `date`" >> /tmp/testfile

### Additional commands to run on login may be added here

### Run the normal xfce-session 

### Uncomment the next line to test that this script runs on logout.
#echo "Logout : `date`" >> /tmp/testfile

### Additional commands to run on logout may be added here:
killall timidity

Finally, save the script and make it executable:

sudo chmod a+x /usr/local/bin/xfce4-session