A library that can load Soundfont (.sf2) files, and format it neatly so it can be used in applications like a software sampler.
- 16-bit sample loading
- Full preset parsing, including all the preset and instrument zones
- Add the
common.h
,soundfont.h
,soundfont.cpp
, andstructs.h
files to your project. In what folder the files are exactly is not important, but make sure all those files are in the same folder together. - Quick example to load a soundfont:
int main() {
// You can do this...
Flan::Soundfont soundfont1("path/to/soundfont.sf2");
// ...or, alternatively this
Flan::Soundfont soundfont2;
soundfont2.from_file("path/to/soundfont.sf2");
}
- None! Please report if you find any.
- Single header file?
The Soundfont struct exposes two fields:
std::map<u16, Preset> presets;
std::vector<Sample> samples;
A Sample
is a data structure that contains:
- A pointer to raw 16-bit signed PCM sample data
- A pointer to the linked sample's 16-bit signed PCM sample data
- A base sample rate (original root key and fine tuning are already applied here)
- The length of the sample
- The loop start and loop end of the sample
- The sample type (used to see if it's mono, the left channel, or the right channel)
A Preset
is a data structure that only contains a list of Zone
, a collection of settings meant for a sampler to use.
The map is indexed by a u16, with the bank number in the high byte, and the preset number in the low byte.
A Zone
is a collection of settings meant for a software sampler. It has:
- Key range
- Velocity range
- Index of the sample (used to index the
Sample
array inSoundfont
) - Sample start, end, loop start, and loop end offsets (some soundfonts, notably the ones VGMtrans export use this a lot)
- Root key offset, relative to the
Sample
's root key - Sample loop enable flag
- Stereo panning
- Volume envelopes: Delay, Attack, Hold, Decay, Sustain, and Release values. All in either
1.0 / time in seconds
,volume in dB
ordB per second
(note: usually 6 dB = 0.5x) - Tuning scale: how many semitones there are between each MIDI key
- Initial attenuation: volume in dB to subtract from zone volume (note: usually 15 dB = 0.5x)
To determine which zones to use when playing a note, there are key ranges and velocity ranges. For a given Preset
, you can loop over each Zone
, check if the midi key and velocity are in-between or equal to those range values, and if they are, that zone should be used for that note.