From 5b6f6a93ccb3baa340710aba7f5b35e3f965ee84 Mon Sep 17 00:00:00 2001 From: Pragya Date: Fri, 14 Aug 2020 10:36:24 -0400 Subject: [PATCH 1/2] replacement of es5 function to es6 classes featuring p5.delay --- src/app.js | 5 ++++- src/delay.js | 52 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/app.js b/src/app.js index daf13be6..4d746f16 100644 --- a/src/app.js +++ b/src/app.js @@ -31,7 +31,10 @@ import './filter'; import './eq'; import './panner3d'; import './listener3d'; -import './delay'; + +import Delay from './delay'; +p5.Delay = Delay ; + import './reverb'; import './metro'; import './looper'; diff --git a/src/delay.js b/src/delay.js index 95608e06..16dd0ce6 100644 --- a/src/delay.js +++ b/src/delay.js @@ -49,16 +49,16 @@ import Effect from './effect'; * } * */ -p5.Delay = function () { - Effect.call(this); +class Delay extends Effect { + constructor(){ + super(); - this._split = this.ac.createChannelSplitter(2); - this._merge = this.ac.createChannelMerger(2); - - this._leftGain = this.ac.createGain(); - this._rightGain = this.ac.createGain(); - - /** + this._split = this.ac.createChannelSplitter(2); + this._merge = this.ac.createChannelMerger(2); + + this._leftGain = this.ac.createGain(); + this._rightGain = this.ac.createGain(); + /** * The p5.Delay is built with two * * Web Audio Delay Nodes, one for each stereo channel. @@ -104,10 +104,8 @@ p5.Delay = function () { // set initial feedback to 0.5 this.feedback(0.5); -}; - -p5.Delay.prototype = Object.create(Effect.prototype); -/** + } + /** * Add delay to an audio signal according to a set * of delay parameters. * @@ -124,7 +122,7 @@ p5.Delay.prototype = Object.create(Effect.prototype); * below the lowPass will be part of the * delay. */ -p5.Delay.prototype.process = function (src, _delayTime, _feedback, _filter) { +process (src, _delayTime, _feedback, _filter) { var feedback = _feedback || 0; var delayTime = _delayTime || 0; if (feedback >= 1.0) { @@ -148,6 +146,7 @@ p5.Delay.prototype.process = function (src, _delayTime, _feedback, _filter) { } }; + /** * Set the delay (echo) time, in seconds. Usually this value will be * a floating point number between 0.0 and 1.0. @@ -156,7 +155,7 @@ p5.Delay.prototype.process = function (src, _delayTime, _feedback, _filter) { * @for p5.Delay * @param {Number} delayTime Time (in seconds) of the delay */ -p5.Delay.prototype.delayTime = function (t) { +delayTime (t) { // if t is an audio node... if (typeof t !== 'number') { t.connect(this.leftDelay.delayTime); @@ -184,7 +183,7 @@ p5.Delay.prototype.delayTime = function (t) { * @returns {Number} Feedback value * */ -p5.Delay.prototype.feedback = function (f) { +feedback(f) { // if f is an audio node... if (f && typeof f !== 'number') { f.connect(this._leftGain.gain); @@ -214,7 +213,7 @@ p5.Delay.prototype.feedback = function (f) { * High numbers (i.e. 15) will produce a resonance, * low numbers (i.e. .2) will produce a slope. */ -p5.Delay.prototype.filter = function (freq, q) { +filter(freq, q) { this._leftFilter.set(freq, q); this._rightFilter.set(freq, q); }; @@ -228,7 +227,7 @@ p5.Delay.prototype.filter = function (freq, q) { * @for p5.Delay * @param {String|Number} type 'pingPong' (1) or 'default' (0) */ -p5.Delay.prototype.setType = function (t) { +setType (t) { if (t === 1) { t = 'pingPong'; } @@ -278,8 +277,8 @@ p5.Delay.prototype.setType = function (t) { * @for p5.Delay */ -p5.Delay.prototype.dispose = function () { - Effect.prototype.dispose.apply(this); +dispose () { + super.dispose(); this._split.disconnect(); this._leftFilter.dispose(); @@ -299,4 +298,15 @@ p5.Delay.prototype.dispose = function () { this.leftDelay = undefined; this.rightDelay = undefined; }; -export default p5.Delay; + +}; + + + + + + + + + +export default Delay; From a18c3990f87ad719e46db2d31f78c364b4e43cba Mon Sep 17 00:00:00 2001 From: endurance21 Date: Sat, 15 Aug 2020 15:45:09 +0530 Subject: [PATCH 2/2] fixed linting --- src/app.js | 4 +- src/delay.js | 455 +++++++++++++++++++++++++-------------------------- 2 files changed, 227 insertions(+), 232 deletions(-) diff --git a/src/app.js b/src/app.js index 4d746f16..5d4a009c 100644 --- a/src/app.js +++ b/src/app.js @@ -32,8 +32,8 @@ import './eq'; import './panner3d'; import './listener3d'; -import Delay from './delay'; -p5.Delay = Delay ; +import Delay from './delay'; +p5.Delay = Delay; import './reverb'; import './metro'; diff --git a/src/delay.js b/src/delay.js index 16dd0ce6..2ebd5c03 100644 --- a/src/delay.js +++ b/src/delay.js @@ -50,263 +50,258 @@ import Effect from './effect'; * */ class Delay extends Effect { - constructor(){ + constructor() { super(); this._split = this.ac.createChannelSplitter(2); this._merge = this.ac.createChannelMerger(2); - + this._leftGain = this.ac.createGain(); this._rightGain = this.ac.createGain(); - /** - * The p5.Delay is built with two - * - * Web Audio Delay Nodes, one for each stereo channel. - * - * @for p5.Delay - * @property {DelayNode} leftDelay - */ - this.leftDelay = this.ac.createDelay(); - /** - * The p5.Delay is built with two - * - * Web Audio Delay Nodes, one for each stereo channel. - * @for p5.Delay - * @property {DelayNode} rightDelay - */ - this.rightDelay = this.ac.createDelay(); + /** + * The p5.Delay is built with two + * + * Web Audio Delay Nodes, one for each stereo channel. + * + * @for p5.Delay + * @property {DelayNode} leftDelay + */ + this.leftDelay = this.ac.createDelay(); + /** + * The p5.Delay is built with two + * + * Web Audio Delay Nodes, one for each stereo channel. + * @for p5.Delay + * @property {DelayNode} rightDelay + */ + this.rightDelay = this.ac.createDelay(); - this._leftFilter = new Filter(); - this._rightFilter = new Filter(); - this._leftFilter.disconnect(); - this._rightFilter.disconnect(); + this._leftFilter = new Filter(); + this._rightFilter = new Filter(); + this._leftFilter.disconnect(); + this._rightFilter.disconnect(); - this._leftFilter.biquad.frequency.setValueAtTime(1200, this.ac.currentTime); - this._rightFilter.biquad.frequency.setValueAtTime(1200, this.ac.currentTime); - this._leftFilter.biquad.Q.setValueAtTime(0.3, this.ac.currentTime); - this._rightFilter.biquad.Q.setValueAtTime(0.3, this.ac.currentTime); + this._leftFilter.biquad.frequency.setValueAtTime(1200, this.ac.currentTime); + this._rightFilter.biquad.frequency.setValueAtTime( + 1200, + this.ac.currentTime + ); + this._leftFilter.biquad.Q.setValueAtTime(0.3, this.ac.currentTime); + this._rightFilter.biquad.Q.setValueAtTime(0.3, this.ac.currentTime); - // graph routing - this.input.connect(this._split); - this.leftDelay.connect(this._leftGain); - this.rightDelay.connect(this._rightGain); - this._leftGain.connect(this._leftFilter.input); - this._rightGain.connect(this._rightFilter.input); - this._merge.connect(this.wet); + // graph routing + this.input.connect(this._split); + this.leftDelay.connect(this._leftGain); + this.rightDelay.connect(this._rightGain); + this._leftGain.connect(this._leftFilter.input); + this._rightGain.connect(this._rightFilter.input); + this._merge.connect(this.wet); - this._leftFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime); - this._rightFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime); + this._leftFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime); + this._rightFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime); - // default routing - this.setType(0); + // default routing + this.setType(0); - this._maxDelay = this.leftDelay.delayTime.maxValue; + this._maxDelay = this.leftDelay.delayTime.maxValue; - // set initial feedback to 0.5 - this.feedback(0.5); - } - /** - * Add delay to an audio signal according to a set - * of delay parameters. - * - * @method process - * @for p5.Delay - * @param {Object} Signal An object that outputs audio - * @param {Number} [delayTime] Time (in seconds) of the delay/echo. - * Some browsers limit delayTime to - * 1 second. - * @param {Number} [feedback] sends the delay back through itself - * in a loop that decreases in volume - * each time. - * @param {Number} [lowPass] Cutoff frequency. Only frequencies - * below the lowPass will be part of the - * delay. - */ -process (src, _delayTime, _feedback, _filter) { - var feedback = _feedback || 0; - var delayTime = _delayTime || 0; - if (feedback >= 1.0) { - throw new Error('Feedback value will force a positive feedback loop.'); - } - if (delayTime >= this._maxDelay) { - throw new Error( - 'Delay Time exceeds maximum delay time of ' + this._maxDelay + ' second.' - ); + // set initial feedback to 0.5 + this.feedback(0.5); } + /** + * Add delay to an audio signal according to a set + * of delay parameters. + * + * @method process + * @for p5.Delay + * @param {Object} Signal An object that outputs audio + * @param {Number} [delayTime] Time (in seconds) of the delay/echo. + * Some browsers limit delayTime to + * 1 second. + * @param {Number} [feedback] sends the delay back through itself + * in a loop that decreases in volume + * each time. + * @param {Number} [lowPass] Cutoff frequency. Only frequencies + * below the lowPass will be part of the + * delay. + */ + process(src, _delayTime, _feedback, _filter) { + var feedback = _feedback || 0; + var delayTime = _delayTime || 0; + if (feedback >= 1.0) { + throw new Error('Feedback value will force a positive feedback loop.'); + } + if (delayTime >= this._maxDelay) { + throw new Error( + 'Delay Time exceeds maximum delay time of ' + + this._maxDelay + + ' second.' + ); + } - src.connect(this.input); - this.leftDelay.delayTime.setValueAtTime(delayTime, this.ac.currentTime); - this.rightDelay.delayTime.setValueAtTime(delayTime, this.ac.currentTime); - this._leftGain.gain.value = feedback; - this._rightGain.gain.value = feedback; + src.connect(this.input); + this.leftDelay.delayTime.setValueAtTime(delayTime, this.ac.currentTime); + this.rightDelay.delayTime.setValueAtTime(delayTime, this.ac.currentTime); + this._leftGain.gain.value = feedback; + this._rightGain.gain.value = feedback; - if (_filter) { - this._leftFilter.freq(_filter); - this._rightFilter.freq(_filter); + if (_filter) { + this._leftFilter.freq(_filter); + this._rightFilter.freq(_filter); + } } -}; - -/** - * Set the delay (echo) time, in seconds. Usually this value will be - * a floating point number between 0.0 and 1.0. - * - * @method delayTime - * @for p5.Delay - * @param {Number} delayTime Time (in seconds) of the delay - */ -delayTime (t) { - // if t is an audio node... - if (typeof t !== 'number') { - t.connect(this.leftDelay.delayTime); - t.connect(this.rightDelay.delayTime); - } else { - this.leftDelay.delayTime.cancelScheduledValues(this.ac.currentTime); - this.rightDelay.delayTime.cancelScheduledValues(this.ac.currentTime); - this.leftDelay.delayTime.linearRampToValueAtTime(t, this.ac.currentTime); - this.rightDelay.delayTime.linearRampToValueAtTime(t, this.ac.currentTime); - } -}; - -/** - * Feedback occurs when Delay sends its signal back through its input - * in a loop. The feedback amount determines how much signal to send each - * time through the loop. A feedback greater than 1.0 is not desirable because - * it will increase the overall output each time through the loop, - * creating an infinite feedback loop. The default value is 0.5 - * - * @method feedback - * @for p5.Delay - * @param {Number|Object} feedback 0.0 to 1.0, or an object such as an - * Oscillator that can be used to - * modulate this param - * @returns {Number} Feedback value - * - */ -feedback(f) { - // if f is an audio node... - if (f && typeof f !== 'number') { - f.connect(this._leftGain.gain); - f.connect(this._rightGain.gain); - } else if (f >= 1.0) { - throw new Error('Feedback value will force a positive feedback loop.'); - } else if (typeof f === 'number') { - this._leftGain.gain.value = f; - this._rightGain.gain.value = f; + /** + * Set the delay (echo) time, in seconds. Usually this value will be + * a floating point number between 0.0 and 1.0. + * + * @method delayTime + * @for p5.Delay + * @param {Number} delayTime Time (in seconds) of the delay + */ + delayTime(t) { + // if t is an audio node... + if (typeof t !== 'number') { + t.connect(this.leftDelay.delayTime); + t.connect(this.rightDelay.delayTime); + } else { + this.leftDelay.delayTime.cancelScheduledValues(this.ac.currentTime); + this.rightDelay.delayTime.cancelScheduledValues(this.ac.currentTime); + this.leftDelay.delayTime.linearRampToValueAtTime(t, this.ac.currentTime); + this.rightDelay.delayTime.linearRampToValueAtTime(t, this.ac.currentTime); + } } - // return value of feedback - return this._leftGain.gain.value; -}; - -/** - * Set a lowpass filter frequency for the delay. A lowpass filter - * will cut off any frequencies higher than the filter frequency. - * - * @method filter - * @for p5.Delay - * @param {Number|Object} cutoffFreq A lowpass filter will cut off any - * frequencies higher than the filter frequency. - * @param {Number|Object} res Resonance of the filter frequency - * cutoff, or an object (i.e. a p5.Oscillator) - * that can be used to modulate this parameter. - * High numbers (i.e. 15) will produce a resonance, - * low numbers (i.e. .2) will produce a slope. - */ -filter(freq, q) { - this._leftFilter.set(freq, q); - this._rightFilter.set(freq, q); -}; + /** + * Feedback occurs when Delay sends its signal back through its input + * in a loop. The feedback amount determines how much signal to send each + * time through the loop. A feedback greater than 1.0 is not desirable because + * it will increase the overall output each time through the loop, + * creating an infinite feedback loop. The default value is 0.5 + * + * @method feedback + * @for p5.Delay + * @param {Number|Object} feedback 0.0 to 1.0, or an object such as an + * Oscillator that can be used to + * modulate this param + * @returns {Number} Feedback value + * + */ + feedback(f) { + // if f is an audio node... + if (f && typeof f !== 'number') { + f.connect(this._leftGain.gain); + f.connect(this._rightGain.gain); + } else if (f >= 1.0) { + throw new Error('Feedback value will force a positive feedback loop.'); + } else if (typeof f === 'number') { + this._leftGain.gain.value = f; + this._rightGain.gain.value = f; + } -/** - * Choose a preset type of delay. 'pingPong' bounces the signal - * from the left to the right channel to produce a stereo effect. - * Any other parameter will revert to the default delay setting. - * - * @method setType - * @for p5.Delay - * @param {String|Number} type 'pingPong' (1) or 'default' (0) - */ -setType (t) { - if (t === 1) { - t = 'pingPong'; - } - this._split.disconnect(); - this._leftFilter.disconnect(); - this._rightFilter.disconnect(); - this._split.connect(this.leftDelay, 0); - this._split.connect(this.rightDelay, 1); - switch (t) { - case 'pingPong': - this._rightFilter.setType(this._leftFilter.biquad.type); - this._leftFilter.output.connect(this._merge, 0, 0); - this._rightFilter.output.connect(this._merge, 0, 1); - this._leftFilter.output.connect(this.rightDelay); - this._rightFilter.output.connect(this.leftDelay); - break; - default: - this._leftFilter.output.connect(this._merge, 0, 0); - this._rightFilter.output.connect(this._merge, 0, 1); - this._leftFilter.output.connect(this.leftDelay); - this._rightFilter.output.connect(this.rightDelay); + // return value of feedback + return this._leftGain.gain.value; } -}; - -// DocBlocks for methods inherited from p5.Effect -/** - * Set the output level of the delay effect. - * - * @method amp - * @for p5.Delay - * @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 - */ -/** - * Send output to a p5.sound or web audio object - * - * @method connect - * @for p5.Delay - * @param {Object} unit - */ -/** - * Disconnect all output. - * - * @method disconnect - * @for p5.Delay - */ - -dispose () { - super.dispose(); - - this._split.disconnect(); - this._leftFilter.dispose(); - this._rightFilter.dispose(); - this._merge.disconnect(); - this._leftGain.disconnect(); - this._rightGain.disconnect(); - this.leftDelay.disconnect(); - this.rightDelay.disconnect(); - - this._split = undefined; - this._leftFilter = undefined; - this._rightFilter = undefined; - this._merge = undefined; - this._leftGain = undefined; - this._rightGain = undefined; - this.leftDelay = undefined; - this.rightDelay = undefined; -}; - -}; - - + /** + * Set a lowpass filter frequency for the delay. A lowpass filter + * will cut off any frequencies higher than the filter frequency. + * + * @method filter + * @for p5.Delay + * @param {Number|Object} cutoffFreq A lowpass filter will cut off any + * frequencies higher than the filter frequency. + * @param {Number|Object} res Resonance of the filter frequency + * cutoff, or an object (i.e. a p5.Oscillator) + * that can be used to modulate this parameter. + * High numbers (i.e. 15) will produce a resonance, + * low numbers (i.e. .2) will produce a slope. + */ + filter(freq, q) { + this._leftFilter.set(freq, q); + this._rightFilter.set(freq, q); + } + /** + * Choose a preset type of delay. 'pingPong' bounces the signal + * from the left to the right channel to produce a stereo effect. + * Any other parameter will revert to the default delay setting. + * + * @method setType + * @for p5.Delay + * @param {String|Number} type 'pingPong' (1) or 'default' (0) + */ + setType(t) { + if (t === 1) { + t = 'pingPong'; + } + this._split.disconnect(); + this._leftFilter.disconnect(); + this._rightFilter.disconnect(); + this._split.connect(this.leftDelay, 0); + this._split.connect(this.rightDelay, 1); + switch (t) { + case 'pingPong': + this._rightFilter.setType(this._leftFilter.biquad.type); + this._leftFilter.output.connect(this._merge, 0, 0); + this._rightFilter.output.connect(this._merge, 0, 1); + this._leftFilter.output.connect(this.rightDelay); + this._rightFilter.output.connect(this.leftDelay); + break; + default: + this._leftFilter.output.connect(this._merge, 0, 0); + this._rightFilter.output.connect(this._merge, 0, 1); + this._leftFilter.output.connect(this.leftDelay); + this._rightFilter.output.connect(this.rightDelay); + } + } + // DocBlocks for methods inherited from p5.Effect + /** + * Set the output level of the delay effect. + * + * @method amp + * @for p5.Delay + * @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 + */ + /** + * Send output to a p5.sound or web audio object + * + * @method connect + * @for p5.Delay + * @param {Object} unit + */ + /** + * Disconnect all output. + * + * @method disconnect + * @for p5.Delay + */ + dispose() { + super.dispose(); + this._split.disconnect(); + this._leftFilter.dispose(); + this._rightFilter.dispose(); + this._merge.disconnect(); + this._leftGain.disconnect(); + this._rightGain.disconnect(); + this.leftDelay.disconnect(); + this.rightDelay.disconnect(); + this._split = undefined; + this._leftFilter = undefined; + this._rightFilter = undefined; + this._merge = undefined; + this._leftGain = undefined; + this._rightGain = undefined; + this.leftDelay = undefined; + this.rightDelay = undefined; + } +} export default Delay;