Skip to content

Commit 56b3b6e

Browse files
committed
set osc phase based on this.f and update phase when freq is changed
1 parent d7721f4 commit 56b3b6e

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

lib/p5.sound.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.sound.js v0.2.16 2015-11-30 */
1+
/*! p5.sound.js v0.2.16 2015-12-06 */
22
(function (root, factory) {
33
if (typeof define === 'function' && define.amd)
44
define('p5.sound', ['p5'], function (p5) { (factory(p5));});
@@ -3612,6 +3612,7 @@ oscillator = function () {
36123612
}
36133613
this.started = false;
36143614
// components
3615+
this.phaseAmount = undefined;
36153616
this.oscillator = p5sound.audiocontext.createOscillator();
36163617
this.f = freq || 440;
36173618
// frequency
@@ -3762,6 +3763,10 @@ oscillator = function () {
37623763
} else {
37633764
this.oscillator.frequency.linearRampToValueAtTime(val, tFromNow + rampTime + now);
37643765
}
3766+
// reset phase if oscillator has a phase
3767+
if (this.phaseAmount) {
3768+
this.phase(this.phaseAmount);
3769+
}
37653770
} else if (val) {
37663771
if (val.output) {
37673772
val = val.output;
@@ -3849,23 +3854,27 @@ oscillator = function () {
38493854
}
38503855
};
38513856
/**
3852-
* Set the phase of an oscillator between 0.0 and 1.0
3857+
* Set the phase of an oscillator between 0.0 and 1.0.
3858+
* In this implementation, phase is a delay time
3859+
* based on the oscillator's current frequency.
38533860
*
38543861
* @method phase
38553862
* @param {Number} phase float between 0.0 and 1.0
38563863
*/
38573864
p5.Oscillator.prototype.phase = function (p) {
3865+
var delayAmt = p5.prototype.map(p, 0, 1, 0, 1 / this.f);
3866+
var now = p5sound.audiocontext.currentTime;
3867+
this.phaseAmount = p;
38583868
if (!this.dNode) {
38593869
// create a delay node
38603870
this.dNode = p5sound.audiocontext.createDelay();
38613871
// put the delay node in between output and panner
3862-
this.output.disconnect();
3863-
this.output.connect(this.dNode);
3864-
this.dNode.connect(this.panner);
3872+
this.oscillator.disconnect();
3873+
this.oscillator.connect(this.dNode);
3874+
this.dNode.connect(this.output);
38653875
}
3866-
// set delay time based on PWM width
3867-
var now = p5sound.audiocontext.currentTime;
3868-
this.dNode.delayTime.linearRampToValueAtTime(p5.prototype.map(p, 0, 1, 0, 1 / this.oscillator.frequency.value), now);
3876+
// set delay time to match phase:
3877+
this.dNode.delayTime.setValueAtTime(delayAmt, now);
38693878
};
38703879
// ========================== //
38713880
// SIGNAL MATH FOR MODULATION //

0 commit comments

Comments
 (0)