Core engine (wauvio/core)

osc.hpp — oscillators and noise

Namespace wauvio::osc. Waveform generators, from stateless functions up to a wavetable oscillator.

Waveform functions

osc::wave provides naive waveforms (sine, square, triangle, sawtooth, pulse(p, duty)) of a phase in [0, 2π). osc::wave_bl provides band-limited versions using PolyBLEP: polyblep(t, dt), sawtooth(phase, dt), square, pulse, and triangle(phase, dt, prev) which needs one float of caller-owned state. Here dt = frequency / sample_rate.

WaveShape

Sine, Square, Triangle, Sawtooth, Pulse, BL_Square, BL_Triangle, BL_Sawtooth, BL_Pulse. The BL_ variants use PolyBLEP and are preferable for high notes.

Oscillator

A phase-accumulating oscillator. Public fields: shape, frequency, amplitude, phase_offset, duty (for pulse shapes) and hard_sync.

  • tick(sample_rate) advances one sample.
  • render(seconds, sample_rate) produces a mono buffer.
  • Portamento: glide_to(target, seconds) ramps the frequency during subsequent ticks; is_gliding() and cancel_glide() manage it.
  • Hard sync: the caller invokes sync_reset() on the slave when the master wraps.
  • reset(), phase(), set_phase() expose the phase accumulator.

Supersaw

Renders voices (default 7) detuned sawtooth waves. Fields: frequency, detune_cents (12), amplitude, mix_center (0.7). render() gives mono; render_stereo() spreads voices across the stereo field.

WavetableOscillator

A 2048-sample table (TABLE_SIZE) with linear-interpolated reads; initialised to a sine. Fill it with load_wave(shape, ref_freq, ref_sr), load_sine(), load_custom(fn) (a functor of phase 0–2π) or write table directly. tick(frequency, sample_rate) and tick(frequency, amplitude, sample_rate) advance a normalised phase; render(frequency, amplitude, seconds) fills a buffer. BL tables are precomputed at the reference frequency, so aliasing reduction is approximate.

NoiseGenerator

