Klang C++
Language Reference (draft)
Loading...
Searching...
No Matches
klang::ADSR

Attack-Decay-Sustain-Release Envelope. More...

#include <klang.h>

+ Inheritance diagram for klang::ADSR:

Public Types

enum  Mode { Time , Rate }
 
enum  Stage { Sustain , Release , Off }
 Envelope stage. More...
 

Public Member Functions

 ADSR ()
 
void set (param attack, param decay, param sustain, param release) override
 
void release (float time=0.f, float level=0.f) override
 
bool operator== (Envelope::Stage stage) const
 
bool operator!= (Stage stage) const
 
 operator float () const
 
void set (const std::vector< Point > &points)
 
void set (const Points &point)
 
void set (Ramp *ramp)
 
void sequence ()
 
void setLoop (int startPoint, int endPoint)
 
signal at (param time) const
 
void resetLoop ()
 
void setStage (Stage stage)
 
const Stage getStage () const
 
float getLength () const
 
bool finished () const
 
void initialise ()
 
void resize (float length)
 
void setTarget (const Point &point, float time=0.0)
 
signaloperator++ (int)
 
void process () override
 
const Pointoperator[] (int point) const
 
void setMode (Mode mode)
 
Mode mode () const
 
Output< signal > & operator() (params... p)
 
 operator const signal & () override
 
 operator const signal & () const override
 
virtual const signaloutput () const
 
TYPE & operator>> (TYPE &destination)
 
signal operator+ (TYPE &other)
 
signal operator* (TYPE &other)
 
signal operator- (TYPE &other)
 
signal operator/ (TYPE &other)
 

Public Attributes

param A
 
param D
 
param S
 
param R
 
enum klang::ADSR::Mode mode = Time
 
signal out
 

Protected Attributes

void(Envelope::* setTargetFunction )(const Point &point, float time) = &Envelope::setTargetTime
 
std::vector< Pointpoints
 
Loop loop
 
int point
 
float time
 
float timeInc
 
Stage stage
 
std::shared_ptr< Rampramp
 

Detailed Description

Definition at line 2988 of file klang.h.

Member Enumeration Documentation

◆ Mode

Values
Time 
Rate 

Definition at line 2994 of file klang.h.

2994{ Time, Rate } mode = Time;
enum klang::ADSR::Mode mode

◆ Stage

enum klang::Envelope::Stage
inherited
Values
Sustain 
Release 
Off 

Definition at line 2696 of file klang.h.

Constructor & Destructor Documentation

◆ ADSR()

klang::ADSR::ADSR ( )
inline

Definition at line 2996 of file klang.h.

2996{ set(0.5, 0.5, 1, 0.5); }
void set(param attack, param decay, param sustain, param release) override
Definition klang.h:2998

References set().

Member Function Documentation

◆ at()

signal klang::Envelope::at ( param time) const
inlineinherited

Definition at line 2815 of file klang.h.

2815 {
2816 if (points.empty()) return 0;
2817 Point last = { 0, points[0].y };
2818 for (const Point& point : points) {
2819 if (point.x >= time) {
2820 const float dx = point.x - last.x;
2821 const float dy = point.y - last.y;
2822 const float x = time - last.x;
2823 return dx == 0 ? last.y : (last.y + x * dy / dx);
2824 }
2825 last = point;
2826 }
2827 return points.back().y;
2828 }
std::vector< Point > points
Definition klang.h:2977

References klang::Envelope::Point::Point(), klang::signal::operator-(), klang::Envelope::points, klang::Envelope::Point::x, and klang::Envelope::Point::y.

◆ finished()

bool klang::Envelope::finished ( ) const
inlineinherited

Definition at line 2855 of file klang.h.

2855 {
2856 return getStage() == Stage::Off;
2857 }
const Stage getStage() const
Definition klang.h:2841

References klang::Envelope::getStage(), and klang::Envelope::Off.

◆ getLength()

float klang::Envelope::getLength ( ) const
inlineinherited

Definition at line 2844 of file klang.h.

2844{ return points.size() ? points[points.size() - 1].x : 0.f; }

References klang::Envelope::points, and klang::Envelope::Point::x.

Referenced by klang::Envelope::resize().

◆ getStage()

const Stage klang::Envelope::getStage ( ) const
inlineinherited

Definition at line 2841 of file klang.h.

2841{ return stage; }

References klang::Envelope::stage.

Referenced by klang::Envelope::finished(), and operator==().

◆ initialise()

void klang::Envelope::initialise ( )
inlineinherited

