Skip to content

Commit

Permalink
Merge branch 'master' into moduleSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
endurance21 authored Jun 22, 2020
2 parents 9729cb4 + 093a7b7 commit bfb8c3b
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 152 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: processing
custom: https://processingfoundation.org/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Interactive documentation at [p5js.org/reference/#/libraries/p5.sound](http://p5

### Latest Build
* Visit http://p5js.org/download/ for the latest official release of p5 with the latest p5.sound included.
* The sound library [here](https://github.com/therewasaguy/p5.sound/blob/master/lib) is updated more frequently, and we occasionally offer new [releases](https://github.com/processing/p5.js-sound/releases) before p5's release cycle.
* The sound library [here](https://github.com/processing/p5.js-sound/blob/master/lib) is updated more frequently, and we occasionally offer new [releases](https://github.com/processing/p5.js-sound/releases) before p5's release cycle.


### Contribute
Expand Down
4 changes: 2 additions & 2 deletions fragments/before.frag
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
* </li>
* <li><a href="#/p5/userStartAudio">userStartAudio</a>: Enable audio in a
* browser- and user-friendly way.</a>
* <p>p5.sound is on <a href="https://github.com/therewasaguy/p5.sound/">GitHub</a>.
* <p>p5.sound is on <a href="https://github.com/processing/p5.js-sound/">GitHub</a>.
* Download the latest version
* <a href="https://github.com/therewasaguy/p5.sound/blob/master/lib/p5.sound.js">here</a>.</p>
* <a href="https://github.com/processing/p5.js-sound/blob/master/lib/p5.sound.js">here</a>.</p>
*
* @module p5.sound
* @submodule p5.sound
Expand Down
178 changes: 169 additions & 9 deletions src/oscillator.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,73 @@ p5.Oscillator.prototype.freq = function (val, rampTime = 0, tFromNow = 0) {
if (rampTime === 0) {
this.oscillator.frequency.setValueAtTime(val, tFromNow + now);
} else {
if (val > 0) {
this.oscillator.frequency.exponentialRampToValueAtTime(
val,
tFromNow + rampTime + now
);
// return the Gain Node
return this.output.gain;
}
};

// these are now the same thing
p5.Oscillator.prototype.fade = p5.Oscillator.prototype.amp;


/**
* Returns the value of output gain
*
* @method getAmp
* @for p5.Oscillator
*
* @returns {number} Amplitude value between 0.0 and 1.0
*/
p5.Oscillator.prototype.getAmp = function() {

return this.output.gain.value;
};

/**
* Set frequency of an oscillator to a value. Or, pass in an object
* such as an oscillator to modulate the frequency with an audio signal.
*
* @method freq
* @for p5.Oscillator
* @param {Number|Object} Frequency Frequency in Hz
* or modulating signal/oscillator
* @param {Number} [rampTime] Ramp time (in seconds)
* @param {Number} [timeFromNow] Schedule this event to happen
* at x seconds from now
* @return {AudioParam} Frequency If no value is provided,
* returns the Web Audio API
* AudioParam that controls
* this oscillator's frequency
* @example
* <div><code>
* let osc;
*
* function setup() {
* let cnv = createCanvas(100, 100);
* cnv.mousePressed(playOscillator);
* osc = new p5.Oscillator(300);
* background(220);
* text('tap to play', 20, 20);
* }
*
* function playOscillator() {
* osc.start();
* osc.amp(0.5);
* // start at 700Hz
* osc.freq(700);
* // ramp to 60Hz over 0.7 seconds
* osc.freq(60, 0.7);
* osc.amp(0, 0.1, 0.7);
* }
* </code></div>
*/
p5.Oscillator.prototype.freq = function (val, rampTime = 0, tFromNow = 0) {
if (typeof val === 'number' && !isNaN(val)) {
this.f = val;
var now = p5sound.audiocontext.currentTime;

if (rampTime === 0) {
this.oscillator.frequency.setValueAtTime(val, tFromNow + now);
} else {
this.oscillator.frequency.linearRampToValueAtTime(
val,
Expand All @@ -278,9 +340,57 @@ p5.Oscillator.prototype.freq = function (val, rampTime = 0, tFromNow = 0) {
if (this.phaseAmount) {
this.phase(this.phaseAmount);
}
} else if (val) {
if (val.output) {
val = val.output;
};
/**
* Returns the value of frequency of oscillator
*
* @method getFreq
* @for p5.Oscillator
* @returns {number} Frequency of oscillator in Hertz
*/

p5.Oscillator.prototype.getFreq = function () {
return this.oscillator.frequency.value;
};

/**
* Set type to 'sine', 'triangle', 'sawtooth' or 'square'.
*
* @method setType
* @for p5.Oscillator
* @param {String} type 'sine', 'triangle', 'sawtooth' or 'square'.
*/
p5.Oscillator.prototype.setType = function (type) {
this.oscillator.type = type;
};
/**
* Returns current type of oscillator eg. 'sine', 'triangle', 'sawtooth' or 'square'.
*
* @method getType
* @for p5.Oscillator
* @returns {String} type of oscillator eg . 'sine', 'triangle', 'sawtooth' or 'square'.
*/

p5.Oscillator.prototype.getType = function () {
return this.oscillator.type;
};

/**
* Connect to a p5.sound / Web Audio object.
*
* @method connect
* @for p5.Oscillator
* @param {Object} unit A p5.sound or Web Audio object
*/
p5.Oscillator.prototype.connect = function (unit) {
if (!unit) {
this.panner.connect(p5sound.input);
} else if (unit.hasOwnProperty('input')) {
this.panner.connect(unit.input);
this.connection = unit.input;
} else {
this.panner.connect(unit);
this.connection = unit;
}
val.connect(this.oscillator.frequency);

Expand Down Expand Up @@ -344,7 +454,57 @@ p5.Oscillator.prototype.disconnect = function () {
if (this.panner) {
this.panner.disconnect();
if (this.output) {
this.output.connect(this.panner);
this.output.disconnect();
}
if (this.panner) {
this.panner.disconnect();
if (this.output) {
this.output.connect(this.panner);
}
}
this.oscMods = [];
};

/**
* Pan between Left (-1) and Right (1)
*
* @method pan
* @for p5.Oscillator
* @param {Number} panning Number between -1 and 1
* @param {Number} timeFromNow schedule this event to happen
* seconds from now
*/
p5.Oscillator.prototype.pan = function (pval, tFromNow) {
this.panPosition = pval;
this.panner.pan(pval, tFromNow);
};

/**
* Returns the current value of panPosition , between Left (-1) and Right (1)
*
* @method getPan
* @for p5.Oscillator
*
* @returns {number} panPosition of oscillator , between Left (-1) and Right (1)
*/

p5.Oscillator.prototype.getPan = function() {

return this.panPosition;
};

// get rid of the oscillator
p5.Oscillator.prototype.dispose = function () {
// remove reference from soundArray
var index = p5sound.soundArray.indexOf(this);
p5sound.soundArray.splice(index, 1);

if (this.oscillator) {
var now = p5sound.audiocontext.currentTime;
this.stop(now);
this.disconnect();
this.panner = null;
this.oscillator = null;
}
}
this.oscMods = [];
Expand Down
Loading

0 comments on commit bfb8c3b

Please sign in to comment.