hydrogen 1.2.6
Sampler Class Reference

Waveform based sampler. More...

#include <Sampler.h>

Inheritance diagram for Sampler:
Object< Sampler > Base

Public Types

enum  PAN_LAW_TYPES {
  RATIO_STRAIGHT_POLYGONAL = 0 , RATIO_CONST_POWER , RATIO_CONST_SUM , LINEAR_STRAIGHT_POLYGONAL ,
  LINEAR_CONST_POWER , LINEAR_CONST_SUM , POLAR_STRAIGHT_POLYGONAL , POLAR_CONST_POWER ,
  POLAR_CONST_SUM , QUADRATIC_STRAIGHT_POLYGONAL , QUADRATIC_CONST_POWER , QUADRATIC_CONST_SUM ,
  LINEAR_CONST_K_NORM , RATIO_CONST_K_NORM , POLAR_CONST_K_NORM , QUADRATIC_CONST_K_NORM
}
 PAN LAWS The following pan law functions return pan_L (==L, which is the gain for Left channel). More...
 

Public Member Functions

 Sampler ()
 Constructor of the Sampler.
 
 ~Sampler ()
 
Interpolation::InterpolateMode getInterpolateMode ()
 
std::shared_ptr< InstrumentgetPlaybackTrackInstrument () const
 
int getPlayingNotesNumber ()
 
const std::vector< Note * > getPlayingNotesQueue () const
 
std::shared_ptr< InstrumentgetPreviewInstrument () const
 
void handleSongSizeChange ()
 Recalculates all note starts and positions to make them valid again after the song size changed, e.g.
 
void handleTimelineOrTempoChange ()
 Recalculates all note starts to make them valid again after a TempoMarker was added to or deleted from the Timeline or the latter was activated.
 
bool isInstrumentPlaying (std::shared_ptr< Instrument > pInstr)
 
bool isRenderingNotes () const
 
void midiKeyboardNoteOff (int key)
 
void noteOff (Note *pNote)
 Stop playing a note.
 
void noteOn (Note *pNote)
 Start playing a note.
 
void preview_instrument (std::shared_ptr< Instrument > pInstr)
 
void preview_sample (std::shared_ptr< Sample > pSample, int length)
 Preview, uses only the first layer.
 
void process (uint32_t nFrames)
 
void reinitializePlaybackTrack ()
 Loading of the playback track.
 
void setInterpolateMode (Interpolation::InterpolateMode mode)
 
void stopPlayingNotes (std::shared_ptr< Instrument > pInstr=nullptr)
 
- Public Member Functions inherited from Object< Sampler >
 Object ()
 
 Object (const Object< Sampler > &other)
 
- Public Member Functions inherited from Base
 Base ()
 
 Base (const Base &other)
 
virtual const char * class_name () const
 
void logBacktrace () const
 Print the current stack at point into the debug log.
 
void Print (bool bShort=true) const
 Prints content of toQString() via DEBUGLOG.
 
virtual QString toQString (const QString &sPrefix="", bool bShort=true) const
 Formatted string version for debugging purposes.
 

Static Public Member Functions

static float getRatioPan (float fPan_L, float fPan_R)
 This function is used to load old version files (v<=1.1).
 
static float linearConstKNormPanLaw (float fPan, float k)
 
static float linearConstPowerPanLaw (float fPan)
 
static float linearConstSumPanLaw (float fPan)
 
static float linearStraightPolygonalPanLaw (float fPan)
 
static float polarConstKNormPanLaw (float fPan, float k)
 
static float polarConstPowerPanLaw (float fPan)
 
static float polarConstSumPanLaw (float fPan)
 
static float polarStraightPolygonalPanLaw (float fPan)
 
static float quadraticConstKNormPanLaw (float fPan, float k)
 
static float quadraticConstPowerPanLaw (float fPan)
 
static float quadraticConstSumPanLaw (float fPan)
 
static float quadraticStraightPolygonalPanLaw (float fPan)
 
static float ratioConstKNormPanLaw (float fPan, float k)
 
static float ratioConstPowerPanLaw (float fPan)
 
