-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
166 lines (122 loc) · 3.69 KB
/
index.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import outputLevel from './output-level';
import outputLUT from './output-lut';
export default class Envelope {
constructor(config) {
this.levels = config.levels;
this.rates = config.rates;
this.currentLevel = 0;
this.sampleRate = 49096;
this.decayIncrement = 0;
// for calculating breakpoints
this._currentIndex = 0;
this._points = {};
this.advance(0);
this.getADCurve();
this.breakpoints = {};
this.breakpoints[0] = (this._points[1] - this._points[0]) / this.sampleRate;
this.breakpoints[1] = (this._points[2] - this._points[1]) / this.sampleRate;
this.breakpoints[2] = (this._points[3] - this._points[2]) / this.sampleRate;
this._points = {};
this.getReleaseCurve();
this.breakpoints[3] = (this._points[4] - this._points[3]) / this.sampleRate;
}
getADCurve() {
if (this.attackDecayCurve) {
return this.attackDecayCurve;
}
this._currentIndex = 0;
const curve = [];
let sameInARowCount = 0;
let lastSeenValue = false;
let maxLength = this.sampleRate * 60;
while (curve.length < maxLength) {
this._currentIndex++;
if (this.state === 3) {
break;
}
const nextValue = this.render();
if (lastSeenValue === nextValue) {
sameInARowCount++;
} else {
sameInARowCount = 0;
}
lastSeenValue = nextValue;
curve.push(nextValue);
if (sameInARowCount > 44100 && curve.length > 1000) {
break;
}
}
this.attackDecayCurve = new Float32Array(curve.length);
this.attackDecayCurve.set(curve);
return curve;
}
getReleaseCurve() {
if (this.releaseCurve) {
return this.releaseCurve;
}
this._currentIndex = 0;
this.currentLevel = 0;
this.levels[2] = Math.max.apply(Math, this.levels.slice(0, 3));
this.advance(2);
this.currentLevel = this.targetLevel;
const curve = [];
let sameInARowCount = 0;
let lastSeenValue = false;
let maxLength = this.sampleRate * 60;
while (curve.length < maxLength) {
this._currentIndex++;
const nextValue = this.render();
if (this.state === 4) {
break;
}
if (this.state !== 3) {
continue;
}
if (lastSeenValue === nextValue) {
sameInARowCount++;
} else {
sameInARowCount = 0;
}
lastSeenValue = nextValue;
curve.push(nextValue);
// Normalize assuming we are going down
curve[curve.length - 1] = curve[curve.length - 1] / curve[0];
if (sameInARowCount > 44100 && curve.length > 1000) {
break;
}
}
this.releaseCurve = new Float32Array(curve.length);
this.releaseCurve.set(curve);
return curve;
}
_getTickIncrement() {
if (this.rising) {
const newVal = this.decayIncrement * (2 + (this.targetLevel - this.currentLevel) / 256);
return newVal;
} else {
return -1 * this.decayIncrement;
}
}
render() {
const newLevel = this.currentLevel + this._getTickIncrement();
if (this.rising === (newLevel >= this.targetLevel)) {
this.currentLevel = this.targetLevel;
this.advance(this.state + 1);
} else {
this.currentLevel = newLevel;
}
return outputLUT[Math.floor(this.currentLevel)];
}
advance(newState) {
this._points[newState] = this._currentIndex;
this.state = newState;
if (this.state === 4) {
return;
}
const newLevel = this.levels[this.state];
this.targetLevel = Math.max(0, (outputLevel[newLevel] << 5) - 224);
this.rising = this.targetLevel > this.currentLevel;
this.qr = Math.min(63, (this.rates[this.state] * 41) >> 6);
this.decayIncrement = Math.pow(2, this.qr / 4) / 2048;
}
}