|
1 | 1 | 'use strict'; |
2 | 2 | define(function (require) { |
3 | 3 |
|
4 | | - var p5sound = require('master'); |
5 | | - var CrossFade = require('Tone/component/CrossFade'); |
6 | | - |
7 | | - /** |
8 | | - * Effect is a base class for audio effects in p5. <br> |
9 | | - * This module handles the nodes and methods that are |
10 | | - * common and useful for current and future effects. |
11 | | - * |
12 | | - * |
13 | | - * This class is extended by <a href="/reference/#/p5.Distortion">p5.Distortion</a>, |
14 | | - * <a href="/reference/#/p5.Compressor">p5.Compressor</a>, |
15 | | - * <a href="/reference/#/p5.Delay">p5.Delay</a>, |
16 | | - * <a href="/reference/#/p5.Filter">p5.Filter</a>, |
17 | | - * <a href="/reference/#/p5.Reverb">p5.Reverb</a>. |
18 | | - * |
19 | | - * @class p5.Effect |
20 | | - * @constructor |
21 | | - * |
22 | | - * @param {Object} [ac] Reference to the audio context of the p5 object |
23 | | - * @param {AudioNode} [input] Gain Node effect wrapper |
24 | | - * @param {AudioNode} [output] Gain Node effect wrapper |
25 | | - * @param {Object} [_drywet] Tone.JS CrossFade node (defaults to value: 1) |
26 | | - * @param {AudioNode} [wet] Effects that extend this class should connect |
27 | | - * to the wet signal to this gain node, so that dry and wet |
28 | | - * signals are mixed properly. |
29 | | - */ |
30 | | - p5.Effect = function() { |
31 | | - this.ac = p5sound.audiocontext; |
32 | | - |
33 | | - this.input = this.ac.createGain(); |
34 | | - this.output = this.ac.createGain(); |
35 | | - |
36 | | - /** |
37 | | - * The p5.Effect class is built |
38 | | - * using Tone.js CrossFade |
39 | | - * @private |
40 | | - */ |
41 | | - |
42 | | - this._drywet = new CrossFade(1); |
43 | | - |
44 | | - /** |
45 | | - * In classes that extend |
46 | | - * p5.Effect, connect effect nodes |
47 | | - * to the wet parameter |
48 | | - */ |
49 | | - this.wet = this.ac.createGain(); |
50 | | - |
51 | | - this.input.connect(this._drywet.a); |
52 | | - this.wet.connect(this._drywet.b); |
53 | | - this._drywet.connect(this.output); |
54 | | - |
55 | | - this.connect(); |
56 | | - |
57 | | - //Add to the soundArray |
58 | | - p5sound.soundArray.push(this); |
59 | | - }; |
60 | | - |
61 | | - /** |
62 | | - * Set the output volume of the filter. |
63 | | - * |
64 | | - * @method amp |
65 | | - * @param {Number} [vol] amplitude between 0 and 1.0 |
66 | | - * @param {Number} [rampTime] create a fade that lasts until rampTime |
67 | | - * @param {Number} [tFromNow] schedule this event to happen in tFromNow seconds |
68 | | - */ |
69 | | - p5.Effect.prototype.amp = function(vol, rampTime, tFromNow){ |
70 | | - var rampTime = rampTime || 0; |
71 | | - var tFromNow = tFromNow || 0; |
72 | | - var now = p5sound.audiocontext.currentTime; |
73 | | - var currentVol = this.output.gain.value; |
74 | | - this.output.gain.cancelScheduledValues(now); |
75 | | - this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + .001); |
76 | | - this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + .001); |
77 | | - }; |
78 | | - |
79 | | - /** |
80 | | - * Link effects together in a chain |
81 | | - * Example usage: filter.chain(reverb, delay, panner); |
82 | | - * May be used with an open-ended number of arguments |
83 | | - * |
84 | | - * @method chain |
| 4 | + var p5sound = require('master'); |
| 5 | + var CrossFade = require('Tone/component/CrossFade'); |
| 6 | + |
| 7 | + /** |
| 8 | + * Effect is a base class for audio effects in p5. <br> |
| 9 | + * This module handles the nodes and methods that are |
| 10 | + * common and useful for current and future effects. |
| 11 | + * |
| 12 | + * |
| 13 | + * This class is extended by <a href="/reference/#/p5.Distortion">p5.Distortion</a>, |
| 14 | + * <a href="/reference/#/p5.Compressor">p5.Compressor</a>, |
| 15 | + * <a href="/reference/#/p5.Delay">p5.Delay</a>, |
| 16 | + * <a href="/reference/#/p5.Filter">p5.Filter</a>, |
| 17 | + * <a href="/reference/#/p5.Reverb">p5.Reverb</a>. |
| 18 | + * |
| 19 | + * @class p5.Effect |
| 20 | + * @constructor |
| 21 | + * |
| 22 | + * @param {Object} [ac] Reference to the audio context of the p5 object |
| 23 | + * @param {AudioNode} [input] Gain Node effect wrapper |
| 24 | + * @param {AudioNode} [output] Gain Node effect wrapper |
| 25 | + * @param {Object} [_drywet] Tone.JS CrossFade node (defaults to value: 1) |
| 26 | + * @param {AudioNode} [wet] Effects that extend this class should connect |
| 27 | + * to the wet signal to this gain node, so that dry and wet |
| 28 | + * signals are mixed properly. |
| 29 | + */ |
| 30 | + p5.Effect = function() { |
| 31 | + this.ac = p5sound.audiocontext; |
| 32 | + |
| 33 | + this.input = this.ac.createGain(); |
| 34 | + this.output = this.ac.createGain(); |
| 35 | + |
| 36 | + /** |
| 37 | + * The p5.Effect class is built |
| 38 | + * using Tone.js CrossFade |
| 39 | + * @private |
| 40 | + */ |
| 41 | + |
| 42 | + this._drywet = new CrossFade(1); |
| 43 | + |
| 44 | + /** |
| 45 | + * In classes that extend |
| 46 | + * p5.Effect, connect effect nodes |
| 47 | + * to the wet parameter |
| 48 | + */ |
| 49 | + this.wet = this.ac.createGain(); |
| 50 | + |
| 51 | + this.input.connect(this._drywet.a); |
| 52 | + this.wet.connect(this._drywet.b); |
| 53 | + this._drywet.connect(this.output); |
| 54 | + |
| 55 | + this.connect(); |
| 56 | + |
| 57 | + //Add to the soundArray |
| 58 | + p5sound.soundArray.push(this); |
| 59 | + }; |
| 60 | + |
| 61 | + /** |
| 62 | + * Set the output volume of the filter. |
| 63 | + * |
| 64 | + * @method amp |
| 65 | + * @param {Number} [vol] amplitude between 0 and 1.0 |
| 66 | + * @param {Number} [rampTime] create a fade that lasts until rampTime |
| 67 | + * @param {Number} [tFromNow] schedule this event to happen in tFromNow seconds |
| 68 | + */ |
| 69 | + p5.Effect.prototype.amp = function(vol, rampTime, tFromNow){ |
| 70 | + var rampTime = rampTime || 0; |
| 71 | + var tFromNow = tFromNow || 0; |
| 72 | + var now = p5sound.audiocontext.currentTime; |
| 73 | + var currentVol = this.output.gain.value; |
| 74 | + this.output.gain.cancelScheduledValues(now); |
| 75 | + this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + .001); |
| 76 | + this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + .001); |
| 77 | + }; |
| 78 | + |
| 79 | + /** |
| 80 | + * Link effects together in a chain |
| 81 | + * Example usage: filter.chain(reverb, delay, panner); |
| 82 | + * May be used with an open-ended number of arguments |
| 83 | + * |
| 84 | + * @method chain |
85 | 85 | * @param {Object} [arguments] Chain together multiple sound objects |
86 | | - */ |
87 | | - p5.Effect.prototype.chain = function(){ |
88 | | - if (arguments.length>0){ |
89 | | - this.connect(arguments[0]); |
90 | | - for(var i=1;i<arguments.length; i+=1){ |
91 | | - arguments[i-1].connect(arguments[i]); |
92 | | - } |
93 | | - } |
94 | | - return this; |
95 | | - }; |
96 | | - |
97 | | - /** |
98 | | - * Adjust the dry/wet value. |
99 | | - * |
100 | | - * @method drywet |
101 | | - * @param {Number} [fade] The desired drywet value (0 - 1.0) |
102 | | - */ |
103 | | - p5.Effect.prototype.drywet = function(fade){ |
104 | | - if (typeof fade !=="undefined"){ |
105 | | - this._drywet.fade.value = fade |
106 | | - } |
107 | | - return this._drywet.fade.value; |
108 | | - }; |
109 | | - |
110 | | - /** |
111 | | - * Send output to a p5.js-sound, Web Audio Node, or use signal to |
112 | | - * control an AudioParam |
113 | | - * |
114 | | - * @method connect |
115 | | - * @param {Object} unit |
116 | | - */ |
117 | | - p5.Effect.prototype.connect = function (unit) { |
118 | | - var u = unit || p5.soundOut.input; |
119 | | - this.output.connect(u.input ? u.input : u); |
120 | | - }; |
121 | | - |
122 | | - /** |
123 | | - * Disconnect all output. |
124 | | - * |
125 | | - * @method disconnect |
126 | | - */ |
127 | | - p5.Effect.prototype.disconnect = function() { |
128 | | - this.output.disconnect(); |
129 | | - }; |
130 | | - |
131 | | - p5.Effect.prototype.dispose = function() { |
132 | | - // remove refernce form soundArray |
133 | | - var index = p5sound.soundArray.indexOf(this); |
134 | | - p5sound.soundArray.splice(index, 1); |
135 | | - |
136 | | - this.input.disconnect(); |
137 | | - this.input = undefined; |
138 | | - |
139 | | - this.output.disconnect(); |
140 | | - this.output = undefined; |
141 | | - |
142 | | - this._drywet.disconnect(); |
143 | | - delete this._drywet; |
144 | | - |
145 | | - this.wet.disconnect(); |
146 | | - delete this.wet; |
147 | | - |
148 | | - this.ac = undefined; |
149 | | - }; |
150 | | - |
151 | | - return p5.Effect; |
| 86 | + */ |
| 87 | + p5.Effect.prototype.chain = function(){ |
| 88 | + if (arguments.length>0){ |
| 89 | + this.connect(arguments[0]); |
| 90 | + for(var i=1;i<arguments.length; i+=1){ |
| 91 | + arguments[i-1].connect(arguments[i]); |
| 92 | + } |
| 93 | + } |
| 94 | + return this; |
| 95 | + }; |
| 96 | + |
| 97 | + /** |
| 98 | + * Adjust the dry/wet value. |
| 99 | + * |
| 100 | + * @method drywet |
| 101 | + * @param {Number} [fade] The desired drywet value (0 - 1.0) |
| 102 | + */ |
| 103 | + p5.Effect.prototype.drywet = function(fade){ |
| 104 | + if (typeof fade !=="undefined"){ |
| 105 | + this._drywet.fade.value = fade |
| 106 | + } |
| 107 | + return this._drywet.fade.value; |
| 108 | + }; |
| 109 | + |
| 110 | + /** |
| 111 | + * Send output to a p5.js-sound, Web Audio Node, or use signal to |
| 112 | + * control an AudioParam |
| 113 | + * |
| 114 | + * @method connect |
| 115 | + * @param {Object} unit |
| 116 | + */ |
| 117 | + p5.Effect.prototype.connect = function (unit) { |
| 118 | + var u = unit || p5.soundOut.input; |
| 119 | + this.output.connect(u.input ? u.input : u); |
| 120 | + }; |
| 121 | + |
| 122 | + /** |
| 123 | + * Disconnect all output. |
| 124 | + * |
| 125 | + * @method disconnect |
| 126 | + */ |
| 127 | + p5.Effect.prototype.disconnect = function() { |
| 128 | + if (this.output) { |
| 129 | + this.output.disconnect(); |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + p5.Effect.prototype.dispose = function() { |
| 134 | + // remove refernce form soundArray |
| 135 | + var index = p5sound.soundArray.indexOf(this); |
| 136 | + p5sound.soundArray.splice(index, 1); |
| 137 | + |
| 138 | + if (this.input) { |
| 139 | + this.input.disconnect(); |
| 140 | + delete this.input; |
| 141 | + } |
| 142 | + |
| 143 | + if (this.output) { |
| 144 | + this.output.disconnect(); |
| 145 | + delete this.output; |
| 146 | + } |
| 147 | + |
| 148 | + if (this._drywet) { |
| 149 | + this._drywet.disconnect(); |
| 150 | + delete this._drywet; |
| 151 | + } |
| 152 | + |
| 153 | + if (this.wet) { |
| 154 | + this.wet.disconnect(); |
| 155 | + delete this.wet; |
| 156 | + } |
| 157 | + |
| 158 | + this.ac = undefined; |
| 159 | + }; |
| 160 | + |
| 161 | + return p5.Effect; |
152 | 162 |
|
153 | 163 | }); |
0 commit comments