static float ratioConstSumPanLaw (float fPan)
 
static float ratioStraightPolygonalPanLaw (float fPan)
 
- Static Public Member Functions inherited from Base
static const char * _class_name ()
 return the class name
 
static QString base_clock (const QString &sMsg)
 Measures the current time and stores it in __last_clock.
 
static QString base_clock_in (const QString &sMsg)
 
static int bootstrap (Logger *logger, bool count=false)
 must be called before any Object instantiation !
 
static bool count_active ()
 
static int getAliveObjectCount ()
 
static object_map_t getObjectMap ()
 
static Loggerlogger ()
 return the logger instance
 
static int objects_count ()
 
static void printObjectMapDiff (object_map_t map)
 Creates the difference between a snapshot of the object map and its current state and prints it to std::cout.
 
static void set_count (bool flag)
 enable/disable class instances counting
 
static void write_objects_map_to (std::ostream &out, object_map_t *map=nullptr)
 output the full objects map to a given ostream
 
static void write_objects_map_to_cerr ()
 output objects map to stderr
 

Data Fields

float * m_pMainOut_L
 sampler main out (left channel)
 
float * m_pMainOut_R
 sampler main out (right channel)
 

Static Public Attributes

static constexpr float K_NORM_DEFAULT = 1.33333333333333
 default k for pan law with -4.5dB center compensation, given L^k + R^k = const it is the mean compromise between constant sum and constant power
 
- Static Public Attributes inherited from Base
static QString sPrintIndention = " "
 String used to format the debugging string output of some core classes.
 

Private Member Functions

float panLaw (float fPan, std::shared_ptr< Song > pSong)
 function to direct the computation to the selected pan law function
 
bool processPlaybackTrack (int nBufferSize)
 
bool renderNote (Note *pNote, unsigned nBufferSize)
 Render a note.
 
bool renderNoteResample (std::shared_ptr< Sample > pSample, Note *pNote, std::shared_ptr< SelectedLayerInfo > pSelectedLayerInfo, std::shared_ptr< InstrumentComponent > pCompo, std::shared_ptr< DrumkitComponent > pDrumCompo, int nBufferSize, int nInitialBufferPos, float cost_L, float cost_R, float cost_track_L, float cost_track_R, float fLayerPitch)
 

Private Attributes

Interpolation::InterpolateMode m_interpolateMode
 
int m_nMaxLayers
 Maximum number of layers to be used in the Instrument editor.
 
int m_nPlayBackSamplePosition
 
std::vector< Note * > m_playingNotesQueue
 
std::shared_ptr< Instrumentm_pPlaybackTrackInstrument
 Instrument used for the playback track feature.
 
std::shared_ptr< Instrumentm_pPreviewInstrument
 Instrument used for the preview feature.
 
std::vector< Note * > m_queuedNoteOffs
 

Additional Inherited Members

- Protected Member Functions inherited from Object< Sampler >
 ~Object ()
 
- Protected Member Functions inherited from Base
 ~Base ()
 
- Static Protected Member Functions inherited from Base
static void registerClass (const char *name, const atomic_obj_cpt_t *counters)
 
- Static Protected Attributes inherited from Base
static bool __count = false
 should we count class instances
 
static timeval __last_clock = { 0, 0 }
 
static Logger__logger = nullptr
 
static bool bLogColors = true
 

Detailed Description

Waveform based sampler.

Definition at line 50 of file Sampler.h.

Member Enumeration Documentation

◆ PAN_LAW_TYPES

PAN LAWS The following pan law functions return pan_L (==L, which is the gain for Left channel).

They assume the fPan argument domain = [-1;1], which is used in Note and Instrument classes.

For the right channel use: R(p) == pan_R(p) = pan_L(-p) == L(-p)

thanks to the Left-Right symmetry.

The prefix of the function name tells the interpretation of the fPan argument:

"ratio" parameter: fPan = R/L - 1 if panned to the left, fPan = 1 - L/R if panned to the right.

"linear" parameter (arithmetic mean with linear weights): fPan = ( R - L ) / ( R + L ).

