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:
/home/<user>/timidity


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):

timidity
|-- 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.

10 comments:

Unknown said...

Thanks for this...it's a really good guide to a program that even by Linux standards is a bit shrouded in mystery. Do you know how to add percussion to .cfg file?

JTP said...

Nicholas, I've never actually used drums before with timidity (so far just for classical).

I've found that "MIDI Channel 10 is for percussion" so that might help.

Opening up the gsdrums.cfg in eawpats sound patch I see a structure like this:

# Room drums
drumset 8

36 gsdrum08/roomkick pan=center note=36
41 gsdrum08/roomlow2 pan=-63
43 gsdrum08/roomlow1 pan=-40
# ... etc

# Power
drumset 16

35 power/powrkic1 pan=center amp=150 note=36
36 power/powrkic3 pan=center amp=150 note=36
37 power/h-rim pan=center

Maybe that will be enough to figure it out. I would love to hear how it works if you figure it out and will certainly post it inside the main post so others can benefit.

Graham said...

Hey, thanks for this, I have been scratching my head for days trying to work out how to to switch in a particular instrument soundfont. This did the trick.

Still a few things. I want to install the FluidR3_GS.sf2 soundfont. Do I follow you correctly that I need a .cfg file to map out all the instruments appropriately? Short of doing this by hand, I assume that I should be able to get a cfg download from somewhere? Help?

Finally my quest is to make a wind controller, I am learning that being able to model the notes well will delve into the realms of expensive software but for the moment I am looking for the best sound font I can. I have one, but its no inspiring. Any leads you can offer? Thanks
Graham

JTP said...

@graham_20705

>> Do I follow you correctly that I need a .cfg file to map out all the instruments appropriately?

yes, timidity has to have a config file that already 'knows' about the soundfont.

>> Short of doing this by hand, I assume that I should be able to get a cfg download from somewhere?

For many free soundfonts I think various folks have put together config files. For FluidR3_GS.sf2 see sites such as http://cr.opensolaris.org/~mcermak/webrev/raw_files/new/usr/src/cmd/fluid-sf/fluidr3_gs.cfg

If one isn't already made, you do have to go in by hand and create the config file. Using swami (or similar) it isn't too difficult. I poked around swami to see if I could output some kind of text file describing the soundfont which could be used to generate a config file automatically. No luck. Does anyone know how to programmatically get instrument banks and patches out of a soundfont? I'd be very interested in a solution. Someone could alter the swami code to do this. Or, if soundfont formats are fairly straightforward, one could decipher and 'unpack' the binary using ruby/perl/python.

>> the best sound font ... Any leads you can offer?

I've collected a variety of free sound fonts over the years but haven't played around with them enough to recommend a single one. So far, I have tried to stick with one instrument at a time (rather than the compilations). merlin_plus21 seems pretty good. Anyone else have a recommendation?

Best of luck on your project!

JTP said...

Just for reference, here is the list of soundfonts I've collected. Quality varies a lot. If you can't find one of these by googling for it, send me an email and I will email it (them) to you. jtprince [at] thedomain.com (where thedomain.com is actually gmail.com)

