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

Polyphonic synthesizer with analog, FM and wavetable engines and a sample source. More...

#include <Synthesizer.h>

Public Types

using Engine = synthesizer::Engine
 
using VoiceMode = synthesizer::VoiceMode
 
using GlideMode = synthesizer::GlideMode
 
using PhaseMode = synthesizer::PhaseMode
 
using OscWave = synthesizer::OscWave
 
using SubWave = synthesizer::SubWave
 
using FilterType = synthesizer::FilterType
 
using LfoMode = synthesizer::LfoMode
 
using ModSource = synthesizer::ModSource
 
using ModDest = synthesizer::ModDest
 
using Param = synthesizer::Param
 
using ParamInfo = synthesizer::ParamInfo
 

Public Member Functions

 Synthesizer () noexcept
 
 ~Synthesizer ()
 
 Synthesizer (const Synthesizer &)=delete
 
Synthesizer & operator= (const Synthesizer &)=delete
 
void prepare (const AudioSpec &spec, int maxVoices=16)
 Allocates the voices, buffers and oversamplers and builds the shared tables (minBLEP, sample kernel, built-in wavetables).
 
void reset () noexcept
 Silences every voice, drops queued events and restarts the note count, the controllers and the free LFOs. Audio thread.
 
int getLatency () const noexcept
 Output delay in base-rate samples (0 at 1x; 64 at 2x, 68 at 4x).
 
int getActiveVoiceCount () const noexcept
 Voices sounding after the last block, stealing fades included.
 
int getMaxVoices () const noexcept
 Polyphony prepared (the upper bound of voice.polyphony).
 
void setParameter (Param param, float value) noexcept
 Sets a parameter, clamped to its range and rounded to its steps; NaN is ignored. Takes effect at the next block.
 
bool setParameter (std::string_view id, float value) noexcept
 Sets a parameter by id; false for an unknown id.
 
float getParameter (Param param) const noexcept
 The value last set (any thread).
 
void setTempo (double bpm) noexcept
 Tempo for tempo-synced LFOs, 1..999 BPM (default 120).
 
bool setSampleZones (std::span< const SampleZoneSpec< T > > zones)
 Replaces the zones the sample source plays (sample.level).
 
bool loadSample (AudioBufferView< const T > audio, double sampleRate, int rootKey=60)
 One zone over the whole keyboard and velocity range.
 
void clearSampleZones ()
 Removes the sample zones (the user wavetable is kept).
 
bool loadUserWavetable (const T *frames, int frameSize, int numFrames)
 Loads the user wavetable (bank "User"): consecutive single-cycle frames, analysed as WavetableBank::loadFrames() does.
 
void noteOn (int key, T velocity, int offset=0) noexcept
 Starts a note offset samples into the next block; velocity 0..1 (0 is a note-off, as in MIDI).
 
void noteOnTuned (int key, T velocity, T tuningCents, int offset=0) noexcept
 Starts a note with individual fine tuning in cents, clamped to -100..100. The tuned pitch is the glide target and combines with master tuning, oscillators/zones and pitch bend. Key identity, key modulation/tracking and sample-zone selection use the integer key. Fixed-frequency FM operators and untracked sample sources ignore it. Mono/legato return restores the tuning of the most recent key still held. Non-finite velocity or tuning is ignored. Zero velocity is note-off. Audio thread; the event applies at offset in the next block.
 
void noteOff (int key, T releaseVelocity=T(0), int offset=0) noexcept
 Releases a note; releaseVelocity 0..1 is a modulation source.
 
void setPitchBend (T bend, int offset=0) noexcept
 Pitch bend, -1..+1 (scaled by voice.bendRange semitones).
 
void setModWheel (T value, int offset=0) noexcept
 Mod wheel (CC 1), 0..1.
 
void setChannelPressure (T value, int offset=0) noexcept
 Channel pressure, 0..1.
 
void setPolyPressure (int key, T value, int offset=0) noexcept
 Polyphonic pressure of one key, 0..1.
 
void setSustainPedal (bool down, int offset=0) noexcept
 Sustain pedal: while down, released keys keep sounding.
 
