MIDI & SoundFont guide
Wauvio reads Standard MIDI Files and plays each part with an instrument chosen from the General MIDI program number. You can override any choice, restyle the whole piece with a genre preset, export stems, or use a SoundFont. Include wauvio/midi.hpp.
The examples first write a tiny MIDI file so they run on their own; replace that with your own .mid.
g++ -std=c++17 -O2 -I/path/to/wauvio-repo NAME.cpp -o NAME && ./NAME1. Load and render a MIDI file
load_midi returns a MidiMusic containing one part per track or channel. Each part already has an instrument; render mixes them to a WAV.
// Load a MIDI file, render it with Wauvio's built-in General MIDI instruments, and save a WAV.
#include "wauvio/midi.hpp"
using namespace wauvio;
int main() {
// (Self-contained demo: first write a small MIDI file. Skip this part if you have your own .mid.)
{
music::Arrangement a;
instruments::Trumpet tp; instruments::UprightBass bs;
a.add(tp, {{72, 0.5}, {74, 0.5}, {76, 1.0}, {72, 1.0}});
a.add(bs, {{48, 1.0}, {43, 1.0}, {48, 1.0}});
midi::track::save_midi(a, "demo.mid");
}
// load_midi gives you a MidiMusic: one "part" per track/channel, each with an instrument picked from its
// General MIDI program number. (This demo file stores program 0 = piano for every part; real files vary.)
midi::track::MidiMusic song = midi::track::load_midi("demo.mid");
std::printf("%zu parts, %.1f seconds, %.0f BPM\n", song.part_count(), song.duration_seconds, song.initial_bpm);
for (size_t i = 0; i < song.part_count(); ++i)
std::printf(" part %zu: %s (GM program %d)\n", i, song.part(i).name.c_str(), song.part(i).gm_program);
render(song, "midi_01_load_and_render.wav");
}Download midi_01_load_and_render.cpp
Try this: inspect song.part(i).notes to see the resolved notes.
2. Choose the instruments
Assign instruments before loading with the resolver (by channel, program, or bank), or after loading with set_instrument. Pinning a part protects it from later genre changes.
// Choose which instruments play. Three ways: before loading (resolver), after loading (set_instrument), or per channel.
#include "wauvio/midi.hpp"
using namespace wauvio;
int main() {
{ music::Arrangement a; instruments::Trumpet t; instruments::UprightBass b;
a.add(t, {{72, 0.5}, {74, 0.5}, {76, 1.0}}); a.add(b, {{48, 1.0}, {43, 1.0}});
midi::track::save_midi(a, "demo2.mid"); }
// (Note: save_midi writes every track as GM program 0 = piano, so without the rules below both parts would be piano.)
// 1. Before loading: tell the resolver what to use. Rules are checked in a fixed priority order:
// channel override > bank/program override > bank provider (SF2) > program override > built-in GM.
// Here we assign by MIDI channel (0-based; the demo file uses channel 0 and 1).
midi::track::LoadOptions opts;
opts.resolver.override_channel(0, [] { return std::make_shared<instruments::Trumpet>(); });
opts.resolver.override_channel(1, [] { return std::make_shared<instruments::UprightBass>(); });
// Other rules: override_program(gm_program, factory), override_bank_program(msb, lsb, program, factory),
// override_percussion_note(note, factory)
midi::track::MidiMusic song = midi::track::load_midi("demo2.mid", opts);
// 2. After loading: swap a part by index (any music::Instrument works).
song.set_instrument<instruments::Harp>(1);
song.pin_instrument(1); // pinned parts are never changed by a genre preset later
render(song, "midi_02_choose_instruments.wav");
}Download midi_02_choose_instruments.cpp
Try this: use override_percussion_note to replace a single drum sound.
3. Restyle with a genre
setGenre re-performs the file in a genre’s style: timing (swing, humanise), dynamics, articulations, and optionally instruments. It is reversible: clearGenre() restores the original, and calling it twice never compounds.
// Restyle a whole MIDI file with a genre preset. It is reversible, and deterministic for a given seed.
#include "wauvio/midi.hpp"
using namespace wauvio;
int main() {
{ music::Arrangement a; instruments::ElectricPiano ep; instruments::BassGuitar bg;
music::Melody m; for (int n : {60, 64, 67, 72, 67, 64, 60, 55}) m.push_back({n, 0.25});
a.add(ep, m); a.add(bg, {{36, 1.0}, {41, 1.0}});
midi::track::save_midi(a, "demo3.mid"); }
midi::track::MidiMusic song = midi::track::load_midi("demo3.mid");
midi::genre::GenreApplyOptions go;
go.allow_instrumentation = true; // let the preset swap in genre-typical instruments
go.seed = 3; // different seed = different (but repeatable) humanisation
song.setGenre(midi::genre::presets::jazz(), go);
render(song, "midi_03_genre_jazz.wav");
song.setGenre(midi::genre::presets::metal(), go); // starts again from the original, not from the jazz version
render(song, "midi_03_genre_metal.wav");
song.clearGenre(); // back to the file as loaded
render(song, "midi_03_genre_original.wav");
}Try this: set allow_instrumentation = false to keep your instruments and change only the performance.
4. Stems and writing MIDI
render_stems writes one WAV per part for mixing elsewhere (or feed parts to the Mixer with mixer_from_midi). save_midi writes the score back out.
// Export one WAV per part (stems) for use in a DAW, and write the (possibly edited) score back out as MIDI.
#include "wauvio/midi.hpp"
using namespace wauvio;
int main() {
{ music::Arrangement a; instruments::Flute f; instruments::Cello c;
a.add(f, {{76, 0.5}, {79, 0.5}, {83, 1.0}}); a.add(c, {{52, 1.0}, {55, 1.0}});
midi::track::save_midi(a, "demo4.mid"); }
midi::track::MidiMusic song = midi::track::load_midi("demo4.mid");
RenderOptions opt;
opt.bit_depth = 24;
opt.normalize = false; // keep each stem's relative level
// render_stems writes <dir>/<part name>.wav for every part and returns the paths.
// (The directory must exist.)
std::system("mkdir -p stems");
auto files = midi::track::render_stems(song, "stems", opt);
for (auto& f : files) std::printf("wrote %s\n", f.c_str());
midi::track::save_midi(song, "demo4_out.mid");
}Download midi_04_stems_and_export.cpp
Try this: set opt.normalize = true to normalise each stem separately.
5. Use a SoundFont
A SoundFont is a bank provider: plug it into the resolver and SF2 presets win over the built-in instruments. Pass any .sf2 file and a MIDI file on the command line. (This example needs your own SF2, so it exits politely without arguments.)
// Use a SoundFont (.sf2) as the instrument source for a MIDI file.
// Pass the path to any SF2 on the command line: ./midi_05_soundfont GeneralUser.sf2 song.mid
#include "wauvio/sf2.hpp"
#include "wauvio/midi.hpp"
using namespace wauvio;
int main(int argc, char** argv) {
if (argc < 3) { std::printf("usage: %s soundfont.sf2 song.mid\n", argv[0]); return 0; }
auto sf = std::make_shared<sf2::SoundFont>(argv[1]); // throws sf2::Sf2ParseError on a bad file
std::printf("loaded '%s' with %zu presets\n", sf->name().c_str(), sf->list_presets().size());
midi::track::LoadOptions opts;
opts.resolver.set_bank_provider(sf); // SF2 presets now win over the built-in instruments
auto song = midi::track::load_midi(argv[2], opts);
render(song, "midi_05_soundfont.wav");
}Download midi_05_soundfont.cpp
Try this: combine with override_channel to keep one part on a built-in instrument. More on the format: SoundFont reference.
Next
Process the result in the FX guide, or add a sung vocal line from the Vocal guide.