sfMidi

What is sfMidi?


sfMidi is a static library for playing MIDI files with SoundFont 2 in SFML (http://www.sfml-dev.org/).
sfMidi uses FluidSynth (http://sourceforge.net/apps/trac/fluidsynth/) to load SoundFont 2 and MIDI files.

Latest version:


1.1.0

Last update:


Changed namespace to sfmidi.
Changed naming conventions of functions to camelCase.
Added error functions.
Fixed compatibility with latest SFML version (mainly naming conventions).
Dropped SFML 1.6 support.

License


zlib License

Note:


FluidSynth does not support seeking by itself, therefore, in order to seek forward, sfMidi reads and ignores the audio data from FluidSynth until it reaches the desired offset.
However, in order to seek backwards, sfMidi has to reload the MIDI data, and seek forward from the beginning again, hence, seeking backwards will be slower than seeking forward.

Naturally, stopping is considered seeking to the beginning and playing it again, which means the MIDI data has to be reloaded again if we want to stop the MIDI. Thankfully, reloading the MIDI data doesn't take much time.

Also, for looping, I've tried using both the SFML and FluidSynth's loop feature, but unfortunately they did not work at all, so I had to implement my own loop function, which again, requires reloading the MIDI data.

Edit:
I've added a feature to preload MIDI data into audio samples in version 1.0.1, this means you can load the entire MIDI data into memory as audio samples, and this will result in faster and more efficient playback (no more problems with seeking).
Preloading takes quite some time to load, so be careful in choosing whether to preload or not.
Of course, preloading the MIDI data into audio sample means you get to unload the sound font, so you get to save quite some memory, unless your song is long enough to be larger than the sound font.
Preloading takes about 10MB of memory per minute.

For SFML 1.6

If you're still using SFML 1.6, sfMidi 1.0.2 (the last version that still supports SFML 1.6) is still available for download.

Install

1. Download the "Clean package".
2. Extract the contents to "C:\" (You can extract it to anywhere you want, but I'll use "C:\" for this example)
3. Add the include and lib paths to your compiler.
4. Copy the contents of the "dependencies" folder into your project folder (where your executable is).
5. You're set!

Note:
If you're using the CMake package, you'll have to link fluidsynth to your project.

Short Tutorial

sfMidi is very easy and straight forward to use, first, create a Midi instance:
sfmidi::Midi testMidi;

Then load your sound font and midi file:
testMidi.loadSoundFontFromFile("GeneralUser GS FluidSynth v1.44.sf2");
testMidi.loadMidiFromFile("All Night Long.mid");


or even simpler:
sfmidi::Midi testMidi("GeneralUser GS FluidSynth v1.44.sf2",
"All Night Long.mid");


To check if everything is alright:
if (testMidi.hasError()) {
// Handle error
std::cout<<"Error: "<<testMidi.getError()<<"\n";
return 1;
}


Now, just play it like a regular SFML music instance!
testMidi.play();

And... that's it!

To preload the MIDI instead:
sfmidi::Music testMidi;

{
sfmidi::Loader loader("GeneralUser GS FluidSynth v1.44.sf2");
if (loader.hasError()) {
// Handle error
std::cout<<"Error: "<<loader.getError()<<"\n";
return 1;
}

testMidi.loadMidiFromFile(loader, "All Night Long.mid");
}

testMidi.play();