Upto: Table of Contents of full book "Programming and Using Linux Sound"

User level tools


Sound fonts

The tools described in this chapter each include a software synthesizer. This produces audio as PCM data from the MID data fed to it. The MIDI data contains information about the instrument playing each note, and of course, every instrument sounds different. So the synthesizer must make use of mapping information from MIDI notes + instrument into PCM data.

The mapping is usually done using soundfont files. There are various formats for this, of which the primary one is the .sf2 format. Some synthesizers (such as Timidity) can also use Gravis UltraSound patches, which are recorded real instruments.

Many soundfont files have been created. See for example Links to SoundFonts and other similar files (although many of the links are broken).

Possibly surprisingly, using different soundfonts doesn't seem to make much difference to CPU usage - for FluidSynth they all use about 60-70% CPU on one song. They do, of course, sound different.


Timidity is a "Software sound renderer (MIDI sequencer, MOD player)"

Its home page is Maemo.org

Timidity can be used to play MIDI files by giving them on the command line

	  timidity rehab.mid

The default soundfonts used by Timidity are Gravis UltraSound patches, from the /usr/share/midi/freepats/ directory. These soundfonts are missing many instruments, so should be replaced by another such as the FluidSynth fonts. The settings are made in the configurations file /etc/timidity/timidity.cfg

TiMidity as a server

It can also be run as an ALSA server listening on a port (Using MIDI with UNIX):

	  timidity -iAD -B2,8 -Os1l -s 44100
The "-iAD" option runs it as a Daemon process in the background as an ALSA sequencer client. The "-B2,8" option selects the number of buffer fragments. The "-Os1l" option selects ALSA output as PCM. The "-s" option is the sample size. (For the Raspberry Pi, I found that -B0,12 worked better than -B2,8).

In this mode, ALSA can send messages to it. The command

	  aconnect -0
will show output such as
	  client 14: 'Midi Through' [type=kernel]
	  0 'Midi Through Port-0'
	  laptop:/home/httpd/html/LinuxSound/MIDI/Python/pyPortMidi-0.0.3$aconnect -o
	  client 14: 'Midi Through' [type=kernel]
	  0 'Midi Through Port-0'
	  client 128: 'TiMidity' [type=user]
	  0 'TiMidity port 0 '
	  1 'TiMidity port 1 '
	  2 'TiMidity port 2 '
	  3 'TiMidity port 3 '
The "Midi Through" port is not useful but the Timidity ports are. MIDI files can then be played by an ALSA sequencer such as
	  aplaymidi -p128:0 rehab.mid

Setting Timidity output device

The default output for TiMidity can be changed using the -O option. TiMidity help (timidity -h) shows, for example,

	  Available output modes (-O, --output-mode option):
	  -Os          ALSA pcm device
	  -Ow          RIFF WAVE file
	  -Or          Raw waveform data
	  -Ou          Sun audio file
	  -Oa          AIFF file
	  -Ol          List MIDI event
	  -Om          Write MIDI file
	  -OM          MOD -> MIDI file conversion

For some of these modes the device name can also be set, using the -o option. For example, to play a file using the hw:2 ALSA device, use

	  timidity -Os -o hw:2 ... 

TiMidity and Jack

TiMidity can be run with Jack output using the -Oj option. In a user-based environment such as Ubuntu you may need to stop or pause PulseAudio, start the Jack server and then run TiMidity. PulseAudion may be paused by e.g.

pasuspender cat
in one terminal. In another, start the Jack daemon using ALSA input and output
jackd -dalsa
In a third terminal, run TiMidity
timidity -Oj 54154.mid

The links may be shown graphically by also running qjackctl.


"Rosegarden is a well-rounded audio and MIDI sequencer, score editor, and general-purpose music composition and editing environment"

Its home page is RoseGarden

It is not a standalone synthsesizer: it uses FluidSynth, for example.


GStreamer allows you to build "pipelines" that can be played using gst-launch. It can play MIDI files by e.g.

	  gst-launch filesrc location="rehab.mid" ! decodebin ! alsasink


fluidsynth is a command line MIDI player. It runs under ALSA with a command line

	  fluidsynth -a alsa -l <soundfont> <files...>
The soundfont is set explicitly on the command line, so can be set to another soundfont.

qsynth is a GUI interface to fluidsynth.

You can use FluidSynth to convert MIDI files to WAV files by

	  fluidsynth -F out.wav /usr/share/sounds/sf2/FluidR3_GM.sf2 myfile.mid

Fluidsynth as a server

Fluidsynth can be run as a server in the same way as TiMidity. Use

fluidsynth --server --audio-driver=alsa /usr/share/sounds/sf2/FluidR3_GM.sf2
Then a connect -o will show the ports and it can be played to by e.g.
amidi -p 128:0 <midi-file>


The aim of this sequencer/synthesizer is to be small. It succeeds at this!.


On playing the same song with the different systems, I observed the following CPU patterns:

Timidity + pulseaudio (with GUS or SF2 soundfonts)
12-20% CPU
FluidSynth + pulseaudio
65-72% CPU
6% CPU
15-20% CPU


VLC is a general purpose media player. There is a VLC module to handle MIDI files using FluidSynth. To get this working on a Debian system you first need to intall the vlc-plugin-fluidsynth package. Then in Advanced Options of VLC, choose Codecs-Audio Codecs-FluidSynth. You will need to set the soundfont, eg. to /usr/share/sounds/sf2/FluidR3_GM.sf2.

Copyright © Jan Newmarch, jan@newmarch.name
Creative Commons License
"Programming and Using Linux Sound - in depth" by Jan Newmarch is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://jan.newmarch.name/LinuxSound/.

If you like this book, please contribute using Flattr
or donate using PayPal