DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::WavetableBank Class Reference

Mipmapped multi-frame wavetable storage and its morphing reader. More...

#include <WavetableBank.h>

Classes

struct  Harmonic
 One harmonic's cosine and sine amplitudes. More...
 
struct  Mip
 The level pair and crossfade weight for one pitch (selectMip()). More...
 

Public Types

enum class  Builtin { Basic , Harmonic , Formant , Fm }
 Built-in banks (see builtin()). More...
 
enum class  Normalization { PerFrame , Bank }
 How a bank's frames are scaled. More...
 

Public Member Functions

 WavetableBank ()=default
 
template<typename Coefficients >
bool buildFromHarmonics (int numFrames, int numHarmonics, Coefficients &&coefficients, Normalization normalization=Normalization::PerFrame)
 Builds the bank from per-frame harmonic amplitudes.
 
template<FloatType S>
bool loadFrames (const S *data, int frameSize, int numFrames, Normalization normalization=Normalization::Bank)
 Builds the bank from consecutive single-cycle frames.
 
int getNumFrames () const noexcept
 Frames in the bank (0 before a successful build).
 
int getNumHarmonics () const noexcept
 Highest harmonic the frames hold (0 before a build).
 
size_t getMemoryBytes () const noexcept
 Bytes of table storage.
 
Mip selectMip (double frequencyHz, double sampleRate) const noexcept
 Chooses the level pair and crossfade weight for a pitch.
 
template<FloatType T>
T read (double phase, double position, const Mip &mip) const noexcept
 One sample of the bank.
 
template<FloatType T>
void readBlock (const double *phases, double position, const Mip &mip, T *out, int n) const noexcept
 A block of samples at one frame position: read() at each phase.
 
template<FloatType T>
void readBlock (const double *phases, const double *positions, const Mip &mip, T *out, int n) const noexcept
 A block of samples with one frame position per sample.
 

Static Public Member Functions

static const WavetableBank & builtin (Builtin which)
 A built-in bank, built on the first call and shared by the process.
 
static double interpolatorResponse (double cyclesPerSample) noexcept
 Amplitude response of the reader's interpolator at a frequency in cycles per table sample (the value pre-emphasis divides by).
 

Static Public Attributes

static constexpr int kMipLevels = 20
 Mip levels, half an octave apart.
 
static constexpr int kMaxHarmonics = 1023
 Harmonics of the richest level.
 
static constexpr int kMaxFrames = 256
 Most frames a bank holds.
 
static constexpr int kMaxFrameSize = 1 << 20
 Largest single-cycle frame loadFrames() analyses.
 
static constexpr int kNumBuiltins = 4
 

Detailed Description

Mipmapped multi-frame wavetable storage and its morphing reader.

auto mip = bank.selectMip(frequencyHz, sampleRate); // on pitch changes
double phase = 0.0;
double phases[64]; // n <= 64
for (int i = 0; i < n; ++i)
{
phases[i] = phase;
phase += frequencyHz / sampleRate;
if (phase >= 1.0) phase -= 1.0;
}
bank.readBlock<float>(phases, position, mip, out, n); // position 0..frames-1
static const WavetableBank & builtin(Builtin which)
A built-in bank, built on the first call and shared by the process.
@ Basic
6 frames: sine, triangle, saw, square, 25% and 12.5% pulse.

Definition at line 893 of file WavetableBank.h.

Member Enumeration Documentation

◆ Builtin

enum class dspark::WavetableBank::Builtin
strong

Built-in banks (see builtin()).

Enumerator
Basic 

6 frames: sine, triangle, saw, square, 25% and 12.5% pulse.

Harmonic 

8 frames: 1/h^p spectra from p = 3 (near sine) to p = 0.5 (bright).

Formant 

9 frames: vowels A, E, I, O, U and the four transitions between them.

Fm 

8 frames: sin(x + b sin x) with the index b rising from 0 to 6.

Definition at line 906 of file WavetableBank.h.

◆ Normalization

How a bank's frames are scaled.

Enumerator
PerFrame 

Every frame peaks at 1 (equal peak level across a morph).

Bank 

One factor for the whole bank: the frames keep their relative levels.

Definition at line 916 of file WavetableBank.h.

Constructor & Destructor Documentation

◆ WavetableBank()

dspark::WavetableBank::WavetableBank ( )
default

Member Function Documentation

◆ buildFromHarmonics()

template<typename Coefficients >
bool dspark::WavetableBank::buildFromHarmonics ( int  numFrames,
int  numHarmonics,
Coefficients &&  coefficients,
Normalization  normalization = Normalization::PerFrame 
)
inline

Builds the bank from per-frame harmonic amplitudes.

Parameters
numFramesFrame count, 1..kMaxFrames.
numHarmonicsHighest harmonic the frames hold, 1..kMaxHarmonics.
coefficientsCallable Harmonic(int frame, int harmonic) for harmonic 1..numHarmonics (the DC is not stored).
normalizationFrame scaling.
Returns
False (bank unchanged) on an invalid count or a non-finite coefficient, or when every frame is silent.

Definition at line 953 of file WavetableBank.h.

◆ builtin()

static const WavetableBank & dspark::WavetableBank::builtin ( Builtin  which)
inlinestatic

A built-in bank, built on the first call and shared by the process.

