The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

Envelope.h
1#ifndef STK_ENVELOPE_H
2#define STK_ENVELOPE_H
3
4#include "Generator.h"
5
6namespace stk {
7
8/***************************************************/
20/***************************************************/
21
22 class Envelope : public Generator
23{
24 public:
25
27 Envelope( void );
28
30 ~Envelope( void );
31
33 Envelope& operator= ( const Envelope& e );
34
36 void keyOn( StkFloat target = 1.0 ) { this->setTarget( target ); };
37
39 void keyOff( StkFloat target = 0.0 ) { this->setTarget( target ); };
40
42
45 void setRate( StkFloat rate );
46
48
52 void setTime( StkFloat time );
53
55 void setTarget( StkFloat target );
56
58 void setValue( StkFloat value );
59
61 int getState( void ) const { return state_; };
62
64 StkFloat lastOut( void ) const { return lastFrame_[0]; };
65
67 StkFloat tick( void );
68
70
77 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
78
79 protected:
80
81 void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
82
83 StkFloat value_;
84 StkFloat target_;
85 StkFloat rate_;
86 int state_;
87};
88
89 inline StkFloat Envelope :: tick( void )
90{
91 if ( state_ ) {
92 if ( target_ > value_ ) {
93 value_ += rate_;
94 if ( value_ >= target_ ) {
95 value_ = target_;
96 state_ = 0;
97 }
98 }
99 else {
100 value_ -= rate_;
101 if ( value_ <= target_ ) {
102 value_ = target_;
103 state_ = 0;
104 }
105 }
106 lastFrame_[0] = value_;
107 }
108
109 return value_;
110}
111
112 inline StkFrames& Envelope :: tick( StkFrames& frames, unsigned int channel )
113{
114#if defined(_STK_DEBUG_)
115 if ( channel >= frames.channels() ) {
116 oStream_ << "Envelope::tick(): channel and StkFrames arguments are incompatible!";
117 handleError( StkError::FUNCTION_ARGUMENT );
118 }
119#endif
120
121 StkFloat *samples = &frames[channel];
122 unsigned int hop = frames.channels();
123 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
124 *samples = tick();
125
126 return frames;
127}
128
129} // stk namespace
130
131#endif
stk::Envelope
STK linear line envelope class.
Definition: Envelope.h:23
stk::Envelope::keyOff
void keyOff(StkFloat target=0.0)
Start ramping to specified target (default = 0).
Definition: Envelope.h:39
stk::Envelope::operator=
Envelope & operator=(const Envelope &e)
Assignment operator.
stk::Envelope::setTarget
void setTarget(StkFloat target)
Set the target value.
stk::Envelope::~Envelope
~Envelope(void)
Class destructor.
stk::Envelope::getState
int getState(void) const
Return the current envelope state (0 = at target, 1 otherwise).
Definition: Envelope.h:61
stk::Envelope::keyOn
void keyOn(StkFloat target=1.0)
Start ramping to specified target (default = 1).
Definition: Envelope.h:36
stk::Envelope::Envelope
Envelope(void)
Default constructor.
stk::Envelope::tick
StkFloat tick(void)
Compute and return one output sample.
Definition: Envelope.h:89
stk::Envelope::setValue
void setValue(StkFloat value)
Set current and target values to value.
stk::Envelope::setRate
void setRate(StkFloat rate)
Set the rate.
stk::Envelope::setTime
void setTime(StkFloat time)
Set the rate based on a positive time duration (seconds).
stk::Envelope::lastOut
StkFloat lastOut(void) const
Return the last computed output value.
Definition: Envelope.h:64
stk::Generator
STK abstract unit generator parent class.
Definition: Generator.h:21
stk::StkFrames
An STK class to handle vectorized audio data.
Definition: Stk.h:279
stk::StkFrames::channels
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:416
stk::StkFrames::frames
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:419
stk::Stk::handleError
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
stk
The STK namespace.
Definition: ADSR.h:6

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