Seeded (mt19937, default seed 42). tick_white(), tick_pink() (Paul Kellet's filter), tick() (alias for white), seed(s), render(seconds), render_pink(seconds). Fixed seeds make output deterministic.

Full API listing

wauvio/core/osc.hpp

wauvio::osc::Free functions, constants and aliases

//  §4  WAVEFORM PRIMITIVES  (stateless, phase in [0,2π))
    inline float sine    (double p) noexcept { return static_cast<float>(std::sin(p)); }
    inline float square  (double p) noexcept { return (std::fmod(p,TWO_PI) < PI) ? 1.f : -1.f; }
    inline float triangle(double p) noexcept
    inline float sawtooth(double p) noexcept
    inline float pulse(double p, double duty = 0.5) noexcept
//  §4b  POLYBLEP BAND-LIMITED WAVEFORMS
//  PolyBLEP (Polynomial Band-Limited Step) correction reduces aliasing at
//  discontinuities in square, sawtooth, and triangle waves.  Drop-in
//  replacements for the naive wave:: functions; phase in [0, 2π).
    /// PolyBLEP residual: smooths a unit step at normalised phase t ∈ [0,1).
    /// dt = phase_increment / TWO_PI  (one sample's worth of normalised phase)
    inline double polyblep(double t, double dt) noexcept
    /// Band-limited sawtooth.  dt = freq / sample_rate.
    inline float sawtooth(double phase, double dt) noexcept
    /// Band-limited square.  dt = freq / sample_rate.
    inline float square(double phase, double dt) noexcept
    /// Band-limited triangle (integrated square, naturally alias-free below Nyquist).
    /// Requires one sample of state; pass `prev` by reference.
    inline float triangle(double phase, double dt, float& prev) noexcept
    /// Band-limited pulse with variable duty cycle.  dt = freq / sample_rate.
    inline float pulse(double phase, double dt, double duty = 0.5) noexcept

wauvio::osc::enum class WaveShape

//  §5  WAVESHAPE ENUM
enum class WaveShape { Sine, Square, Triangle, Sawtooth, Pulse,
                       BL_Square, BL_Triangle, BL_Sawtooth, BL_Pulse };

wauvio::osc::class Oscillator

//  §6  OSCILLATOR
//  Phase-accumulating oscillator with hard-sync and portamento support.
//  Hard sync: when sync_source phase resets, this oscillator resets too.
//  Portamento: call glide_to(target, seconds) and tick() handles the ramp.
class Oscillator
public:
    WaveShape shape        = WaveShape::Sine;
    double    frequency    = 440.0;
    double    amplitude    = 1.0;
    double    phase_offset = 0.0;
    double    duty         = 0.5;
    bool      hard_sync    = false;   ///< If true, caller must call sync_reset() at slave reset
    Oscillator() = default;
    explicit Oscillator(WaveShape s, double f = 440.0, double a = 1.0)
        : shape(s), frequency(f), amplitude(a) {}
    float tick(int sample_rate) noexcept
    /// Begin a smooth frequency glide to `target_freq` over `seconds`.
    /// Call before the first tick() of a new note.
    void glide_to(double target_freq, double seconds, int sample_rate = 0)
    /// Cancel any in-progress glide and snap to current frequency.
    void cancel_glide() noexcept { glide_samples_left_ = 0; }
    bool is_gliding() const noexcept { return glide_samples_left_ > 0; }
    /// Hard sync: reset phase when master crosses zero upward.
    void sync_reset() noexcept { phase_ = 0.0; }
    void   reset()             noexcept { phase_ = 0.0; }
    double phase()       const noexcept { return phase_; }
    void   set_phase(double p) noexcept { phase_ = p; }
    Buffer render(double seconds, int sample_rate = 0)

wauvio::osc::class Supersaw

//  §7  SUPERSAW HELPER
//  Renders N detuned sawtooth oscillators into a mono buffer.
//  Classic JP-8000 supersaw: 7 voices, center at 0 Hz detune, outer voices
//  spread symmetrically up to ±detune_cents cents.
class Supersaw
public:
    int    voices       = 7;      ///< Number of oscillator voices
    double frequency    = 440.0;
    double detune_cents = 12.0;   ///< Total spread in cents (split across voices)
    double amplitude    = 1.0;
    double mix_center   = 0.7;    ///< Gain of centre voice relative to side voices
    /// Render `seconds` of supersaw audio (mono).
    Buffer render(double seconds, int sample_rate = 0) const
    /// Render into a stereo buffer with panning spread across voices.
    StereoBuffer render_stereo(double seconds, int sample_rate = 0) const

wauvio::osc::class WavetableOscillator

//  §7b  WAVETABLE OSCILLATOR
//  Lookup-table oscillator with linear interpolation.  The table can be any
//  arbitrary waveform — fill it from the wave:: helpers, load from disk, or
//  compute your own.  Linear-interpolated reads give good quality; combine
//  with PolyBLEP (wave_bl::) tables for alias-free output.
//
//  Usage:
//    WavetableOscillator osc;
//    osc.load_wave(WaveShape::Sawtooth);   // or fill osc.table manually
//    float s = osc.tick(440.0, sample_rate);
class WavetableOscillator
public:
    static constexpr size_t TABLE_SIZE = 2048;
    std::array<float, TABLE_SIZE> table{};
    WavetableOscillator() { load_sine(); }
    /// Fill the table with a standard WaveShape.
    /// BL_ variants are pre-computed at a reference frequency of 440 Hz with
    /// dt = 440/44100 — good alias reduction for most musical use.
    void load_wave(WaveShape shape, double ref_freq = 440.0, int ref_sr = 44100)
    void load_sine() { load_wave(WaveShape::Sine); }
    /// Fill table from an arbitrary functor: float fn(double phase_0_to_2pi).
    template<typename Fn>
    void load_custom(Fn&& fn)
    /// Advance phase and return the next sample (linearly interpolated).
    float tick(double frequency, int sample_rate) noexcept
    float tick(double frequency, double amplitude, int sample_rate) noexcept
    void  reset()              noexcept { phase_ = 0.0; }
    double phase()       const noexcept { return phase_; }
    void   set_phase(double p) noexcept { phase_ = std::fmod(p, 1.0); }
    Buffer render(double frequency, double amplitude,
                  double seconds, int sample_rate = 0)

wauvio::osc::class NoiseGenerator

//  §8  NOISE GENERATOR
class NoiseGenerator
public:
    explicit NoiseGenerator(uint32_t seed = 42u)
        : rng_(seed), dist_(-1.0f, 1.0f) {}
    /// White noise sample in [-1, 1].
    float tick_white() { return dist_(rng_); }
    /// Pink noise sample (Paul Kellet's refined method, 1/f spectrum).
    /// Amplitude is approximately normalised to [-1, 1].
    float tick_pink()
    /// Convenience alias: tick() returns white noise (preserves prior behaviour).
    float tick() { return tick_white(); }
    void seed(uint32_t s)
    Buffer render(double seconds, int sample_rate = 0)
    Buffer render_pink(double seconds, int sample_rate = 0)