-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRamp.cpp
80 lines (67 loc) · 1.41 KB
/
Ramp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
C++ Real-Time Audio Programming with Bela - Lecture 14: ADSR
*/
// Ramp.cpp: a simple line segment generator
#include <cmath>
#include "Ramp.h"
// Constructor
Ramp::Ramp()
{
currentValue_ = 0;
increment_ = 0;
counter_ = 0;
sampleRate_ = 1;
}
// Constructor specifying a sample rate
Ramp::Ramp(float sampleRate)
{
currentValue_ = 0;
increment_ = 0;
counter_ = 0;
sampleRate_ = sampleRate;
}
// Set the sample rate, used for all calculations
void Ramp::setSampleRate(float rate)
{
sampleRate_ = rate;
}
// Jump to a value
void Ramp::setValue(float value)
{
currentValue_ = value;
increment_ = 0;
counter_ = 0;
}
// Ramp to a value over a period of time
void Ramp::rampTo(float value, float time)
{
// Calculate the increment to get from the current value to the target
// in the specified amount of time
increment_ = (value - currentValue_) / (sampleRate_ * time);
counter_ = (int)(sampleRate_ * time);
}
// Generate and return the next ramp output
float Ramp::process()
{
if(counter_ > 0) {
counter_--;
currentValue_ += increment_;
}
return currentValue_;
}
// Return whether the ramp is finished
bool Ramp::finished()
{
// The ramp is finished when the counter has counted down to 0
return (counter_ == 0);
}
// Destructor
Ramp::~Ramp()
{
}