Mipmapped multi-frame wavetable storage and its morphing reader.
More...
#include <WavetableBank.h>
|
| | 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 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).
|
| |
Mipmapped multi-frame wavetable storage and its morphing reader.
auto mip = bank.selectMip(frequencyHz, sampleRate);
double phase = 0.0;
double phases[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);
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.
◆ Builtin
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.
◆ WavetableBank()
| dspark::WavetableBank::WavetableBank |
( |
| ) |
|
|
default |
◆ buildFromHarmonics()
template<typename Coefficients >
Builds the bank from per-frame harmonic amplitudes.
- Parameters
-
| numFrames | Frame count, 1..kMaxFrames. |
| numHarmonics | Highest harmonic the frames hold, 1..kMaxHarmonics. |
| coefficients | Callable Harmonic(int frame, int harmonic) for harmonic 1..numHarmonics (the DC is not stored). |
| normalization | Frame 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()
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 |
◆ 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()
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
-
| data | numFrames * frameSize samples, frame after frame. |
| frameSize | Samples per cycle, 3..kMaxFrameSize. |
| numFrames | Frame count, 1..kMaxFrames. |
| normalization | Frame 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
-
| phase | Cycle phase in [0, 1) (wrapped if outside). |
| position | Frame position in [0, frames - 1], blended linearly between adjacent frames (clamped). |
| mip | Selection 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
-
| phases | n cycle phases in [0, 1) (wrapped if outside). |
| positions | n frame positions (clamped as by read()). |
| mip | Selection from selectMip() for the current pitch. |
| out | n output samples (0 for an empty bank). |
| n | Sample 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
-
| phases | n cycle phases in [0, 1) (wrapped if outside). |
| position | Frame position of every sample (clamped as by read()). |
| mip | Selection from selectMip() for the current pitch. |
| out | n output samples (0 for an empty bank). |
| n | Sample 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.
◆ kMaxFrames
| constexpr int dspark::WavetableBank::kMaxFrames = 256 |
|
staticconstexpr |
◆ kMaxFrameSize
| constexpr int dspark::WavetableBank::kMaxFrameSize = 1 << 20 |
|
staticconstexpr |
◆ kMaxHarmonics
| constexpr int dspark::WavetableBank::kMaxHarmonics = 1023 |
|
staticconstexpr |
◆ kMipLevels
| constexpr int dspark::WavetableBank::kMipLevels = 20 |
|
staticconstexpr |
◆ kNumBuiltins
| constexpr int dspark::WavetableBank::kNumBuiltins = 4 |
|
staticconstexpr |
The documentation for this class was generated from the following file: