Jump to content

FluidSynth

From ArchWiki

FluidSynth is a real-time software synthesizer based on the SoundFont 2 specifications. It is optionally used by gst-plugins-bad.

Installation

Install the fluidsynth package.

A SoundFont is also needed. See MIDI#List of SoundFonts for a list of SoundFonts.

Remember to either modify the default SOUND_FONT or redirect that location to a shared sound font with a symlink.

Usage

This article or section is out of date.

Reason: Since FluidSynth 2.0 the configuration options are in a different format and in a different location. (Discuss in Talk:FluidSynth)

There are two ways to use FluidSynth. Either as a MIDI player or as a daemon which provides an ALSA MIDI input and outputs synthesized audio via PipeWire, PulseAudio, JACK, ALSA, etc.

Standalone mode

You can simply use fluidsynth to play MIDI files:

$ fluidsynth -a alsa -m alsa_seq -l -i /usr/share/soundfonts/FluidR3_GM.sf2 example.mid

assuming than you installed soundfont-fluid.

There are many other options to FluidSynth; see fluidsynth(1) or use -h to get help.

One may wish to use PipeWire or PulseAudio instead of alsa as the argument to the -a option.

Tip: The SoundFont does not needed to be specified every time if a symbolic link is created for the default SoundFont, e.g. ln -s FluidR3_GM.sf2 /usr/share/soundfonts/default.sf2

Daemon mode

fluidsynth --help | grep -A2 audio-driver enumerates valid output devices. See audio.driver for available options.

Common configuration

  • /etc/conf.d/fluidsynth — System wide defaults, such as a default SOUND_FONT, audio rate, output device.
  • ~/.config/fluidsynth — Per user overrides for the above, it is safe to copy the template from the system wide configuration and customize.

PipeWire / PulseAudio / JACK

Software defined audio running as the desktop user (not root). Likely what is in use on your system.

Configure the system wide or user specific options, to include the audio-driver, midi-driver, and sample-rate respectively:

OTHER_OPTS='-a pipewire -m alsa_seq -r 48000'

Additional arguments might be required:

  • --audio-bufsize=128 documentation suggests the default might be 64, my system required 128 to work reliably
  • --gain=1.0 A float value to adjust the volume (in case you do not want to use PipeWire / PulseAudio / etc to do that)
  • --audio-bufcount=???
  • fluidsynth --help Any other options

Start/enable the fluidsynth.service user unit.

ALSA / OSS

ALSA or OSS should offer the lowest latency, however multiple audio clients—e.g. any other audio application, PipeWire, PulseAudio, JACK, etc.—require hardware mixing or ALSA dmix plugin as the first audio client (to provide software mixing in ALSA). Additionally the rest of the audio stack on a modern system will already be routed (and volume mixed) via PipeWire (usually) or PulseAudio (on new / updated installs likely a PipeWire compatibility interface).

If you want fluidsynth to run as an ALSA sequencer client, edit /etc/conf.d/fluidsynth and add your SoundFont along with any other changes you would like to make. For e.g., Fluid:

SOUND_FONT=/usr/share/soundfonts/FluidR3_GM.sf2
OTHER_OPTS='-a alsa -m alsa_seq -r 48000'

After that, you can start/enable fluidsynth.service.

Note: You cannot use root to start the fluidsynth service, if you are using the PulseAudio driver. PulseAudio will not allow root to connect, since the PulseAudio server is usually started by the user (and not root). Hence the service is provided as a user unit.

Command line test

The following will give you an output software MIDI port (in addition of hardware MIDI ports on your system, if any):

$ aconnect -o
client 128: 'FLUID Synth (5117)' [type=user]
   0 'Synth input port (5117:0)'

An example of usage for this is aplaymidi:

$ aplaymidi -p128:0 example.mid
Note: To achieve a low latency in a real-time setup, it is important to set the RATE/NUM/SIZE parameters described in the FluidSynth wiki. Common values of these parameters and additional tips are also listed at Professional audio#JACK parameters.

SDL_Mixer

To use fluidsynth with programs that use SDL_Mixer, you need to specify the soundfont as:

 $ SDL_SOUNDFONTS=/usr/share/soundfonts/FluidR3_GM.sf2 ./program

