mirror of
https://github.com/thestk/stk
synced 2026-02-07 01:36:16 +00:00
Version 4.4.0
This commit is contained in:
committed by
Stephen Sinclair
parent
d199342e86
commit
eccd8c9981
147
include/ADSR.h
147
include/ADSR.h
@@ -1,86 +1,163 @@
|
||||
#ifndef STK_ADSR_H
|
||||
#define STK_ADSR_H
|
||||
|
||||
#include "Generator.h"
|
||||
|
||||
namespace stk {
|
||||
|
||||
/***************************************************/
|
||||
/*! \class ADSR
|
||||
\brief STK ADSR envelope class.
|
||||
|
||||
This Envelope subclass implements a
|
||||
traditional ADSR (Attack, Decay,
|
||||
Sustain, Release) envelope. It
|
||||
responds to simple keyOn and keyOff
|
||||
messages, keeping track of its state.
|
||||
The \e state = ADSR::DONE after the
|
||||
envelope value reaches 0.0 in the
|
||||
ADSR::RELEASE state.
|
||||
This class implements a traditional ADSR (Attack, Decay, Sustain,
|
||||
Release) envelope. It responds to simple keyOn and keyOff
|
||||
messages, keeping track of its state. The \e state = ADSR::DONE
|
||||
after the envelope value reaches 0.0 in the ADSR::RELEASE state.
|
||||
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2007.
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2009.
|
||||
*/
|
||||
/***************************************************/
|
||||
|
||||
#ifndef STK_ADSR_H
|
||||
#define STK_ADSR_H
|
||||
|
||||
#include "Envelope.h"
|
||||
|
||||
class ADSR : public Envelope
|
||||
class ADSR : public Generator
|
||||
{
|
||||
public:
|
||||
|
||||
//! Envelope states.
|
||||
enum { ATTACK, DECAY, SUSTAIN, RELEASE, DONE };
|
||||
//! ADSR envelope states.
|
||||
enum {
|
||||
ATTACK, /*!< Attack */
|
||||
DECAY, /*!< Decay */
|
||||
SUSTAIN, /*!< Sustain */
|
||||
RELEASE, /*!< Release */
|
||||
DONE /*!< End of release */
|
||||
};
|
||||
|
||||
//! Default constructor.
|
||||
ADSR(void);
|
||||
ADSR( void );
|
||||
|
||||
//! Class destructor.
|
||||
~ADSR(void);
|
||||
~ADSR( void );
|
||||
|
||||
//! Set target = 1, state = \e ADSR::ATTACK.
|
||||
void keyOn(void);
|
||||
void keyOn( void );
|
||||
|
||||
//! Set target = 0, state = \e ADSR::RELEASE.
|
||||
void keyOff(void);
|
||||
void keyOff( void );
|
||||
|
||||
//! Set the attack rate.
|
||||
void setAttackRate(StkFloat rate);
|
||||
void setAttackRate( StkFloat rate );
|
||||
|
||||
//! Set the decay rate.
|
||||
void setDecayRate(StkFloat rate);
|
||||
void setDecayRate( StkFloat rate );
|
||||
|
||||
//! Set the sustain level.
|
||||
void setSustainLevel(StkFloat level);
|
||||
void setSustainLevel( StkFloat level );
|
||||
|
||||
//! Set the release rate.
|
||||
void setReleaseRate(StkFloat rate);
|
||||
void setReleaseRate( StkFloat rate );
|
||||
|
||||
//! Set the attack rate based on a time duration.
|
||||
void setAttackTime(StkFloat time);
|
||||
void setAttackTime( StkFloat time );
|
||||
|
||||
//! Set the decay rate based on a time duration.
|
||||
void setDecayTime(StkFloat time);
|
||||
void setDecayTime( StkFloat time );
|
||||
|
||||
//! Set the release rate based on a time duration.
|
||||
void setReleaseTime(StkFloat time);
|
||||
void setReleaseTime( StkFloat time );
|
||||
|
||||
//! Set sustain level and attack, decay, and release time durations.
|
||||
void setAllTimes(StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime);
|
||||
void setAllTimes( StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime );
|
||||
|
||||
//! Set the target value.
|
||||
void setTarget(StkFloat target);
|
||||
void setTarget( StkFloat target );
|
||||
|
||||
//! Return the current envelope \e state (ATTACK, DECAY, SUSTAIN, RELEASE, DONE).
|
||||
int getState(void) const;
|
||||
int getState( void ) const { return state_; };
|
||||
|
||||
//! Set to state = ADSR::SUSTAIN with current and target values of \e aValue.
|
||||
void setValue(StkFloat value);
|
||||
//! Set to state = ADSR::SUSTAIN with current and target values of \e value.
|
||||
void setValue( StkFloat value );
|
||||
|
||||
//! Return the last computed output value.
|
||||
StkFloat lastOut( void ) const { return lastFrame_[0]; };
|
||||
|
||||
//! Compute and return one output sample.
|
||||
StkFloat tick( void );
|
||||
|
||||
//! Fill a channel of the StkFrames object with computed outputs.
|
||||
/*!
|
||||
The \c channel argument must be less than the number of
|
||||
channels in the StkFrames argument (the first channel is specified
|
||||
by 0). However, range checking is only performed if _STK_DEBUG_
|
||||
is defined during compilation, in which case an out-of-range value
|
||||
will trigger an StkError exception.
|
||||
*/
|
||||
StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
|
||||
|
||||
protected:
|
||||
|
||||
StkFloat computeSample( void );
|
||||
void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
|
||||
|
||||
int state_;
|
||||
StkFloat value_;
|
||||
StkFloat target_;
|
||||
StkFloat attackRate_;
|
||||
StkFloat decayRate_;
|
||||
StkFloat sustainLevel_;
|
||||
StkFloat releaseRate_;
|
||||
StkFloat sustainLevel_;
|
||||
};
|
||||
|
||||
inline StkFloat ADSR :: tick( void )
|
||||
{
|
||||
switch ( state_ ) {
|
||||
|
||||
case ATTACK:
|
||||
value_ += attackRate_;
|
||||
if ( value_ >= target_ ) {
|
||||
value_ = target_;
|
||||
target_ = sustainLevel_;
|
||||
state_ = DECAY;
|
||||
}
|
||||
lastFrame_[0] = value_;
|
||||
break;
|
||||
|
||||
case DECAY:
|
||||
value_ -= decayRate_;
|
||||
if ( value_ <= sustainLevel_ ) {
|
||||
value_ = sustainLevel_;
|
||||
state_ = SUSTAIN;
|
||||
}
|
||||
lastFrame_[0] = value_;
|
||||
break;
|
||||
|
||||
case RELEASE:
|
||||
value_ -= releaseRate_;
|
||||
if ( value_ <= 0.0 ) {
|
||||
value_ = (StkFloat) 0.0;
|
||||
state_ = DONE;
|
||||
}
|
||||
lastFrame_[0] = value_;
|
||||
|
||||
}
|
||||
|
||||
return value_;
|
||||
}
|
||||
|
||||
inline StkFrames& ADSR :: tick( StkFrames& frames, unsigned int channel )
|
||||
{
|
||||
#if defined(_STK_DEBUG_)
|
||||
if ( channel >= frames.channels() ) {
|
||||
errorString_ << "ADSR::tick(): channel and StkFrames arguments are incompatible!";
|
||||
handleError( StkError::FUNCTION_ARGUMENT );
|
||||
}
|
||||
#endif
|
||||
|
||||
StkFloat *samples = &frames[channel];
|
||||
unsigned int hop = frames.channels();
|
||||
for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
|
||||
*samples = ADSR::tick();
|
||||
|
||||
return frames;
|
||||
}
|
||||
|
||||
} // stk namespace
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user