Skip to content

Commit 33e1cfb

Browse files
author
Lauren McCarthy
committed
Merge pull request #337 from therewasaguy/p5SOUND
p5.sound update - Safari fix
2 parents 263772a + 7206243 commit 33e1cfb

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

lib/addons/p5.sound.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.sound.js v0.14 2014-08-20 */
1+
/*! p5.sound.js v0.14 2014-08-22 */
22
/**
33
* p5.sound extends p5 with <a href="http://caniuse.com/audio-api"
44
* target="_blank">Web Audio</a> functionality including audio input,
@@ -28,7 +28,7 @@
2828
* <b><a href="#/p5.Convolver">p5.Convolver:</a></b> Extends
2929
* <a href="#/p5.Reverb">p5.Reverb</a> to simulate the sound of real
3030
* physical spaces through convolution.<br/>
31-
* <a href="#/p5.SoundRecorder">p5.SoundRecorder</a>: Record sound for playback
31+
* <b><a href="#/p5.SoundRecorder">p5.SoundRecorder</a></b>: Record sound for playback
3232
* / save the .wav file.
3333
* <br/><br/>
3434
* p5.sound is on <a href="https://github.com/therewasaguy/p5.sound/">GitHub</a>.
@@ -1892,14 +1892,8 @@ signal = function () {
18921892
p5.Signal = function (value) {
18931893
// scales the constant output to desired output
18941894
this.scalar = ac.createGain();
1895-
this.scalar.gain.maxValue = 10000;
1896-
this.scalar.gain.minValue = -10000;
18971895
this.input = ac.createGain();
1898-
this.input.gain.maxValue = 10000;
1899-
this.input.gain.minValue = -10000;
19001896
this.output = ac.createGain();
1901-
this.output.gain.maxValue = 10000;
1902-
this.output.gain.minValue = -10000;
19031897
// the ratio of this value to the control signal
19041898
this._syncRatio = 1;
19051899
// connect the constant output to the scalar
@@ -1949,7 +1943,7 @@ signal = function () {
19491943
*/
19501944
p5.Signal.prototype.setValueAtTime = function (value, time) {
19511945
value *= this._syncRatio;
1952-
var t = time || 0;
1946+
var t = time || ac.currentTime;
19531947
this.scalar.gain.setValueAtTime(value, t);
19541948
};
19551949
p5.Signal.prototype.setCurrentValueNow = function () {
@@ -1960,16 +1954,16 @@ signal = function () {
19601954
return currentVal;
19611955
};
19621956
p5.Signal.prototype.cancelScheduledValues = function (time) {
1963-
var t = time || 0;
1957+
var t = time || ac.currentTime;
19641958
this.scalar.gain.cancelScheduledValues(t);
19651959
};
19661960
p5.Signal.prototype.linearRampToValueAtTime = function (value, endTime) {
1967-
var t = endTime || 0;
1961+
var t = endTime || ac.currentTime;
19681962
value *= this._syncRatio;
19691963
this.scalar.gain.linearRampToValueAtTime(value, t);
19701964
};
19711965
p5.Signal.prototype.exponentialRampToValueAtTime = function (value, endTime) {
1972-
var t = endTime || 0;
1966+
var t = endTime || ac.currentTime;
19731967
value *= this._syncRatio;
19741968
this.scalar.gain.exponentialRampToValueAtTime(value, t);
19751969
};
@@ -1981,8 +1975,7 @@ signal = function () {
19811975
* @param {[Number]} secondsFromNow Length of fade, in seconds from now
19821976
*/
19831977
p5.Signal.prototype.fade = function (value, secondsFromNow) {
1984-
var s = secondsFromNow || 0;
1985-
var t = ac.currentTime + s + 0.01;
1978+
var t = secondsFromNow || ac.currentTime;
19861979
value *= this._syncRatio;
19871980
this.scalar.gain.linearRampToValueAtTime(value, t);
19881981
};
@@ -2103,8 +2096,6 @@ signal = function () {
21032096
p5.SignalMult = function (num, _input) {
21042097
var mult = new p5.Signal();
21052098
mult.output = mult.input;
2106-
mult.input.gain.maxValue = 10000;
2107-
mult.input.gain.minValue = -10000;
21082099
mult.setValue = function (value) {
21092100
if (typeof value === 'number') {
21102101
this.input.gain.value = value;
@@ -2715,8 +2706,7 @@ env = function () {
27152706
}
27162707
var currentVal = this.control.getValue();
27172708
this.control.cancelScheduledValues(t);
2718-
// this.control.fade(currentVal, now + tFromNow);
2719-
this.control.fade(0, t);
2709+
this.control.fade(currentVal, now + tFromNow);
27202710
if (unit) {
27212711
if (this.connection !== unit) {
27222712
this.connect(unit);
@@ -2764,22 +2754,23 @@ env = function () {
27642754
p5.Env.prototype.triggerAttack = function (unit, secondsFromNow) {
27652755
var now = p5sound.audiocontext.currentTime;
27662756
var tFromNow = secondsFromNow || 0;
2767-
var t = now + tFromNow;
2757+
var tMinus = now + tFromNow;
2758+
var t = tMinus + 0.03;
27682759
this.lastAttack = t;
27692760
if (typeof this.timeoutID === 'number') {
27702761
window.clearTimeout(this.timeoutID);
27712762
}
27722763
var currentVal = this.control.getValue();
2773-
this.control.cancelScheduledValues(t);
2774-
this.control.fade(currentVal);
2764+
this.control.cancelScheduledValues(tMinus);
2765+
this.control.fade(currentVal, t);
27752766
if (unit) {
27762767
if (this.connection !== unit) {
27772768
this.connect(unit);
27782769
}
27792770
}
27802771
// if unit is an oscillator, set its amp to 0 and start it
27812772
if (this.connection && this.connection instanceof p5.Oscillator) {
2782-
if (this.connection.started) {
2773+
if (!this.connection.started) {
27832774
this.connection.stop();
27842775
}
27852776
}
@@ -2805,42 +2796,49 @@ env = function () {
28052796
* @param {Number} secondsFromNow time to trigger the release
28062797
*/
28072798
p5.Env.prototype.triggerRelease = function (unit, secondsFromNow) {
2808-
var now = p5sound.audiocontext.currentTime + 0.001;
2799+
var now = p5sound.audiocontext.currentTime;
28092800
var tFromNow = secondsFromNow || 0;
2810-
var t = now + tFromNow;
2811-
// + envTime;
2801+
var tMinus = now + tFromNow;
2802+
var t = tMinus + 0.03;
28122803
var relTime;
28132804
if (unit) {
28142805
if (this.connection !== unit) {
28152806
this.connect(unit);
28162807
}
28172808
}
28182809
var currentVal = this.control.getValue();
2819-
this.control.cancelScheduledValues(t);
2820-
this.control.fade(currentVal);
2810+
this.control.cancelScheduledValues(tMinus);
2811+
this.control.fade(currentVal, t);
28212812
// release based on how much time has passed since this.lastAttack
2822-
if (now - this.lastAttack > this.aTime + this.dTime + this.sTime + this.rTime) {
2823-
this.control.linearRampToValueAtTime(this.sLevel, t + this.sTime);
2824-
this.control.linearRampToValueAtTime(this.rLevel, t + this.sTime + this.rTime);
2825-
relTime = t + this.rTime;
2826-
} else if (now - this.lastAttack > this.aTime + this.dTime) {
2827-
this.control.linearRampToValueAtTime(this.dLevel, t + this.dTime);
2828-
this.control.linearRampToValueAtTime(this.sLevel, t + this.dTime + this.sTime);
2829-
this.control.linearRampToValueAtTime(this.rLevel, t + this.dTime + this.sTime + this.rTime);
2813+
if (now - this.lastAttack < this.aTime) {
2814+
var a = this.aTime - (now - this.lastAttack);
2815+
this.control.linearRampToValueAtTime(this.aLevel, t + a);
2816+
this.control.linearRampToValueAtTime(this.dLevel, t + this.aTime + this.dTime);
2817+
this.control.linearRampToValueAtTime(this.sLevel, t + this.aTime + this.dTime + this.sTime);
2818+
this.control.linearRampToValueAtTime(this.rLevel, t + this.aTime + this.dTime + this.sTime + this.rTime);
2819+
relTime = t + this.dTime + this.sTime + this.rTime;
2820+
} else if (now - this.lastAttack < this.aTime + this.dTime) {
2821+
var d = this.aTime + this.dTime - (now - this.lastAttack);
2822+
this.control.linearRampToValueAtTime(this.dLevel, t + d);
2823+
this.control.linearRampToValueAtTime(this.sLevel, t + d + this.sTime);
2824+
this.control.linearRampToValueAtTime(this.rLevel, t + d + this.sTime + this.rTime);
28302825
relTime = t + this.sTime + this.rTime;
2831-
} else if (now - this.lastAttack > this.aTime) {
2832-
this.control.linearRampToValueAtTime(this.dLevel, t + this.dTime);
2833-
this.control.linearRampToValueAtTime(this.sLevel, t + this.dTime + this.sTime);
2834-
this.control.linearRampToValueAtTime(this.rLevel, t + this.dTime + this.sTime + this.rTime);
2826+
} else if (now - this.lastAttack < this.aTime + this.dTime + this.sTime) {
2827+
var s = this.aTime + this.dTime + this.sTime - (now - this.lastAttack);
2828+
this.control.linearRampToValueAtTime(this.sLevel, t + s);
2829+
this.control.linearRampToValueAtTime(this.rLevel, t + s + this.rTime);
2830+
relTime = t + this.rTime;
2831+
} else {
2832+
this.control.linearRampToValueAtTime(this.rLevel, t + this.rTime);
28352833
relTime = t + this.dTime + this.sTime + this.rTime;
28362834
}
2837-
if (this.connection.hasOwnProperty('oscillator')) {
2835+
if (this.connection && this.connection.hasOwnProperty('oscillator')) {
28382836
var clearTime = relTime * 1000;
28392837
this.timeoutID = window.setTimeout(clearThing, clearTime);
28402838
}
28412839
// if unit is an oscillator, and volume is 0, stop it to save memory
28422840
function clearThing() {
2843-
if (this.connection.hasOwnProperty('oscillator') && unit.started) {
2841+
if (this.connection && this.connection.hasOwnProperty('oscillator') && unit.started) {
28442842
this.connection.amp(0);
28452843
this.connection.stop();
28462844
}
@@ -4755,7 +4753,7 @@ soundRecorder = function () {
47554753
*
47564754
* else if (state === 2) {
47574755
* soundFile.play(); // play the result!
4758-
* saveSound(soundFile, 'mySound.wav');
4756+
* save(soundFile, 'mySound.wav');
47594757
* state++;
47604758
* }
47614759
* }
@@ -4975,5 +4973,6 @@ var src_app;
49754973
src_app = function () {
49764974
'use strict';
49774975
var p5SOUND = sndcore;
4976+
// require('metro');
49784977
return p5SOUND;
49794978
}(sndcore, master, helpers, soundfile, amplitude, fft, signal, oscillator, env, pulse, noise, audioin, filter, delay, reverb, looper, soundRecorder);

0 commit comments

Comments
 (0)