Definition at line 2860 of file klang.h.

2860 {
2861 point = 0;
2862 timeInc = 1.0f / fs;
2863 loop.reset();
2864 stage = Sustain;
2865 if(points.size()){
2866 out = points[0].y;
2867 ramp->setValue(points[0].y);
2868 if(points.size() > 1)
2869 setTarget(points[1], points[0].x);
2870 }else{
2871 out = 1.0f;
2872 ramp->setValue(1.0f);
2873 }
2874 }
std::shared_ptr< Ramp > ramp
Definition klang.h:2984
void setTarget(const Point &point, float time=0.0)
Definition klang.h:2893
float timeInc
Definition klang.h:2981
klang::dB fs

References klang::Envelope::loop, klang::Envelope::point, klang::Envelope::points, klang::Envelope::ramp, klang::Envelope::Loop::reset(), klang::Envelope::setTarget(), klang::Envelope::Ramp::setValue(), klang::Envelope::stage, klang::Envelope::Sustain, klang::Envelope::timeInc, klang::Envelope::Point::x, and klang::Envelope::Point::y.

Referenced by klang::Envelope::resize(), klang::Envelope::sequence(), set(), klang::Envelope::set(), klang::Envelope::set(), and klang::Envelope::set().

◆ mode()

Mode klang::Envelope::mode ( ) const
inlineinherited

Definition at line 2955 of file klang.h.

void setTargetTime(const Point &point, float time=0.0)
Definition klang.h:2961
void(Envelope::* setTargetFunction)(const Point &point, float time)
Definition klang.h:2959

References klang::Envelope::Rate, klang::Envelope::setTargetFunction, klang::Envelope::setTargetTime(), and klang::Envelope::Time.

Referenced by klang::Envelope::operator++().

◆ operator const signal &() [1/2]

klang::Generic::Generator< signal >::operator const signal & ( ) const
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Output< signal >.

Definition at line 1416 of file klang.h.

1416{ return out; } // return last output

◆ operator const signal &() [2/2]

klang::Generic::Generator< signal >::operator const signal & ( )
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Output< signal >.

Definition at line 1415 of file klang.h.

1415{ process(); return out; } // return processed output

◆ operator float()

klang::Envelope::operator float ( ) const
inlineinherited

Definition at line 2770 of file klang.h.

2770{ return out; }

◆ operator!=()

bool klang::Envelope::operator!= ( Stage stage) const
inlineinherited

Definition at line 2768 of file klang.h.

2768{ return Envelope::stage != stage; }

References klang::Envelope::stage.

◆ operator()()

Output< signal > & klang::Generic::Generator< signal >::operator() ( params... p)
inlineinherited

Definition at line 1409 of file klang.h.

1409 {
1410 set(p...); return *this;
1411 }

◆ operator*()

signal klang::Generic::Output< signal >::operator* ( TYPE & other)
inlineinherited

Definition at line 1372 of file klang.h.

1372{ process(); return out * (other); }

◆ operator+()

signal klang::Generic::Output< signal >::operator+ ( TYPE & other)
inlineinherited

Definition at line 1371 of file klang.h.

1371{ process(); return out + (other); }

◆ operator++()

signal & klang::Envelope::operator++ ( int )
inlineinherited

Definition at line 2898 of file klang.h.

2898 {
2899 out = (*ramp)++;
2900
2901 switch(stage){
2902 case Sustain:
2903 time += timeInc;
2904 if (!ramp->isActive()) { // envelop segment end reached
2905 if (loop.isActive() && (point + 1) >= loop.end) {
2906 point = loop.start;
2907 ramp->setValue(points[point].y);
2908 if (loop.start != loop.end)
2909 setTarget(points[point + 1], points[point].x);
2910 } else if ((point + 1) < points.size()) {
2911 if (mode() == Rate || time >= points[point + 1].x) { // reached target point
2912 point++;
2913 ramp->setValue(points[point].y); // make sure exact value is set
2914
2915 if ((point + 1) < points.size()) // new target point?
2916 setTarget(points[point + 1], points[point].x);
2917 }
2918 } else {
2919 stage = Off;
2920 }
2921 } break;
2922 case Release:
2923 if (!ramp->isActive()) //if(out == 0.0)
2924 stage = Off;
2925 break;
2926 case Off:
2927 break;
2928 }
2929
2930 return out;
2931 }
Mode mode() const
Definition klang.h:2955
bool isActive() const
Definition klang.h:2746

References klang::Envelope::Loop::end, klang::Envelope::Loop::isActive(), klang::Envelope::Ramp::isActive(), klang::Envelope::loop, klang::Envelope::mode(), klang::Envelope::Off, klang::Envelope::Ramp::operator++(), klang::Envelope::point, klang::Envelope::points, klang::Envelope::ramp, klang::Envelope::Rate, klang::Envelope::Release, klang::Envelope::setTarget(), klang::Envelope::Ramp::setValue(), klang::Envelope::stage, klang::Envelope::Loop::start, klang::Envelope::Sustain, klang::Envelope::time, klang::Envelope::timeInc, klang::Envelope::Point::x, and klang::Envelope::Point::y.

◆ operator-()

signal klang::Generic::Output< signal >::operator- ( TYPE & other)
inlineinherited

Definition at line 1373 of file klang.h.

1373{ process(); return out - (other); }

◆ operator/()

signal klang::Generic::Output< signal >::operator/ ( TYPE & other)
inlineinherited

Definition at line 1374 of file klang.h.

1374{ process(); return out / (other); }

◆ operator==()

bool klang::ADSR::operator== ( Envelope::Stage stage) const
inline

Definition at line 3017 of file klang.h.

3017 {
3018 return getStage() == stage;
3019 }

References klang::Envelope::getStage().

◆ operator>>()

TYPE & klang::Generic::Output< signal >::operator>> ( TYPE & destination)
inlineinherited

Definition at line 1364 of file klang.h.

1364{ process(); return destination = out; }

◆ operator[]()

const Point & klang::Envelope::operator[] ( int point) const
inlineinherited

Definition at line 2938 of file klang.h.

2938 {
2939 return points[point];
2940 }

References klang::Envelope::points.

◆ output()

virtual const signal & klang::Generic::Output< signal >::output ( ) const
inlinevirtualinherited

Definition at line 1360 of file klang.h.

1360{ return out; }

◆ process()

void klang::Envelope::process ( )
inlineoverridevirtualinherited

Implements klang::Generic::Output< signal >.

Definition at line 2933 of file klang.h.

2933 { /* do nothing -> only process on ++ */
2934 out = *ramp;
2935 }

References klang::Envelope::ramp.

◆ release()

void klang::ADSR::release ( float time = 0.f,
float level = 0.f )
inlineoverridevirtual

Reimplemented from klang::Envelope.

Definition at line 3013 of file klang.h.

3013 {
3014 Envelope::release(time ? time : float(R), level);
3015 }
virtual void release(float time, float level=0.f)
Definition klang.h:2847
param R
Definition klang.h:2992

References R, and klang::Envelope::release().

◆ resetLoop()

void klang::Envelope::resetLoop ( )
inlineinherited

◆ resize()

void klang::Envelope::resize ( float length)
inlineinherited

Definition at line 2877 of file klang.h.

2877 {
2878 const float old_length = getLength();
2879 if(old_length == 0.0)
2880 return;
2881
2882 const float multiplier = length / (fs * old_length);
2883 std::vector<Point>::iterator point = points.begin();
2884 while(point != points.end()){
2885 point->x *= multiplier;
2886 point++;
2887 }
2888
2889 initialise();
2890 }
void initialise()
Definition klang.h:2860
float getLength() const
Definition klang.h:2844

References klang::Envelope::getLength(), klang::Envelope::initialise(), klang::Envelope::points, and klang::Envelope::Point::x.

◆ sequence()

void klang::Envelope::sequence ( )
inlineinherited

Definition at line 2798 of file klang.h.

2798 {
2799 float time = 0.f;
2800 for(Point& point : points) {
2801 const float delta = point.x;
2802 time += delta + 0.00001f;
2803 point.x = time;
2804 }
2805 initialise();
2806 }

References klang::Envelope::initialise(), klang::Envelope::points, and klang::Envelope::Point::x.

◆ set() [1/4]

void klang::ADSR::set ( param attack,
param decay,
param sustain,
param release )
inlineoverride

Definition at line 2998 of file klang.h.

2998 {
2999 A = attack + 0.005f;
3000 D = decay + 0.005f;
3001 S = sustain;
3002 R = release + 0.005f;
3003
3004 points.resize(3);
3005 points[0] = { 0, 0 };
3006 points[1] = { A, 1 };
3007 points[2] = { A + D, S };
3008
3009 initialise();
3010 setLoop(2, 2);
3011 }
void setLoop(int startPoint, int endPoint)
Definition klang.h:2809
param S
Definition klang.h:2992
param D
Definition klang.h:2992
void release(float time=0.f, float level=0.f) override
Definition klang.h:3013
param A
Definition klang.h:2992

