@@ -9648,18 +9648,32 @@ reverb = function () {
9648
9648
*/
9649
9649
p5 . Reverb = function ( ) {
9650
9650
Effect . call ( this ) ;
9651
- this . convolverNode = this . ac . createConvolver ( ) ;
9651
+ this . _initConvolverNode ( ) ;
9652
9652
// otherwise, Safari distorts
9653
9653
this . input . gain . value = 0.5 ;
9654
- this . input . connect ( this . convolverNode ) ;
9655
- this . convolverNode . connect ( this . wet ) ;
9656
9654
// default params
9657
9655
this . _seconds = 3 ;
9658
9656
this . _decay = 2 ;
9659
9657
this . _reverse = false ;
9660
9658
this . _buildImpulse ( ) ;
9661
9659
} ;
9662
9660
p5 . Reverb . prototype = Object . create ( Effect . prototype ) ;
9661
+ p5 . Reverb . prototype . _initConvolverNode = function ( ) {
9662
+ this . convolverNode = this . ac . createConvolver ( ) ;
9663
+ this . input . connect ( this . convolverNode ) ;
9664
+ this . convolverNode . connect ( this . wet ) ;
9665
+ } ;
9666
+ p5 . Reverb . prototype . _teardownConvolverNode = function ( ) {
9667
+ if ( this . convolverNode ) {
9668
+ this . convolverNode . disconnect ( ) ;
9669
+ delete this . convolverNode ;
9670
+ }
9671
+ } ;
9672
+ p5 . Reverb . prototype . _setBuffer = function ( audioBuffer ) {
9673
+ this . _teardownConvolverNode ( ) ;
9674
+ this . _initConvolverNode ( ) ;
9675
+ this . convolverNode . buffer = audioBuffer ;
9676
+ } ;
9663
9677
/**
9664
9678
* Connect a source to the reverb, and assign reverb parameters.
9665
9679
*
@@ -9759,14 +9773,11 @@ reverb = function () {
9759
9773
impulseL [ i ] = ( Math . random ( ) * 2 - 1 ) * Math . pow ( 1 - n / length , decay ) ;
9760
9774
impulseR [ i ] = ( Math . random ( ) * 2 - 1 ) * Math . pow ( 1 - n / length , decay ) ;
9761
9775
}
9762
- this . convolverNode . buffer = impulse ;
9776
+ this . _setBuffer ( impulse ) ;
9763
9777
} ;
9764
9778
p5 . Reverb . prototype . dispose = function ( ) {
9765
9779
Effect . prototype . dispose . apply ( this ) ;
9766
- if ( this . convolverNode ) {
9767
- this . convolverNode . buffer = null ;
9768
- this . convolverNode = null ;
9769
- }
9780
+ this . _teardownConvolverNode ( ) ;
9770
9781
} ;
9771
9782
// =======================================================================
9772
9783
// *** p5.Convolver ***
@@ -9825,19 +9836,17 @@ reverb = function () {
9825
9836
* </code></div>
9826
9837
*/
9827
9838
p5 . Convolver = function ( path , callback , errorCallback ) {
9828
- Effect . call ( this ) ;
9839
+ p5 . Reverb . call ( this ) ;
9829
9840
/**
9830
9841
* Internally, the p5.Convolver uses the a
9831
9842
* <a href="http://www.w3.org/TR/webaudio/#ConvolverNode">
9832
9843
* Web Audio Convolver Node</a>.
9833
9844
*
9834
- * @property {ConvolverNode } convolverNod
9845
+ * @property {ConvolverNode } convolverNode
9835
9846
*/
9836
- this . convolverNode = this . ac . createConvolver ( ) ;
9847
+ this . _initConvolverNode ( ) ;
9837
9848
// otherwise, Safari distorts
9838
9849
this . input . gain . value = 0.5 ;
9839
- this . input . connect ( this . convolverNode ) ;
9840
- this . convolverNode . connect ( this . wet ) ;
9841
9850
if ( path ) {
9842
9851
this . impulses = [ ] ;
9843
9852
this . _loadBuffer ( path , callback , errorCallback ) ;
@@ -9935,7 +9944,7 @@ reverb = function () {
9935
9944
buffer . name = chunks [ chunks . length - 1 ] ;
9936
9945
buffer . audioBuffer = buff ;
9937
9946
self . impulses . push ( buffer ) ;
9938
- self . convolverNode . buffer = buffer . audioBuffer ;
9947
+ self . _setBuffer ( buffer . audioBuffer ) ;
9939
9948
if ( callback ) {
9940
9949
callback ( buffer ) ;
9941
9950
}
@@ -10072,29 +10081,25 @@ reverb = function () {
10072
10081
*/
10073
10082
p5 . Convolver . prototype . toggleImpulse = function ( id ) {
10074
10083
if ( typeof id === 'number' && id < this . impulses . length ) {
10075
- this . convolverNode . buffer = this . impulses [ id ] . audioBuffer ;
10084
+ this . _setBuffer ( this . impulses [ id ] . audioBuffer ) ;
10076
10085
}
10077
10086
if ( typeof id === 'string' ) {
10078
10087
for ( var i = 0 ; i < this . impulses . length ; i ++ ) {
10079
10088
if ( this . impulses [ i ] . name === id ) {
10080
- this . convolverNode . buffer = this . impulses [ i ] . audioBuffer ;
10089
+ this . _setBuffer ( this . impulses [ i ] . audioBuffer ) ;
10081
10090
break ;
10082
10091
}
10083
10092
}
10084
10093
}
10085
10094
} ;
10086
10095
p5 . Convolver . prototype . dispose = function ( ) {
10087
- Effect . prototype . dispose . apply ( this ) ;
10096
+ p5 . Reverb . prototype . dispose . apply ( this ) ;
10088
10097
// remove all the Impulse Response buffers
10089
10098
for ( var i in this . impulses ) {
10090
10099
if ( this . impulses [ i ] ) {
10091
10100
this . impulses [ i ] = null ;
10092
10101
}
10093
10102
}
10094
- if ( this . convolverNode ) {
10095
- this . convolverNode . disconnect ( ) ;
10096
- this . concolverNode = null ;
10097
- }
10098
10103
} ;
10099
10104
} ( errorHandler , effect ) ;
10100
10105
/** Tone.js module by Yotam Mann, MIT License 2016 http://opensource.org/licenses/MIT **/
0 commit comments