Sunday, May 31, 2009

Play MIDI with fluidsynth in my EeePC

A few days ago, I read in the EeeUser Forum someone saying fluidsynth plays MIDI better than timidity on his EeePC. As I have reported in this thread, timidity is the way I used to play MIDI in my EeePC. Now that I have installed a fresh new 1.6.1 verson EeePC Linux to my 4G, I would like to try out something new. So I take this opportunity to test on installing fluidsynth to play MIDI.

Setting up Debian etch repositories

fluidsynth is available from the Debian repositories. So add this line (as root) to your /etc/apt/sources.list
deb http://ftp.debian.org/debian/ etch main non-free contrib
It seems that recently Debian mirrors have extended their PGP keys even to etch (oldstable) repositories, so if you don't want to see the system complain, you will need to install the PGP key as follows:
/home/user> gpg --keyserver wwwkeys.eu.pgp.net --recv-keys 9AA38DCD55BE302B
gpg: directory `/home/user/.gnupg' created
gpg: can't open `/gnupg/options.skel': No such file or directory
gpg: keyring `/home/user/.gnupg/secring.gpg' created
gpg: keyring `/home/user/.gnupg/pubring.gpg' created
gpg: requesting key 55BE302B from hkp server wwwkeys.eu.pgp.net
gpg: /home/user/.gnupg/trustdb.gpg: trustdb created
gpg: key 55BE302B: public key "Debian Archive Automatic Signing Key (5.0/lenny) " imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
/home/user> gpg --armor --export 9AA38DCD55BE302B | sudo apt-key add -
OK

Installing fluidsynth

Then, you can update apt cache and proceed to install fluidsynth.
/home/user> sudo apt-get update
/home/user> sudo apt-get install fluidsynth
Reading package lists... Done
Building dependency tree... Done
The following extra packages will be installed:
ladcca2 libfluidsynth1
The following NEW packages will be installed:
fluidsynth ladcca2 libfluidsynth1
0 upgraded, 3 newly installed, 0 to remove and 226 not upgraded.
Need to get 0B/215kB of archives.
After unpacking 606kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Selecting previously deselected package ladcca2.
(Reading database ... 72482 files and directories currently installed.)
Unpacking ladcca2 (from .../ladcca2_0.4.0-6_i386.deb) ...
Selecting previously deselected package libfluidsynth1.
Unpacking libfluidsynth1 (from .../libfluidsynth1_1.0.7a-1_i386.deb) ...
Selecting previously deselected package fluidsynth.
Unpacking fluidsynth (from .../fluidsynth_1.0.7a-1_i386.deb) ...
Setting up ladcca2 (0.4.0-6) ...

Setting up libfluidsynth1 (1.0.7a-1) ...

Setting up fluidsynth (1.0.7a-1) ...

/home/user>
I have also tried installing qsynth, a GUI to fluidsynth, but it is not particularly interesting. So, I just work with fluidsynth on the command line.

Setting up fluidsynth to play MIDI

Before you can get fluidsynth to play MIDI files to alsa, you first need to load the alsa MIDI sequencer kernel module.
/home/user> sudo modprobe snd-seq-midi
A better idea is to add "snd-seq-midi" to your /etc/modules file so that the system will automatically load it at start up.
/home/user> sudo bash
asus-1624754341:/home/user> echo snd-seq-midi >> /etc/modules
Now you will need to download a sf2 soundfont for fluidsynth to play MIDI. After all, fluidsynth is just a soundfont player.

There are plenty of sf2 you can download from the web. The one I am using is 5MBGMGS.sf2 - not too heavy for the EeePC, but acceptable quality.

Now you are ready to play MIDI with fluidsynth. Of course, you will also need a MIDI file, and the one I use to test MIDI players is the beautiful As Time Goes By in piano.
/home/user> fluidsynth -ni 5MBGMGS.sf2 AsTimeGoesBy.mid

Setting up File Association in File Manager

To set up File Association in File Manager to play MIDIs with fluidsynth, you will have to create a kde desktop shortcut for fluidsynth:
home/user> sudo touch /usr/share/applications/fluidsynth.desktop
home/user> sudo kwrite /usr/share/applications/fluidsynth.desktop &
Paste the following content into the file:
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=MIDI Player
GenericName=fluidsynth MIDI player front-end
Exec=fluidsynth -ni /home/user/5MBGMGS.sf2 %f
Comment=fluidsynth MIDI soundfont player front-end
Icon=amarok
MimeType=audio/x-midi
Terminal=false
Categories=Qt;KDE;AudioVideo;Player;
Then double-click on a MIDI file in File Manager, and select "Open with": /usr/share/applications/fluidsynth.desktop

Also, remember to check the box "Remember application association..." Then, clicking on a MIDI file will have fludisynth playing it in the background.

Comparing fluidsynth and timidity

But I found that, unlike timidity, playing MIDI with fluidsynth is often affected by running of other applications. Comparing the "top" output running the 2 programs reveal that fluidsynth is using about 80-100% CPU usage, whereas timidity only use 17% at max.


So, for my 4G, timidity is obviously the better choice.

Setting up qsynth

For those who would like to use qsynth as GUI interface to fluidsynth for easy setting of Reverbs, chorus effects, etc. Here is how to set it up:

From the "Audio" tab, select "alsa" as Audio Driver.

Load a sf2 soundfont file from the "Soundfonts" tab. Then click [OK].

This error is before I loaded the "snd-seq-midi" kernel module.

1 comment:

  1. I suspect that the high CPU usage of Fluidsynth is related to the large soundfonts that it loads. The soundfonts on mine are over 160mb, which is quite a lot of data to load and prepare in memory.

    If you leave Fluidsynth running as a server daemon and send MIDI files to it, the CPU usage might be lower. However, I haven't figured out how to do this (without 3rd party programs like aplaymidi which don't seem to work on my Mac).

    It's a pity you can't start Fluidsynth as a server, then call a second instance and tell it "send this MIDI file to the running server".

    ReplyDelete