Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Effects w inheritance #178

Merged
merged 4 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 66 additions & 157 deletions lib/p5.sound.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions lib/p5.sound.min.js

Large diffs are not rendered by default.

64 changes: 10 additions & 54 deletions src/delay.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ define(function (require) {

var p5sound = require('master');
var Filter = require('filter');
var Effect = require('effect');

/**
* Delay is an echo effect. It processes an existing sound source,
* and outputs a delayed version of that sound. The p5.Delay can
Expand Down Expand Up @@ -53,10 +55,7 @@ define(function (require) {
* </code></div>
*/
p5.Delay = function() {
this.ac = p5sound.audiocontext;

this.input = this.ac.createGain();
this.output = this.ac.createGain();
p5.Effect.call(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5.Effect works because of the order of app.js, but it's safer not to depend on that and to instead require dependencies explicitly. Try to use Effect, since that's what we required on line 6. Same goes for Filter, if you feel like cleaning up the lines that refer to p5.Filter (if you don't get to it in this PR, I will be happy to)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I will merge and make this change in a separate PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, p5.Filter module needed to return/export the p5.Filter...


this._split = this.ac.createChannelSplitter(2);
this._merge = this.ac.createChannelMerger(2);
Expand Down Expand Up @@ -99,8 +98,8 @@ define(function (require) {
this.rightDelay.connect(this._rightGain);
this._leftGain.connect(this._leftFilter.input);
this._rightGain.connect(this._rightFilter.input);
this._merge.connect(this.output);
this.output.connect(p5.soundOut.input);
this._merge.connect(this.wet);


this._leftFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime);
this._rightFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime);
Expand All @@ -113,10 +112,10 @@ define(function (require) {
// set initial feedback to 0.5
this.feedback(0.5);

// add this p5.SoundFile to the soundArray
p5sound.soundArray.push(this);

};

p5.Delay.prototype = Object.create(Effect.prototype);
/**
* Add delay to an audio signal according to a set
* of delay parameters.
Expand Down Expand Up @@ -261,52 +260,11 @@ define(function (require) {
}
};

/**
* Set the output level of the delay effect.
*
* @method amp
* @param {Number} volume amplitude between 0 and 1.0
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
p5.Delay.prototype.amp = function(vol, rampTime, tFromNow){
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + .001);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + .001);
};

/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
p5.Delay.prototype.connect = function(unit) {
var u = unit || p5.soundOut.input;
this.output.connect(u);
};

/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Delay.prototype.disconnect = function() {
this.output.disconnect();
};

p5.Delay.prototype.dispose = function() {
// remove reference from soundArray
var index = p5sound.soundArray.indexOf(this);
p5sound.soundArray.splice(index, 1);

this.input.disconnect();
this.output.disconnect();
Effect.prototype.dispose.apply(this);

this._split.disconnect();
this._leftFilter.disconnect();
this._rightFilter.disconnect();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are p5.Filters, so we should call dispose instead of disconnect to dispose of all its resources/references

this._leftFilter.dispose();
this._rightFilter.dispose();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make this change

Expand All @@ -316,8 +274,6 @@ define(function (require) {
this.leftDelay.disconnect();
this.rightDelay.disconnect();

this.input = undefined;
this.output = undefined;
this._split = undefined;
this._leftFilter = undefined;
this._rightFilter = undefined;
Expand All @@ -328,4 +284,4 @@ define(function (require) {
this.rightDelay = undefined;
}

});
});
79 changes: 45 additions & 34 deletions src/distortion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ define(function (require) {
'use strict';

var p5sound = require('master');
var Effect = require('effect');

/*
* Adapted from [Kevin Ennis on StackOverflow](http://stackoverflow.com/questions/22312841/waveshaper-node-in-webaudio-how-to-emulate-distortion)
Expand Down Expand Up @@ -34,6 +35,8 @@ define(function (require) {
* @return {Object} Distortion object
*/
p5.Distortion = function(amount, oversample) {
Effect.call(this);

if (typeof amount === 'undefined') {
amount = 0.25;
} if (typeof amount !== 'number') {
Expand All @@ -45,10 +48,11 @@ define(function (require) {
}

var curveAmount = p5.prototype.map(amount, 0.0, 1.0, 0, 2000);
this.ac = p5sound.audiocontext;

// this.ac = p5sound.audiocontext;

this.input = this.ac.createGain();
this.output = this.ac.createGain();
// this.input = this.ac.createGain();
// this.output = this.ac.createGain();

/**
* The p5.Distortion is built with a
Expand All @@ -65,14 +69,17 @@ define(function (require) {
this.waveShaperNode.oversample = oversample;

this.input.connect(this.waveShaperNode);
this.waveShaperNode.connect(this.output);

this.connect();
this.waveShaperNode.connect(this.wet);

// this.connect();

// add to the soundArray
p5sound.soundArray.push(this);
// // add to the soundArray
// p5sound.soundArray.push(this);
}

p5.Distortion.prototype = Object.create(Effect.prototype);

p5.Distortion.prototype.process = function(src, amount, oversample) {
src.connect(this.input);
this.set(amount, oversample);
Expand Down Expand Up @@ -118,38 +125,42 @@ define(function (require) {
return this.waveShaperNode.oversample;
}

/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
p5.Distortion.prototype.connect = function(unit) {
var u = unit || p5.soundOut.input;
this.output.connect(u);
};

/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Distortion.prototype.disconnect = function() {
this.output.disconnect();
};
// /**
// * Send output to a p5.sound or web audio object
// *
// * @method connect
// * @param {Object} unit
// */
// p5.Distortion.prototype.connect = function(unit) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, we should probably keep documentation like this within each class/module, even though the methods are covered in p5.Effect module. These documentation blocks determine what shows up on https://p5js.org/reference/#/p5.Distortion methods section.

Another way to handle it might be to create documentation for p5.Effect, and then each class that inherits from p5.Effect includes a link in the documentation saying "inherits methods from p5.Effect".

// var u = unit || p5.soundOut.input;
// this.output.connect(u);
// };

// /**
// * Disconnect all output.
// *
// * @method disconnect
// */
// p5.Distortion.prototype.disconnect = function() {
// this.output.disconnect();
// };

p5.Distortion.prototype.dispose = function() {
var index = p5sound.soundArray.indexOf(this);
p5sound.soundArray.splice(index, 1);

this.input.disconnect();
Effect.prototype.dispose.apply(this);

// var index = p5sound.soundArray.indexOf(this);
// p5sound.soundArray.splice(index, 1);

// this.input.disconnect();

this.waveShaperNode.disconnect();
this.input = null;
// this.input = null;
this.waveShaperNode = null;

if (typeof this.output !== 'undefined') {
this.output.disconnect();
this.output = null;
}
// if (typeof this.output !== 'undefined') {
// this.output.disconnect();
// this.output = null;
// }
}
});
1 change: 1 addition & 0 deletions src/gain.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define(function (require) {

var p5sound = require('master');
require('sndcore');



/**
Expand Down
Loading