-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADSR.cpp
148 lines (122 loc) · 3.02 KB
/
ADSR.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// ADSR.cpp
//
// 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.
//
// 1.01 2016-01-02 njr added calcCoef to SetTargetRatio functions that were in the ADSR widget but missing in this code
// 1.02 2017-01-04 njr in calcCoef, checked for rate 0, to support non-IEEE compliant compilers
//
#include "ADSR.h"
ADSR::ADSR() {
Reset();
this->sampleRate = 44100;
SetADSR(1.0, 1.0, 1.0, 1.0);
}
ADSR::ADSR(float sampleRate) {
Reset();
this->sampleRate = sampleRate;
SetADSR(1.0, 1.0, 1.0, 1.0);
}
ADSR::~ADSR(void) {
}
void ADSR::SetADSR(byte attack, byte decay, byte sustain, byte release)
{
SetSustainMidi(sustain);
SetAttackMidi(attack);
SetDecayMidi(decay);
SetReleaseMidi(release);
}
void ADSR::SetAttack(float attack) {
this->attack = attack;
if (attack < 0.001)
{
attack = 0.001;
}
attackCoef = 1.0/attack;
attackBase = 0.0;
}
void ADSR::SetDecay(float decay) {
this->decay = decay;
if (decay < 0.001)
{
decay = 0.001;
}
decayCoef = (1.0 - this->sustainLevel) / this->decay;
}
void ADSR::SetSustain(float level) {
sustainLevel = level;
decayBase = sustainLevel;
}
void ADSR::SetRelease(float releaseval)
{
release = releaseval;
if (release < 0.001)
{
release = 0.001;
}
releaseCoef = (this->sustainLevel / this->release);
releaseBase = this->sustainLevel;
}
void ADSR::SetAttackMidi(byte attack)
{
this->midiAttack=attack;
this->attack = ((float)midiAttack)/127.0;
double maxlength = 1.0*sampleRate;
if(midiAttack==0)
{
attackCoef = 1.0;
}
else
{
attackCoef = 127.0 /(maxlength*(double)midiAttack);
}
attackBase = 0.0;
}
void ADSR::SetDecayMidi(byte decay)
{
this->midiDecay=decay;
this->decay = ((float)this->midiDecay)/127.0;
double maxlength = 1.0*sampleRate;
if(midiDecay==0)
{
decayCoef=1.0 - this->sustainLevel;
}
else
{
decayCoef = ((1.0 - this->sustainLevel)*127.0) / (maxlength*(float)midiDecay);
}
}
void ADSR::SetSustainMidi(byte sustain)
{
this->midiSustain=sustain;
this->sustainLevel = ((float)midiSustain)/127.0;
decayBase = sustainLevel;
}
void ADSR::SetReleaseMidi(byte release)
{
this->midiRelease=release;
this->release = ((float)this->midiRelease)/127.0;
double maxlength = sampleRate*5.0;
if(midiRelease==0)
{
releaseCoef=this->sustainLevel;
}
else
{
releaseCoef = (this->sustainLevel*127.0) / (maxlength*((double)midiRelease));
//releaseCoef = ((this->sustainLevel * 127.0)/maxlength)/(float)midiRelease;
}
releaseBase = this->sustainLevel;
}