References klang::Envelope::Point::Point(), A, D, klang::Envelope::initialise(), klang::signal::operator+(), klang::Envelope::points, R, S, and klang::Envelope::setLoop().

Referenced by ADSR().

◆ set() [2/4]

void klang::Envelope::set ( const Points & point)
inlineinherited

Definition at line 2785 of file klang.h.

2785 {
2786 points.clear();
2787
2788 const Points* pPoint = &point;
2789 while(pPoint){
2790 points.push_back(*pPoint);
2791 pPoint = pPoint->next;
2792 }
2793
2794 initialise();
2795 }

References klang::Envelope::initialise(), and klang::Envelope::points.

Referenced by klang::Envelope::Envelope(), and klang::Envelope::Envelope().

◆ set() [3/4]

void klang::Envelope::set ( const std::vector< Point > & points)
inlineinherited

◆ set() [4/4]

void klang::Envelope::set ( Ramp * ramp)
inlineinherited

Definition at line 2943 of file klang.h.

2943 {
2944 Envelope::ramp = std::shared_ptr<Ramp>(ramp);
2945 initialise();
2946 }

References klang::Envelope::initialise(), and klang::Envelope::ramp.

◆ setLoop()

void klang::Envelope::setLoop ( int startPoint,
int endPoint )
inlineinherited

Definition at line 2809 of file klang.h.

2809 {
2810 if(startPoint >= 0 && endPoint < points.size())
2811 loop.set(startPoint, endPoint);
2812 }
void set(int from, int to)
Definition klang.h:2743

References klang::Envelope::loop, klang::Envelope::points, and klang::Envelope::Loop::set().

Referenced by set().

◆ setMode()

void klang::Envelope::setMode ( Mode mode)
inlineinherited

Definition at line 2948 of file klang.h.

2948 {
2949 if (mode == Time)
2951 else
2953 }
void setTargetRate(const Point &point, float rate=0.0)
Definition klang.h:2967

References klang::Envelope::setTargetFunction, klang::Envelope::setTargetRate(), klang::Envelope::setTargetTime(), and klang::Envelope::Time.

◆ setStage()

void klang::Envelope::setStage ( Stage stage)
inlineinherited

Definition at line 2838 of file klang.h.

2838{ this->stage = stage; }

References klang::Envelope::stage.

◆ setTarget()

void klang::Envelope::setTarget ( const Point & point,
float time = 0.0 )
inlineinherited

◆ setTargetRate()

void klang::Envelope::setTargetRate ( const Point & point,
float rate = 0.0 )
inlineprotectedinherited

Definition at line 2967 of file klang.h.

2967 {
2968 this->time = 0;
2969 if (point.x == 0) {
2970 ramp->setValue(point.y);
2971 } else {
2972 ramp->setTarget(point.y);
2973 ramp->setRate(point.x);
2974 }
2975 }

References klang::Envelope::ramp, klang::Envelope::Ramp::setRate(), klang::Envelope::Ramp::setTarget(), klang::Envelope::Ramp::setValue(), klang::Envelope::time, klang::Envelope::Point::x, and klang::Envelope::Point::y.

Referenced by klang::Envelope::setMode().

◆ setTargetTime()

void klang::Envelope::setTargetTime ( const Point & point,
float time = 0.0 )
inlineprotectedinherited

Member Data Documentation

◆ A

param klang::ADSR::A

Definition at line 2992 of file klang.h.

Referenced by set().

◆ D

param klang::ADSR::D

Definition at line 2992 of file klang.h.

Referenced by set().

◆ loop

Loop klang::Envelope::loop
protectedinherited

◆ mode

enum klang::ADSR::Mode klang::ADSR::mode = Time

◆ out

Definition at line 1357 of file klang.h.

1357{ 0.f };

◆ point

int klang::Envelope::point
protectedinherited

◆ points

◆ R

param klang::ADSR::R

Definition at line 2992 of file klang.h.

Referenced by release(), and set().

◆ ramp

◆ S

param klang::ADSR::S

Definition at line 2992 of file klang.h.

Referenced by set().

◆ setTargetFunction

void(Envelope::* klang::Envelope::setTargetFunction) (const Point &point, float time) = &Envelope::setTargetTime
protectedinherited

◆ stage

◆ time

float klang::Envelope::time
protectedinherited

◆ timeInc

float klang::Envelope::timeInc
protectedinherited

Definition at line 2981 of file klang.h.

Referenced by klang::Envelope::initialise(), and klang::Envelope::operator++().