import { getInstance } from '../instance' ; import { Filter } from './Filter' ; memberof class ReverbFilter extends Filter { private _seconds : number ; private _decay : number ; private _reverse : boolean ; param seconds param decay param reverse constructor ( seconds = 3 , decay = 2 , reverse = false ) { if ( getInstance ( ) . useLegacy ) { super ( null ) ; return ; } super ( null ) ; this . _seconds = this . _clamp ( seconds , 1 , 50 ) ; this . _decay = this . _clamp ( decay , 0 , 100 ) ; this . _reverse = reverse ; this . _rebuild ( ) ; } param value param min param max return private _clamp ( value , min , max ) { return Math . min ( max , Math . max ( min , value ) ) ; } default get seconds ( ) : number { return this . _seconds ; } set seconds ( seconds ) { this . _seconds = this . _clamp ( seconds , 1 , 50 ) ; this . _rebuild ( ) ; } default get decay ( ) : number { return this . _decay ; } set decay ( decay ) { this . _decay = this . _clamp ( decay , 0 , 100 ) ; this . _rebuild ( ) ; } default get reverse ( ) : boolean { return this . _reverse ; } set reverse ( reverse ) { this . _reverse = reverse ; this . _rebuild ( ) ; } /**
     * Utility function for building an impulse response
     * from the module parameters.
     */ private _rebuild ( ) { const context = getInstance ( ) . context . audioContext ; const rate = context . sampleRate ; const length = rate * this . _seconds ; const impulse = context . createBuffer ( 2 , length , rate ) ; const impulseL = impulse . getChannelData ( 0 ) ; const impulseR = impulse . getChannelData ( 1 ) ; let n ; for ( let i = 0 ; i < length ; i ++ ) { n = this . _reverse ? length - i : i ; impulseL [ i ] = ( ( Math . random ( ) * 2 ) - 1 ) * Math . pow ( 1 - ( n / length ) , this . _decay ) ; impulseR [ i ] = ( ( Math . random ( ) * 2 ) - 1 ) * Math . pow ( 1 - ( n / length ) , this . _decay ) ; } const convolver = getInstance ( ) . context . audioContext . createConvolver ( ) ; convolver . buffer = impulse ; this . init ( convolver ) ; } } export { ReverbFilter } ;