void setController (int controller, T value, int offset=0) noexcept
 A MIDI control change, value 0..1: 1 mod wheel, 2 breath, 11 expression (it also scales the amplifier, as MIDI defines), 64 sustain pedal (down at 0.5 and above), 120 and 123 all notes off. Other controllers are ignored.
 
void allNotesOff (int offset=0) noexcept
 Releases every sounding note (and lifts the pedal).
 
void processBlock (AudioBufferView< T > out) noexcept
 Renders the block, overwriting out, and applies the queued events at their offsets. Allocation free, lock free. A block longer than prepare()'s maxBlockSize is rendered in pieces.
 
std::vector< uint8_t > getState () const
 Serializes every parameter by id (allocates). Samples and the user wavetable are data, not parameters: they are not included.
 
bool setState (const uint8_t *data, size_t size)
 Restores parameters from a blob (tolerant: missing ids keep their defaults, unknown ids are ignored; rejects foreign blobs).
 

Static Public Member Functions

static constexpr const ParamInfo & getParamInfo (Param p) noexcept
 The description of a parameter.
 
static int findParam (std::string_view id) noexcept
 Index of the parameter with this id, or -1.
 

Static Public Attributes

static constexpr int kMaxEvents = 1024
 
static constexpr int kNumParams = synthesizer::kNumParams
 
static constexpr int kMaxUnison = synthesizer::kMaxUnison
 
static constexpr int kModSlots = synthesizer::kModSlots
 
static constexpr int kMaxPolyphony = synthesizer::kMaxPolyphony
 
static constexpr int kControlPeriod = 16
 Base-rate samples between modulation evaluations (interpolated between).
 

Detailed Description

template<FloatType T>
class dspark::Synthesizer< T >

Polyphonic synthesizer with analog, FM and wavetable engines and a sample source.

