Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
wavetable_osc.hpp
Go to the documentation of this file.
1// sbl/widgets/source/wavetable_osc.hpp — Wavetable oscillator (Audio Stack — Widgets)
2//
3// Composes PhaseAccumulator + WavetableReader into a complete oscillator.
4// Frequency can be set via Hz, MIDI note, or direct phase increment.
5//
6// Usage:
7// sbl::widgets::source::WavetableOsc<1024> osc;
8// osc.set_wavetable(sbl::dsp::lut::sin_1024);
9// osc.set_frequency(440.0f);
10// osc.process(buf, frames);
11
12#pragma once
13
14#include <cstdint>
15
16#include <sbl/dsp/phase.hpp>
17#include <sbl/dsp/wavetable.hpp>
18#include <sbl/dsp/pitch.hpp>
19
20namespace sbl::widgets::source {
21
22template<uint16_t TableSize>
24public:
25 /// @note All public methods are ISR-safe — bounded computation, no I/O.
26
27 /** @brief Set wavetable (must have guard points for interpolation) */
28 void set_wavetable(const float* table) { reader_.set_table(table); }
29
30 /**
31 * @brief Set frequency in Hz
32 * @param freq_hz Frequency in Hz (e.g., 440.0f)
33 * @param sr Sample rate (default 48000)
34 */
35 void set_frequency(float freq_hz, float sr = 48000.0f) {
37 }
38
39 /**
40 * @brief Set frequency from MIDI note number (requires FPU)
41 * @param midi_note MIDI note (69 = A4 = 440 Hz)
42 * @param sample_rate Sample rate
43 */
44 void set_note(float midi_note, float sample_rate = 48000.0f) {
45 phase_.set_increment(dsp::note_to_phase_increment(midi_note, sample_rate));
46 }
47
48 /** @brief Set phase increment directly */
49 void set_increment(uint32_t inc) { phase_.set_increment(inc); }
50
51 /** @brief Set output amplitude (0.0 = silence, 1.0 = full scale) */
52 void set_amplitude(float amp) { amplitude_ = amp; }
53
54 /**
55 * @brief Generate audio samples
56 * @param out Output buffer (float, [-1.0, 1.0])
57 * @param frames Number of samples
58 */
59 void process(float* out, uint16_t frames) {
60 for (uint16_t i = 0; i < frames; ++i) {
61 phase_.advance();
62 out[i] = reader_.read(phase_.phase()) * amplitude_;
63 }
64 }
65
66 /** @brief Hard sync — reset phase to zero */
67 void sync() { phase_.reset(); }
68
69 /** @brief Reset phase to a specific value */
70 void sync(uint32_t phase) { phase_.reset(phase); }
71
72 /** @brief Current phase */
73 uint32_t phase() const { return phase_.phase(); }
74
75 /** @brief Current phase increment */
76 uint32_t increment() const { return phase_.increment(); }
77
78private:
81 float amplitude_ = 1.0f;
82};
83
84} // namespace sbl::widgets::source
void reset()
Reset phase to zero.
Definition phase.hpp:58
void set_increment(uint32_t inc)
Set the phase increment per sample.
Definition phase.hpp:33
uint32_t increment() const
Current phase increment.
Definition phase.hpp:55
void advance()
Advance phase by one sample.
Definition phase.hpp:36
static uint32_t freq_to_inc(float freq_hz, float sr=48000.0f)
Convert frequency in Hz to phase increment.
Definition phase.hpp:70
uint32_t phase() const
Current phase value.
Definition phase.hpp:52
float read(uint32_t phase) const
Read a single sample with linear interpolation.
Definition wavetable.hpp:39
void set_table(const float *table)
Set the wavetable to read from (must have Size+1 guard points for linear)
Definition wavetable.hpp:29
void set_wavetable(const float *table)
Set wavetable (must have guard points for interpolation)
void sync(uint32_t phase)
Reset phase to a specific value.
void set_note(float midi_note, float sample_rate=48000.0f)
Set frequency from MIDI note number (requires FPU)
void set_amplitude(float amp)
Set output amplitude (0.0 = silence, 1.0 = full scale)
void sync()
Hard sync — reset phase to zero.
uint32_t increment() const
Current phase increment.
void process(float *out, uint16_t frames)
Generate audio samples.
uint32_t phase() const
Current phase.
void set_frequency(float freq_hz, float sr=48000.0f)
Set frequency in Hz.
void set_increment(uint32_t inc)
Set phase increment directly.
uint32_t note_to_phase_increment(float midi_note, float sample_rate)
Definition pitch.hpp:58
Sound source widgets.
Definition noise.hpp:15