Getting Started with MIDI on Linux

Like many computer people, I tinker around with music. I noodle around on the guitar, and sometimes want to lay down a bass track for accompaniment, or transcribe some music. When I bought a MIDI keyboard for the family a few years ago, I took the easy option and picked up a Windows music-editing application, but this always rankled. However, like many Linux users, I found that MIDI didn't seem to work on my system; and with lots of other things to work on, figuring out MIDI became a fairly low priority. But I finally found time to get it all sorted out, and now the musicians in our family are enjoying synthesized drums and other backing as they practice.

Whenever I investigated MIDI and read articles on various web sites, I always felt as though I'd come in half-way through a movie; everyone else seemed to know what was going on, but I was completely lost. In this article, I'll try to provide an overview that puts the details (found on various referenced websites) into perspective and gets you going fairly easily. The goal here is to get a minimal working setup which you can use as a basis for further exploration.

Step one: get some decent audio drivers.

ALSA Sound System

With the advent of the Linux 2.6 kernel, ALSA (the Advanced Linux Sound Architecture, http://www.alsa-project.org/) has become the standard sound system, replacing the earlier OSS (Open Source Sound) drivers. As a result, your Linux installation may already have ALSA drivers loaded and working. Among older distributions, SuSE has long supported ALSA, but many others used the OSS drivers.

If you do not have ALSA drivers installed, you should visit the ALSA web site, starting at http://www.alsa-project.org/alsa-doc/ and selecting the manufacturer and then the model of your sound card. Once you have found the card, note the chipset it uses, and click on the driver link to find details of the driver download and installation procedure.

If you don't know what soundcard you have in your computer, fall back on the time-honoured techniques: if it's working in Windows, use the Windows Control Panel System - > Hardware -> Device Manager to find out. If you don't have Windows, use the lspci command to list the PCI devices in your computer, and that should give you more than a few clues.

In general, you will have to download the following files:


In general, the instructions provided for your sound card at the ALSA site are detailed and very helpful. There's an occasional gotcha - for example, the documentation often implies that you don't need to run the snddevices script, but users with 2.4 kernels generally will have to.

I encountered one little glitch while installing the ALSA drivers on one of our systems with a Soundblaster Live! card - the make step for the drivers gave an "unresolved symbol in snd.o" error, and of course, any attempt to modprobe the snd-emu10k1 driver failed. A little Googling produced the tip that, after the ./configure step, one should execute these commands, while still in /usr/src/alsa/alsa-drivers-1.0.8:

mkdir -p include/linux
touch include/linux/workqueue.h

and then continue with the make && make install steps. Sure enough, that fixed the problem. The final little gotcha with ALSA is that output is muted until you use alsamixer to set it up - don't forget to do that!

Once you have ALSA set up, the next thing you'll need is a MIDI synthesizer.

MIDI Synthesizers

Here's what turned out - for me - to be the missing part of the puzzle. To produce sound output from a midi file, you'll need a MIDI synthesizer. This is the device that reads MIDI events from the MIDI sequencer and produces the sounds of piano, organ, etc. There are three types of MIDI synthesizers:


An external synthesizer connects via either a MIDI interface (which is the game controller port on some sound cards) or increasingly, via a USB interface. ALSA provides the snd-usb-audio module for USB devices. External synthesizers are the choice of music professionals.
These are generally cards based on the Emu chipset - this generally means the Creative Labs Soundblaster product range - or the OPL3 and similar FM synthesizers. In my experiments, the Soundblaster cards have given consistently excellent results with less complexity and a lower load on the CPU of the host system, due to their wavetable synthesizers. Cards which use an FM synthesizer, by comparison, create a buzzy sound that is instantly recognizable and accounts for the poor opinion of MIDI held by most computer users.
When I originally set up MIDI under Slackware 96, years ago, it was on a system with a Soundblaster AWE32 card, which of course has a built-in synth. It sounded really good, too, and I amassed a small collection of MIDI files. But subsequent hardware upgrades saw me with various systems with other sound cards, or on-board sound chipsets, and either no MIDI sound (under Linux) or terrible-sounding MIDI (under Windows). The reason was simple: those soundcards had no MIDI synthesizer, and Linux distributions often don't incorporate a software synthesizer and don't provide any kind of automatic configuration of sound cards that do have some MIDI support.

Fluidsynth (http://www.fluidsynth.org/) is a command-line driven software synth which is a good choice for those who don't have some kind of hardware synthesizer. It's initially a little intimidating, but if you don't like working at the command line - and musical people often don't; it's the old right brain/left brain dichotomy - then QSynth (http://qsynth.sourceforge.net/qsynth-index.html) is a graphical front end for fluidsynth which will make life much easier for you. However, in my experiments, I found that fluidsynth produced much better quality sound when invoked from the command line, rather than through QSynth, where it produces strange juddering sounds that, to date, I have not been able to get rid of. To invoke fluidsynth from the command line:

[les@sleipnir les]$ fluidsynth -m alsa_seq -a alsa
fluidsynth: ALSA driver: Using format s16, rw, interleaved


fluidsynth version 1.0.5
Copyright (C) 2000-2002 Peter Hanappe and others.
FLUID Synth comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; see the COPYING file for details.
SoundFont(R) is a registered trademark of E-mu Systems, Inc.


Type 'help' to get information on the shell commands.

> load soundfonts/PC51f.sf2
fluidsynth: warning: Failed to pin the sample data to RAM; swapping is possible.
loaded SoundFont has ID 1
>


When I initially attempted to run qsynth, it complained about not being able to find its libraries, even through they were in the right place (/ur/local/lib) and that directory was configured in /etc/ld.so.conf. I ran out of time to troubleshoot this, and so I use the following command to run qsynth:

LD_LIBRARY_PATH=/usr/local/lib qsynth &

A softsynth works by replaying sound samples which have either been created by recording a real instrument or have been synthesized electronically. The synth usually does not come with those samples - instead you'll need to download them separately, in the form of what are sometimes called soundfonts. I've had good results with Personal Copy 5.1f, which is available from http://www.personalcopy.com/linuxfiles.htm.
To load a soundfont into qsynth, click on "Setup...", and then select the "Soundfonts" tab in the "qsynth: Setup" dialog.You can now click on the "Open..." button and use a standard dialog to navigate to and load a .sf2 soundfont file. Once you've done this, qsynth will remember the setting for future use.

If you have a Soundblaster card, you'll find the soundfont files for it on the driver CD. For example, the SB Live! CD has three soundfont files of increasing size (up to 8 MB - not a problem for most modern machines) in the directory /mnt/cdrom/AUDIO/COMMON/SFBANK. In order to load the soundfont into memory, you'll need the sfxload command, which is part of the awesfx-0.4.4 package, available from http://mitglied.lycos.de/iwai/awedrv.html. You don't need the AWE32/64 driver itself, just the utilities package, and you'll find the instructions for building it in docs/INSTALL once you've untarred it. Once you've compiled and installed it, you can use it to load a soundfont with a command like:

sfxload soundfonts/8MBGMSFX.SF2

If you don't do this, then everything else will appear to work, but your speakers will produce only stunned silence.

Playing a track requires a MIDI player. There are many of these out there, and some distributions come with Kmid and Kmidi or other players as standard. However, for basic use, I've found pmidi (http://www.parabola.me.uk/alsa/pmidi.html) to be very quick and easy.

To play a track, you need to know the MIDI port number your synthesizer is listening on. The quickest way to find this out is to let pmidi figure it out for you:

[les@baldur les]$ pmidi -l
Port     Client name                       Port name
64:0     EMU10K1 MPU-401 (UART)            EMU10K1 MPU-401 (UART)
65:0     Emu10k1 WaveTable                 Emu10k1 Port 0
65:1     Emu10k1 WaveTable                 Emu10k1 Port 1
65:2     Emu10k1 WaveTable                 Emu10k1 Port 2
65:3     Emu10k1 WaveTable                 Emu10k1 Port 3

Here, we can see that port 64:0 is the external MIDI interface on the sound card, while port 65 is the SB Live!'s wavetable synthesizer. So, to play an old Emerson Lake & Palmer track (but an excellent MIDI demonstration):

pmidi -p 65:0 tarkus.mid

Using fluidsynth, with or without qsynth, I'd send the MIDI events to port 128:0, like this:

pmidi -p 128:0 tarkus.mid

Music Composition and Editing

If you've followed the instructions so far, you've got MIDI music playing, perhaps from samples you've downloaded off the web. Now you're ready for the piece de resistance: being able to edit and even compose music.

The application I use for this is Rosegarden (http://www.rosegardenmusic.com), a completely graphical application that combines MIDI editing with editing of audio clips (although to do that, you'll need to add the Jack audio panel to allow mixing - in this article we're only covering MIDI, which will work without Jack).

Rosegarden provides the familiar three ways of editing a MIDI composition:


Rosegarden is fairly intuitive to use, although there are a lot of subtleties in using it well. Drag and drop works pretty much as you'd expect - drag and drop to move a segment, hold down Ctrl to copy - and the program provides infinite levels of undo and redo, which helps while you're learning.

Notation Editor

To get started with Rosgarden, perhaps the simplest approach is to import an existing MIDI file (File -> Import MIDI File...) and then play around with all the controls - click on a track segment, then press "M" to open the Matrix Editor, "N" to open the Notation Editor or "E" for the Event Editor. You can use the Shift key to select multiple tracks and open the Notation Editor, then select View -> Layout Mode -> Multiple Page Layout to get rather neat-looking sheet music.

As you work with Rosegarden, you'll see toolbars all around many of the editor windows; in some cases, it is obvious what a tool does, but there are tooltips on the buttons which give an indication of what they do.

If you have a MIDI keyboard, you can set it up (Composition -> Studio -> Manage MIDI Devices, then click on "New" beside the "Record devices" list and connect to the MIDI port) and now you can simply hit record and play a track directly into your composition.

A little experimenting will go a long way. . .

It's taken a little work and a lot of compiling to get this far, but it's been worth it - you should now have some some high-quality MIDI sound along with tools to allow you to produce your own backing tracks for practice or even complete compositions.

References and Further Reading

The Linux MIDI HOW-TO at http://www.midi-howto.com/

Sound & MIDI Software for Linux: http://linux-sound.org/

Using Rosegarden tutorial: http://rosegarden.sourceforge.net/tutorial/using-rosegarden/en/chapter-0.html

Download of the Month - Rosegarden

If you want to take MIDI editing beyond the basic level covered in this article, then Rosegarden is the program for you.

Price: Free

Download from: http://www.rosegardenmusic.com/getting/


Sidebar - Compiling Drivers and Programs from Source

As a first example which is typical of most installations from source code, take qsynth-0.2.2. Here are the commands to install it:

tar xzvf qsynth-0.2.2.tar.gz
cd qsynth-0.2.2
./configure
make
su
(Enter the root password)
make install

It's a good idea, with all software packages, to read the INSTALL file (or the README file if there's no INSTALL file) after untarring the source, to see if there are any wrinkles or special options.

Compiling the alsa code is similar (using alsa-utils as an example):

tar xjvf alsa-utils-1.0.8.tar.bz2
cd alsa-utils-1.0.8
./configure
su
(Enter the root password)
make install

However, you should always follow the specific instructions for your card given at the ALSA web site. You should also read the INSTALL file after cd'ing into the source code directory. For example, to compile your ALSA driver(s) you'll need to identify your sound card, and then run the configure script with the appropriate options. For example, to compile the drivers for the SoundBlaster Live!:

./configure --with-cards=emu10k1 --with-sequencer=yes
make
make install

All this is covered in the documentation at the ALSA site, however.

The only problems I encountered in installing all the software listed occurred with Rosegarden, on which the compilation bombed out a few times due to duplicate definitions of symbols in libmad and Rosegarden's own code. In each case, the fix was to edit the Makefile in the appropriate directory, find the line that defines the linked libraries and delete the references to libmad. I did this by duplicating the offending line, commenting out the original, and then editing the copy, like this:

#LIBS = -lmad -lasound -lm -ldl -lpthread
LIBS = -lasound -lm -ldl -lpthread

I had to do this a couple of times in order to get Rosegarden to compile and install cleanly, but then all was well and the program has performed well.


Page last updated: 19/Aug/2005 Back to Home Copyright © 1987-2010 Les Bell and Associates Pty Ltd. All rights reserved. webmaster@lesbell.com.au

...........................