"polar" parameter (polar coordinate in LR plane): fPan = 4 / pi * arctan( R/L ) - 1 if L != 0, fPan = 1 if L == 0.

"quadratic" parameter (arithmetic mean with squared weights): fPan = ( R^2 - L^2 ) / ( R^2 + L^2 ).

Note: using a different fPan interpretation makes the output signal more central or more lateral. From more central to more lateral:

"quadratic" ---> "ratio" ---> "polar" ---> "linear"

After the prefix, the name describes the Image of the pan law in the LR plane. (remember that each pan law is a parametrized curve in LR plane. E.g.: "ConstantSum": it's properly used in an anechoic room, where there are no reflections. Ensures uniform volumes in MONO export, has -6.02 dB center compensation. "ConstantPower": probably makes uniform volumes in a common room, has -3.01 dB center compensation. "ConstantKNorm": L^k + R^k = const generalises constant sum (k = 1) and constant power (k = 2) "StraightPolygonal": one gain is constant while the other varies. It's ideal as BALANCE law of DUAL-channel track,

has 0 dB center compensation.

Some pan laws use expensive math functions like pow() and sqrt(). Pan laws can be approximated by polynomials, e.g. with degree = 2, to adjust the center compensation, but then you cannot control the interpretation of the fPan argument exactly.

Enumerator
RATIO_STRAIGHT_POLYGONAL 
RATIO_CONST_POWER 
RATIO_CONST_SUM 
LINEAR_STRAIGHT_POLYGONAL 
LINEAR_CONST_POWER 
LINEAR_CONST_SUM 
POLAR_STRAIGHT_POLYGONAL 
POLAR_CONST_POWER 
POLAR_CONST_SUM 
QUADRATIC_STRAIGHT_POLYGONAL 
QUADRATIC_CONST_POWER 
QUADRATIC_CONST_SUM 
LINEAR_CONST_K_NORM 
RATIO_CONST_K_NORM 
POLAR_CONST_K_NORM 
QUADRATIC_CONST_K_NORM 

Definition at line 104 of file Sampler.h.

Constructor & Destructor Documentation

◆ Sampler()

Sampler ( )

Constructor of the Sampler.

It is called by AudioEngine::AudioEngine() and stored in AudioEngine::m_pSampler.

Definition at line 70 of file Sampler.cpp.

◆ ~Sampler()

~Sampler ( )

Definition at line 95 of file Sampler.cpp.

Member Function Documentation

◆ getInterpolateMode()

Interpolation::InterpolateMode getInterpolateMode ( )
inline

Definition at line 211 of file Sampler.h.

◆ getPlaybackTrackInstrument()

std::shared_ptr< Instrument > getPlaybackTrackInstrument ( ) const
inline

Definition at line 207 of file Sampler.h.

◆ getPlayingNotesNumber()

int getPlayingNotesNumber ( )
inline

Definition at line 190 of file Sampler.h.

◆ getPlayingNotesQueue()

const std::vector< Note * > getPlayingNotesQueue ( ) const
inline

Definition at line 290 of file Sampler.h.

◆ getPreviewInstrument()

std::shared_ptr< Instrument > getPreviewInstrument ( ) const
inline

Definition at line 203 of file Sampler.h.

◆ getRatioPan()

float getRatioPan ( float fPan_L,
float fPan_R )
static

This function is used to load old version files (v<=1.1).

It returns the single pan parameter in [-1,1] from the L,R gains as it was input from the GUI (up to scale and translation, which is arbitrary). Default output is 0 (=central pan) if arguments are invalid. --—Historical Note--— Originally (version <= 1.0) pan_L,pan_R were actually gains for each channel; "instrument" and "note" pans were multiplied as in a gain CHAIN in each separate channel, so the chain killed the signal if instrument and note pans were hard-sided to opposites sides! In v1.1, pan_L and pan_R were still the members of Note/Instrument representing the pan knob position, still using the ratioStraightPolygonalPanLaw() for the correspondence (up to constant multiplication), but pan_L,pan_R were reconverted to single parameter in the Sampler, and fPan was used in the selected pan law.

Definition at line 252 of file Sampler.cpp.

◆ handleSongSizeChange()

void handleSongSizeChange ( )

Recalculates all note starts and positions to make them valid again after the song size changed, e.g.

a pattern was inserted or it's length was changed.

Definition at line 481 of file Sampler.cpp.

◆ handleTimelineOrTempoChange()

void handleTimelineOrTempoChange ( )

Recalculates all note starts to make them valid again after a TempoMarker was added to or deleted from the Timeline or the latter was activated.

Definition at line 421 of file Sampler.cpp.

◆ isInstrumentPlaying()

bool isInstrumentPlaying ( std::shared_ptr< Instrument > pInstr)

Definition at line 1418 of file Sampler.cpp.

◆ isRenderingNotes()

bool isRenderingNotes ( ) const
Returns
True, if the Sampler is still processing notes.

Definition at line 181 of file Sampler.cpp.

◆ linearConstKNormPanLaw()

float linearConstKNormPanLaw ( float fPan,
float k )
static

Definition at line 353 of file Sampler.cpp.

◆ linearConstPowerPanLaw()

float linearConstPowerPanLaw ( float fPan)
static

Definition at line 302 of file Sampler.cpp.

◆ linearConstSumPanLaw()

float linearConstSumPanLaw ( float fPan)
static

Definition at line 307 of file Sampler.cpp.

◆ linearStraightPolygonalPanLaw()

float linearStraightPolygonalPanLaw ( float fPan)
static

Definition at line 293 of file Sampler.cpp.

◆ midiKeyboardNoteOff()

void midiKeyboardNoteOff ( int key)

Definition at line 224 of file Sampler.cpp.

◆ noteOff()

void noteOff ( Note * pNote)

Stop playing a note.

This old note_off function is only used by right click on mixer channel strip play button all other note_off stuff will handle in midi_keyboard_note_off() and note_on()

Definition at line 236 of file Sampler.cpp.

◆ noteOn()

void noteOn ( Note * pNote)

Start playing a note.

Definition at line 185 of file Sampler.cpp.

◆ panLaw()

float panLaw ( float fPan,
std::shared_ptr< Song > pSong )
inlineprivate

function to direct the computation to the selected pan law function

Definition at line 380 of file Sampler.cpp.

◆ polarConstKNormPanLaw()

float polarConstKNormPanLaw ( float fPan,
float k )
static

Definition at line 363 of file Sampler.cpp.

◆ polarConstPowerPanLaw()

float polarConstPowerPanLaw ( float fPan)
static

Definition at line 322 of file Sampler.cpp.

◆ polarConstSumPanLaw()

float polarConstSumPanLaw ( float fPan)
static

Definition at line 328 of file Sampler.cpp.

◆ polarStraightPolygonalPanLaw()

float polarStraightPolygonalPanLaw ( float fPan)
static

Definition at line 312 of file Sampler.cpp.

◆ preview_instrument()

void preview_instrument ( std::shared_ptr< Instrument > pInstr)

Definition at line 1392 of file Sampler.cpp.

◆ preview_sample()

void preview_sample ( std::shared_ptr< Sample > pSample,
int length )

Preview, uses only the first layer.

Definition at line 1362 of file Sampler.cpp.

◆ process()

void process ( uint32_t nFrames)

Definition at line 106 of file Sampler.cpp.

◆ processPlaybackTrack()

bool processPlaybackTrack ( int nBufferSize)
private

Definition at line 797 of file Sampler.cpp.

◆ quadraticConstKNormPanLaw()

float quadraticConstKNormPanLaw ( float fPan,
float k )
static

Definition at line 358 of file Sampler.cpp.

◆ quadraticConstPowerPanLaw()

float quadraticConstPowerPanLaw ( float fPan)
static

Definition at line 343 of file Sampler.cpp.

◆ quadraticConstSumPanLaw()

float quadraticConstSumPanLaw ( float fPan)
static

Definition at line 348 of file Sampler.cpp.

◆ quadraticStraightPolygonalPanLaw()

float quadraticStraightPolygonalPanLaw ( float fPan)
static

Definition at line 334 of file Sampler.cpp.

◆ ratioConstKNormPanLaw()

float ratioConstKNormPanLaw ( float fPan,
float k )
static

Definition at line 370 of file Sampler.cpp.

◆ ratioConstPowerPanLaw()

float ratioConstPowerPanLaw ( float fPan)
static

Definition at line 275 of file Sampler.cpp.

◆ ratioConstSumPanLaw()

float ratioConstSumPanLaw ( float fPan)
static

Definition at line 284 of file Sampler.cpp.

◆ ratioStraightPolygonalPanLaw()

float ratioStraightPolygonalPanLaw ( float fPan)
static

Definition at line 266 of file Sampler.cpp.

◆ reinitializePlaybackTrack()

void reinitializePlaybackTrack ( )

Loading of the playback track.

The playback track is added to m_pPlaybackTrackInstrument as a new InstrumentLayer containing the loaded Sample. If Song::__playback_track_filename is empty, the layer will be loaded with a nullptr instead.

Definition at line 1430 of file Sampler.cpp.

◆ renderNote()

bool renderNote ( Note * pNote,
unsigned nBufferSize )
private

Render a note.

Returns
false - the note is not ended, true - the note is ended

Definition at line 511 of file Sampler.cpp.

◆ renderNoteResample()

bool renderNoteResample ( std::shared_ptr< Sample > pSample,
Note * pNote,
std::shared_ptr< SelectedLayerInfo > pSelectedLayerInfo,
std::shared_ptr< InstrumentComponent > pCompo,
std::shared_ptr< DrumkitComponent > pDrumCompo,
int nBufferSize,
int nInitialBufferPos,
float cost_L,
float cost_R,
float cost_track_L,
float cost_track_R,
float fLayerPitch )
private

Definition at line 987 of file Sampler.cpp.

◆ setInterpolateMode()

void setInterpolateMode ( Interpolation::InterpolateMode mode)
inline

Definition at line 199 of file Sampler.h.

◆ stopPlayingNotes()

void stopPlayingNotes ( std::shared_ptr< Instrument > pInstr = nullptr)

Definition at line 1335 of file Sampler.cpp.

Field Documentation

◆ K_NORM_DEFAULT

float K_NORM_DEFAULT = 1.33333333333333
staticconstexpr

default k for pan law with -4.5dB center compensation, given L^k + R^k = const it is the mean compromise between constant sum and constant power

Definition at line 126 of file Sampler.h.

◆ m_interpolateMode

Interpolation::InterpolateMode m_interpolateMode
private

Definition at line 272 of file Sampler.h.

◆ m_nMaxLayers

int m_nMaxLayers
private

Maximum number of layers to be used in the Instrument editor.

It will be inferred from InstrumentComponent::m_nMaxLayers, which itself is inferred from Preferences::m_nMaxLayers. Default value assigned in Preferences::Preferences(): 16.

Definition at line 253 of file Sampler.h.

◆ m_nPlayBackSamplePosition

int m_nPlayBackSamplePosition
private

Definition at line 255 of file Sampler.h.

◆ m_playingNotesQueue

std::vector<Note*> m_playingNotesQueue
private

Definition at line 239 of file Sampler.h.

◆ m_pMainOut_L

float* m_pMainOut_L

sampler main out (left channel)

Definition at line 162 of file Sampler.h.

◆ m_pMainOut_R

float* m_pMainOut_R

sampler main out (right channel)

Definition at line 163 of file Sampler.h.

◆ m_pPlaybackTrackInstrument

std::shared_ptr<Instrument> m_pPlaybackTrackInstrument
private

Instrument used for the playback track feature.

Definition at line 243 of file Sampler.h.

◆ m_pPreviewInstrument

std::shared_ptr<Instrument> m_pPreviewInstrument
private

Instrument used for the preview feature.

Definition at line 246 of file Sampler.h.

◆ m_queuedNoteOffs

std::vector<Note*> m_queuedNoteOffs
private

Definition at line 240 of file Sampler.h.