hydrogen 1.2.3
ADSR Class Reference

Attack Decay Sustain Release envelope. More...

#include <Adsr.h>

Inheritance diagram for ADSR:
Object< ADSR > Base

Public Types

enum class  State {
  Attack = 0 , Decay , Sustain , Release ,
  Idle
}
 possible states More...
 

Public Member Functions

 ADSR (const std::shared_ptr< ADSR > other)
 copy constructor
 
 ADSR (unsigned int attack=0, unsigned int decay=0, float sustain=1.0, unsigned int release=1000)
 constructor
 
 ~ADSR ()
 destructor
 
bool applyADSR (float *pLeft, float *pRight, int nFinalBufferPos, int nReleaseFrame, float fStep)
 Compute and apply successive ADSR values to stereo buffers.
 
void attack ()
 Sets m_state to State::Attack.
 
unsigned int getAttack ()
 
unsigned int getDecay ()
 
unsigned int getRelease ()
 
State getState () const
 
float getSustain ()
 
float release ()
 Sets m_state to State::Release and return the current m_fReleaseValue.
 
void setAttack (unsigned int value)
 
void setDecay (unsigned int value)
 
void setRelease (unsigned int value)
 
void setSustain (float value)
 
QString toQString (const QString &sPrefix="", bool bShort=true) const override
 Formatted string version for debugging purposes.
 
- Public Member Functions inherited from Object< ADSR >
 Object ()
 
 Object (const Object< ADSR > &other)
 
- Public Member Functions inherited from Base
 Base ()
 
 Base (const Base &other)
 
virtual const char * class_name () const
 
void Print (bool bShort=true) const
 Prints content of toQString() via DEBUGLOG.
 

Static Public Member Functions

static QString StateToQString (State state)
 
- 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
 

Private Member Functions

void normalise ()
 

Private Attributes

float m_fFramesInState
 Tracks the number of frames passed in the current m_state.
 
double m_fQ
 exponential decay state
 
float m_fReleaseValue
 value when the release state was entered
 
float m_fSustain
 Sustain level.
 
float m_fValue
 current value
 
unsigned int m_nAttack
 Attack phase duration in frames.
 
unsigned int m_nDecay
 Decay phase duration in frames.
 
unsigned int m_nRelease
 Release phase duration in frames.
 
State m_state
 current state
 

Additional Inherited Members

- Static Public Attributes inherited from Base
static QString sPrintIndention = " "
 String used to format the debugging string output of some core classes.
 
- Protected Member Functions inherited from Object< ADSR >
 ~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
 

Detailed Description

Attack Decay Sustain Release envelope.

Definition at line 37 of file Adsr.h.

Member Enumeration Documentation

◆ State

enum class State
strong

possible states

Enumerator
Attack 
Decay 
Sustain 
Release 
Idle 

Definition at line 110 of file Adsr.h.

Constructor & Destructor Documentation

◆ ADSR() [1/2]

ADSR ( unsigned int attack = 0,
unsigned int decay = 0,
float sustain = 1.0,
unsigned int release = 1000 )

constructor

Parameters
attackphase duration in frames
decayphase duration in frames
sustainlevel
releasephase duration in frames

Definition at line 36 of file Adsr.cpp.

◆ ADSR() [2/2]

ADSR ( const std::shared_ptr< ADSR > other)

copy constructor

Definition at line 50 of file Adsr.cpp.

◆ ~ADSR()

~ADSR ( )

destructor

Definition at line 63 of file Adsr.cpp.

Member Function Documentation

◆ applyADSR()

bool applyADSR ( float * pLeft,
float * pRight,
int nFinalBufferPos,
int nReleaseFrame,
float fStep )

Compute and apply successive ADSR values to stereo buffers.

Apply ADSR envelope to stereo pair sample fragments.

In case the ADSR still hold its default values, the provided buffers aren't altered.

Conceptionally it multiplies the first m_nAttack frames of a sample with a rising attack exponential, the successive m_nDecay frames with a falling decay exponential, and remaining frames with m_fSustain. Which of these steps are covered in a run of applyADSR() depends on m_state.

