48#include "AudioBuffer.h"
50#include "WavetableBank.h"
87 assert(sampleRate > 0.0);
88 if (!(sampleRate > 0.0))
return;
90 sampleRate_ = sampleRate;
91 phasor_.prepare(sampleRate);
113 return (harmonic % 2 == 0) ? T(-1.0 / harmonic) : T(1.0 / harmonic);
124 return (harmonic % 2 == 0) ? T(0) : T(1.0 / harmonic);
135 if (harmonic % 2 == 0)
return T(0);
136 T sign = ((harmonic / 2) % 2 == 0) ? T(1) : T(-1);
137 return sign /
static_cast<T
>(harmonic * harmonic);
150 updateMipSelection();
161 template <
typename HarmonicFunc>
167 updateMipSelection();
183 if (!data || size < 3)
return;
185 updateMipSelection();
198 updateMipSelection();
207 if (position != position)
return;
208 position_ = position;
211 [[nodiscard]] T
getPosition() const noexcept {
return position_; }
225 if (frequencyHz != frequencyHz)
return;
226 const bool changed = frequencyHz != frequency_;
227 frequency_ = frequencyHz;
228 phasor_.setFrequency(frequencyHz);
229 if (changed) updateMipSelection();
245 const T phase = phasor_.advance();
246 return bank().template read<T>(
static_cast<double>(phase),
247 static_cast<double>(position_), mip_);
257 for (
int i = 0; i < numSamples; ++i)
267 const int nCh = buffer.getNumChannels();
268 const int nS = buffer.getNumSamples();
269 for (
int i = 0; i < nS; ++i)
272 for (
int ch = 0; ch < nCh; ++ch)
273 buffer.getChannel(ch)[i] = s;
283 phasor_.reset(phase);
289 return external_ !=
nullptr ? *external_ : own_;
293 void updateMipSelection() noexcept
295 mip_ = bank().
selectMip(
static_cast<double>(frequency_), sampleRate_);
298 double sampleRate_ = 48000.0;
299 T frequency_ = T(440);
305 const WavetableBank* external_ =
nullptr;
306 WavetableBank::Mip mip_;
Non-owning view over audio channel data.
Mipmapped multi-frame wavetable storage and its morphing reader.
static constexpr int kMaxHarmonics
Harmonics of the richest level.
@ PerFrame
Every frame peaks at 1 (equal peak level across a morph).
static constexpr int kMipLevels
Mip levels, half an octave apart.
bool loadFrames(const S *data, int frameSize, int numFrames, Normalization normalization=Normalization::Bank)
Builds the bank from consecutive single-cycle frames.
Mip selectMip(double frequencyHz, double sampleRate) const noexcept
Chooses the level pair and crossfade weight for a pitch.
bool buildFromHarmonics(int numFrames, int numHarmonics, Coefficients &&coefficients, Normalization normalization=Normalization::PerFrame)
Builds the bank from per-frame harmonic amplitudes.
Professional mipmapped wavetable oscillator for bandlimited synthesis.
void prepare(double sampleRate)
Prepares the oscillator for a given sample rate.
void setBank(const WavetableBank *bank) noexcept
Plays an external bank instead of the oscillator's own table.
void buildTriangle()
Builds a bandlimited triangle wavetable with mipmaps.
static constexpr int kTableSize
Samples of the shortest level table (brighter levels hold more; see WavetableBank).
void prepare(const AudioSpec &spec)
Prepares from AudioSpec (unified API).
T getPosition() const noexcept
T getFrequency() const noexcept
Returns the current internal frequency.
void buildFromHarmonics(HarmonicFunc harmonicFunc)
Builds contiguous mipmapped tables from a harmonic amplitude function.
static constexpr int kMaxMipLevels
Mip levels, half an octave apart.
void buildSquare()
Builds a bandlimited square wavetable with mipmaps.
void buildSine()
Builds a pure sine wavetable.
void generateBlock(AudioBufferView< T > buffer) noexcept
Fills every channel of the view with the generated waveform. Satisfies the GeneratorProcessor concept...
void loadWavetable(const T *data, int size)
Loads and analyzes a custom single-cycle wavetable.
T getSample() noexcept
Generates the next sample.
void buildSaw()
Builds a bandlimited sawtooth wavetable with mipmaps.
void reset(T phase=T(0)) noexcept
Hard resets the oscillator phase.
void processBlock(T *output, int numSamples) noexcept
Generates a block of samples.
WavetableOscillator()=default
static constexpr int kTableMask
void setFrequency(T frequencyHz) noexcept
Sets the fundamental oscillation frequency.
void setPosition(T position) noexcept
Morph position across the bank's frames, 0 .. frames - 1 (fractional positions blend adjacent frames;...
Main namespace for the DSPark framework.
Describes the audio environment for a DSP processor.
double sampleRate
Sample rate in Hz.
One harmonic's cosine and sine amplitudes.