-
-
Notifications
You must be signed in to change notification settings - Fork 682
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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); | ||
|
||
this._split = this.ac.createChannelSplitter(2); | ||
this._merge = this.ac.createChannelMerger(2); | ||
|
@@ -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); | ||
|
@@ -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. | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll make this change |
||
|
@@ -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; | ||
|
@@ -328,4 +284,4 @@ define(function (require) { | |
this.rightDelay = undefined; | ||
} | ||
|
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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') { | ||
|
@@ -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 | ||
|
@@ -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); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
// } | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ define(function (require) { | |
|
||
var p5sound = require('master'); | ||
require('sndcore'); | ||
|
||
|
||
|
||
/** | ||
|
There was a problem hiding this comment.
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 useEffect
, since that's what we required on line 6. Same goes forFilter
, if you feel like cleaning up the lines that refer top5.Filter
(if you don't get to it in this PR, I will be happy to)There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...