Tips and tricks

Convert MIDI to MP3/OGG

Requires soundfont-fluid or any other SoundFont of your choice.

/usr/share/soundfonts is the default location of FluidR3_GM

Simple command lines to convert midi to mp3:

$ fluidsynth -l -T raw -F - /usr/share/soundfonts/FluidR3_GM.sf2 example.mid | twolame -b 256 -r - example.mp3 

Requires twolame.

Simple command lines to convert midi to ogg:

$ fluidsynth -nli -r 48000 -o synth.cpu-cores=2 -T oga -F example.ogg /usr/share/soundfonts/FluidR3_GM.sf2 example.MID

Here is a little script to convert multiple midi files to ogg in parallel:

#!/bin/bash
maxjobs=$(grep processor /proc/cpuinfo | wc -l)
midi2ogg() {
	name=$(echo $@ | sed -r s/[.][mM][iI][dD][iI]?$//g | sed s/^[.][/]//g)
	for arg; do 
	fluidsynth -nli -r 48000 -o synth.cpu-cores=$maxjobs -F "/dev/shm/$name.raw" /usr/share/soundfonts/FluidR3_GM.sf2 "$@"
	oggenc -r -B 16 -C 2 -R 48000 "/dev/shm/$name.raw" -o "$name.ogg"
	rm "/dev/shm/$name.raw"
	## Uncomment for replaygain tagging
	#vorbisgain -f "$name.ogg" 
	done
}
export -f midi2ogg
find . -regex '.*[.][mM][iI][dD][iI]?$' -print0 | xargs -0 -n 1 -P $maxjobs bash -c 'midi2ogg "$@"' --

Troubleshooting

Conflicting with PulseAudio

If your fluidsynth application is set to use alsa as driver, the sound card will be accessed directly and PulseAudio and applications using PulseAudio will not be able to work properly. You can modify the configuration file /etc/conf.d/fluidsynth and change the driver to pulseaudio, then restart fluidsynth and PulseAudio:

/etc/conf.d/fluidsynth
OTHER_OPTS='-a pulseaudio -m alsa_seq -r 48000'

No MIDI sound / Not the lowest or first MIDI device

$ aconnect -o
client 14: 'Midi Through' [type=kernel]
    0 'Midi Through Port-0'
client 128: 'FLUID Synth (5117)' [type=user]
    0 'Synth input port (5117:0)'

Wine, Proton and many other programs will grab the first MIDI device as their default output. Many have methods to configure a non-default or specific MIDI device but disabling the snd_seq_dummy module is simpler, even if it can be useful for professionals who need to duplex MIDI output to multiple devices.

Remove the module right now.

# rmmod snd_seq_dummy

Then blacklist it from being even loaded in the first place.

Stuck 'key' (constant sound)

Stop the fluidsynth.service user unit, kill all remaining fluidsynth processes, then start the fluidsynth.service user unit.

If using ALSA or OSS, and running as a root service: follow the same instruction for fluidsynth.service.

Broken Pipe and all sound stops with Pipewire

If even simple playback tests sometimes make pipewire get stuck and the pipewire log shows messages like snd_pcm_avail after recover: Broken pipe, the audio buffer size might need to be increased:

   fluidsynth -ni --audio-bufsize=128 test.mid        # Fixes "Broken pipe".

Audio distortions using PipeWire

This article or section is a candidate for merging with #PipeWire / PulseAudio / JACK.

Notes:
  • #PipeWire / PulseAudio / JACK subsection contains recommendation about --audio-bufsize already, so it seems logical to combine them all in one place.
  • #PipeWire / PulseAudio / JACK also suggests -r 48000, and linked mail thread gives it as a solution for the audio distortion problem too—but, for some reason, it is not recorded in our article.
(Discuss in Talk:FluidSynth)

After starting FluidSynth if you start hearing audio distortions or crackles during any playback, try setting --audio-bufsize to 512 or 8192, i.e -z512, in the OTHER_OPTS section inside the /etc/conf.d/fluidsynth file, if the recommended value of 128 didn't help.[1]