|
1 | | -/*! p5.sound.js v0.2.16 2015-11-30 */ |
| 1 | +/*! p5.sound.js v0.2.16 2015-12-06 */ |
2 | 2 | (function (root, factory) { |
3 | 3 | if (typeof define === 'function' && define.amd) |
4 | 4 | define('p5.sound', ['p5'], function (p5) { (factory(p5));}); |
@@ -3612,6 +3612,7 @@ oscillator = function () { |
3612 | 3612 | } |
3613 | 3613 | this.started = false; |
3614 | 3614 | // components |
| 3615 | + this.phaseAmount = undefined; |
3615 | 3616 | this.oscillator = p5sound.audiocontext.createOscillator(); |
3616 | 3617 | this.f = freq || 440; |
3617 | 3618 | // frequency |
@@ -3762,6 +3763,10 @@ oscillator = function () { |
3762 | 3763 | } else { |
3763 | 3764 | this.oscillator.frequency.linearRampToValueAtTime(val, tFromNow + rampTime + now); |
3764 | 3765 | } |
| 3766 | + // reset phase if oscillator has a phase |
| 3767 | + if (this.phaseAmount) { |
| 3768 | + this.phase(this.phaseAmount); |
| 3769 | + } |
3765 | 3770 | } else if (val) { |
3766 | 3771 | if (val.output) { |
3767 | 3772 | val = val.output; |
@@ -3849,23 +3854,27 @@ oscillator = function () { |
3849 | 3854 | } |
3850 | 3855 | }; |
3851 | 3856 | /** |
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. |
3853 | 3860 | * |
3854 | 3861 | * @method phase |
3855 | 3862 | * @param {Number} phase float between 0.0 and 1.0 |
3856 | 3863 | */ |
3857 | 3864 | 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; |
3858 | 3868 | if (!this.dNode) { |
3859 | 3869 | // create a delay node |
3860 | 3870 | this.dNode = p5sound.audiocontext.createDelay(); |
3861 | 3871 | // 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); |
3865 | 3875 | } |
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); |
3869 | 3878 | }; |
3870 | 3879 | // ========================== // |
3871 | 3880 | // SIGNAL MATH FOR MODULATION // |
|
0 commit comments