Language Guide / DSP and Analysis
ADSR
klang::ADSR
Attack, decay, sustain, and release envelope.
Set the attack, decay and release times in seconds and the sustain level as a linear value. Read the envelope once per sample to advance it. On note-off, call release() so it moves towards zero from its current level.
Example · Configure an amplitude envelope
With ADSR adsr; and Sine sine; as members, configure the envelope when the note starts.
adsr.set(0.01, 0.15, 0.5, 0.3);
Example · Retrigger with the existing settings
Call restart() to begin the attack again with the existing settings. The envelope still holds at the sustain level until you call release().
adsr.restart();
In process(), use the configured object for each sample.
sine * adsr >> out;
Attack-Decay-Sustain-Release Envelope.
More...
#include <klang.h>
Definition at line 5429 of file klang.h.
◆ Mode
Definition at line 5435 of file klang.h.
enum klang::ADSR::Mode mode
◆ Stage
| Values |
|---|
| Sustain | |
| Release | |
| Off | |
Definition at line 5128 of file klang.h.
◆ ADSR()
Definition at line 5437 of file klang.h.
5437{
set(0.5, 0.5, 1, 0.5); }
void set(param attack, param decay, param sustain, param release) override
◆ at()
Definition at line 5247 of file klang.h.
5247 {
5248 if (
points.empty())
return 0;
5249 Point last = { 0,
points[0].y };
5252 const float dx =
point.x - last.x;
5253 const float dy =
point.y - last.y;
5254 const float x =
time - last.x;
5255 return dx == 0 ? last.y : (last.y + x * dy / dx);
5256 }
5258 }
5260 }
std::vector< Point > points
◆ finished()
| bool klang::Envelope::finished |
( |
| ) |
const |
|
inlineinherited |
Definition at line 5287 of file klang.h.
5287 {
5289 }
const Stage getStage() const
◆ getLength()
| float klang::Envelope::getLength |
( |
| ) |
const |
|
inlineinherited |
◆ getStage()
| const Stage klang::Envelope::getStage |
( |
| ) |
const |
|
inlineinherited |
◆ initialise()
| void klang::Envelope::initialise |
( |
| ) |
|
|
inlineinherited |
Definition at line 5292 of file klang.h.
5292 {
5295 }
void restart()
Restart from the first point, preserving the configured loop.
◆ mode()
| Mode klang::Envelope::mode |
( |
| ) |
const |
|
inlineinherited |
Definition at line 5395 of file klang.h.
void setTargetTime(const Point &point, float time=0.0)
void(Envelope::* setTargetFunction)(const Point &point, float time)
◆ operator const signal &() [1/2]
|
|
inlineoverridevirtualinherited |
◆ operator const signal &() [2/2]
|
|
inlineoverridevirtualinherited |
◆ operator!=()
| bool klang::Envelope::operator!= |
( |
Stage | stage | ) |
const |
|
inlineinherited |
◆ operator()()
Definition at line 3050 of file klang.h.
3050 {
3051 set(std::forward<params>(p)...); return *this;
3052 }
◆ operator*()
◆ operator+()
◆ operator++()
| signal & klang::Envelope::operator++ |
( |
int | | ) |
|
|
inlineinherited |
◆ operator-()
◆ operator/()
◆ operator==()
◆ operator>>()
◆ operator[]()
| const Point & klang::Envelope::operator[] |
( |
int | point | ) |
const |
|
inlineinherited |
◆ output()
◆ process()
| void klang::Envelope::process |
( |
| ) |
|
|
inlinevirtualinherited |
Implements klang::Generic::Output< signal >.
Definition at line 5342 of file klang.h.
5342 {
5344
5348 if (!
ramp->isActive()) {
5354 }
5359
5362 }
5363 }
5364 else {
5366 }
5367 } break;
5369 if (!
ramp->isActive())
5371 break;
5373 break;
5374 }
5375 }
std::shared_ptr< Ramp > ramp
void setTarget(const Point &point, float time=0.0)
◆ release()
| void klang::ADSR::release |
( |
float | time = 0.f, |
|
|
float | level = 0.f ) |
|
inlineoverridevirtual |
Reimplemented from klang::Envelope.
Definition at line 5454 of file klang.h.
5454 {
5456 }
virtual void release(float time, float level=0.f)
◆ reset()
◆ resetLoop()
| void klang::Envelope::resetLoop |
( |
| ) |
|
|
inlineinherited |
◆ resize()
| void klang::Envelope::resize |
( |
float | length | ) |
|
|
inlineinherited |
Definition at line 5316 of file klang.h.
5316 {
5318 if (old_length == 0.0)
5319 return;
5320
5321 const float multiplier = length / (
fs * old_length);
5322 std::vector<Point>::iterator
point =
points.begin();
5324 point->x *= multiplier;
5326 }
5327
5329 }
◆ restart()
| void klang::Envelope::restart |
( |
| ) |
|
|
inlineinherited |
Definition at line 5298 of file klang.h.
5298 {
5308 }
5309 else {
5311 ramp->setValue(1.0f);
5312 }
5313 }
◆ sequence()
| void klang::Envelope::sequence |
( |
| ) |
|
|
inlineinherited |
Definition at line 5230 of file klang.h.
5230 {
5233 const float delta =
point.x;
5234 time += delta + 0.00001f;
5236 }
5238 }
◆ set() [1/4]
Definition at line 5439 of file klang.h.
5439 {
5444
5449
5452 }
void setLoop(int startPoint, int endPoint)
void release(float time=0.f, float level=0.f) override
◆ set() [2/4]
| void klang::Envelope::set |
( |
const Points & | point | ) |
|
|
inlineinherited |
Definition at line 5217 of file klang.h.
5217 {
5219
5220 const Points* pPoint = &
point;
5221 while (pPoint) {
5222 points.push_back(*pPoint);
5223 pPoint = pPoint->next;
5224 }
5225
5227 }
◆ set() [3/4]
| void klang::Envelope::set |
( |
const std::vector< Point > & | points | ) |
|
|
inlineinherited |
◆ set() [4/4]
| void klang::Envelope::set |
( |
Ramp * | ramp | ) |
|
|
inlineinherited |
◆ setLoop()
| void klang::Envelope::setLoop |
( |
int | startPoint, |
|
|
int | endPoint ) |
|
inlineinherited |
Definition at line 5241 of file klang.h.
5241 {
5242 if (startPoint >= 0 && endPoint <
points.size())
5243 loop.
set(startPoint, endPoint);
5244 }
void set(int from, int to)
◆ setMode()
| void klang::Envelope::setMode |
( |
Mode | mode | ) |
|
|
inlineinherited |
Definition at line 5388 of file klang.h.
5388 {
5391 else
5393 }
void setTargetRate(const Point &point, float rate=0.0)
◆ setStage()
| void klang::Envelope::setStage |
( |
Stage | stage | ) |
|
|
inlineinherited |
◆ 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 5407 of file klang.h.
5407 {
5411 }
5412 else {
5415 }
5416 }
◆ setTargetTime()
| void klang::Envelope::setTargetTime |
( |
const Point & | point, |
|
|
float | time = 0.0 ) |
|
inlineprotectedinherited |
◆ loop
| Loop klang::Envelope::loop |
|
protectedinherited |
◆ mode
◆ out
◆ point
| int klang::Envelope::point |
|
protectedinherited |
◆ points
| std::vector<Point> klang::Envelope::points |
|
protectedinherited |
◆ ramp
| std::shared_ptr<Ramp> klang::Envelope::ramp |
|
protectedinherited |
◆ setTargetFunction
◆ stage
| Stage klang::Envelope::stage |
|
protectedinherited |
◆ time
| float klang::Envelope::time |
|
protectedinherited |
◆ timeInc
| float klang::Envelope::timeInc |
|
protectedinherited |