Music layer guide
The music layer adds instruments you can write for like a composer: MIDI note numbers, dynamics (p, mf, f…), articulations and 300+ ready-made instruments in wauvio::instruments. Include wauvio/ext.hpp. Browse the instrument catalog for names.
g++ -std=c++17 -O2 -I/path/to/wauvio-repo NAME.cpp -o NAME && ./NAME1. A melody
A Melody is a list of notes {midi, seconds, dynamics, articulation}. Add it to an Arrangement with an instrument, and render writes the WAV. Effects like reverb attach to the instrument.
// A melody with MIDI note numbers, dynamics and articulations.
#include "wauvio/ext.hpp"
using namespace wauvio;
using music::Dynamics;
using music::Articulation;
int main() {
instruments::Flute flute;
flute.reverb(0.7, 0.25); // optional per-instrument effect
// A Note is {midi_note, duration_in_seconds, dynamics, articulation}.
// MIDI 60 = middle C, 69 = A440. Each +1 is one semitone.
music::Melody m = {
{67, 0.5, Dynamics::mp},
{69, 0.5, Dynamics::mf},
{72, 1.0, Dynamics::f, Articulation::Legato},
music::rest(0.5),
{71, 0.5, Dynamics::mp, Articulation::Staccato},
{67, 1.5, Dynamics::p},
};
music::Arrangement song;
song.add(flute, m);
render(song, "music_01_melody.wav"); // 44.1 kHz stereo 16-bit by default
}Try this: swap Flute for Violin or Trumpet; change the dynamics to hear the difference.
2. Chords and scales
music::scale::* returns the notes of a scale as MIDI numbers, so you can loop over them. A Track holds notes and chords for one instrument and can be added to an arrangement with a start time.
// Chords and scales: build harmony without computing note numbers by hand.
#include "wauvio/ext.hpp"
using namespace wauvio;
int main() {
instruments::Cello cello;
instruments::Harp harp;
// scale::* return MIDI note numbers. build(root, steps, octaves) makes custom scales.
auto scale = music::scale::natural_minor(57 /*A3*/, 2); // two octaves of A minor
music::Melody run;
for (int n : scale) run.push_back({n, 0.2, music::Dynamics::mf});
// A Chord is a list of notes played together.
music::Track chords(harp);
for (int root : {57, 53, 48, 55}) { // Am F C G (roots)
// third = +3 (minor) or +4 (major) semitones; here we just stack a fifth and an octave for an open sound
chords.add_chord({{root, root + 7, root + 12, root + 16}, 1.5, music::Dynamics::mp});
}
music::Arrangement song;
song.add_track(chords);
song.add(cello, run, /*start_time=*/6.0); // enters after 6 seconds
song.master_reverb(0.7, 0.2);
render(song, "music_02_chords_scales.wav");
}Download music_02_chords_scales.cpp
Try this: try scale::blues or dorian.
3. A small band
Each instrument gets its own track with a start time and gain. Tempo helpers convert beats to seconds. limiter() prevents clipping on the master.
// A small band: several instruments, each on its own track, with start offsets and gains.
#include "wauvio/ext.hpp"
using namespace wauvio;
using music::Dynamics;
int main() {
music::Arrangement band;
band.tempo = music::Tempo(100);
const double beat = band.tempo.beat_seconds(); // seconds per beat at 100 BPM
instruments::UprightBass bass;
instruments::AcousticGrandPiano piano;
instruments::Trumpet trumpet; trumpet.reverb();
// Bass: root notes, one per bar (4 beats).
music::Melody bassline;
for (int n : {36, 41, 43, 36}) bassline.push_back({n, 4 * beat, Dynamics::mf});
// Piano: eighth-note pattern.
music::Melody comp;
for (int bar = 0; bar < 4; ++bar)
for (int n : {60, 64, 67, 64, 60, 64, 67, 64}) comp.push_back({n, beat / 2, Dynamics::mp});
// Trumpet joins in bar 3.
music::Melody lead = {{72, 2 * beat, Dynamics::f}, {71, beat, Dynamics::mf}, {67, beat, Dynamics::mf}};
band.add(bass, bassline, 0.0, /*track_gain=*/0.9);
band.add(piano, comp, 0.0, 0.7);
band.add(trumpet, lead, 8 * beat, 0.8);
band.limiter(); // keep the master from clipping
render(band, "music_03_arrangement.wav");
}Download music_03_arrangement.cpp
Try this: add a drum track using the kit from the next example.
4. Drum kits
A DrumKit maps MIDI note numbers (General MIDI: 36 kick, 38 snare, 42 closed hi-hat) to sounds. render_pattern takes (time, note) pairs, so any pattern you can compute, you can play.
// Drum kits map MIDI note numbers (General MIDI) to drum sounds.
#include "wauvio/ext.hpp"
using namespace wauvio;
int main() {
instruments::Kit808 kit; // also: IndustrialKit, and others in the catalog
const int KICK = 36, SNARE = 38, HAT = 42;
// render_pattern takes (time in seconds, midi note) pairs.
std::vector<std::pair<double,int>> hits;
const double step = 0.125; // an eighth note at 120 BPM
for (int bar = 0; bar < 4; ++bar)
for (int i = 0; i < 8; ++i) {
double t = (bar * 8 + i) * step;
hits.push_back({t, HAT});
if (i % 4 == 0) hits.push_back({t, KICK});
if (i % 4 == 2) hits.push_back({t, SNARE});
}
StereoBuffer loop = kit.render_pattern(hits, 4.5, music::Dynamics::f);
normalize(loop);
save_wav_stereo(loop, "music_04_drum_kit.wav");
}Download music_04_drum_kit.cpp
Try this: swap the kit for another one from the catalog’s “Drum kits” section.
5. Design your own instrument
Modeled instruments are defined by a TimbreRecipe: oscillator shapes, envelope, filter, vibrato and more. Subclass a family (SynthInstrument, KeyboardInstrument…) and set fields in the constructor. See the full field list.
// Design your own modeled instrument by changing its TimbreRecipe.
#include "wauvio/ext.hpp"
using namespace wauvio;
// Start from an existing family class and adjust the recipe in the constructor.
class GlassPad : public music::SynthInstrument {
public:
GlassPad() : SynthInstrument("Glass Pad") {
recipe.osc1_shape = osc::WaveShape::BL_Triangle;
recipe.osc2_shape = osc::WaveShape::Sine;
recipe.osc2_ratio = 3.0; // a twelfth above: glassy overtone
recipe.osc_mix = 0.45;
recipe.detune_cents = 9;
recipe.envelope = env::DAHDSR(0, 0.6, 0, 0.5, 0.8, 1.2); // slow attack, long release
recipe.filter_cutoff = 4200;
recipe.vibrato_depth_cents = 6;
recipe.stereo_width = 0.5;
}
};
int main() {
GlassPad pad;
pad.reverb(0.9, 0.35);
music::Track t(pad);
t.add_chord({{60, 64, 67, 71}, 4.0, music::Dynamics::mp});
t.add_chord({{58, 62, 65, 69}, 4.0, music::Dynamics::mp});
music::Arrangement a; a.add_track(t);
render(a, "music_05_custom_instrument.wav");
}Download music_05_custom_instrument.cpp
Try this: change osc2_ratio to 2.0 and lower filter_cutoff to 1500 for a warmer pad.
6. Your own samples
A SampledInstrument plays audio files, pitch-shifting them across a key range. Add several files with different root notes for realism; velocity layers and round-robin are supported on SampleZone.
// Use your own audio files as an instrument. (This example first makes a sample so it is self-contained.)
#include "wauvio/ext.hpp"
using namespace wauvio;
int main() {
// 1. Make a short sample: a plucked tone at middle C. In real use, point at any .wav file.
osc::Oscillator o; o.shape = osc::WaveShape::BL_Sawtooth; o.frequency = theory::midi_to_freq(60);
Buffer b = o.render(1.0, 44100);
env::ADSR(0.002, 0.3, 0.0, 0.3).apply(b, 1.0, 0.1);
save_wav(b, "music_06_sample_c4.wav");
// 2. Tell the instrument which MIDI note the sample was recorded at (root) and the range it covers.
music::SampledInstrument inst;
inst.name = "My Sample";
inst.load_sample_file("music_06_sample_c4.wav", /*root=*/60, /*low=*/36, /*high=*/84);
// 3. Play it. Other notes are made by pitch-shifting the sample.
music::Melody m = {{60, 0.5}, {64, 0.5}, {67, 0.5}, {72, 1.0}, {55, 1.0}};
music::Arrangement a; a.add(inst, m);
render(a, "music_06_sampled.wav");
}Try this: record a real instrument note, point load_sample_file at it and set its root note.
7. Genre styling
A genre preset rewrites dynamics, register and articulation, then restyles the instrument. The same melody comes out differently for jazz, metal or ambient. The seed makes the variation repeatable. All presets are listed in the genre reference.
// Genre presets restyle a melody (dynamics, register, articulation) and its instrument (timbre, effects).
#include "wauvio/ext.hpp"
using namespace wauvio;
namespace gn = wauvio::music::genre;
int main() {
music::Melody m;
for (int n : {60, 62, 64, 65, 67, 69, 71, 72}) m.push_back({n, 0.25, music::Dynamics::mf});
for (auto preset : { gn::presets::jazz(), gn::presets::metal(), gn::presets::ambient() }) {
instruments::CleanElectricGuitar gtr; // a fresh instrument per style
music::Melody styled = m;
gn::apply_to_melody(styled, *preset, gn::Role::Melodic, >r, /*seed=*/7);
gn::style_instrument(gtr, *preset, gn::Role::Melodic); // timbre + effect wishes
music::Arrangement a; a.add(gtr, styled);
render(a, "music_07_genre_" + preset->name + ".wav");
}
}Try this: pass a different seed; try presets::chiptune().
8. Output options
RenderOptions control the file: bit depth, normalisation, extra time for reverb tails, looping and dithering.
// RenderOptions control the output file: bit depth, loudness, reverb tails, looping.
#include "wauvio/ext.hpp"
using namespace wauvio;
int main() {
instruments::Marimba marimba;
marimba.reverb(0.8, 0.4);
music::Arrangement a;
a.add(marimba, {{60, 0.25}, {64, 0.25}, {67, 0.25}, {72, 0.75}});
RenderOptions opt;
opt.bit_depth = 24; // 16, 24 or 32 (float)
opt.normalize = true; // scale the loudest peak ...
opt.normalize_peak = 0.9; // ... to this level
opt.render_tail_sec = 2.0; // extra silence so the reverb is not cut off
opt.loop_count = 2; // repeat the result
opt.dither = DitherType::Triangular;
opt.deterministic_seed_enabled = true; // reproducible dither noise
render(a, "music_08_render_options.wav", opt);
}Download music_08_render_options.cpp
Try this: set bit_depth = 32 to write floating-point audio.
Next
Polish and mix these results in the FX guide, or play a MIDI file in the MIDI guide.