Examples
Each example is a standalone program in wauvio/sfx/examples/. Build with g++ -std=c++17 -O2 -I<repo root> NAME.cpp; WAVs are written to sfx_examples_out/.
01_render_preset.cpp
Render one preset to a WAV file.
// Render one preset to a WAV file.
// g++ -std=c++17 -O2 -I<repo root> 01_render_preset.cpp -o ex01 && ./ex01
#include "../../sfx.hpp"
#include <filesystem>
#include <iostream>
int main() {
namespace sfx = wauvio::sfx;
std::filesystem::create_directories("sfx_examples_out");
sfx::Effect riser = sfx::make("noise_riser"); // a preset id; throws sfx::Error (with suggestions) if unknown
sfx::Rendered audio = riser.render(); // 44.1 kHz stereo, peak normalised to -1 dBFS
audio.save("sfx_examples_out/noise_riser.wav");
const sfx::Stats s = audio.stats();
std::cout << "noise_riser: " << s.seconds << " s, peak " << s.peak_db() << " dB, rms " << s.rms_db() << " dB\n";
}
02_customize.cpp
Change parameters, seed and sample rate; list what a preset offers.
// Change parameters, seed and sample rate; list what a preset offers.
#include "../../sfx.hpp"
#include <filesystem>
#include <iostream>
int main() {
namespace sfx = wauvio::sfx;
std::filesystem::create_directories("sfx_examples_out");
const sfx::Preset* p = sfx::find("cinematic_impact");
std::cout << p->name << ": " << p->description << "\nparameters:\n";
for (const auto& s : p->params) std::cout << " " << s.name << " = " << s.def << " [" << s.min << " .. " << s.max << " " << s.unit << "]\n";
sfx::Effect hit = sfx::make("cinematic_impact");
hit.set("duration", 3.0).set("sub", 1.0).set("brightness", 0.3).set("tail", 4.0).seed(1234);
sfx::RenderSettings settings;
settings.sample_rate = 48000; // 8000 .. 192000
settings.peak_db = -3.0; // leave some headroom
hit.render(settings).save("sfx_examples_out/impact_dark_long.wav");
try { hit.set("brightness", 7.0); } // invalid values are rejected, never silently clamped
catch (const sfx::Error& e) { std::cout << "rejected: " << e.what() << "\n"; }
}
03_layered_transition.cpp
Compose a transition from several presets with Layered: a riser, a reverse cymbal and an impact on the downbeat.
// Compose a transition from several presets with Layered: a riser, a reverse cymbal and an impact on the downbeat.
#include "../../sfx.hpp"
#include <filesystem>
#include <iostream>
int main() {
namespace sfx = wauvio::sfx;
std::filesystem::create_directories("sfx_examples_out");
sfx::Layered transition;
transition.tempo(128).seed(7);
transition.add("noise_riser", sfx::LayerOpts().dur(3.75).gain(-3).place(-0.2)); // builds for 8 beats (3.75 s at 128 bpm)
transition.add("tonal_riser", sfx::LayerOpts().dur(3.75).gain(-8).set("pitch", 45)); // parameters can be overridden per layer
transition.add("reverse_cymbal", sfx::LayerOpts().dur(1.8).at(1.95).gain(-6)); // swells into the downbeat
transition.add("cinematic_impact", sfx::LayerOpts().at_beats(8).dur(2.0).gain(0)); // lands exactly on beat 8
sfx::Rendered audio = transition.render();
audio.save("sfx_examples_out/transition.wav");
std::cout << "transition: " << audio.seconds() << " s, " << transition.size() << " layers\n";
}
04_tempo_riser.cpp
Tempo-aware effects: durations in beats, rhythmic steps locked to the tempo.
// Tempo-aware effects: durations in beats, rhythmic steps locked to the tempo.
#include "../../sfx.hpp"
#include <filesystem>
#include <iostream>
int main() {
namespace sfx = wauvio::sfx;
std::filesystem::create_directories("sfx_examples_out");
for (double bpm : { 90.0, 140.0 }) {
sfx::Effect riser = sfx::make("rhythmic_riser");
riser.tempo(bpm).duration_beats(8); // 8 beats, resolved to seconds when rendering
std::cout << bpm << " bpm: 8 beats = " << riser.duration() << " s\n";
riser.render().save("sfx_examples_out/rhythmic_riser_" + std::to_string(static_cast<int>(bpm)) + "bpm.wav");
}
sfx::Effect gate = sfx::make("gated_noise"); // "subdivision" is the step length in beats (0.25 = sixteenth)
gate.tempo(120).set("subdivision", 0.5).set("swing", 0.2).duration_beats(8);
gate.render().save("sfx_examples_out/gated_noise_eighths.wav");
}
05_atmosphere.cpp
A sustained atmosphere: three textures layered for twenty seconds.
// A sustained atmosphere: three textures layered for twenty seconds.
#include "../../sfx.hpp"
#include <filesystem>
#include <iostream>
int main() {
namespace sfx = wauvio::sfx;
std::filesystem::create_directories("sfx_examples_out");
sfx::Layered bed;
bed.seed(3);
bed.add("detuned_atmosphere", sfx::LayerOpts().dur(20).set("pitch", 41).set("brightness", 0.35)); // warm pad underneath
bed.add("granular_cloud", sfx::LayerOpts().dur(20).gain(-9).set("pitch", 77).set("density", 0.4)); // bell grains on top
bed.add("air_texture", sfx::LayerOpts().dur(20).gain(-14)); // a little hiss
sfx::RenderSettings settings;
settings.peak_db = -6.0;
sfx::Rendered audio = bed.render(settings);
audio.save("sfx_examples_out/atmosphere.wav");
std::cout << "atmosphere: " << audio.seconds() << " s\n";
}
06_deterministic_random.cpp
Seeds: the same seed always gives the same audio; a different seed gives a different take.
// Seeds: the same seed always gives the same audio; a different seed gives a different take.
#include "../../sfx.hpp"
#include <filesystem>
#include <iostream>
int main() {
namespace sfx = wauvio::sfx;
std::filesystem::create_directories("sfx_examples_out");
auto take = [](uint64_t seed) { sfx::Effect e = sfx::make("glitch_fill"); e.seed(seed); return e.render(); };
const sfx::Rendered a = take(100), b = take(100), c = take(101);
std::cout << "same seed identical: " << (a.left == b.left && a.right == b.right ? "yes" : "NO") << "\n"
<< "different seed identical: " << (a.left == c.left ? "yes" : "no") << "\n";
for (uint64_t s = 1; s <= 4; ++s) take(s).save("sfx_examples_out/glitch_fill_take" + std::to_string(s) + ".wav");
}
07_custom_recipe.cpp
Build your own effect from sources and processors, render it, and register it as a preset.
// Build your own effect from sources and processors, render it, and register it as a preset.
#include "../../sfx.hpp"
#include <filesystem>
#include <iostream>
int main() {
namespace sfx = wauvio::sfx;
using sfx::Traj; using sfx::Wave; using sfx::NoiseColor;
std::filesystem::create_directories("sfx_examples_out");
// 1) a Recipe is plain data: layers of one source plus a chain of processors
sfx::Recipe r;
r.name = "my_whoosh"; r.duration = 2.0; r.tail = 1.0;
r.add("air").noise(NoiseColor::Pink)
.bp(Traj::exp(200, 6000), 3.0, 4) // cutoff trajectory in Hz, exponential
.shape(sfx::Traj(0.02).to(0.7, 1.0, sfx::Interp::Exp).to(1.0, 0.001, sfx::Interp::Exp))
.place(0.0, 0.9).reverb(1.2, 0.5, 0.5);
r.add("tone").osc(Wave::Saw, Traj::linear(40, 76)).unison(3, 12).lp(Traj::exp(300, 5000), 1.5, 4).gain(0.4);
sfx::render(r, /*seed=*/5).save("sfx_examples_out/my_whoosh.wav");
// 2) the same thing as a preset with parameters, in your own registry next to the built-in catalog
sfx::Registry mine = sfx::builtin_registry();
sfx::Preset p;
p.name = "My Whoosh"; p.category = sfx::Category::Risers; p.behavior = sfx::Behavior::Evolving;
p.description = "A pink-noise whoosh with a saw underneath, tunable brightness.";
p.tags = { "custom", "whoosh" };
p.params = sfx::params_from({ "duration=2", "brightness=0.6", "pitch=40" });
p.build = [](sfx::Recipe& rec, const sfx::BuildCtx& c) {
rec.add("air").noise(NoiseColor::Pink).bp(Traj::exp(200, c.lg(c.p("brightness"), 2000, 9000)), 3.0, 4).shape(sfx::env_swell());
rec.add("tone").osc(Wave::Saw, Traj::linear(c.p("pitch"), c.p("pitch") + 36)).lp(4000).gain(0.4);
};
mine.add(p);
sfx::Effect e(*mine.find("my_whoosh"));
e.set("brightness", 0.9).render().save("sfx_examples_out/my_whoosh_preset.wav");
std::cout << "registry now holds " << mine.size() << " presets (built-in catalog: " << sfx::builtin_registry().size() << ")\n";
}
08_collection.cpp
Render a collection: every preset tagged "riser", at the same tempo and seed, into one folder.
// Render a collection: every preset tagged "riser", at the same tempo and seed, into one folder.
#include "../../sfx.hpp"
#include <filesystem>
#include <iostream>
int main() {
namespace sfx = wauvio::sfx;
std::filesystem::create_directories("sfx_examples_out/risers");
size_t n = 0;
for (const sfx::Preset* p : sfx::search("riser")) {
sfx::Effect e(*p);
e.tempo(128).seed(42);
try { e.render().save("sfx_examples_out/risers/" + p->id + ".wav"); ++n; }
catch (const sfx::Error& err) { std::cerr << p->id << ": " << err.what() << "\n"; return 1; } // never skip a failure silently
}
std::cout << "rendered " << n << " presets\n";
}