Skip to content

Commit b773607

Browse files
committed
fixed envelopes to not break existing code
1 parent e81b1db commit b773607

File tree

1 file changed

+62
-12
lines changed

1 file changed

+62
-12
lines changed

src/env.js

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,33 @@ define(function (require) {
8484
* @property decayTime
8585
*/
8686
this.dTime = t2 || 0;
87+
/**
88+
* @property decayLevel
89+
*/
90+
this.dLevel = l2 || 0;
91+
/**
92+
* @property sustainTime
93+
*/
94+
this.sTime = t3 || 0;
8795
/**
8896
* @property sustainLevel
8997
*/
90-
this.sLevel = l2 || 0;
98+
this.sLevel = l3 || 0;
9199
/**
92100
* @property releaseTime
93101
*/
94-
this.rTime = t3 || 0;
102+
this.rTime = t4 || 0;
95103
/**
96104
* @property releaseLevel
97105
*/
98-
this.rLevel = l3 || 0;
99-
106+
this.rLevel = l4 || 0;
107+
console.log(t1 + " " + l1 + " " + t2 + " " + l2 + " " + t3 + " " + l3 + " " + t4 + " " + l4 + " ")
100108

101109
this.output = p5sound.audiocontext.createGain();;
102110

103111
this.control = new TimelineSignal();
112+
this.init();
104113
this.control.connect(this.output);
105-
this.control.setValueAtTime(0, now);
106114

107115
this.connection = null; // store connection
108116

@@ -124,6 +132,12 @@ define(function (require) {
124132
p5sound.soundArray.push(this);
125133
};
126134

135+
136+
p5.Env.prototype.init = function () {
137+
var now = p5sound.audiocontext.currentTime;
138+
var t = now;
139+
this.control.setTargetAtTime(0.00001, t, .001);
140+
};
127141
/**
128142
* Reset the envelope with a series of time/value pairs.
129143
*
@@ -139,10 +153,24 @@ define(function (require) {
139153
* @param {Number} [rLevel] Amplitude 0.0 to 1.0
140154
141155
*/
142-
p5.Env.prototype.set = function(t1, l1, t2, l2, t3, l3){
156+
p5.Env.prototype.set = function(t1, l1, t2, l2, t3, l3, t4, l4){
157+
this.aTime = t1;
158+
this.aLevel = l1;
159+
this.dTime = t2 || 0;
160+
this.dLevel = l2 || 0;
161+
this.sTime = t3 || 0;
162+
this.sLevel = l3 || 0;
163+
this.rTime = t4 || 0;
164+
this.rLevel = l4 || 0;
165+
};
166+
167+
168+
p5.Env.prototype.setADSR = function(t1, l1, t2, l2, t3, l3){
143169
this.aTime = t1;
144170
this.aLevel = l1;
145171
this.dTime = t2 || 0;
172+
this.dLevel = l2 || 0;
173+
this.sTime = 0;
146174
this.sLevel = l2 || 0;
147175
this.rTime = t3 || 0;
148176
this.rLevel = l3 || 0;
@@ -207,7 +235,7 @@ define(function (require) {
207235

208236
this.triggerAttack(unit, secondsFromNow);
209237

210-
this.triggerRelease(unit, secondsFromNow + this.aTime + this.dTime);
238+
this.triggerRelease(unit, secondsFromNow + this.aTime + this.dTime + this.sTime);
211239

212240
};
213241

@@ -238,6 +266,7 @@ define(function (require) {
238266

239267
// get and set value (with linear ramp) to anchor automation
240268
var valToSet = this.control.getValueAtTime(t);
269+
this.control.cancelScheduledValues(t); // not sure if this is necessary
241270
if (this.isExponential == true)
242271
{
243272
this.control.exponentialRampToValueAtTime(this.checkExpInput(valToSet), t);
@@ -247,16 +276,13 @@ define(function (require) {
247276
this.control.linearRampToValueAtTime(valToSet, t);
248277
}
249278

250-
251279
// after each ramp completes, cancel scheduled values
252280
// (so they can be overridden in case env has been re-triggered)
253281
// then, set current value (with linearRamp to avoid click)
254282
// then, schedule the next automation...
255283

256284
// attack
257285
t += this.aTime;
258-
259-
260286
if (this.isExponential == true)
261287
{
262288
this.control.exponentialRampToValueAtTime(this.checkExpInput(this.aLevel), t);
@@ -272,9 +298,26 @@ define(function (require) {
272298
this.control.linearRampToValueAtTime(valToSet, t);
273299
}
274300

275-
// decay to decay level
301+
// decay to decay level (if using ADSR, then decay level == sustain level)
276302
t += this.dTime;
277303
if (this.isExponential == true)
304+
{
305+
this.control.exponentialRampToValueAtTime(this.checkExpInput(this.dLevel), t);
306+
valToSet = this.checkExpInput(this.control.getValueAtTime(t));
307+
this.control.cancelScheduledValues(t);
308+
this.control.exponentialRampToValueAtTime(valToSet, t);
309+
}
310+
else
311+
{
312+
this.control.linearRampToValueAtTime(this.dLevel, t);
313+
valToSet = this.control.getValueAtTime(t);
314+
this.control.cancelScheduledValues(t);
315+
this.control.linearRampToValueAtTime(valToSet, t);
316+
}
317+
318+
// move to sustain level and hold for sustain time (if using ADSR, sustain time is set to 0 and sustain level is set to decay level)
319+
t += this.sTime;
320+
if (this.isExponential == true)
278321
{
279322
this.control.exponentialRampToValueAtTime(this.checkExpInput(this.sLevel), t);
280323
valToSet = this.checkExpInput(this.control.getValueAtTime(t));
@@ -303,6 +346,13 @@ define(function (require) {
303346

304347
// only trigger a release if an attack was triggered
305348
if (!this.wasTriggered) {
349+
// this currently causes a bit of trouble:
350+
// if a later release has been scheduled (via the play function)
351+
// a new earlier release won't interrupt it, because
352+
// this.wasTriggered has already been set to false.
353+
// If we want new earlier releases to override, then we need to
354+
// keep track of the last release time, and if the new release time is
355+
// earlier, then use it.
306356
return;
307357
}
308358

@@ -318,6 +368,7 @@ define(function (require) {
318368

319369
// get and set value (with linear or exponential ramp) to anchor automation
320370
var valToSet = this.control.getValueAtTime(t);
371+
this.control.cancelScheduledValues(t); // not sure if this is necessary
321372
if (this.isExponential == true)
322373
{
323374
this.control.exponentialRampToValueAtTime(this.checkExpInput(valToSet), t);
@@ -345,7 +396,6 @@ define(function (require) {
345396
this.control.linearRampToValueAtTime(valToSet, t);
346397
}
347398

348-
349399
this.wasTriggered = false;
350400
};
351401

0 commit comments

Comments
 (0)