Skip to content

Commit a7a9883

Browse files
committed
p5.sound env tweaks
1 parent 6a58e0d commit a7a9883

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

lib/addons/p5.sound.js

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ signal = function () {
19431943
*/
19441944
p5.Signal.prototype.setValueAtTime = function (value, time) {
19451945
value *= this._syncRatio;
1946-
var t = time || 0;
1946+
var t = time || ac.currentTime;
19471947
this.scalar.gain.setValueAtTime(value, t);
19481948
};
19491949
p5.Signal.prototype.setCurrentValueNow = function () {
@@ -1954,16 +1954,16 @@ signal = function () {
19541954
return currentVal;
19551955
};
19561956
p5.Signal.prototype.cancelScheduledValues = function (time) {
1957-
var t = time || 0;
1957+
var t = time || ac.currentTime;
19581958
this.scalar.gain.cancelScheduledValues(t);
19591959
};
19601960
p5.Signal.prototype.linearRampToValueAtTime = function (value, endTime) {
1961-
var t = endTime || 0;
1961+
var t = endTime || ac.currentTime;
19621962
value *= this._syncRatio;
19631963
this.scalar.gain.linearRampToValueAtTime(value, t);
19641964
};
19651965
p5.Signal.prototype.exponentialRampToValueAtTime = function (value, endTime) {
1966-
var t = endTime || 0;
1966+
var t = endTime || ac.currentTime;
19671967
value *= this._syncRatio;
19681968
this.scalar.gain.exponentialRampToValueAtTime(value, t);
19691969
};
@@ -1975,8 +1975,7 @@ signal = function () {
19751975
* @param {[Number]} secondsFromNow Length of fade, in seconds from now
19761976
*/
19771977
p5.Signal.prototype.fade = function (value, secondsFromNow) {
1978-
var s = secondsFromNow || 0;
1979-
var t = ac.currentTime + s + 0.01;
1978+
var t = secondsFromNow || ac.currentTime;
19801979
value *= this._syncRatio;
19811980
this.scalar.gain.linearRampToValueAtTime(value, t);
19821981
};
@@ -2707,8 +2706,7 @@ env = function () {
27072706
}
27082707
var currentVal = this.control.getValue();
27092708
this.control.cancelScheduledValues(t);
2710-
// this.control.fade(currentVal, now + tFromNow);
2711-
this.control.fade(0, t);
2709+
this.control.fade(currentVal, now + tFromNow);
27122710
if (unit) {
27132711
if (this.connection !== unit) {
27142712
this.connect(unit);
@@ -2756,22 +2754,24 @@ env = function () {
27562754
p5.Env.prototype.triggerAttack = function (unit, secondsFromNow) {
27572755
var now = p5sound.audiocontext.currentTime;
27582756
var tFromNow = secondsFromNow || 0;
2759-
var t = now + tFromNow;
2757+
var tMinus = now + tFromNow;
2758+
var t = tMinus + 0.03;
27602759
this.lastAttack = t;
27612760
if (typeof this.timeoutID === 'number') {
27622761
window.clearTimeout(this.timeoutID);
27632762
}
27642763
var currentVal = this.control.getValue();
2765-
this.control.cancelScheduledValues(t);
2766-
this.control.fade(currentVal);
2764+
console.log(currentVal);
2765+
this.control.cancelScheduledValues(tMinus);
2766+
this.control.fade(currentVal, t);
27672767
if (unit) {
27682768
if (this.connection !== unit) {
27692769
this.connect(unit);
27702770
}
27712771
}
27722772
// if unit is an oscillator, set its amp to 0 and start it
27732773
if (this.connection && this.connection instanceof p5.Oscillator) {
2774-
if (this.connection.started) {
2774+
if (!this.connection.started) {
27752775
this.connection.stop();
27762776
}
27772777
}
@@ -2797,42 +2797,49 @@ env = function () {
27972797
* @param {Number} secondsFromNow time to trigger the release
27982798
*/
27992799
p5.Env.prototype.triggerRelease = function (unit, secondsFromNow) {
2800-
var now = p5sound.audiocontext.currentTime + 0.001;
2800+
var now = p5sound.audiocontext.currentTime;
28012801
var tFromNow = secondsFromNow || 0;
2802-
var t = now + tFromNow;
2803-
// + envTime;
2802+
var tMinus = now + tFromNow;
2803+
var t = tMinus + 0.03;
28042804
var relTime;
28052805
if (unit) {
28062806
if (this.connection !== unit) {
28072807
this.connect(unit);
28082808
}
28092809
}
28102810
var currentVal = this.control.getValue();
2811-
this.control.cancelScheduledValues(t);
2812-
this.control.fade(currentVal);
2811+
this.control.cancelScheduledValues(tMinus);
2812+
this.control.fade(currentVal, t);
28132813
// release based on how much time has passed since this.lastAttack
2814-
if (now - this.lastAttack > this.aTime + this.dTime + this.sTime + this.rTime) {
2815-
this.control.linearRampToValueAtTime(this.sLevel, t + this.sTime);
2816-
this.control.linearRampToValueAtTime(this.rLevel, t + this.sTime + this.rTime);
2817-
relTime = t + this.rTime;
2818-
} else if (now - this.lastAttack > this.aTime + this.dTime) {
2819-
this.control.linearRampToValueAtTime(this.dLevel, t + this.dTime);
2820-
this.control.linearRampToValueAtTime(this.sLevel, t + this.dTime + this.sTime);
2821-
this.control.linearRampToValueAtTime(this.rLevel, t + this.dTime + this.sTime + this.rTime);
2814+
if (now - this.lastAttack < this.aTime) {
2815+
var a = this.aTime - (now - this.lastAttack);
2816+
this.control.linearRampToValueAtTime(this.aLevel, t + a);
2817+
this.control.linearRampToValueAtTime(this.dLevel, t + this.aTime + this.dTime);
2818+
this.control.linearRampToValueAtTime(this.sLevel, t + this.aTime + this.dTime + this.sTime);
2819+
this.control.linearRampToValueAtTime(this.rLevel, t + this.aTime + this.dTime + this.sTime + this.rTime);
2820+
relTime = t + this.dTime + this.sTime + this.rTime;
2821+
} else if (now - this.lastAttack < this.aTime + this.dTime) {
2822+
var d = this.aTime + this.dTime - (now - this.lastAttack);
2823+
this.control.linearRampToValueAtTime(this.dLevel, t + d);
2824+
this.control.linearRampToValueAtTime(this.sLevel, t + d + this.sTime);
2825+
this.control.linearRampToValueAtTime(this.rLevel, t + d + this.sTime + this.rTime);
28222826
relTime = t + this.sTime + this.rTime;
2823-
} else if (now - this.lastAttack > this.aTime) {
2824-
this.control.linearRampToValueAtTime(this.dLevel, t + this.dTime);
2825-
this.control.linearRampToValueAtTime(this.sLevel, t + this.dTime + this.sTime);
2826-
this.control.linearRampToValueAtTime(this.rLevel, t + this.dTime + this.sTime + this.rTime);
2827+
} else if (now - this.lastAttack < this.aTime + this.dTime + this.sTime) {
2828+
var s = this.aTime + this.dTime + this.sTime - (now - this.lastAttack);
2829+
this.control.linearRampToValueAtTime(this.sLevel, t + s);
2830+
this.control.linearRampToValueAtTime(this.rLevel, t + s + this.rTime);
2831+
relTime = t + this.rTime;
2832+
} else {
2833+
this.control.linearRampToValueAtTime(this.rLevel, t + this.rTime);
28272834
relTime = t + this.dTime + this.sTime + this.rTime;
28282835
}
2829-
if (this.connection.hasOwnProperty('oscillator')) {
2836+
if (this.connection && this.connection.hasOwnProperty('oscillator')) {
28302837
var clearTime = relTime * 1000;
28312838
this.timeoutID = window.setTimeout(clearThing, clearTime);
28322839
}
28332840
// if unit is an oscillator, and volume is 0, stop it to save memory
28342841
function clearThing() {
2835-
if (this.connection.hasOwnProperty('oscillator') && unit.started) {
2842+
if (this.connection && this.connection.hasOwnProperty('oscillator') && unit.started) {
28362843
this.connection.amp(0);
28372844
this.connection.stop();
28382845
}

0 commit comments

Comments
 (0)