import { getInstance } from '../instance' ; import { WebAudioUtils } from '../webaudio/WebAudioUtils' ; import { Filter } from './Filter' ; interface Band { f : number ; type : string ; gain : number ; } memberof class EqualizerFilter extends Filter { readonly public static readonly F32 : number = 32 ; readonly public static readonly F64 : number = 64 ; readonly public static readonly F125 : number = 125 ; readonly public static readonly F250 : number = 250 ; readonly public static readonly F500 : number = 500 ; readonly public static readonly F1K : number = 1000 ; readonly public static readonly F2K : number = 2000 ; readonly public static readonly F4K : number = 4000 ; readonly public static readonly F8K : number = 8000 ; readonly public static readonly F16K : number = 16000 ; readonly public readonly bands : BiquadFilterNode [ ] ; readonly public readonly bandsMap : Record < number , BiquadFilterNode > ; param f32 param f64 param f125 param f250 param f500 param f1k param f2k param f4k param f8k param f16k constructor ( f32 = 0 , f64 = 0 , f125 = 0 , f250 = 0 , f500 = 0 , f1k = 0 , f2k = 0 , f4k = 0 , f8k = 0 , f16k = 0 ) { if ( getInstance ( ) . useLegacy ) { super ( null ) ; return ; } const equalizerBands = [ { f : EqualizerFilter . F32 , type : 'lowshelf' , gain : f32 , } , { f : EqualizerFilter . F64 , type : 'peaking' , gain : f64 , } , { f : EqualizerFilter . F125 , type : 'peaking' , gain : f125 , } , { f : EqualizerFilter . F250 , type : 'peaking' , gain : f250 , } , { f : EqualizerFilter . F500 , type : 'peaking' , gain : f500 , } , { f : EqualizerFilter . F1K , type : 'peaking' , gain : f1k , } , { f : EqualizerFilter . F2K , type : 'peaking' , gain : f2k , } , { f : EqualizerFilter . F4K , type : 'peaking' , gain : f4k , } , { f : EqualizerFilter . F8K , type : 'peaking' , gain : f8k , } , { f : EqualizerFilter . F16K , type : 'highshelf' , gain : f16k , } , ] ; const bands = equalizerBands . map ( ( band ) => { const node = getInstance ( ) . context . audioContext . createBiquadFilter ( ) ; node . type = band . type as BiquadFilterType ; WebAudioUtils . setParamValue ( node . Q , 1 ) ; node . frequency . value = band . f ; WebAudioUtils . setParamValue ( node . gain , band . gain ) ; return node ; } ) ; super ( bands [ 0 ] , bands [ bands . length - 1 ] ) ; this . bands = bands ; this . bandsMap = { } ; for ( let i = 0 ; i < this . bands . length ; i ++ ) { const node = this . bands [ i ] ; if ( i > 0 ) { this . bands [ i - 1 ] . connect ( node ) ; } this . bandsMap [ node . frequency . value ] = node ; } } param frequency param gain public setGain ( frequency , gain = 0 ) { if ( ! this . bandsMap [ frequency ] ) { throw new Error ( `No band found for frequency ${ frequency }` ) ; } WebAudioUtils . setParamValue ( this . bandsMap [ frequency ] . gain , gain ) ; } return public getGain ( frequency ) { if ( ! this . bandsMap [ frequency ] ) { throw new Error ( `No band found for frequency ${ frequency }` ) ; } return this . bandsMap [ frequency ] . gain . value ; } default public set f32 ( value ) { this . setGain ( EqualizerFilter . F32 , value ) ; } public get f32 ( ) : number { return this . getGain ( EqualizerFilter . F32 ) ; } default public set f64 ( value ) { this . setGain ( EqualizerFilter . F64 , value ) ; } public get f64 ( ) : number { return this . getGain ( EqualizerFilter . F64 ) ; } default public set f125 ( value ) { this . setGain ( EqualizerFilter . F125 , value ) ; } public get f125 ( ) : number { return this . getGain ( EqualizerFilter . F125 ) ; } default public set f250 ( value ) { this . setGain ( EqualizerFilter . F250 , value ) ; } public get f250 ( ) : number { return this . getGain ( EqualizerFilter . F250 ) ; } default public set f500 ( value ) { this . setGain ( EqualizerFilter . F500 , value ) ; } public get f500 ( ) : number { return this . getGain ( EqualizerFilter . F500 ) ; } default public set f1k ( value ) { this . setGain ( EqualizerFilter . F1K , value ) ; } public get f1k ( ) : number { return this . getGain ( EqualizerFilter . F1K ) ; } default public set f2k ( value ) { this . setGain ( EqualizerFilter . F2K , value ) ; } public get f2k ( ) : number { return this . getGain ( EqualizerFilter . F2K ) ; } default public set f4k ( value ) { this . setGain ( EqualizerFilter . F4K , value ) ; } public get f4k ( ) : number { return this . getGain ( EqualizerFilter . F4K ) ; } default public set f8k ( value ) { this . setGain ( EqualizerFilter . F8K , value ) ; } public get f8k ( ) : number { return this . getGain ( EqualizerFilter . F8K ) ; } default public set f16k ( value ) { this . setGain ( EqualizerFilter . F16K , value ) ; } public get f16k ( ) : number { return this . getGain ( EqualizerFilter . F16K ) ; } /** Reset all frequency bands to have gain of 0 */ public reset ( ) { this . bands . forEach ( ( band ) => { WebAudioUtils . setParamValue ( band . gain , 0 ) ; } ) ; } public destroy ( ) { this . bands . forEach ( ( band ) => { band . disconnect ( ) ; } ) ; ( this as any ) . bands = null ; ( this as any ) . bandsMap = null ; } } export { EqualizerFilter } ;