Skip to content

Commit 7ac7b1d

Browse files
committed
changed TimeConstant calculation to happen only when attack and decay times for ramps are changed, rather than at every envelope trigger
1 parent 6b547ef commit 7ac7b1d

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

src/env.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ define(function (require) {
110110
this.rampLowPercentage = 0.02;
111111

112112
this.rampAttackTime = 0.01;
113-
this.rampDecayTime = 0.01;
113+
this.rampDecayTime = 0.5;
114+
115+
this.rampAttackTC = 0.12;
116+
this.rampDecayTC = 0.12;
114117

115118
this.output = p5sound.audiocontext.createGain();;
116119

@@ -146,6 +149,8 @@ define(function (require) {
146149
var now = p5sound.audiocontext.currentTime;
147150
var t = now;
148151
this.control.setTargetAtTime(0.00001, t, .001);
152+
//also, compute the correct time constants
153+
this.setRampAD(this.rampAttackTime, this.rampDecayTime)
149154
};
150155

151156
/**
@@ -189,14 +194,27 @@ define(function (require) {
189194

190195
p5.Env.prototype.setRampAD = function(t1, t2){
191196
//sets the time constants for simple exponential ramps
192-
this.rampAttackTime = t1;
193-
this.rampDecayTime = t2;
197+
this.rampAttackTime = this.checkExpInput(t1);
198+
this.rampDecayTime = this.checkExpInput(t2);
199+
var TCDenominator = 1.0;
200+
/// Aatish Bhatia's calculation for time constant for rise(to adjust 1/1-e calculation to any percentage)
201+
TCDenominator = Math.log(1.0 / (this.checkExpInput(1.0 - this.rampHighPercentage)));
202+
this.rampAttackTC = (t1 / this.checkExpInput(TCDenominator));
203+
TCDenominator = Math.log(1.0 / this.rampLowPercentage);
204+
this.rampDecayTC = (t2 / this.checkExpInput(TCDenominator));
194205
};
195206

196207
p5.Env.prototype.setRampPercentages = function(p1, p2){
197208
//set the percentages that the simple exponential ramps go to
198-
this.rampHighPercentage = p1;
199-
this.rampLowPercentage = p2;
209+
this.rampHighPercentage = this.checkExpInput(p1);
210+
this.rampLowPercentage = this.checkExpInput(p2);
211+
var TCDenominator = 1.0;
212+
//now re-compute the time constants based on those percentages
213+
/// Aatish Bhatia's calculation for time constant for rise(to adjust 1/1-e calculation to any percentage)
214+
TCDenominator = Math.log(1.0 / (this.checkExpInput(1.0 - this.rampHighPercentage)));
215+
this.rampAttackTC = (this.rampAttackTime / this.checkExpInput(TCDenominator));
216+
TCDenominator = Math.log(1.0 / this.rampLowPercentage);
217+
this.rampDecayTC = (this.rampDecayTime / this.checkExpInput(TCDenominator));
200218
};
201219

202220

@@ -446,18 +464,14 @@ define(function (require) {
446464
//if it's going up
447465
if(destination > currentVal)
448466
{
449-
/// Aatish Bhatia's calculation for time constant for rise(to adjust 1/1-e calculation to any percentage)
450-
var rampTC = (this.rampAttackTime / (log((destination - currentVal)/((1.0 - this.rampHighPercentage) * destination))));
451-
this.control.setTargetAtTime(destination, t, rampTC);
467+
this.control.setTargetAtTime(destination, t, this.rampAttackTC);
452468
}
453469

454470
//if it's going down
455471
if(destination < currentVal)
456472
{
457-
/// Aatish Bhatia's calculation for time constant for fall(to adjust 1/1-e calculation to any percentage)
458-
//not sure about this one, should it be 1-rampLowPercentage or not?
459-
var rampTC = (this.rampDecayTime / (log((currentVal - destination)/((this.rampLowPercentage) * currentVal))));
460-
this.control.setTargetAtTime(destination, t, rampTC);
473+
474+
this.control.setTargetAtTime(destination, t, this.rampDecayTC);
461475
}
462476
};
463477

@@ -477,49 +491,37 @@ define(function (require) {
477491
}
478492
}
479493

480-
// get and set value (with linear or exponential ramp) to anchor automation
494+
//get current value
481495
var currentVal = this.checkExpInput(this.control.getValueAtTime(t));
482496

483497
this.control.cancelScheduledValues(t);
484498

485499
//if it's going up
486500
if(destination1 > currentVal)
487501
{
488-
/// Aatish Bhatia's calculation for time constant for rise(to adjust 1/1-e calculation to any percentage)
489-
var rampTC = (this.rampAttackTime / (log((destination1 - currentVal)/((1.0 - this.rampHighPercentage) * destination1))));
490-
//console.log("ramp up1 TC = " + rampTC);
491-
this.control.setTargetAtTime(destination1, t, rampTC);
502+
this.control.setTargetAtTime(destination1, t, this.rampAttackTC);
492503
t += this.rampAttackTime;
493504
}
494505

495506
//if it's going down
496507
else if(destination1 < currentVal)
497508
{
498-
/// Aatish Bhatia's calculation for time constant for fall(to adjust 1/1-e calculation to any percentage)
499-
var rampTC = (this.rampDecayTime / (log((currentVal - destination1)/((this.rampLowPercentage) * currentVal))));
500-
//console.log("ramp down1 TC = " + rampTC);
501-
this.control.setTargetAtTime(destination1, t, rampTC);
509+
this.control.setTargetAtTime(destination1, t, this.rampDecayTC);
502510
t += this.rampDecayTime;
503511
}
504512

505-
// second part of envelope begins
513+
// Now the second part of envelope begins
506514

507515
//if it's going up
508516
if(destination2 > destination1)
509517
{
510-
/// Aatish Bhatia's calculation for time constant for rise(to adjust 1/1-e calculation to any percentage)
511-
var rampTC = (this.rampAttackTime / (log((destination2 - destination1)/((1.0 - this.rampHighPercentage) * destination2))));
512-
//console.log("ramp up2 TC = " + rampTC);
513-
this.control.setTargetAtTime(destination2, t, rampTC);
518+
this.control.setTargetAtTime(destination2, t, this.rampAttackTC);
514519
}
515520

516521
//if it's going down
517522
else if(destination2 < destination1)
518523
{
519-
/// Aatish Bhatia's calculation for time constant for fall(to adjust 1/1-e calculation to any percentage)
520-
var rampTC = (this.rampDecayTime / (log((destination1 - destination2)/((this.rampLowPercentage) * destination1))));
521-
//console.log("ramp down2 TC = " + rampTC);
522-
this.control.setTargetAtTime(destination2, t, rampTC);
524+
this.control.setTargetAtTime(destination2, t, this.rampDecayTC);
523525
}
524526

525527

0 commit comments

Comments
 (0)