Skip to content

Commit

Permalink
replacement of es5 functions to es6 classes feat p5.Eq (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
endurance21 authored Aug 15, 2020
1 parent b326270 commit 57a352c
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 107 deletions.
5 changes: 4 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import Effect from './effect' ;
p5.Effect = Effect;

import './filter';
import './eq';

import EQ from './eq';
p5.EQ = EQ;

import './panner3d';
import './listener3d';
import './delay';
Expand Down
212 changes: 106 additions & 106 deletions src/eq.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,125 +79,125 @@ import EQFilter from './eqFilter';
* }
* </code></div>
*/
p5.EQ = function (_eqsize) {
Effect.call(this);
class EQ extends Effect {
constructor(_eqsize) {
super();

//p5.EQ can be of size (3) or (8), defaults to 3
_eqsize = _eqsize === 3 || _eqsize === 8 ? _eqsize : 3;
//p5.EQ can be of size (3) or (8), defaults to 3
_eqsize = _eqsize === 3 || _eqsize === 8 ? _eqsize : 3;

var factor;
_eqsize === 3 ? (factor = Math.pow(2, 3)) : (factor = 2);
var factor;
_eqsize === 3 ? (factor = Math.pow(2, 3)) : (factor = 2);

/**
* The p5.EQ is built with abstracted p5.Filter objects.
* To modify any bands, use methods of the <a
* href="/reference/#/p5.Filter" title="p5.Filter reference">
* p5.Filter</a> API, especially `gain` and `freq`.
* Bands are stored in an array, with indices 0 - 3, or 0 - 7
* @property {Array} bands
*
*/
this.bands = [];
/**
* The p5.EQ is built with abstracted p5.Filter objects.
* To modify any bands, use methods of the <a
* href="/reference/#/p5.Filter" title="p5.Filter reference">
* p5.Filter</a> API, especially `gain` and `freq`.
* Bands are stored in an array, with indices 0 - 3, or 0 - 7
* @property {Array} bands
*
*/
this.bands = [];

var freq, res;
for (var i = 0; i < _eqsize; i++) {
if (i === _eqsize - 1) {
freq = 21000;
res = 0.01;
} else if (i === 0) {
freq = 100;
res = 0.1;
} else if (i === 1) {
freq = _eqsize === 3 ? 360 * factor : 360;
res = 1;
} else {
freq = this.bands[i - 1].freq() * factor;
res = 1;
}
this.bands[i] = this._newBand(freq, res);
var freq, res;
for (var i = 0; i < _eqsize; i++) {
if (i === _eqsize - 1) {
freq = 21000;
res = 0.01;
} else if (i === 0) {
freq = 100;
res = 0.1;
} else if (i === 1) {
freq = _eqsize === 3 ? 360 * factor : 360;
res = 1;
} else {
freq = this.bands[i - 1].freq() * factor;
res = 1;
}
this.bands[i] = this._newBand(freq, res);

if (i > 0) {
this.bands[i - 1].connect(this.bands[i].biquad);
} else {
this.input.connect(this.bands[i].biquad);
if (i > 0) {
this.bands[i - 1].connect(this.bands[i].biquad);
} else {
this.input.connect(this.bands[i].biquad);
}
}
this.bands[_eqsize - 1].connect(this.output);
}
this.bands[_eqsize - 1].connect(this.output);
};
p5.EQ.prototype = Object.create(Effect.prototype);

/**
* Process an input by connecting it to the EQ
* @method process
* @param {Object} src Audio source
*/
p5.EQ.prototype.process = function (src) {
src.connect(this.input);
};
/**
* Process an input by connecting it to the EQ
* @method process
* @param {Object} src Audio source
*/
process(src) {
src.connect(this.input);
}

// /**
// * Set the frequency and gain of each band in the EQ. This method should be
// * called with 3 or 8 frequency and gain pairs, depending on the size of the EQ.
// * ex. eq.set(freq0, gain0, freq1, gain1, freq2, gain2);
// *
// * @method set
// * @for p5.EQ
// * @param {Number} [freq0] Frequency value for band with index 0
// * @param {Number} [gain0] Gain value for band with index 0
// * @param {Number} [freq1] Frequency value for band with index 1
// * @param {Number} [gain1] Gain value for band with index 1
// * @param {Number} [freq2] Frequency value for band with index 2
// * @param {Number} [gain2] Gain value for band with index 2
// * @param {Number} [freq3] Frequency value for band with index 3
// * @param {Number} [gain3] Gain value for band with index 3
// * @param {Number} [freq4] Frequency value for band with index 4
// * @param {Number} [gain4] Gain value for band with index 4
// * @param {Number} [freq5] Frequency value for band with index 5
// * @param {Number} [gain5] Gain value for band with index 5
// * @param {Number} [freq6] Frequency value for band with index 6
// * @param {Number} [gain6] Gain value for band with index 6
// * @param {Number} [freq7] Frequency value for band with index 7
// * @param {Number} [gain7] Gain value for band with index 7
// */
p5.EQ.prototype.set = function () {
if (arguments.length === this.bands.length * 2) {
for (var i = 0; i < arguments.length; i += 2) {
this.bands[i / 2].freq(arguments[i]);
this.bands[i / 2].gain(arguments[i + 1]);
// /**
// * Set the frequency and gain of each band in the EQ. This method should be
// * called with 3 or 8 frequency and gain pairs, depending on the size of the EQ.
// * ex. eq.set(freq0, gain0, freq1, gain1, freq2, gain2);
// *
// * @method set
// * @for p5.EQ
// * @param {Number} [freq0] Frequency value for band with index 0
// * @param {Number} [gain0] Gain value for band with index 0
// * @param {Number} [freq1] Frequency value for band with index 1
// * @param {Number} [gain1] Gain value for band with index 1
// * @param {Number} [freq2] Frequency value for band with index 2
// * @param {Number} [gain2] Gain value for band with index 2
// * @param {Number} [freq3] Frequency value for band with index 3
// * @param {Number} [gain3] Gain value for band with index 3
// * @param {Number} [freq4] Frequency value for band with index 4
// * @param {Number} [gain4] Gain value for band with index 4
// * @param {Number} [freq5] Frequency value for band with index 5
// * @param {Number} [gain5] Gain value for band with index 5
// * @param {Number} [freq6] Frequency value for band with index 6
// * @param {Number} [gain6] Gain value for band with index 6
// * @param {Number} [freq7] Frequency value for band with index 7
// * @param {Number} [gain7] Gain value for band with index 7
// */
set() {
if (arguments.length === this.bands.length * 2) {
for (var i = 0; i < arguments.length; i += 2) {
this.bands[i / 2].freq(arguments[i]);
this.bands[i / 2].gain(arguments[i + 1]);
}
} else {
console.error(
'Argument mismatch. .set() should be called with ' +
this.bands.length * 2 +
' arguments. (one frequency and gain value pair for each band of the eq)'
);
}
} else {
console.error(
'Argument mismatch. .set() should be called with ' +
this.bands.length * 2 +
' arguments. (one frequency and gain value pair for each band of the eq)'
);
}
};

/**
* Add a new band. Creates a p5.Filter and strips away everything but
* the raw biquad filter. This method returns an abstracted p5.Filter,
* which can be added to p5.EQ.bands, in order to create new EQ bands.
* @private
* @for p5.EQ
* @method _newBand
* @param {Number} freq
* @param {Number} res
* @return {Object} Abstracted Filter
*/
p5.EQ.prototype._newBand = function (freq, res) {
return new EQFilter(freq, res);
};
/**
* Add a new band. Creates a p5.Filter and strips away everything but
* the raw biquad filter. This method returns an abstracted p5.Filter,
* which can be added to p5.EQ.bands, in order to create new EQ bands.
* @private
* @for p5.EQ
* @method _newBand
* @param {Number} freq
* @param {Number} res
* @return {Object} Abstracted Filter
*/
_newBand(freq, res) {
return new EQFilter(freq, res);
}

p5.EQ.prototype.dispose = function () {
Effect.prototype.dispose.apply(this);
dispose() {
super.dispose();

if (this.bands) {
while (this.bands.length > 0) {
delete this.bands.pop().dispose();
if (this.bands) {
while (this.bands.length > 0) {
delete this.bands.pop().dispose();
}
delete this.bands;
}
delete this.bands;
}
};

export default p5.EQ;
}
export default EQ;

0 comments on commit 57a352c

Please sign in to comment.