-
Notifications
You must be signed in to change notification settings - Fork 8
/
a_fader.cpp
70 lines (62 loc) · 1.6 KB
/
a_fader.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
#include "a_fader.h"
#include "m_const.h"
#include "m_trig.h"
namespace a {
Fader::Fader()
: m_current(0.0f)
, m_from(0.0f)
, m_to(0.0f)
, m_delta(0.0f)
, m_time(0.0f)
, m_startTime(0.0f)
, m_endTime(0.0f)
, m_active(0)
{
}
void Fader::lerp(float from, float to, float time, float startTime) {
m_current = from;
m_from = from;
m_to = to;
m_time = time;
m_startTime = startTime;
m_delta = to - from;
m_endTime = m_startTime + time;
m_active = 1;
}
void Fader::lfo(float from, float to, float time, float startTime) {
m_active = 2;
m_current = 0.0f;
m_from = from;
m_to = to;
m_time = time;
m_delta = m::abs(to - from) / 2.0f;
m_startTime = startTime;
m_endTime = m::kPi * 2.0f / m_time;
}
float Fader::operator()(float currentTime) {
if (m_active == 2) {
// LFO
if (m_startTime > currentTime) {
// time rolled over
m_startTime = currentTime;
}
const float delta = currentTime - m_startTime;
return m::sin(delta * m_endTime) * m_delta + (m_from + m_delta);
}
if (m_startTime > currentTime) {
// time rolled over
float delta = (m_current - m_from) / m_delta;
m_from = m_current;
m_startTime = currentTime;
m_time = m_time * (1.0f - delta); // time left
m_delta = m_to - m_from;
m_endTime = m_startTime + m_time;
}
if (currentTime > m_endTime) {
m_active = -1;
return m_to;
}
m_current = m_from + m_delta * ((currentTime - m_startTime) / m_time);
return m_current;
}
}