Note
The first call per bank allocates about 1.7 to 2.5 MB and runs the synthesis: make it from a setup thread.

Definition at line 1041 of file WavetableBank.h.

◆ getMemoryBytes()

size_t dspark::WavetableBank::getMemoryBytes ( ) const
inlinenoexcept

Bytes of table storage.

Definition at line 1063 of file WavetableBank.h.

◆ getNumFrames()

int dspark::WavetableBank::getNumFrames ( ) const
inlinenoexcept

Frames in the bank (0 before a successful build).

Definition at line 1057 of file WavetableBank.h.

◆ getNumHarmonics()

int dspark::WavetableBank::getNumHarmonics ( ) const
inlinenoexcept

Highest harmonic the frames hold (0 before a build).

Definition at line 1060 of file WavetableBank.h.

◆ interpolatorResponse()

static double dspark::WavetableBank::interpolatorResponse ( double  cyclesPerSample)
inlinestaticnoexcept

Amplitude response of the reader's interpolator at a frequency in cycles per table sample (the value pre-emphasis divides by).

Definition at line 1189 of file WavetableBank.h.

◆ loadFrames()

template<FloatType S>
bool dspark::WavetableBank::loadFrames ( const S *  data,
int  frameSize,
int  numFrames,
Normalization  normalization = Normalization::Bank 
)
inline

Builds the bank from consecutive single-cycle frames.

Each frame is analysed exactly (an FFT for power-of-two sizes, a direct transform otherwise); its DC is discarded, and so is the Nyquist bin of an even size. A frame of N samples yields (N - 1) / 2 harmonics, at most kMaxHarmonics. Non-finite samples count as 0.

Parameters
datanumFrames * frameSize samples, frame after frame.
frameSizeSamples per cycle, 3..kMaxFrameSize.
numFramesFrame count, 1..kMaxFrames.
normalizationFrame scaling (Bank keeps the frames' relative levels).
Returns
False (bank unchanged) on invalid arguments or silent input.

Definition at line 987 of file WavetableBank.h.

◆ read()

template<FloatType T>
T dspark::WavetableBank::read ( double  phase,
double  position,
const Mip &  mip 
) const
inlinenoexcept

One sample of the bank.

Parameters
phaseCycle phase in [0, 1) (wrapped if outside).
positionFrame position in [0, frames - 1], blended linearly between adjacent frames (clamped).
mipSelection from selectMip() for the current pitch.
Returns
0 for an empty bank.

Definition at line 1110 of file WavetableBank.h.

◆ readBlock() [1/2]

template<FloatType T>
void dspark::WavetableBank::readBlock ( const double *  phases,
const double *  positions,
const Mip &  mip,
T *  out,
int  n 
) const
inlinenoexcept

A block of samples with one frame position per sample.

Parameters
phasesn cycle phases in [0, 1) (wrapped if outside).
positionsn frame positions (clamped as by read()).
mipSelection from selectMip() for the current pitch.
outn output samples (0 for an empty bank).
nSample count; nothing happens for n <= 0.

Definition at line 1179 of file WavetableBank.h.

◆ readBlock() [2/2]

template<FloatType T>
void dspark::WavetableBank::readBlock ( const double *  phases,
double  position,
const Mip &  mip,
T *  out,
int  n 
) const
inlinenoexcept

A block of samples at one frame position: read() at each phase.

The values of read() sample by sample (bit for bit, unless the compiler contracts the scalar arithmetic into fused multiply-adds) at a fraction of its cost: the level and frame setup is done once per block and, on SSE2 targets, four float or two double samples are interpolated at once. Real-time safe.

Parameters
phasesn cycle phases in [0, 1) (wrapped if outside).
positionFrame position of every sample (clamped as by read()).
mipSelection from selectMip() for the current pitch.
outn output samples (0 for an empty bank).
nSample count; nothing happens for n <= 0.

Definition at line 1165 of file WavetableBank.h.

◆ selectMip()

Mip dspark::WavetableBank::selectMip ( double  frequencyHz,
double  sampleRate 
) const
inlinenoexcept

Chooses the level pair and crossfade weight for a pitch.

Call it when the frequency changes (a synthesizer voice does at its control rate), not per sample. A negative frequency selects as its magnitude; a non-finite one selects the dullest level.

Definition at line 1074 of file WavetableBank.h.

Member Data Documentation

◆ kMaxFrames

constexpr int dspark::WavetableBank::kMaxFrames = 256
staticconstexpr

Most frames a bank holds.

Definition at line 901 of file WavetableBank.h.

◆ kMaxFrameSize

constexpr int dspark::WavetableBank::kMaxFrameSize = 1 << 20
staticconstexpr

Largest single-cycle frame loadFrames() analyses.

Definition at line 903 of file WavetableBank.h.

◆ kMaxHarmonics

constexpr int dspark::WavetableBank::kMaxHarmonics = 1023
staticconstexpr

Harmonics of the richest level.

Definition at line 899 of file WavetableBank.h.

◆ kMipLevels

constexpr int dspark::WavetableBank::kMipLevels = 20
staticconstexpr

Mip levels, half an octave apart.

Definition at line 897 of file WavetableBank.h.

◆ kNumBuiltins

constexpr int dspark::WavetableBank::kNumBuiltins = 4
staticconstexpr

Definition at line 913 of file WavetableBank.h.


The documentation for this class was generated from the following file: