Logo Search packages:      
Sourcecode: csound version File versions  Download package

Envelope.cpp

/***************************************************/
/*! \class Envelope
    \brief STK envelope base class.

    This class implements a simple envelope
    generator which is capable of ramping to
    a target value by a specified \e rate.
    It also responds to simple \e keyOn and
    \e keyOff messages, ramping to 1.0 on
    keyOn and to 0.0 on keyOff.

    by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
*/
/***************************************************/

#include "Envelope.h"

00018 Envelope :: Envelope(void) : Generator()
{    
  target_ = 0.0;
  value_ = 0.0;
  rate_ = 0.001;
  state_ = 0;
}

00026 Envelope :: Envelope ( const Envelope& e )
{
  target_ = 0.0;
  value_ = 0.0;
  rate_ = 0.001;
  state_ = 0;
}

00034 Envelope :: ~Envelope(void)
{
}

00038 Envelope& Envelope :: operator= ( const Envelope& e )
{
  if ( this != &e ) {
    target_ = e.target_;
    value_ = e.value_;
    rate_ = e.rate_;
    state_ = e.state_;
  }

  return *this;
}

00050 void Envelope :: keyOn(void)
{
  target_ = 1.0;
  if (value_ != target_) state_ = 1;
}

00056 void Envelope :: keyOff(void)
{
  target_ = 0.0;
  if (value_ != target_) state_ = 1;
}

00062 void Envelope :: setRate(StkFloat rate)
{
  if (rate < 0.0) {
    errorString_ << "Envelope::setRate: negative rates not allowed ... correcting!";
    handleError( StkError::WARNING );
    rate_ = -rate;
  }
  else
    rate_ = rate;
}

00073 void Envelope :: setTime(StkFloat time)
{
  if (time < 0.0) {
    errorString_ << "Envelope::setTime: negative times not allowed ... correcting!";
    handleError( StkError::WARNING );
    rate_ = 1.0 / (-time * Stk::sampleRate());
  }
  else
    rate_ = 1.0 / (time * Stk::sampleRate());
}

00084 void Envelope :: setTarget(StkFloat target)
{
  target_ = target;
  if (value_ != target_) state_ = 1;
}

00090 void Envelope :: setValue(StkFloat value)
{
  state_ = 0;
  target_ = value;
  value_ = value;
}

00097 int Envelope :: getState(void) const
{
  return state_;
}

StkFloat Envelope :: computeSample(void )
{
  if (state_) {
    if (target_ > value_) {
      value_ += rate_;
      if (value_ >= target_) {
        value_ = target_;
        state_ = 0;
      }
    }
    else {
      value_ -= rate_;
      if (value_ <= target_) {
        value_ = target_;
        state_ = 0;
      }
    }
  }

  lastOutput_ = value_;
  return value_;
}

Generated by  Doxygen 1.6.0   Back to index