RolandSuperTenor.sfpack
_Compilations/dx9.sf2
_Compilations/MagicSFver2.sf2
_Compilations/Unison.sf2
_Compilations/PC51d.sf2
_Compilations/merlin_plus21.sf2
Reed/RolandBassoon.sfpack
Reed/ClavinovaPanFlute.sfpack
Ensemble/choir/Boychoir.sfpack
Ensemble/choir/052_Florestan_Ahh_Choir.sfpack
Ensemble/Zemljak_Overture.sfpack
Ensemble/smlorch.sfpack
RolandPlayersEP.sfpack
Organ/K4Organs.sf2
Organ/Jeux_14.sfpack
Organ/StefansCathedralPipeOrgan.sf2
Organ/JJs_Eng_Chamber_Organ1.1.sf2
Organ/englishOrgan102.sf2
Organ/jeux2.sf2
Organ/GothicOrgan.sf2
Organ/jeux2.sfpack
Organ/Orgue.sf2
Organ/dsika_Ob-3.sf2
Organ/jeux14.sf2
Organ/Open_Diapason_Pipe_Organ.sf2
Organ/Roland_rock_organ.sf2
RolandRd-1000.sfpack
Piano/RolandXP-80Piano.sfpack
Piano/SC55_Piano_s.sf2
Piano/pianissimum.sfpack
Piano/harpsichord/Campbells_Harpischord_tuned_1.sf2
Piano/harpsichord/MT_Clavicembalo884.sf2
Piano/harpsichord/German8.sfpack
Piano/harpsichord/Blanchet/Blanchet-1720.sfArk
Piano/harpsichord/German8.sf2
Piano/harpsichord/Petit_Italien.sf2
Piano/harpsichord/McCoy_Harpsichord.sf2
Piano/harpsichord/Blanchet-440_ver.1.0.sf2
Piano/harpsichord/Leonhart.sf2
Piano/harpsichord/Leonhart.sfpack
Piano/harpsichord/MT_Clavicembalo884.sfpack
Piano/harpsichord/Campbells_Harpischord_tuned_1.sfpack
Piano/RolandNicePianoV2_5.sfpack
Piano/RolandNicePiano_v2.sfpack
Piano/pianissimum.sf2
Piano/ClavinovaGrandPiano2.sfpack
Piano/RolandPowerGrand.sf2
Piano/SteinwayBright1.2.sfpack
Piano/Roland_64VoicePiano.sfpack
Piano/SC55_Piano_s.sfpack
Piano/RolandPowerGrand.sfpack
Piano/ClavinovaGrandPiano2.sf2
Brass/RolandBassClarinet.sfpack
Brass/RolandTrumpet1.sfpack
Brass/RolandOrchestralBrass.sfpack
Brass/ClavinovaSopranoSax.sfpack
StringsAndOrchestra/CadenzaStrings.sfpack
StringsAndOrchestra/RolandViennaStrings.sfpack
StringsAndOrchestra/stavi_violin.sfpack
StringsAndOrchestra/TronStrings75.sfpack
StringsAndOrchestra/SuspStrgs.sfpack
StringsAndOrchestra/040_violin/RolandViolinSolo.sfpack
StringsAndOrchestra/BHCello.sfpack
StringsAndOrchestra/RolandFastAttacks.sfpack
StringsAndOrchestra/FaithlessPizzicato.sfpack
StringsAndOrchestra/CampbellsStrings.sfpack
StringsAndOrchestra/RolandPizzicatoStrings.sfpack
StringsAndOrchestra/ClavinovaHarp.sfpack
StringsAndOrchestra/RolandMarcatoStrings.sfpack

Markus W. Kropp said...

Thanks for this very-good-explained guide!

Unknown said...

First of all: thank you for this wonderful howto. I set up a configuration along your lines, and it seems to work very well, just tested it. I wonder if you could help me with the following: I just bought a USB MIDI keyboard and would like it to sound like the Blanchet-1720 soundfont. But so far it only sounds like a piano. That means that the connection works. But I am unable to say if I need to configure the keyboard now to use the Blanchet soundfont, and unfortunately the manual is written in terrible English, so it is not of much help. It is a pretty simple keyboard without bells and whistles and, it seems, without anything preinstalled on it.

I would greatly appreciate any hint

Pat said...

Is there an option in the config file of Timidity to boost the volume of the fingered bass for example ? I use the Shan's soundfont (SGM-V2.01.sf2). How I can do it ?

Patrick

Pat said...

Is there an option in the config file of Timidity to boost the volume of the fingered bass for example ? I use the Shan's soundfont (SGM-V2.01.sf2). How I can do it ?

Patrick

JTP said...

@Pat

This example assumes the fingered base is in bank #1, instrument #3 and you are mapping this to midi-instrument #8 [the numbers are just made up for the example, of course]. You can add lots of modifiers to each midi-instrument line. You want the "amp" modifier:

8 %font SGM-V2.01.sf2 1 3 amp=130

See http://manpages.ubuntu.com/manpages/hardy/man5/timidity.cfg.5.html for more modifiers.