Monday, August 3, 2009

starting with timidity and soundfonts on Ubuntu Linux

On the command line:
sudo aptitude install timidity

I like to have a folder with my timidity configuration files and soundfonts in my home directory:

Inside that folder I'll have my "master" configuration called "timidity.cfg" and a layout something like this (I'm actually omitting a lot here):

|-- Piano.cfg
|-- ensemble.cfg
|-- harpsichord.cfg
|-- organ.cfg
|-- patches
|   |-- eawpats12_full
|   |   |-- acbass.pat
|   |   |-- [...]
|-- soundfonts
|   |-- Ensemble
|   |   |-- Zemljak_Overture.sf2
|   |   |-- choir
|   |   |   |-- 052_Florestan_Ahh_Choir.sf2
|   |   |   |-- Boychoir.sf2
|   |   |   `-- boychoir.txt
|   |-- Organ
|   |   |-- GothicOrgan.sf2
|   |   |-- jeux14.sf2
|   |   `-- jeux2.sf2
|   |-- Piano
|   |   |-- ClavinovaGrandPiano2.sf2
|   |   |-- RolandPowerGrand.sf2
|   |   |-- harpsichord
|   |   |   |-- Blanchet-440_ver.1.0.sf2
|   |   |   |-- Campbells_Harpischord_tuned_1.sf2
|   |   |   `-- Petit_Italien.sf2

Now, we can edit the "global" configuration file to point to our timidity configuration file:

sudo gedit /etc/timidity/timidity.cfg
# Instrument configuration file for timidity
# $Id: timidity.cfg,v 1.7 2005/09/03 19:26:03 hmh Exp $

# [... omitting some for brevity ...]
# [ NOTE: With a recent CPU you can leave everything commented in this file no problem ]

# Include a configuration for the selected patchset or soundfont
# By default, try to use the instrument patches from freepats:

source /etc/timidity/freepats.cfg

## added by JTP ##
source /home/john/timidity/timidity.cfg

That file basically turns on freepats and then points to your home configuration. Timidity lets you "cascade" sounds, so activating freepats will ensure that you have some kind of sound for most of the banks accessed by your midi files. Any patches/soundfonts you later specify will override those previously specified.

Here's my timidity.cfg file:

dir /home/john/timidity
# (NOTE: I just comment out the config files I'm not using)
#source jeux14.cfg
#source organ.cfg
#source merlin.cfg
#source Piano.cfg

source harpsichord.cfg

The dir command just "activates" a given directory so that I can then call the configuration files in it by their basename as shown. With this configuration, I can set up lots of preconfigured sounds that I can easily turn on and off in my "master" configuration file.

OK, so now, lets get a soundfont and use it to play some music:

The Blanchet is my favorite harpsichord soundfont at the moment (Download the sf2 file Download the sf2 file). Turns out most sf2 files are compressed with sfark, so you need to download software to decompress it. After you decompress the soundfont, you'll want to look at the soundfont. Linux has a soundfont editor called "swami" that lets you see what's in a soundfont:

# only in Ubuntu 9.04 or earlier !!
sudo aptitude install swami
# you can fire it up with the soundfont on the command line if you want (or without)
swami timidity/soundfonts/Piano/harpsichord/Blanchet-440_ver.1.0.sf2
At least for Karmic and Lucid (9.10) you will need to follow instructions here to install swami. Some guy is working to get GTK2.0 integration at which point we'll probably see the package surface again.

We are interested in the little numbers in the Melodic section: (001-001 ... 001-004). The code is this: <bank>-<instrument>. That means that for this soundfont the 4 "sounds" are in bank 1 and are deposited as instrument 1-4. OK, so now we know the layout of the sound font. Now, when we play a midi file, we can map these to the appropriate instruments to get the sounds we want. So, how do we do that? We do it in timidity configuration files. My "harpsichord.cfg" file that is referenced by my timidity.cfg file looks like this:

dir /home/john/timidity/soundfonts/Piano/harpsichord

bank 0

6 %font Blanchet-440_ver.1.0.sf2 1 1
7 %font Blanchet-440_ver.1.0.sf2 1 2
8 %font Blanchet-440_ver.1.0.sf2 1 3
9 %font Blanchet-440_ver.1.0.sf2 1 4

Again, the "dir" command "activates" the directory that contains my Blanchet-400_ver.1.0.sf2 soundfont file so I can access it in my file by its base filename. The bank 0 is now specifying which instrument sounds we are going to provide for midi files that access bank 0 (which is most of them since bank 0 is the main bank).

<midi_instrument> %font <soundfont.sf2> <soundfont_bank> <soundfont_instrument>

midi_instrument: the program change found in the midi file. The General Midi (GM) typical mapping (note: timidity starts numbering at '0', so you need to subtract '1' from each of these to specify them in timidity).

%font: means this is a soundfont (patches don't require it), then .

soundfont_bank: the bank the instrument is found in your soundfont
soundfont_instrument: the instrument as found in your soundfont

So, here I've mapped the first harpsichord manual (bank 1, instrument 1) to instrument 6 ("harpsichord" in GM), the second harpsichord manual (bank 1, instrument 2) to instrument 7 ("clavinet"), the lute harpsichord sound (bank 1, instrument 3) to instrument 9 ("Celesta") and the sound with both harpsichords (bank 1, instrument 4) to instrument 10 ("Glockenspiel"). Now, a midi file that has a "program change" (instrument) specifying a harpsichord will access my first harpsichord manual and so on.

If a midi file is just one instrument, just match up the soundfont sound with the instrument specified in the midi file. If you have different midi tracks, you can go in and change the midi track to match up with those in your timidity file. This can be done in rosegarden (sudo aptitude install rosegarden) or many other midi editors (I write my own ruby programs based on midilib to modify instruments, volumes, pan, in my midi files but these (pan and volume) can also be specified in your timidity file (as long as each midi track accesses a different instrument). For example:

19 %font FluidR3GM.SF2 0 19 amp=140 pan=0 

Now we can fire up timidity on our midi file (here are some good ones). These each have different numbers of tracks, but each track is for "harpsichord" (which we've pointed to the first soundfont of the Blanchet file in harpsichord.cfg file):

timidity <your_midi>.mid
# or what I often will use:
timidity --verbose  <your_midi>.mid -EFreverb=3,40 -A150
# once you have it sounding like you want, you can record to a .wav file:
timidity <your_midi>.mid -Ow
# then you can convert to an mp3 with lame (install with: 'sudo aptitude install lame')
lame --preset medium <your_midi>.wav <your_midi>.mp3

Let me know if you have any questions/troubles and I'll try to update this post to answer them.