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

Polyphonic multi-zone sample player. More...

#include <Sampler.h>

Classes

struct  ZoneSpec
 One zone, as the caller describes it. The audio is copied. More...
 

Public Types

enum class  LoopMode { None , Continuous , Sustain }
 How a zone loops. More...
 

Public Member Functions

 Sampler ()=default
 
 ~Sampler ()
 
 Sampler (const Sampler &)=delete
 
Sampler & operator= (const Sampler &)=delete
 
void prepare (const AudioSpec &spec, int maxVoices=32)
 Allocates the voices and the event queue.
 
void reset () noexcept
 Silences every voice and drops queued events. Audio thread.
 
bool setZones (std::span< const ZoneSpec > zones)
 Replaces the zone set.
 
bool loadSample (AudioBufferView< const T > audio, double sampleRate, int rootKey=60)
 One zone over the whole keyboard and velocity range.
 
void clearZones ()
 Publishes an empty zone set: sounding voices finish, new notes are silent.
 
void setEnvelope (T attackMs, T decayMs, T sustain, T releaseMs) noexcept
 ADSR for notes that start from now on (release: also for notes released from now on). Times in ms, sustain 0..1.
 
void setVelocityRange (T rangeDb) noexcept
 Velocity sensitivity: velocity 0 plays rangeDb below velocity 1 (0..96 dB, default 24; 0 ignores velocity).
 
void setGain (T dB) noexcept
 Output gain in dB.
 
void noteOn (int key, T velocity, int offset=0) noexcept
 Starts a note at offset samples into the next block. Velocity 0 is a note-off, as in MIDI.
 
void noteOff (int key, int offset=0) noexcept
 Releases a note at offset samples into the next block.
 
void setSustainPedal (bool down, int offset=0) noexcept
 Sustain pedal: while down, released keys keep sounding.
 
void setPitchBend (T semitones, int offset=0) noexcept
 Pitch bend in semitones (-48..48), applied to every voice.
 
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.
 
int getActiveVoiceCount () const noexcept
 Voices sounding after the last block, stealing fades included.
 
int getPolyphony () const noexcept
 Polyphony set by prepare().
 

Detailed Description

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

Polyphonic multi-zone sample player.

sampler.prepare(spec, 32); // 32 voices
sampler.loadSample(piano, 48000.0, 60); // one zone, root C4
// audio thread, per block:
sampler.noteOn(64, 0.8f, offsetInBlock);
sampler.processBlock(output);
Polyphonic multi-zone sample player.
Definition Sampler.h:105
void processBlock(AudioBufferView< T > out) noexcept
Renders the block, overwriting out, and applies the queued events at their offsets....
Definition Sampler.h:304
void noteOn(int key, T velocity, int offset=0) noexcept
Starts a note at offset samples into the next block. Velocity 0 is a note-off, as in MIDI.
Definition Sampler.h:260
bool loadSample(AudioBufferView< const T > audio, double sampleRate, int rootKey=60)
One zone over the whole keyboard and velocity range.
Definition Sampler.h:213
void prepare(const AudioSpec &spec, int maxVoices=32)
Allocates the voices and the event queue.
Definition Sampler.h:156
Template Parameters
TSample type (float or double).

Definition at line 104 of file Sampler.h.

Member Enumeration Documentation

◆ LoopMode

template<FloatType T>
enum class dspark::Sampler::LoopMode
strong

How a zone loops.

Enumerator
None 

Play once to the end of the recording.

Continuous 

Loop for as long as the voice sounds, release included.

Sustain 

Loop while the key is held, then play on past the loop end.

Definition at line 108 of file Sampler.h.

Constructor & Destructor Documentation

◆ Sampler() [1/2]

template<FloatType T>
dspark::Sampler< T >::Sampler ( )
default

◆ ~Sampler()

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

Definition at line 138 of file Sampler.h.

◆ Sampler() [2/2]

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

Member Function Documentation

◆ allNotesOff()

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

Releases every sounding note (and lifts the pedal).

Definition at line 288 of file Sampler.h.

◆ clearZones()

template<FloatType T>
void dspark::Sampler< T >::clearZones ( )
inline

Publishes an empty zone set: sounding voices finish, new notes are silent.

Definition at line 223 of file Sampler.h.

◆ getActiveVoiceCount()

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

Voices sounding after the last block, stealing fades included.

Definition at line 339 of file Sampler.h.

◆ getPolyphony()

template<FloatType T>
int dspark::Sampler< T >::getPolyphony ( ) const
inlinenoexcept

Polyphony set by prepare().

Definition at line 345 of file Sampler.h.

◆ loadSample()

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

One zone over the whole keyboard and velocity range.

Definition at line 213 of file Sampler.h.

◆ noteOff()

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

Releases a note at offset samples into the next block.

Definition at line 269 of file Sampler.h.

◆ noteOn()

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

Starts a note at offset samples into the next block. Velocity 0 is a note-off, as in MIDI.

Definition at line 260 of file Sampler.h.

◆ operator=()

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

◆ prepare()

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

Allocates the voices and the event queue.

Parameters
specOutput sample rate and channel count.
maxVoicesPolyphony, 1..256. Spare voices for stealing fades are added on top.

Definition at line 156 of file Sampler.h.

◆ processBlock()

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

Renders the block, overwriting out, and applies the queued events at their offsets. Allocation free, lock free.

One output channel receives the mean of a zone's channels; two or more receive a mono zone on both of the first two (constant-power pan, unity at centre) or a multichannel zone's first two channels with the pan as balance. Further output channels are left silent.

Definition at line 304 of file Sampler.h.

◆ reset()

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

Silences every voice and drops queued events. Audio thread.

Definition at line 176 of file Sampler.h.

◆ setEnvelope()

template<FloatType T>
void dspark::Sampler< T >::setEnvelope ( T  attackMs,
T  decayMs,
T  sustain,
T  releaseMs 
)
inlinenoexcept

ADSR for notes that start from now on (release: also for notes released from now on). Times in ms, sustain 0..1.

Definition at line 229 of file Sampler.h.

◆ setGain()

template<FloatType T>
void dspark::Sampler< T >::setGain ( T  dB)
inlinenoexcept

Output gain in dB.

Definition at line 249 of file Sampler.h.

◆ setPitchBend()

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

Pitch bend in semitones (-48..48), applied to every voice.

Definition at line 281 of file Sampler.h.

◆ setSustainPedal()

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

Sustain pedal: while down, released keys keep sounding.

Definition at line 275 of file Sampler.h.

◆ setVelocityRange()

template<FloatType T>
void dspark::Sampler< T >::setVelocityRange ( T  rangeDb)
inlinenoexcept

Velocity sensitivity: velocity 0 plays rangeDb below velocity 1 (0..96 dB, default 24; 0 ignores velocity).

Definition at line 241 of file Sampler.h.

◆ setZones()

template<FloatType T>
bool dspark::Sampler< T >::setZones ( std::span< const ZoneSpec >  zones)
inline

Replaces the zone set.

Validates and copies every zone, bakes loop crossfades, and publishes the set without blocking the audio thread. A zone with no audio, an invalid rate or an empty key or velocity range is rejected, and so is the whole call. Loop points are clamped into the recording, and the crossfade to the samples available before the loop start and inside the loop.

Returns
True when the set was accepted.

Definition at line 197 of file Sampler.h.


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