Skip to content

Commit

Permalink
Delay, Reverb and Distortion inherit from Effect (#178)
Browse files Browse the repository at this point in the history
* reformatted delay and reverb to use effect

* refactored distortion

* small changes

* added to tests
  • Loading branch information
jvntf authored and therewasaguy committed Jun 20, 2017
1 parent 326649c commit 90918a3
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 307 deletions.
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);

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();
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) {
// 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

0 comments on commit 90918a3

Please sign in to comment.