From a103b390bc60dba8cfa2c78a4f393758e647c61e Mon Sep 17 00:00:00 2001 From: DIVYANSHU RAJ <43696525+endurance21@users.noreply.github.com> Date: Sat, 15 Aug 2020 11:17:29 +0530 Subject: [PATCH] replacement of es5 functions to es6 class def feat P5.distortion (#516) --- src/app.js | 4 +- src/distortion.js | 181 +++++++++++++++++++++++----------------------- 2 files changed, 93 insertions(+), 92 deletions(-) diff --git a/src/app.js b/src/app.js index 4a18d1ac..f5e8609b 100644 --- a/src/app.js +++ b/src/app.js @@ -43,7 +43,9 @@ import './compressor'; import './soundRecorder'; import './peakDetect'; import './gain'; -import './distortion'; + +import Distortion from './distortion'; +p5.Distortion = Distortion; import AudioVoice from './audioVoice'; p5.AudioVoice = AudioVoice; diff --git a/src/distortion.js b/src/distortion.js index 6eb37b5c..a590d26d 100644 --- a/src/distortion.js +++ b/src/distortion.js @@ -35,107 +35,106 @@ function makeDistortionCurve(amount) { * @param {String} [oversample='none'] 'none', '2x', or '4x'. * */ -p5.Distortion = function (amount, oversample) { - Effect.call(this); +class Distortion extends Effect { + constructor(amount, oversample) { + super(); + if (typeof amount === 'undefined') { + amount = 0.25; + } + if (typeof amount !== 'number') { + throw new Error('amount must be a number'); + } + if (typeof oversample === 'undefined') { + oversample = '2x'; + } + if (typeof oversample !== 'string') { + throw new Error('oversample must be a String'); + } - if (typeof amount === 'undefined') { - amount = 0.25; - } - if (typeof amount !== 'number') { - throw new Error('amount must be a number'); - } - if (typeof oversample === 'undefined') { - oversample = '2x'; - } - if (typeof oversample !== 'string') { - throw new Error('oversample must be a String'); - } - - var curveAmount = p5.prototype.map(amount, 0.0, 1.0, 0, 2000); - - /** - * The p5.Distortion is built with a - * - * Web Audio WaveShaper Node. - * - * @property {AudioNode} WaveShaperNode - */ - this.waveShaperNode = this.ac.createWaveShaper(); - - this.amount = curveAmount; - this.waveShaperNode.curve = makeDistortionCurve(curveAmount); - this.waveShaperNode.oversample = oversample; + var curveAmount = p5.prototype.map(amount, 0.0, 1.0, 0, 2000); - this.input.connect(this.waveShaperNode); + /** + * The p5.Distortion is built with a + * + * Web Audio WaveShaper Node. + * + * @property {AudioNode} WaveShaperNode + */ + this.waveShaperNode = this.ac.createWaveShaper(); - this.waveShaperNode.connect(this.wet); -}; + this.amount = curveAmount; + this.waveShaperNode.curve = makeDistortionCurve(curveAmount); + this.waveShaperNode.oversample = oversample; -p5.Distortion.prototype = Object.create(Effect.prototype); + this.input.connect(this.waveShaperNode); -/** - * Process a sound source, optionally specify amount and oversample values. - * - * @method process - * @for p5.Distortion - * @param {Number} [amount=0.25] Unbounded distortion amount. - * Normal values range from 0-1. - * @param {String} [oversample='none'] 'none', '2x', or '4x'. - */ -p5.Distortion.prototype.process = function (src, amount, oversample) { - src.connect(this.input); - this.set(amount, oversample); -}; + this.waveShaperNode.connect(this.wet); + } -/** - * Set the amount and oversample of the waveshaper distortion. - * - * @method set - * @for p5.Distortion - * @param {Number} [amount=0.25] Unbounded distortion amount. - * Normal values range from 0-1. - * @param {String} [oversample='none'] 'none', '2x', or '4x'. - */ -p5.Distortion.prototype.set = function (amount, oversample) { - if (amount) { - var curveAmount = p5.prototype.map(amount, 0.0, 1.0, 0, 2000); - this.amount = curveAmount; - this.waveShaperNode.curve = makeDistortionCurve(curveAmount); + /** + * Process a sound source, optionally specify amount and oversample values. + * + * @method process + * @for p5.Distortion + * @param {Number} [amount=0.25] Unbounded distortion amount. + * Normal values range from 0-1. + * @param {String} [oversample='none'] 'none', '2x', or '4x'. + */ + process(src, amount, oversample) { + src.connect(this.input); + this.set(amount, oversample); } - if (oversample) { - this.waveShaperNode.oversample = oversample; + + /** + * Set the amount and oversample of the waveshaper distortion. + * + * @method set + * @for p5.Distortion + * @param {Number} [amount=0.25] Unbounded distortion amount. + * Normal values range from 0-1. + * @param {String} [oversample='none'] 'none', '2x', or '4x'. + */ + set(amount, oversample) { + if (amount) { + var curveAmount = p5.prototype.map(amount, 0.0, 1.0, 0, 2000); + this.amount = curveAmount; + this.waveShaperNode.curve = makeDistortionCurve(curveAmount); + } + if (oversample) { + this.waveShaperNode.oversample = oversample; + } } -}; -/** - * Return the distortion amount, typically between 0-1. - * - * @method getAmount - * @for p5.Distortion - * @return {Number} Unbounded distortion amount. - * Normal values range from 0-1. - */ -p5.Distortion.prototype.getAmount = function () { - return this.amount; -}; + /** + * Return the distortion amount, typically between 0-1. + * + * @method getAmount + * @for p5.Distortion + * @return {Number} Unbounded distortion amount. + * Normal values range from 0-1. + */ + getAmount() { + return this.amount; + } -/** - * Return the oversampling. - * - * @method getOversample - * @for p5.Distortion - * @return {String} Oversample can either be 'none', '2x', or '4x'. - */ -p5.Distortion.prototype.getOversample = function () { - return this.waveShaperNode.oversample; -}; + /** + * Return the oversampling. + * + * @method getOversample + * @for p5.Distortion + * @return {String} Oversample can either be 'none', '2x', or '4x'. + */ + getOversample() { + return this.waveShaperNode.oversample; + } -p5.Distortion.prototype.dispose = function () { - Effect.prototype.dispose.apply(this); - if (this.waveShaperNode) { - this.waveShaperNode.disconnect(); - this.waveShaperNode = null; + dispose() { + super.dispose(); + if (this.waveShaperNode) { + this.waveShaperNode.disconnect(); + this.waveShaperNode = null; + } } -}; +} -export default p5.Distortion; +export default Distortion;