@@ -44,14 +44,11 @@ define(function (require) {
44
44
p5 . Reverb = function ( ) {
45
45
Effect . call ( this ) ;
46
46
47
- this . convolverNode = this . ac . createConvolver ( ) ;
47
+ this . _initConvolverNode ( ) ;
48
48
49
49
// otherwise, Safari distorts
50
50
this . input . gain . value = 0.5 ;
51
51
52
- this . input . connect ( this . convolverNode ) ;
53
- this . convolverNode . connect ( this . wet ) ;
54
-
55
52
// default params
56
53
this . _seconds = 3 ;
57
54
this . _decay = 2 ;
@@ -62,6 +59,25 @@ define(function (require) {
62
59
} ;
63
60
64
61
p5 . Reverb . prototype = Object . create ( Effect . prototype ) ;
62
+
63
+ p5 . Reverb . prototype . _initConvolverNode = function ( ) {
64
+ this . convolverNode = this . ac . createConvolver ( ) ;
65
+ this . input . connect ( this . convolverNode ) ;
66
+ this . convolverNode . connect ( this . wet ) ;
67
+ } ;
68
+
69
+ p5 . Reverb . prototype . _teardownConvolverNode = function ( ) {
70
+ if ( this . convolverNode ) {
71
+ this . convolverNode . disconnect ( ) ;
72
+ delete this . convolverNode ;
73
+ }
74
+ } ;
75
+
76
+ p5 . Reverb . prototype . _setBuffer = function ( audioBuffer ) {
77
+ this . _teardownConvolverNode ( ) ;
78
+ this . _initConvolverNode ( ) ;
79
+ this . convolverNode . buffer = audioBuffer ;
80
+ } ;
65
81
/**
66
82
* Connect a source to the reverb, and assign reverb parameters.
67
83
*
@@ -164,15 +180,12 @@ define(function (require) {
164
180
impulseL [ i ] = ( Math . random ( ) * 2 - 1 ) * Math . pow ( 1 - n / length , decay ) ;
165
181
impulseR [ i ] = ( Math . random ( ) * 2 - 1 ) * Math . pow ( 1 - n / length , decay ) ;
166
182
}
167
- this . convolverNode . buffer = impulse ;
183
+ this . _setBuffer ( impulse ) ;
168
184
} ;
169
185
170
186
p5 . Reverb . prototype . dispose = function ( ) {
171
187
Effect . prototype . dispose . apply ( this ) ;
172
- if ( this . convolverNode ) {
173
- this . convolverNode . buffer = null ;
174
- this . convolverNode = null ;
175
- }
188
+ this . _teardownConvolverNode ( ) ;
176
189
} ;
177
190
178
191
// =======================================================================
@@ -233,23 +246,20 @@ define(function (require) {
233
246
* </code></div>
234
247
*/
235
248
p5 . Convolver = function ( path , callback , errorCallback ) {
236
- Effect . call ( this ) ;
249
+ p5 . Reverb . call ( this ) ;
237
250
238
251
/**
239
252
* Internally, the p5.Convolver uses the a
240
253
* <a href="http://www.w3.org/TR/webaudio/#ConvolverNode">
241
254
* Web Audio Convolver Node</a>.
242
255
*
243
- * @property {ConvolverNode } convolverNod
256
+ * @property {ConvolverNode } convolverNode
244
257
*/
245
- this . convolverNode = this . ac . createConvolver ( ) ;
258
+ this . _initConvolverNode ( ) ;
246
259
247
260
// otherwise, Safari distorts
248
261
this . input . gain . value = 0.5 ;
249
262
250
- this . input . connect ( this . convolverNode ) ;
251
- this . convolverNode . connect ( this . wet ) ;
252
-
253
263
if ( path ) {
254
264
this . impulses = [ ] ;
255
265
this . _loadBuffer ( path , callback , errorCallback ) ;
@@ -358,7 +368,7 @@ define(function (require) {
358
368
buffer . name = chunks [ chunks . length - 1 ] ;
359
369
buffer . audioBuffer = buff ;
360
370
self . impulses . push ( buffer ) ;
361
- self . convolverNode . buffer = buffer . audioBuffer ;
371
+ self . _setBuffer ( buffer . audioBuffer ) ;
362
372
if ( callback ) {
363
373
callback ( buffer ) ;
364
374
}
@@ -509,32 +519,27 @@ define(function (require) {
509
519
*/
510
520
p5 . Convolver . prototype . toggleImpulse = function ( id ) {
511
521
if ( typeof id === 'number' && id < this . impulses . length ) {
512
- this . convolverNode . buffer = this . impulses [ id ] . audioBuffer ;
522
+ this . _setBuffer ( this . impulses [ id ] . audioBuffer ) ;
513
523
}
514
524
if ( typeof id === 'string' ) {
515
525
for ( var i = 0 ; i < this . impulses . length ; i ++ ) {
516
526
if ( this . impulses [ i ] . name === id ) {
517
- this . convolverNode . buffer = this . impulses [ i ] . audioBuffer ;
527
+ this . _setBuffer ( this . impulses [ i ] . audioBuffer ) ;
518
528
break ;
519
529
}
520
530
}
521
531
}
522
532
} ;
523
533
524
534
p5 . Convolver . prototype . dispose = function ( ) {
525
- Effect . prototype . dispose . apply ( this ) ;
535
+ p5 . Reverb . prototype . dispose . apply ( this ) ;
526
536
527
537
// remove all the Impulse Response buffers
528
538
for ( var i in this . impulses ) {
529
539
if ( this . impulses [ i ] ) {
530
540
this . impulses [ i ] = null ;
531
541
}
532
542
}
533
-
534
- if ( this . convolverNode ) {
535
- this . convolverNode . disconnect ( ) ;
536
- this . concolverNode = null ;
537
- }
538
543
} ;
539
544
540
545
} ) ;
0 commit comments