Skip to content

Commit 82353cd

Browse files
committed
rampAD --> ramp
1 parent 2035b6b commit 82353cd

File tree

3 files changed

+16
-65
lines changed

3 files changed

+16
-65
lines changed

examples/bells_envelope_test/sketch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
// This example shows a more complex use of the .rampAD function for the envelope.
2+
// This example shows a more complex use of the .ramp function for the envelope.
33
// You can use it to make a simple attack/decay envelope for struck or plucked style notes.
44
// Here, we're creating synthetic bells using additive synthesis, and triggering each of their attacks and decays differently to make different harmonics last for different times.
55
// Have fun! - Jeff Snyder
@@ -124,7 +124,7 @@ function makeSoundAttack(time, playbackRate)
124124
var whichNote = patternArray[note];
125125
for (var i = 0; i < numOsc; i++)
126126
{
127-
envelope[whichNote][i].rampAD(osc[whichNote][i], time, (oscVols[whichNote][i] * random(.8, 1.0))); // the added randomness just makes each strike a little different.
127+
envelope[whichNote][i].ramp(osc[whichNote][i], time, (oscVols[whichNote][i] * random(.8, 1.0)), 0); // the added randomness just makes each strike a little different.
128128
}
129129
note = (note + 1) % patternArray.length;
130130
if (firstNote == 1)

examples/envelopeRamp/sketch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function setup() {
1717
myPhraseAttack = new p5.Phrase('testerAttack', makeSoundAttack, atPattern);
1818
myPart = new p5.Part();
1919
myPart.addPhrase(myPhraseAttack);
20-
myPart.setBPM(360);
20+
myPart.setBPM(240);
2121
myPart.loop();
2222
myPart.start();
2323
fft = new p5.FFT();
@@ -54,7 +54,7 @@ function makeSoundAttack(time, playbackRate)
5454
var midiValue = scaleArray[note];
5555
var freqValue = midiToFreq(midiValue);
5656
osc.freq(freqValue * 2, .001, time);
57-
envelope.rampAD(osc, time, 1 , 0);
57+
envelope.ramp(osc, time, 1, 0);
5858
note = (note + 1) % scaleArray.length;
5959
setTimeout(redrawWaveform, time * 1000.0);
6060
}

src/env.js

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -466,67 +466,25 @@ define(function (require) {
466466
};
467467

468468
/**
469-
* Exponentially ramp to a value, using the time constants
470-
* set by setRampAD. Going up uses attackTime,
471-
* going down uses decayTime.
469+
* Exponentially ramp to a value using time constants.
470+
* If the value is higher than current value, it uses attackTime,
471+
* while a decrease uses decayTime.
472472
*
473473
* @method ramp
474474
* @param {Object} unit p5.sound Object or Web Audio Param
475475
* @param {Number} secondsFromNow When to trigger the ramp
476476
* @param {Number} v Target value
477+
* @param {Number} [v2] Second target value (optional)
477478
*/
478-
p5.Env.prototype.ramp = function(unit, secondsFromNow, v) {
479-
480-
var now = p5sound.audiocontext.currentTime;
481-
var tFromNow = secondsFromNow || 0;
482-
var t = now + tFromNow;
483-
var destination = this.checkExpInput(v);
484-
485-
if (unit) {
486-
if (this.connection !== unit) {
487-
this.connect(unit);
488-
}
489-
}
490-
491-
// get and set value (with linear or exponential ramp) to anchor automation
492-
var currentVal = this.checkExpInput(this.control.getValueAtTime(t));
493-
this.control.cancelScheduledValues(t);
494-
495-
//if it's going up
496-
if(destination > currentVal)
497-
{
498-
this.control.setTargetAtTime(destination, t, this.rampAttackTC);
499-
}
500-
501-
//if it's going down
502-
if(destination < currentVal)
503-
{
504-
505-
this.control.setTargetAtTime(destination, t, this.rampDecayTC);
506-
}
507-
};
508-
509-
510-
/**
511-
* rampAD is a "pingable" Attack/Decay trigger. You give it a value
512-
* to ramp to, and it will use the preset "simpleAD" time constants
513-
* to form an exponential ramp up to the value and back down to zero
514-
* or the 2nd value argument.
515-
*
516-
* @method rampAD
517-
* @param {Object} unit p5.sound Object or Web Audio Param
518-
* @param {Number} secondsFromNow When to trigger the ramp
519-
* @param {Number} level1 Target value 1
520-
* @param {Number} [level2] Target value 2 (optional, defaults to zero)
521-
*/
522-
p5.Env.prototype.rampAD = function(unit, secondsFromNow, v1, v2) {
479+
p5.Env.prototype.ramp = function(unit, secondsFromNow, v1, v2) {
523480

524481
var now = p5sound.audiocontext.currentTime;
525482
var tFromNow = secondsFromNow || 0;
526483
var t = now + tFromNow;
527484
var destination1 = this.checkExpInput(v1);
528-
var destination2 = this.checkExpInput(v2 || 0);
485+
var destination2 = typeof(v2) !== 'undefined' ? this.checkExpInput(v2) : undefined;
529486

487+
// connect env to unit if not already connected
530488
if (unit) {
531489
if (this.connection !== unit) {
532490
this.connect(unit);
@@ -535,42 +493,35 @@ define(function (require) {
535493

536494
//get current value
537495
var currentVal = this.checkExpInput(this.control.getValueAtTime(t));
538-
539496
this.control.cancelScheduledValues(t);
540497

541498
//if it's going up
542-
if(destination1 > currentVal)
543-
{
499+
if (destination1 > currentVal) {
544500
this.control.setTargetAtTime(destination1, t, this.rampAttackTC);
545501
t += this.rampAttackTime;
546502
}
547503

548504
//if it's going down
549-
else if(destination1 < currentVal)
550-
{
505+
else if (destination1 < currentVal) {
551506
this.control.setTargetAtTime(destination1, t, this.rampDecayTC);
552507
t += this.rampDecayTime;
553508
}
554509

555510
// Now the second part of envelope begins
511+
if (destination2 === undefined) return;
556512

557513
//if it's going up
558-
if(destination2 > destination1)
559-
{
514+
if (destination2 > destination1) {
560515
this.control.setTargetAtTime(destination2, t, this.rampAttackTC);
561516
}
562517

563518
//if it's going down
564-
else if(destination2 < destination1)
565-
{
519+
else if (destination2 < destination1) {
566520
this.control.setTargetAtTime(destination2, t, this.rampDecayTC);
567521
}
568-
569-
570522
};
571523

572524

573-
574525
p5.Env.prototype.connect = function(unit){
575526
this.connection = unit;
576527

0 commit comments

Comments
 (0)