Skip to content

Commit 90918a3

Browse files
jvntftherewasaguy
authored andcommitted
Delay, Reverb and Distortion inherit from Effect (#178)
* reformatted delay and reverb to use effect * refactored distortion * small changes * added to tests
1 parent 326649c commit 90918a3

File tree

9 files changed

+190
-307
lines changed

9 files changed

+190
-307
lines changed

lib/p5.sound.js

Lines changed: 66 additions & 157 deletions
Large diffs are not rendered by default.

lib/p5.sound.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/delay.js

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ define(function (require) {
33

44
var p5sound = require('master');
55
var Filter = require('filter');
6+
var Effect = require('effect');
7+
68
/**
79
* Delay is an echo effect. It processes an existing sound source,
810
* and outputs a delayed version of that sound. The p5.Delay can
@@ -53,10 +55,7 @@ define(function (require) {
5355
* </code></div>
5456
*/
5557
p5.Delay = function() {
56-
this.ac = p5sound.audiocontext;
57-
58-
this.input = this.ac.createGain();
59-
this.output = this.ac.createGain();
58+
p5.Effect.call(this);
6059

6160
this._split = this.ac.createChannelSplitter(2);
6261
this._merge = this.ac.createChannelMerger(2);
@@ -99,8 +98,8 @@ define(function (require) {
9998
this.rightDelay.connect(this._rightGain);
10099
this._leftGain.connect(this._leftFilter.input);
101100
this._rightGain.connect(this._rightFilter.input);
102-
this._merge.connect(this.output);
103-
this.output.connect(p5.soundOut.input);
101+
this._merge.connect(this.wet);
102+
104103

105104
this._leftFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime);
106105
this._rightFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime);
@@ -113,10 +112,10 @@ define(function (require) {
113112
// set initial feedback to 0.5
114113
this.feedback(0.5);
115114

116-
// add this p5.SoundFile to the soundArray
117-
p5sound.soundArray.push(this);
115+
118116
};
119117

118+
p5.Delay.prototype = Object.create(Effect.prototype);
120119
/**
121120
* Add delay to an audio signal according to a set
122121
* of delay parameters.
@@ -261,52 +260,11 @@ define(function (require) {
261260
}
262261
};
263262

264-
/**
265-
* Set the output level of the delay effect.
266-
*
267-
* @method amp
268-
* @param {Number} volume amplitude between 0 and 1.0
269-
* @param {Number} [rampTime] create a fade that lasts rampTime
270-
* @param {Number} [timeFromNow] schedule this event to happen
271-
* seconds from now
272-
*/
273-
p5.Delay.prototype.amp = function(vol, rampTime, tFromNow){
274-
var rampTime = rampTime || 0;
275-
var tFromNow = tFromNow || 0;
276-
var now = p5sound.audiocontext.currentTime;
277-
var currentVol = this.output.gain.value;
278-
this.output.gain.cancelScheduledValues(now);
279-
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + .001);
280-
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + .001);
281-
};
282-
283-
/**
284-
* Send output to a p5.sound or web audio object
285-
*
286-
* @method connect
287-
* @param {Object} unit
288-
*/
289-
p5.Delay.prototype.connect = function(unit) {
290-
var u = unit || p5.soundOut.input;
291-
this.output.connect(u);
292-
};
293-
294-
/**
295-
* Disconnect all output.
296-
*
297-
* @method disconnect
298-
*/
299-
p5.Delay.prototype.disconnect = function() {
300-
this.output.disconnect();
301-
};
302263

303264
p5.Delay.prototype.dispose = function() {
304-
// remove reference from soundArray
305-
var index = p5sound.soundArray.indexOf(this);
306-
p5sound.soundArray.splice(index, 1);
307265

308-
this.input.disconnect();
309-
this.output.disconnect();
266+
Effect.prototype.dispose.apply(this);
267+
310268
this._split.disconnect();
311269
this._leftFilter.disconnect();
312270
this._rightFilter.disconnect();
@@ -316,8 +274,6 @@ define(function (require) {
316274
this.leftDelay.disconnect();
317275
this.rightDelay.disconnect();
318276

319-
this.input = undefined;
320-
this.output = undefined;
321277
this._split = undefined;
322278
this._leftFilter = undefined;
323279
this._rightFilter = undefined;
@@ -328,4 +284,4 @@ define(function (require) {
328284
this.rightDelay = undefined;
329285
}
330286

331-
});
287+
});

src/distortion.js

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ define(function (require) {
22
'use strict';
33

44
var p5sound = require('master');
5+
var Effect = require('effect');
56

67
/*
78
* 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) {
3435
* @return {Object} Distortion object
3536
*/
3637
p5.Distortion = function(amount, oversample) {
38+
Effect.call(this);
39+
3740
if (typeof amount === 'undefined') {
3841
amount = 0.25;
3942
} if (typeof amount !== 'number') {
@@ -45,10 +48,11 @@ define(function (require) {
4548
}
4649

4750
var curveAmount = p5.prototype.map(amount, 0.0, 1.0, 0, 2000);
48-
this.ac = p5sound.audiocontext;
51+
52+
// this.ac = p5sound.audiocontext;
4953

50-
this.input = this.ac.createGain();
51-
this.output = this.ac.createGain();
54+
// this.input = this.ac.createGain();
55+
// this.output = this.ac.createGain();
5256

5357
/**
5458
* The p5.Distortion is built with a
@@ -65,14 +69,17 @@ define(function (require) {
6569
this.waveShaperNode.oversample = oversample;
6670

6771
this.input.connect(this.waveShaperNode);
68-
this.waveShaperNode.connect(this.output);
6972

70-
this.connect();
73+
this.waveShaperNode.connect(this.wet);
74+
75+
// this.connect();
7176

72-
// add to the soundArray
73-
p5sound.soundArray.push(this);
77+
// // add to the soundArray
78+
// p5sound.soundArray.push(this);
7479
}
7580

81+
p5.Distortion.prototype = Object.create(Effect.prototype);
82+
7683
p5.Distortion.prototype.process = function(src, amount, oversample) {
7784
src.connect(this.input);
7885
this.set(amount, oversample);
@@ -118,38 +125,42 @@ define(function (require) {
118125
return this.waveShaperNode.oversample;
119126
}
120127

121-
/**
122-
* Send output to a p5.sound or web audio object
123-
*
124-
* @method connect
125-
* @param {Object} unit
126-
*/
127-
p5.Distortion.prototype.connect = function(unit) {
128-
var u = unit || p5.soundOut.input;
129-
this.output.connect(u);
130-
};
131-
132-
/**
133-
* Disconnect all output.
134-
*
135-
* @method disconnect
136-
*/
137-
p5.Distortion.prototype.disconnect = function() {
138-
this.output.disconnect();
139-
};
128+
// /**
129+
// * Send output to a p5.sound or web audio object
130+
// *
131+
// * @method connect
132+
// * @param {Object} unit
133+
// */
134+
// p5.Distortion.prototype.connect = function(unit) {
135+
// var u = unit || p5.soundOut.input;
136+
// this.output.connect(u);
137+
// };
138+
139+
// /**
140+
// * Disconnect all output.
141+
// *
142+
// * @method disconnect
143+
// */
144+
// p5.Distortion.prototype.disconnect = function() {
145+
// this.output.disconnect();
146+
// };
140147

141148
p5.Distortion.prototype.dispose = function() {
142-
var index = p5sound.soundArray.indexOf(this);
143-
p5sound.soundArray.splice(index, 1);
144149

145-
this.input.disconnect();
150+
Effect.prototype.dispose.apply(this);
151+
152+
// var index = p5sound.soundArray.indexOf(this);
153+
// p5sound.soundArray.splice(index, 1);
154+
155+
// this.input.disconnect();
156+
146157
this.waveShaperNode.disconnect();
147-
this.input = null;
158+
// this.input = null;
148159
this.waveShaperNode = null;
149160

150-
if (typeof this.output !== 'undefined') {
151-
this.output.disconnect();
152-
this.output = null;
153-
}
161+
// if (typeof this.output !== 'undefined') {
162+
// this.output.disconnect();
163+
// this.output = null;
164+
// }
154165
}
155166
});

src/gain.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ define(function (require) {
33

44
var p5sound = require('master');
55
require('sndcore');
6+
67

78

89
/**

0 commit comments

Comments
 (0)