SoundFont (wauvio/sf2)
SoundFont (SF2) support
wauvio/sf2.hpp (directory wauvio/sf2/, namespace wauvio::sf2) loads SoundFont 2 files and exposes their presets as Wauvio instruments. It includes ext.hpp because it produces music::SampledInstruments.
See it in action: Use a SoundFont
Loading and using a SoundFont
#include "wauvio/sf2.hpp"
#include "wauvio/midi.hpp"
using namespace wauvio;
auto sf = std::make_shared<sf2::SoundFont>("GeneralUser.sf2");
for (auto& p : sf->list_presets()) { /* p.name, p.bank, p.program */ }
midi::track::LoadOptions opts;
opts.resolver.set_bank_provider(sf); // SF2 presets win over built-in GM instruments
auto music = midi::track::load_midi("song.mid", opts);
SoundFont implements music::IBankProvider, so it plugs directly into the MIDI instrument resolver.
SoundFont class
SoundFont(path)— parses the file; throwsSf2ParseError(astd::runtime_error) on malformed data or if the file has no sample audio.name(),list_presets()→PresetInfo {name, bank, program}.has_preset(bank_msb, bank_lsb, program)/create_instrument(...)— lookup by bank MSB and program; the LSB argument is ignored. Returnsnullptrif missing.has_percussion_preset(...)/create_percussion_instrument(...)— the bank and note arguments are ignored: the first preset found in bank 128 (then bank 120) is used as the whole kit, built once and cached; zones inside it are selected by note.create_instrument_by_name(preset_name)— exact preset-name match.
Presets become a SampledInstrument whose zones come from the preset/instrument zone hierarchy, using global-zone defaults and key/velocity ranges. All presets share one converted stereo sample buffer (shared_audio), so many instruments cost little extra memory. Audio is read as 16-bit samples.
Parsing layers
| Header | Contents |
|---|---|
sf2/core/riff_reader.hpp | Little-endian ByteReader (u8, u16le, u32le, s16le, s8, fourcc, fixed_string), RiffChunk, Sf2ParseError, read_whole_file |
sf2/core/sf2_types.hpp | Generator enum (SoundFont generator operators such as InitialFilterFc, VolEnv stages, KeyRange, VelRange, SampleModes, OverridingRootKey), GenAmount, list entries, PresetHeader, InstHeader, SampleHeader, and the Hydra structure holding all pdta tables |
sf2/core/sf2_parser.hpp | RawSoundFont and parse_sf2_file(path) (INFO, sdta and pdta chunks) |
sf2/sf2_instrument.hpp | SoundFont and instrument construction helpers |
Full API listing
wauvio/sf2/core/riff_reader.hpp
wauvio::sf2::class Sf2ParseError
class Sf2ParseError : public std::runtime_error
public:
explicit Sf2ParseError(const std::string& what) : std::runtime_error(what) {}
wauvio::sf2::class ByteReader
class ByteReader
public:
ByteReader(const unsigned char* data, size_t size) : data_(data), size_(size) {}
size_t position() const noexcept { return pos_; }
size_t remaining() const noexcept { return pos_ < size_ ? size_ - pos_ : 0; }
bool eof() const noexcept { return pos_ >= size_; }
void require(size_t n) const
uint8_t u8() { require(1); return data_[pos_++]; }
uint16_t u16le()
uint32_t u32le()
int16_t s16le() { return static_cast<int16_t>(u16le()); }
int8_t s8() { return static_cast<int8_t>(u8()); }
std::string fourcc()
std::string fixed_string(size_t len)
void skip(size_t n) { require(n); pos_ += n; }
void seek(size_t absolute)
const unsigned char* ptr() const noexcept { return data_ + pos_; }
const unsigned char* base() const noexcept { return data_; }
size_t size() const noexcept { return size_; }
wauvio::sf2::struct RiffChunk
struct RiffChunk
std::string id;
size_t data_offset = 0;
uint32_t data_size = 0;
inline std::vector<unsigned char> read_whole_file(const std::string& path)
wauvio/sf2/core/sf2_types.hpp
wauvio::sf2::enum class Generator
enum class Generator : uint16_t
StartAddrsOffset = 0,
EndAddrsOffset = 1,
StartloopAddrsOffset = 2,
EndloopAddrsOffset = 3,
StartAddrsCoarseOffset = 4,
ModLfoToPitch = 5,
VibLfoToPitch = 6,
ModEnvToPitch = 7,
InitialFilterFc = 8,
InitialFilterQ = 9,
ModLfoToFilterFc = 10,
ModEnvToFilterFc = 11,
EndAddrsCoarseOffset = 12,
ModLfoToVolume = 13,
ChorusEffectsSend = 15,
ReverbEffectsSend = 16,
Pan = 17,
DelayModLFO = 21,
FreqModLFO = 22,
DelayVibLFO = 23,
FreqVibLFO = 24,
DelayModEnv = 25,
AttackModEnv = 26,
HoldModEnv = 27,
DecayModEnv = 28,
SustainModEnv = 29,
ReleaseModEnv = 30,
KeynumToModEnvHold = 31,
KeynumToModEnvDecay = 32,
DelayVolEnv = 33,
AttackVolEnv = 34,
HoldVolEnv = 35,
DecayVolEnv = 36,
SustainVolEnv = 37,
ReleaseVolEnv = 38,
KeynumToVolEnvHold = 39,
KeynumToVolEnvDecay = 40,
Instrument = 41,
KeyRange = 43,
VelRange = 44,
StartloopAddrsCoarseOffset = 45,
Keynum = 46,
Velocity = 47,
InitialAttenuation = 48,
EndloopAddrsCoarseOffset = 50,
CoarseTune = 51,
FineTune = 52,
SampleID = 53,
SampleModes = 54,
ScaleTuning = 56,
ExclusiveClass = 57,
OverridingRootKey = 58,
EndOper = 60
wauvio::sf2::union GenAmount
union GenAmount
int16_t sh_amount;
uint16_t w_amount;
struct { uint8_t lo; uint8_t hi; } ranges;
wauvio::sf2::struct GenListEntry
struct GenListEntry
Generator oper;
GenAmount amount;
wauvio::sf2::struct ModListEntry
struct ModListEntry
uint16_t src_oper = 0;
uint16_t dest_oper = 0;
int16_t amount = 0;
uint16_t amt_src_oper = 0;
uint16_t trans_oper = 0;
wauvio::sf2::struct BagEntry
struct BagEntry
uint16_t gen_ndx = 0;
uint16_t mod_ndx = 0;
wauvio::sf2::struct PresetHeader
struct PresetHeader
std::string name;
uint16_t preset = 0;
uint16_t bank = 0;
uint16_t bag_ndx = 0;
wauvio::sf2::struct InstHeader
struct InstHeader
std::string name;
uint16_t bag_ndx = 0;
wauvio::sf2::struct SampleHeader
struct SampleHeader
std::string name;
uint32_t start = 0;
uint32_t end = 0;
uint32_t loop_start = 0;
uint32_t loop_end = 0;
uint32_t sample_rate = 44100;
uint8_t original_pitch = 60;
int8_t pitch_correction = 0;
uint16_t sample_link = 0;
uint16_t sample_type = 1;
wauvio::sf2::struct Hydra
struct Hydra
std::vector<PresetHeader> phdr;
std::vector<BagEntry> pbag;
std::vector<ModListEntry> pmod;
std::vector<GenListEntry> pgen;
std::vector<InstHeader> inst;
std::vector<BagEntry> ibag;
std::vector<ModListEntry> imod;
std::vector<GenListEntry> igen;
std::vector<SampleHeader> shdr;
wauvio/sf2/core/sf2_parser.hpp
wauvio::sf2::struct RawSoundFont
struct RawSoundFont
std::string name;
std::shared_ptr<std::vector<float>> sample_data;
uint32_t sample_data_frames = 0;
Hydra hydra;
inline void parse_info(ByteReader& r, size_t end, RawSoundFont& out)
inline void parse_sdta(ByteReader& r, size_t end, RawSoundFont& out)
inline void parse_pdta(ByteReader& r, size_t end, RawSoundFont& out)
inline RawSoundFont parse_sf2_file(const std::string& path)
wauvio/sf2/sf2_instrument.hpp
wauvio::sf2::instr_detail::Free functions, constants and aliases
inline std::vector<GenListEntry> bag_slice(const std::vector<BagEntry>& bag,
const std::vector<GenListEntry>& gen_pool, size_t bag_idx)
inline bool has_gen(const std::vector<GenListEntry>& gens, Generator g)
inline int gen_or(const std::vector<GenListEntry>& local, const std::vector<GenListEntry>& global_zone,
bool has_global, Generator g, int def)
inline void key_vel_range(const std::vector<GenListEntry>& local, const std::vector<GenListEntry>& global_zone,
bool has_global, int& key_lo, int& key_hi, int& vel_lo, int& vel_hi)
inline double timecents_to_seconds(int tc)
wauvio::sf2::class SoundFont
class SoundFont : public wauvio::music::IBankProvider
public:
explicit SoundFont(const std::string& path)
const std::string& name() const noexcept { return raw_.name; }
struct PresetInfo { std::string name; int bank; int program; };
std::vector<PresetInfo> list_presets() const
bool has_preset(int bank_msb, int /*bank_lsb*/, int program) const override
wauvio::music::InstrumentPtr create_instrument(int bank_msb, int /*bank_lsb*/, int program) const override
bool has_percussion_preset(int /*bank_msb*/, int /*bank_lsb*/, int /*gm_note*/) const override
wauvio::music::InstrumentPtr create_percussion_instrument(int /*bank_msb*/, int /*bank_lsb*/, int /*gm_note*/) const override
wauvio::music::InstrumentPtr create_instrument_by_name(const std::string& preset_name) const