This article is about using MIDI Keyboard or other MIDI devices on systems that uses PulseAudio Sound System
Definitions
Sound System
Sound System is a part of your OS that knows how make your computer to make certain sound. This article is about using MIDI with Pulse Audio. So you install Pulse Audio in a usual way as said in PulseAudio article. Fine Pulse Audio configuration will be discussed later
MIDI Sequencer
MIDI Sequencer is a system that knows how to connect various MIDI devices, and it also allow organizes communication between them. E.g. your MIDI Keyboard only reports what keys have been pressed, your virtual MIDI Synthesizer knows how to play sound for each key, and MIDI Sequencer arrange their communication, so when you press key, you hear a sound.
As a MIDI Sequencer we will use ALSA Sequencer that is a part of ALSA Sound System. It is ok to use Pulse Audio with ALSA Sequencer as Pulse Audio actually work on top of ALSA, so there would be no inconsistency in it.
MIDI Synthesizer
MIDI Synthesizer is a hardware or virtual MIDI device that converts MIDI commands into actual sound you can hear. E.g. your MIDI Keyboard or MIDI Player sends MIDI commands to a sequencer, sequencer passes them to a synthesizer, and synthesizer actually plays sounds appropriate for each command.
In our case we will use ?FluidSynth from fluidsynth as a MIDI Synthesizer.
Installation and configuration
First install fluidsynth:
$ sudo apt-get install fluidsynth
Then open /etc/default/fluidsynth with you favorite editor
$ sudo editor /etc/default/fluidsynth
and set OTHER_OPTS varizble as shown below:
OTHER_OPTS='-a pulseaudio -m alsa_seq -g 1 -o midi.autoconnect=1 -r 48000'
Here -a specifies what Sound System whould be used, -m specifies what MIDI Sequencer would be used. Set -o midi.autoconnect=1 if you want your MIDI device to be automatically connected to MIDI Sequencer when it is plugged in (in most cases it is the behaviour you need). Set -g to greater values it you want more loud sound.
Starting and autostarting FluidSynth
To start ?FluidSynth type
$ systemctl --user start fluidsynth.service
To make ?FluidSynth autostart on user login you should first change /usr/lib/systemd/user/fluidsynth.service file, replace WantedBy option with default.target value like this:
[Install] WantedBy=default.target
And then enable autostart from command line:
$ systemctl enable --user fluidsynth.service
Testing with MIDI Player
Find and download some free MIDI music in the internet, and install pmidi player
$ sudo apt-get install pmidi
List your MIDI devices with pmidi player
$ pmidi -l Port Client name Port name 14:0 Midi Through Midi Through Port-0 128:0 FLUID Synth (156844) Synth input port (156844:0)
Here you can see that ?FluidSinth is available as a MIDI device at 128:0 port. Let' tell pmidi to use it to play music:
$ pmidi -p 128:0 [File you've found].mid
If you hear the sound, your ?FluidSynth works well
Testing with MIDI keyboard
First, check that your MIDI keyboard is visible by ALSA:
$ amidi -l Dir Device Name IO hw:1,0,0 Keystation MIDI 1
If your MIDI Keyboard is not listed here, check you cable connection, check if you have installed firmware packages for your device, or check if your device is ever supported by ALSA.
If your keyboard is listed, try to play some notes. If there is no sound, your better check with aconnectgui (or via aconnect -l) if your device is connected to ?FluidSynth. If not, connect it and try again.
Solving latency problem
When you start playing your MIDI Keyboard you should find out that sound is played not immediately after you've pressed a key, but after a short while. This makes playing almost impossible.
To fix this problem you should edit /etc/pulse/daemon.conf
$ sudo editor /etc/pulse/daemon.conf
and set following values
default-fragments = 4 default-fragment-size-msec = 5
Restart PulseAudio and ?FluidSynth:
$ systemctl restart --user pulseaudio.service $ systemctl restart --user fluidsynth.service
License
This work is licensed under a Creative Commons Attribution 4.0 International License or newer.