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
22class Envelope : public Generator
23{
24 public:
25
27 Envelope( void );
28
30 ~Envelope( void );
31
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
89inline 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
112inline 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 linear line envelope class.
Definition Envelope.h:23
void keyOff(StkFloat target=0.0)
Start ramping to specified target (default = 0).
Definition Envelope.h:39
Envelope & operator=(const Envelope &e)
Assignment operator.
void setTarget(StkFloat target)
Set the target value.
~Envelope(void)
Class destructor.
int getState(void) const
Return the current envelope state (0 = at target, 1 otherwise).
Definition Envelope.h:61
void keyOn(StkFloat target=1.0)
Start ramping to specified target (default = 1).
Definition Envelope.h:36
Envelope(void)
Default constructor.
StkFloat tick(void)
Compute and return one output sample.
Definition Envelope.h:89
void setValue(StkFloat value)
Set current and target values to value.
void setRate(StkFloat rate)
Set the rate.
void setTime(StkFloat time)
Set the rate based on a positive time duration (seconds).
StkFloat lastOut(void) const
Return the last computed output value.
Definition Envelope.h:64
STK abstract unit generator parent class.
Definition Generator.h:21
An STK class to handle vectorized audio data.
Definition Stk.h:279
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition Stk.h:416
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition Stk.h:419
The STK namespace.
Definition ADSR.h:6

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