-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAD.h
112 lines (89 loc) · 2.07 KB
/
AD.h
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// ADSR.h
//
// Created by Nigel Redmon on 12/18/12.
// EarLevel Engineering: earlevel.com
// Copyright 2012 Nigel Redmon
//
// For a complete explanation of the ADSR envelope generator and code,
// read the series of articles by the author, starting here:
// http://www.earlevel.com/main/2013/06/01/envelope-generators/
//
// License:
//
// This source code is provided as is, without warranty.
// You may copy and distribute verbatim copies of this document.
// You may modify and use this source code to create binary code for your own purposes, free or commercial.
//
#ifndef AD_h
#define AD_h
#include "Num.h"
using namespace Fixie;
class AD {
public:
AD(void);
~AD(void);
Num Process(void);
Num GetOutput(void);
int GetState(void);
void SetAD(Num attack, Num decay);
void Gate(int on);
void SetAttack(Num attack);
void SetDecay(Num decay);
void Reset(void);
enum envState {
env_idle = 0,
env_attack,
env_decay
};
protected:
int state;
Num output;
Num attack;
Num decay;
int32_t counter;
Num attackCoef;
Num decayCoef;
Num attackBase;
Num decayBase;
};
inline Num AD::Process() {
switch (state) {
case env_idle:
output = Num(0);
break;
case env_attack:
output = output+attackBase + attackCoef;
if (output >= Num(1.0)) {
output = Num(1.0); //http://www.earlevel.com/main/2013/06/01/envelope-generators/
state = env_decay;
}
break;
case env_decay:
output = output - decayCoef;
if (output <= Num(0)) {
output = Num(0);
state = env_idle;
}
break;
}
return output;
}
inline void AD::Gate(int gate) {
if (gate)
{
output = Num(0);
state = env_attack;
}
}
inline int AD::GetState() {
return state;
}
inline void AD::Reset() {
state = env_idle;
output = Num(0);
}
inline Num AD::GetOutput() {
return output;
}
#endif