Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

# Envelope.h

    00001 #ifndef STK_ENVELOPE_H
    00002 #define STK_ENVELOPE_H
    00003
    00004 #include "Generator.h"
    00005
    00006 namespace stk {
    00007
    00008 /***************************************************/
    00019 /***************************************************/
    00020
    00021 class Envelope : public Generator
    00022 {
    00023  public:
    00024
    00026   Envelope( void );
    00027
    00029   ~Envelope( void );
    00030
    00032   Envelope& operator= ( const Envelope& e );
    00033
    00035   void keyOn( void ) { this->setTarget( 1.0 ); };
    00036
    00038   void keyOff( void ) { this->setTarget( 0.0 ); };
    00039
    00041
    00044   void setRate( StkFloat rate );
    00045
    00047
    00051   void setTime( StkFloat time );
    00052
    00054   void setTarget( StkFloat target );
    00055
    00057   void setValue( StkFloat value );
    00058
    00060   int getState( void ) const { return state_; };
    00061
    00063   StkFloat lastOut( void ) const { return lastFrame_[0]; };
    00064
    00066   StkFloat tick( void );
    00067
    00069
    00076   StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
    00077
    00078  protected:
    00079
    00080   void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
    00081
    00082   StkFloat value_;
    00083   StkFloat target_;
    00084   StkFloat rate_;
    00085   int state_;
    00086 };
    00087
    00088 inline StkFloat Envelope :: tick( void )
    00089 {
    00090   if ( state_ ) {
    00091     if ( target_ > value_ ) {
    00092       value_ += rate_;
    00093       if ( value_ >= target_ ) {
    00094         value_ = target_;
    00095         state_ = 0;
    00096       }
    00097     }
    00098     else {
    00099       value_ -= rate_;
    00100       if ( value_ <= target_ ) {
    00101         value_ = target_;
    00102         state_ = 0;
    00103       }
    00104     }
    00105     lastFrame_[0] = value_;
    00106   }
    00107
    00108   return value_;
    00109 }
    00110
    00111 inline StkFrames& Envelope :: tick( StkFrames& frames, unsigned int channel )
    00112 {
    00113 #if defined(_STK_DEBUG_)
    00114   if ( channel >= frames.channels() ) {
    00115     oStream_ << "Envelope::tick(): channel and StkFrames arguments are incompatible!";
    00116     handleError( StkError::FUNCTION_ARGUMENT );
    00117   }
    00118 #endif
    00119
    00120   StkFloat *samples = &frames[channel];
    00121   unsigned int hop = frames.channels();
    00122   for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
    00123     *samples = tick();
    00124
    00125   return frames;
    00126 }
    00127
    00128 } // stk namespace
    00129
    00130 #endif

The Synthesis ToolKit in C++ (STK)
©1995-2012 Perry R. Cook and Gary P. Scavone. All Rights Reserved.