Skip to content

Commit f45e497

Browse files
committed
setRampAD --> setADSR
1 parent 82353cd commit f45e497

File tree

3 files changed

+53
-51
lines changed

3 files changed

+53
-51
lines changed

examples/bells_envelope_test/sketch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function setup()
6767
myMaxAttack = washedMax;
6868
print("washed");
6969
}
70-
envelope[i][j].setRampAD(random(.001, myMaxAttack), random(.01, maxDecay)); // turning sustain level to 0. makes an AD envelope
70+
envelope[i][j].setADSR(random(.001, myMaxAttack), random(.01, maxDecay)); // turning sustain level to 0. makes an AD envelope
7171
osc[i][j].amp(0.);
7272
oscVols[i][j] = random(.01, .3);
7373
if (idealOrReal == 0)

examples/envelopeRamp/sketch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function setup() {
1111
createCanvas(710, 200);
1212
osc = new p5.SinOsc();
1313
envelope = new p5.Env();
14-
envelope.setRampAD(.005,0.02);
14+
envelope.setADSR(.005,0.02);
1515
osc.amp(0.);
1616
osc.start();
1717
myPhraseAttack = new p5.Phrase('testerAttack', makeSoundAttack, atPattern);

src/env.js

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ define(function (require) {
7575
/**
7676
* @property attackTime
7777
*/
78-
this.aTime = t1;
78+
this.aTime = t1 || 0.1;
7979
/**
8080
* @property attackLevel
8181
*/
82-
this.aLevel = l1;
82+
this.aLevel = l1 || 1;
8383
/**
8484
* @property decayTime
8585
*/
86-
this.dTime = t2 || 0;
86+
this.dTime = t2 || 0.5;
8787
/**
8888
* @property decayLevel
8989
*/
@@ -105,15 +105,10 @@ define(function (require) {
105105
*/
106106
this.rLevel = l4 || 0;
107107

108-
this.rampHighPercentage = 0.98;
109-
110-
this.rampLowPercentage = 0.02;
108+
this._rampHighPercentage = 0.98;
111109

112-
this.rampAttackTime = 0.01;
113-
this.rampDecayTime = 0.5;
110+
this._rampLowPercentage = 0.02;
114111

115-
this.rampAttackTC = 0.12;
116-
this.rampDecayTC = 0.12;
117112

118113
this.output = p5sound.audiocontext.createGain();;
119114

@@ -150,7 +145,7 @@ define(function (require) {
150145
var t = now;
151146
this.control.setTargetAtTime(0.00001, t, .001);
152147
//also, compute the correct time constants
153-
this.setRampAD(this.rampAttackTime, this.rampDecayTime)
148+
this._setRampAD(this.aTime, this.dTime)
154149
};
155150

156151
/**
@@ -176,6 +171,9 @@ define(function (require) {
176171
this.sLevel = l3 || 0;
177172
this.rTime = t4 || 0;
178173
this.rLevel = l4 || 0;
174+
175+
// set time constants for ramp
176+
this._setRampAD(t1, t2);
179177
};
180178

181179
/**
@@ -192,49 +190,52 @@ define(function (require) {
192190
* @param {Number} [releaseTime] in seconds from now (defaults to 0)
193191
* @param {Number} [releaseVal] value (defaults to 0)
194192
*/
195-
p5.Env.prototype.setADSR = function(t1, l1, t2, l2, t3, l3){
196-
this.aTime = t1;
197-
this.aLevel = l1;
198-
this.dTime = t2 || 0;
199-
this.dLevel = l2 || 0;
200-
this.sTime = 0;
201-
this.sLevel = l2 || 0;
202-
this.rTime = t3 || 0;
203-
this.rLevel = l3 || 0;
193+
p5.Env.prototype.setADSR = function(aTime, dTime, sPercent, rTime){
194+
this.aTime = aTime;
195+
this.dTime = dTime || 0;
196+
this.sPercent = sPercent;
197+
this.rTime = rTime;
198+
199+
// also set time constants for ramp
200+
this._setRampAD(aTime, dTime);
204201
};
205202

206-
/**
207-
* Set the <a href="https://en.wikipedia.org/wiki/RC_time_constant">
208-
* time constants</a> for simple exponential ramps.
209-
* The larger the time constant value, the slower the
210-
* transition will be.
211-
*
212-
* @method setRampAD
213-
* @param {Number} attackTimeConstant attack time constant
214-
* @param {Number} decayTimeConstant decay time constant
215-
*/
216-
p5.Env.prototype.setRampAD = function(t1, t2){
217-
this.rampAttackTime = this.checkExpInput(t1);
218-
this.rampDecayTime = this.checkExpInput(t2);
203+
204+
// private (undocumented) method called when ADSR is set to set time constants for ramp
205+
//
206+
// Set the <a href="https://en.wikipedia.org/wiki/RC_time_constant">
207+
// time constants</a> for simple exponential ramps.
208+
// The larger the time constant value, the slower the
209+
// transition will be.
210+
//
211+
// method _setRampAD
212+
// param {Number} attackTimeConstant attack time constant
213+
// param {Number} decayTimeConstant decay time constant
214+
//
215+
p5.Env.prototype._setRampAD = function(t1, t2){
216+
this._rampAttackTime = this.checkExpInput(t1);
217+
this._rampDecayTime = this.checkExpInput(t2);
218+
219219
var TCDenominator = 1.0;
220220
/// Aatish Bhatia's calculation for time constant for rise(to adjust 1/1-e calculation to any percentage)
221-
TCDenominator = Math.log(1.0 / (this.checkExpInput(1.0 - this.rampHighPercentage)));
222-
this.rampAttackTC = (t1 / this.checkExpInput(TCDenominator));
223-
TCDenominator = Math.log(1.0 / this.rampLowPercentage);
224-
this.rampDecayTC = (t2 / this.checkExpInput(TCDenominator));
221+
TCDenominator = Math.log(1.0 / (this.checkExpInput(1.0 - this._rampHighPercentage)));
222+
this._rampAttackTC = (t1 / this.checkExpInput(TCDenominator));
223+
TCDenominator = Math.log(1.0 / this._rampLowPercentage);
224+
this._rampDecayTC = (t2 / this.checkExpInput(TCDenominator));
225225
};
226226

227+
// private method
227228
p5.Env.prototype.setRampPercentages = function(p1, p2){
228229
//set the percentages that the simple exponential ramps go to
229-
this.rampHighPercentage = this.checkExpInput(p1);
230-
this.rampLowPercentage = this.checkExpInput(p2);
230+
this._rampHighPercentage = this.checkExpInput(p1);
231+
this._rampLowPercentage = this.checkExpInput(p2);
231232
var TCDenominator = 1.0;
232233
//now re-compute the time constants based on those percentages
233234
/// Aatish Bhatia's calculation for time constant for rise(to adjust 1/1-e calculation to any percentage)
234-
TCDenominator = Math.log(1.0 / (this.checkExpInput(1.0 - this.rampHighPercentage)));
235-
this.rampAttackTC = (this.rampAttackTime / this.checkExpInput(TCDenominator));
236-
TCDenominator = Math.log(1.0 / this.rampLowPercentage);
237-
this.rampDecayTC = (this.rampDecayTime / this.checkExpInput(TCDenominator));
235+
TCDenominator = Math.log(1.0 / (this.checkExpInput(1.0 - this._rampHighPercentage)));
236+
this._rampAttackTC = (this._rampAttackTime / this.checkExpInput(TCDenominator));
237+
TCDenominator = Math.log(1.0 / this._rampLowPercentage);
238+
this._rampDecayTC = (this._rampDecayTime / this.checkExpInput(TCDenominator));
238239
};
239240

240241

@@ -475,6 +476,7 @@ define(function (require) {
475476
* @param {Number} secondsFromNow When to trigger the ramp
476477
* @param {Number} v Target value
477478
* @param {Number} [v2] Second target value (optional)
479+
* @example
478480
*/
479481
p5.Env.prototype.ramp = function(unit, secondsFromNow, v1, v2) {
480482

@@ -497,27 +499,27 @@ define(function (require) {
497499

498500
//if it's going up
499501
if (destination1 > currentVal) {
500-
this.control.setTargetAtTime(destination1, t, this.rampAttackTC);
501-
t += this.rampAttackTime;
502+
this.control.setTargetAtTime(destination1, t, this._rampAttackTC);
503+
t += this._rampAttackTime;
502504
}
503505

504506
//if it's going down
505507
else if (destination1 < currentVal) {
506-
this.control.setTargetAtTime(destination1, t, this.rampDecayTC);
507-
t += this.rampDecayTime;
508+
this.control.setTargetAtTime(destination1, t, this._rampDecayTC);
509+
t += this._rampDecayTime;
508510
}
509511

510512
// Now the second part of envelope begins
511513
if (destination2 === undefined) return;
512514

513515
//if it's going up
514516
if (destination2 > destination1) {
515-
this.control.setTargetAtTime(destination2, t, this.rampAttackTC);
517+
this.control.setTargetAtTime(destination2, t, this._rampAttackTC);
516518
}
517519

518520
//if it's going down
519521
else if (destination2 < destination1) {
520-
this.control.setTargetAtTime(destination2, t, this.rampDecayTC);
522+
this.control.setTargetAtTime(destination2, t, this._rampDecayTC);
521523
}
522524
};
523525

0 commit comments

Comments
 (0)