DSPark 1.9.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>

Public Types

using LoopMode = SampleLoopMode
 How a zone loops (see SampleLoopMode).
 
using ZoneSpec = SampleZoneSpec< T >
 One zone, as the caller describes it (see SampleZoneSpec). The audio is copied.
 

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, smoothed over about 10 ms.
 
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 noteOnTuned (int key, T velocity, T tuningCents, int offset=0) noexcept
 Starts a note with individual fine tuning in cents, clamped to -100..100. Tuning adds to the zone tuning and channel pitch bend; key selection, retrigger and note-off identity remain the integer key. 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, 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().
 

Static Public Attributes

static constexpr int kMaxEvents = 1024
 

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:116
void processBlock(AudioBufferView< T > out) noexcept
Renders the block, overwriting out, and applies the queued events at their offsets....
Definition Sampler.h:310
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:255
bool loadSample(AudioBufferView< const T > audio, double sampleRate, int rootKey=60)
One zone over the whole keyboard and velocity range.
Definition Sampler.h:208
void prepare(const AudioSpec &spec, int maxVoices=32)
Allocates the voices and the event queue.
Definition Sampler.h:149
Template Parameters
TSample type (float or double).

Definition at line 115 of file Sampler.h.

Member Typedef Documentation

◆ LoopMode

template<FloatType T>
using dspark::Sampler< T >::LoopMode = SampleLoopMode

How a zone loops (see SampleLoopMode).

Definition at line 124 of file Sampler.h.

◆ ZoneSpec

template<FloatType T>
using dspark::Sampler< T >::ZoneSpec = SampleZoneSpec<T>

One zone, as the caller describes it (see SampleZoneSpec). The audio is copied.

Definition at line 128 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 131 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 294 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 218 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 340 of file Sampler.h.

◆ getPolyphony()

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

Polyphony set by prepare().

Definition at line 346 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 208 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 275 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 255 of file Sampler.h.

◆ noteOnTuned()

template<FloatType T>
void dspark::Sampler< 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. Tuning adds to the zone tuning and channel pitch bend; key selection, retrigger and note-off identity remain the integer key. 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 265 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 149 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 310 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 170 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 224 of file Sampler.h.

◆ setGain()

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

Output gain in dB, smoothed over about 10 ms.

Definition at line 244 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 287 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 281 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 236 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 192 of file Sampler.h.

Member Data Documentation

◆ kMaxEvents

template<FloatType T>
constexpr int dspark::Sampler< 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 121 of file Sampler.h.


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