If no note length was specified by the user in the GUI, m_fSustain will be applied till the end of the corresponding sample and ADSR application won't enter release phase which would apply a falling exponential for m_nRelease frames and zero all following frames.

Parameters
pLeftleft-channel audio buffer
pRightright-channel audio buffer
nFinalBufferPosUp to which frame pLeft and pRight will be processed.
nReleaseFrameFrame number indicating the end of the note or sample at which ADSR processing will enter the release phase.
fStepthe increment to be added to m_fFramesInState.

This function manages the current state of the ADSR state machine, and applies envelope calculations appropriate to each phase.

Definition at line 175 of file Adsr.cpp.

◆ attack()

void attack ( )

Sets m_state to State::Attack.

Definition at line 275 of file Adsr.cpp.

◆ getAttack()

unsigned int getAttack ( )
inline

Definition at line 164 of file Adsr.h.

◆ getDecay()

unsigned int getDecay ( )
inline

Definition at line 174 of file Adsr.h.

◆ getRelease()

unsigned int getRelease ( )
inline

Definition at line 194 of file Adsr.h.

◆ getState()

ADSR::State getState ( ) const
inline

Definition at line 198 of file Adsr.h.

◆ getSustain()

float getSustain ( )
inline

Definition at line 184 of file Adsr.h.

◆ normalise()

void normalise ( )
private

Definition at line 65 of file Adsr.cpp.

◆ release()

float release ( )

Sets m_state to State::Release and return the current m_fReleaseValue.

State setting is only applied if the ADSR is not in State::Idle.

Definition at line 282 of file Adsr.cpp.

◆ setAttack()

void setAttack ( unsigned int value)
inline

Definition at line 159 of file Adsr.h.

◆ setDecay()

void setDecay ( unsigned int value)
inline

Definition at line 169 of file Adsr.h.

◆ setRelease()

void setRelease ( unsigned int value)
inline

Definition at line 189 of file Adsr.h.

◆ setSustain()

void setSustain ( float value)
inline

Definition at line 179 of file Adsr.h.

◆ StateToQString()

QString StateToQString ( State state)
static

Definition at line 298 of file Adsr.cpp.

◆ toQString()

QString toQString ( const QString & sPrefix = "",
bool bShort = true ) const
overridevirtual

Formatted string version for debugging purposes.

Parameters
sPrefixString prefix which will be added in front of every new line
bShortInstead of the whole content of all classes stored as members just a single unique identifier will be displayed without line breaks.
Returns
String presentation of current object.

Reimplemented from Base.

Definition at line 315 of file Adsr.cpp.

Field Documentation

◆ m_fFramesInState

float m_fFramesInState
private

Tracks the number of frames passed in the current m_state.

It is used to determine whether ADSR processing should proceed into the next state and required if multiple process cycles of the H2Core::Sampler render the same state.

Note that it is given in float instead of integer (the basic unit for frames in the H2Core::AudioEngine) in order to account for processing while resampling the original audio.

Definition at line 148 of file Adsr.h.

◆ m_fQ

double m_fQ
private

exponential decay state

Definition at line 152 of file Adsr.h.

◆ m_fReleaseValue

float m_fReleaseValue
private

value when the release state was entered

Definition at line 150 of file Adsr.h.

◆ m_fSustain

float m_fSustain
private

Sustain level.

Definition at line 134 of file Adsr.h.

◆ m_fValue

float m_fValue
private

current value

Definition at line 149 of file Adsr.h.

◆ m_nAttack

unsigned int m_nAttack
private

Attack phase duration in frames.

Definition at line 132 of file Adsr.h.

◆ m_nDecay

unsigned int m_nDecay
private

Decay phase duration in frames.

Definition at line 133 of file Adsr.h.

◆ m_nRelease

unsigned int m_nRelease
private

Release phase duration in frames.

Definition at line 135 of file Adsr.h.

◆ m_state

State m_state
private

current state

Definition at line 136 of file Adsr.h.