synth.prepare({ 48000.0, 512, 2 }, 16);
synth.setParameter(P::Osc1Wave, 3.0f); // pulse
synth.setParameter(P::FilterCutoff, 1200.0f);
synth.setParameter(P::FilterEnvAmount, 4.0f); // octaves
// audio thread, per block:
synth.noteOn(48, 0.9f, offsetInBlock);
synth.processBlock(output);
Polyphonic synthesizer with analog, FM and wavetable engines and a sample source.
void noteOn(int key, T velocity, int offset=0) noexcept
Starts a note offset samples into the next block; velocity 0..1 (0 is a note-off, as in MIDI).
void setParameter(Param param, float value) noexcept
Sets a parameter, clamped to its range and rounded to its steps; NaN is ignored. Takes effect at the ...
void prepare(const AudioSpec &spec, int maxVoices=16)
Allocates the voices, buffers and oversamplers and builds the shared tables (minBLEP,...
void processBlock(AudioBufferView< T > out) noexcept
Renders the block, overwriting out, and applies the queued events at their offsets....
Param
Every parameter, in the order of the parameter table.
Template Parameters
TSample type (float or double).

Definition at line 520 of file Synthesizer.h.

Member Typedef Documentation

◆ Engine

template<FloatType T>
using dspark::Synthesizer< T >::Engine = synthesizer::Engine

Definition at line 528 of file Synthesizer.h.

◆ FilterType

template<FloatType T>
using dspark::Synthesizer< T >::FilterType = synthesizer::FilterType

Definition at line 534 of file Synthesizer.h.

◆ GlideMode

template<FloatType T>
using dspark::Synthesizer< T >::GlideMode = synthesizer::GlideMode

Definition at line 530 of file Synthesizer.h.

◆ LfoMode

template<FloatType T>
using dspark::Synthesizer< T >::LfoMode = synthesizer::LfoMode

Definition at line 535 of file Synthesizer.h.

◆ ModDest

template<FloatType T>
using dspark::Synthesizer< T >::ModDest = synthesizer::ModDest

Definition at line 537 of file Synthesizer.h.

◆ ModSource

template<FloatType T>
using dspark::Synthesizer< T >::ModSource = synthesizer::ModSource

Definition at line 536 of file Synthesizer.h.

◆ OscWave

template<FloatType T>
using dspark::Synthesizer< T >::OscWave = synthesizer::OscWave

Definition at line 532 of file Synthesizer.h.

◆ Param

template<FloatType T>
using dspark::Synthesizer< T >::Param = synthesizer::Param

Definition at line 538 of file Synthesizer.h.

◆ ParamInfo

template<FloatType T>
using dspark::Synthesizer< T >::ParamInfo = synthesizer::ParamInfo

Definition at line 539 of file Synthesizer.h.

◆ PhaseMode

template<FloatType T>
using dspark::Synthesizer< T >::PhaseMode = synthesizer::PhaseMode

Definition at line 531 of file Synthesizer.h.

◆ SubWave

template<FloatType T>
using dspark::Synthesizer< T >::SubWave = synthesizer::SubWave

Definition at line 533 of file Synthesizer.h.

◆ VoiceMode

template<FloatType T>
using dspark::Synthesizer< T >::VoiceMode = synthesizer::VoiceMode

Definition at line 529 of file Synthesizer.h.

Constructor & Destructor Documentation

◆ Synthesizer() [1/2]

template<FloatType T>
dspark::Synthesizer< T >::Synthesizer ( )
inlinenoexcept

Definition at line 559 of file Synthesizer.h.

◆ ~Synthesizer()

template<FloatType T>
dspark::Synthesizer< T >::~Synthesizer ( )
inline

Definition at line 568 of file Synthesizer.h.

◆ Synthesizer() [2/2]

template<FloatType T>
dspark::Synthesizer< T >::Synthesizer ( const Synthesizer< T > &  )
delete

Member Function Documentation

◆ allNotesOff()

template<FloatType T>
void dspark::Synthesizer< T >::allNotesOff ( int  offset = 0)
inlinenoexcept

Releases every sounding note (and lifts the pedal).

Definition at line 832 of file Synthesizer.h.

◆ clearSampleZones()

template<FloatType T>
void dspark::Synthesizer< T >::clearSampleZones ( )
inline

Removes the sample zones (the user wavetable is kept).

Definition at line 733 of file Synthesizer.h.

◆ findParam()

template<FloatType T>
static int dspark::Synthesizer< T >::findParam ( std::string_view  id)
inlinestaticnoexcept

Index of the parameter with this id, or -1.

Definition at line 555 of file Synthesizer.h.

◆ getActiveVoiceCount()

template<FloatType T>
int dspark::Synthesizer< T >::getActiveVoiceCount ( ) const
inlinenoexcept

Voices sounding after the last block, stealing fades included.

Definition at line 650 of file Synthesizer.h.

◆ getLatency()

template<FloatType T>
int dspark::Synthesizer< T >::getLatency ( ) const
inlinenoexcept

Output delay in base-rate samples (0 at 1x; 64 at 2x, 68 at 4x).

Definition at line 647 of file Synthesizer.h.

◆ getMaxVoices()

template<FloatType T>
int dspark::Synthesizer< T >::getMaxVoices ( ) const
inlinenoexcept

Polyphony prepared (the upper bound of voice.polyphony).

Definition at line 656 of file Synthesizer.h.

◆ getParameter()

template<FloatType T>
float dspark::Synthesizer< T >::getParameter ( Param  param) const
inlinenoexcept

The value last set (any thread).

Definition at line 682 of file Synthesizer.h.

◆ getParamInfo()

template<FloatType T>
static constexpr const ParamInfo & dspark::Synthesizer< T >::getParamInfo ( Param  p)
inlinestaticconstexprnoexcept

The description of a parameter.

Definition at line 549 of file Synthesizer.h.

◆ getState()

template<FloatType T>
std::vector< uint8_t > dspark::Synthesizer< T >::getState ( ) const
inline

Serializes every parameter by id (allocates). Samples and the user wavetable are data, not parameters: they are not included.

Definition at line 872 of file Synthesizer.h.

◆ loadSample()

template<FloatType T>
bool dspark::Synthesizer< T >::loadSample ( AudioBufferView< const T >  audio,
double  sampleRate,
int  rootKey = 60 
)
inline

One zone over the whole keyboard and velocity range.

Definition at line 723 of file Synthesizer.h.

◆ loadUserWavetable()

template<FloatType T>
bool dspark::Synthesizer< T >::loadUserWavetable ( const T *  frames,
int  frameSize,
int  numFrames 
)
inline

Loads the user wavetable (bank "User"): consecutive single-cycle frames, analysed as WavetableBank::loadFrames() does.

Returns
False (unchanged) on invalid frames.

Definition at line 744 of file Synthesizer.h.

◆ noteOff()

template<FloatType T>
void dspark::Synthesizer< T >::noteOff ( int  key,
T  releaseVelocity = T(0),
int  offset = 0 
)
inlinenoexcept

Releases a note; releaseVelocity 0..1 is a modulation source.

Definition at line 780 of file Synthesizer.h.

◆ noteOn()

template<FloatType T>
void dspark::Synthesizer< T >::noteOn ( int  key,
T  velocity,
int  offset = 0 
)
inlinenoexcept

Starts a note offset samples into the next block; velocity 0..1 (0 is a note-off, as in MIDI).

Definition at line 757 of file Synthesizer.h.

◆ noteOnTuned()

template<FloatType T>
void dspark::Synthesizer< T >::noteOnTuned ( int  key,
T  velocity,
T  tuningCents,
int  offset = 0 
)
inlinenoexcept

Starts a note with individual fine tuning in cents, clamped to -100..100. The tuned pitch is the glide target and combines with master tuning, oscillators/zones and pitch bend. Key identity, key modulation/tracking and sample-zone selection use the integer key. Fixed-frequency FM operators and untracked sample sources ignore it. Mono/legato return restores the tuning of the most recent key still held. Non-finite velocity or tuning is ignored. Zero velocity is note-off. Audio thread; the event applies at offset in the next block.

Definition at line 770 of file Synthesizer.h.

◆ operator=()

template<FloatType T>
Synthesizer & dspark::Synthesizer< T >::operator= ( const Synthesizer< T > &  )
delete

◆ prepare()

template<FloatType T>
void dspark::Synthesizer< T >::prepare ( const AudioSpec &  spec,
int  maxVoices = 16 
)
inline

Allocates the voices, buffers and oversamplers and builds the shared tables (minBLEP, sample kernel, built-in wavetables).

Parameters
specSample rate and the largest block (channels are taken from the view at processBlock()).
maxVoicesPolyphony available to voice.polyphony, 1..kMaxPolyphony. Spare voices for stealing fades are added on top.

Definition at line 589 of file Synthesizer.h.

◆ processBlock()

template<FloatType T>
void dspark::Synthesizer< T >::processBlock ( AudioBufferView< T >  out)
inlinenoexcept

Renders the block, overwriting out, and applies the queued events at their offsets. Allocation free, lock free. A block longer than prepare()'s maxBlockSize is rendered in pieces.

Definition at line 841 of file Synthesizer.h.

◆ reset()

template<FloatType T>
void dspark::Synthesizer< T >::reset ( )
inlinenoexcept

Silences every voice, drops queued events and restarts the note count, the controllers and the free LFOs. Audio thread.

Definition at line 619 of file Synthesizer.h.

◆ setChannelPressure()

template<FloatType T>
void dspark::Synthesizer< T >::setChannelPressure ( T  value,
int  offset = 0 
)
inlinenoexcept

Channel pressure, 0..1.

Definition at line 797 of file Synthesizer.h.

◆ setController()

template<FloatType T>
void dspark::Synthesizer< T >::setController ( int  controller,
T  value,
int  offset = 0 
)
inlinenoexcept

A MIDI control change, value 0..1: 1 mod wheel, 2 breath, 11 expression (it also scales the amplifier, as MIDI defines), 64 sustain pedal (down at 0.5 and above), 120 and 123 all notes off. Other controllers are ignored.

Definition at line 817 of file Synthesizer.h.

◆ setModWheel()

template<FloatType T>
void dspark::Synthesizer< T >::setModWheel ( T  value,
int  offset = 0 
)
inlinenoexcept

Mod wheel (CC 1), 0..1.

Definition at line 794 of file Synthesizer.h.

◆ setParameter() [1/2]

template<FloatType T>
void dspark::Synthesizer< T >::setParameter ( Param  param,
float  value 
)
inlinenoexcept

Sets a parameter, clamped to its range and rounded to its steps; NaN is ignored. Takes effect at the next block.

Definition at line 662 of file Synthesizer.h.

◆ setParameter() [2/2]

template<FloatType T>
bool dspark::Synthesizer< T >::setParameter ( std::string_view  id,
float  value 
)
inlinenoexcept

Sets a parameter by id; false for an unknown id.

Definition at line 673 of file Synthesizer.h.

◆ setPitchBend()

template<FloatType T>
void dspark::Synthesizer< T >::setPitchBend ( T  bend,
int  offset = 0 
)
inlinenoexcept

Pitch bend, -1..+1 (scaled by voice.bendRange semitones).

Definition at line 787 of file Synthesizer.h.

◆ setPolyPressure()

template<FloatType T>
void dspark::Synthesizer< T >::setPolyPressure ( int  key,
T  value,
int  offset = 0 
)
inlinenoexcept

Polyphonic pressure of one key, 0..1.

Definition at line 800 of file Synthesizer.h.

◆ setSampleZones()

template<FloatType T>
bool dspark::Synthesizer< T >::setSampleZones ( std::span< const SampleZoneSpec< T > >  zones)
inline

Replaces the zones the sample source plays (sample.level).

Validates and copies every zone (Sampler's rules) and publishes them without blocking the audio thread. A note plays the FIRST zone whose key and velocity ranges contain it, so velocity layers and multisamples work, but zones are not layered as in Sampler. The user wavetable is kept.

Returns
False (zones unchanged) when a zone is invalid.

Definition at line 707 of file Synthesizer.h.

◆ setState()

template<FloatType T>
bool dspark::Synthesizer< T >::setState ( const uint8_t *  data,
size_t  size 
)
inline

Restores parameters from a blob (tolerant: missing ids keep their defaults, unknown ids are ignored; rejects foreign blobs).

Definition at line 882 of file Synthesizer.h.

◆ setSustainPedal()

template<FloatType T>
void dspark::Synthesizer< T >::setSustainPedal ( bool  down,
int  offset = 0 
)
inlinenoexcept

Sustain pedal: while down, released keys keep sounding.

Definition at line 806 of file Synthesizer.h.

◆ setTempo()

template<FloatType T>
void dspark::Synthesizer< T >::setTempo ( double  bpm)
inlinenoexcept

Tempo for tempo-synced LFOs, 1..999 BPM (default 120).

Definition at line 689 of file Synthesizer.h.

Member Data Documentation

◆ kControlPeriod

template<FloatType T>
constexpr int dspark::Synthesizer< T >::kControlPeriod = 16
staticconstexpr

Base-rate samples between modulation evaluations (interpolated between).

Definition at line 546 of file Synthesizer.h.

◆ kMaxEvents

template<FloatType T>
constexpr int dspark::Synthesizer< T >::kMaxEvents = 1024
staticconstexpr

Maximum queued events between processBlock calls. Events beyond this capacity are ignored; hosts must split dense spans beforehand. Every event kind shares the same queue.

Definition at line 526 of file Synthesizer.h.

◆ kMaxPolyphony

template<FloatType T>
constexpr int dspark::Synthesizer< T >::kMaxPolyphony = synthesizer::kMaxPolyphony
staticconstexpr

Definition at line 544 of file Synthesizer.h.

◆ kMaxUnison

template<FloatType T>
constexpr int dspark::Synthesizer< T >::kMaxUnison = synthesizer::kMaxUnison
staticconstexpr

Definition at line 542 of file Synthesizer.h.

◆ kModSlots

template<FloatType T>
constexpr int dspark::Synthesizer< T >::kModSlots = synthesizer::kModSlots
staticconstexpr

Definition at line 543 of file Synthesizer.h.

◆ kNumParams

template<FloatType T>
constexpr int dspark::Synthesizer< T >::kNumParams = synthesizer::kNumParams
staticconstexpr

Definition at line 541 of file Synthesizer.h.


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