class Module(BaseModule):
    """
    "Multi-band noise gate module"

    Description

    MultiBandGate implements four separated spectral band 
    noise gaters with independent threshold and gain.
    
    Sliders
    
        # Frequency Splitter : 
            Split points for multi-band processing
        # Threshold Band 1 : 
            dB value at which the gate becomes active on the first band
        # Gain Band 1 : 
            Gain of the gated first band
        # Threshold Band 2 : 
            dB value at which the gate becomes active on the second band
        # Gain Band 2 : 
            Gain of the gated second band
        # Threshold Band 3 : 
            dB value at which the gate becomes active on the third band
        # Gain Band 3 : 
            Gain of the gated third band
        # Threshold Band 4 : 
            dB value at which the gate becomes active on the fourth band
        # Gain Band 4 : 
            Gain of the gated fourth band
        # Rise Time : 
            Time taken by the gate to close
        # Fall Time : 
            Time taken by the gate to open
        # Dry / Wet : 
            Mix between the original signal and the shifted signals

    Graph Only
    
        # Overall Amplitude : 
            The amplitude curve applied on the total duration of the performance
    
    Popups & Toggles
    
        # Polyphony Voices : 
            Number of voices played simultaneously (polyphony), 
            only available at initialization time
        # Polyphony Spread : 
            Pitch variation between voices (chorus), 
            only available at initialization time
    
    """
    def __init__(self):
        BaseModule.__init__(self)
        self.snd = self.addSampler("snd")
        freqs = self.splitter.get(True)
        self.FBfade = SigTo(value=1, time=.01, init=1)
        self.split = FourBand(input=self.snd, freq1=freqs[0], freq2=freqs[1], freq3=freqs[2], mul=self.FBfade)
        self.threshs = self.duplicate([self.thresh1,self.thresh2,self.thresh3,self.thresh4], len(self.snd))
        self.mul1 = DBToA(self.gain1)
        self.mul2 = DBToA(self.gain2)
        self.mul3 = DBToA(self.gain3)
        self.mul4 = DBToA(self.gain4)
        self.muls = self.duplicate([self.mul1,self.mul2,self.mul3,self.mul4], len(self.snd))
        self.gate = Gate(input=self.split, thresh=self.threshs, risetime=self.gaterise, falltime=self.gatefall, lookahead=5.00,
                            outputAmp=False, mul=self.muls).mix(self.nchnls)
        self.out = Interp(Delay(self.snd, 0.005, maxdelay=0.005), self.gate, self.drywet, mul=self.env)

    def splitter_up(self, value):
        self.FBfade.value = 0
        time.sleep(.02)
        self.split.freq1 = value[0]
        self.split.freq2 = value[1]
        self.split.freq3 = value[2]
        time.sleep(.02)
        self.FBfade.value = 1
        
Interface = [   csampler(name="snd"), 
                cgraph(name="env", label="Overall Amplitude", func=[(0,1),(1,1)], col="blue"),
                csplitter(name="splitter", label="Frequency Splitter", min=100, max=18000, init=[150, 500, 2000], 
                          num_knobs=3, rel="log", gliss=0, up=True, unit="Hz", col="grey"),
                cslider(name="thresh1", label="Threshold Band 1", min=-80, max=-0.1, init=-60, rel="lin", unit="dB", col="purple1", half=True),
                cslider(name="gain1", label="Gain Band 1", min=-48, max=18, init=0, rel="lin", unit="dB", col="purple2", half=True),
                cslider(name="thresh2", label="Threshold Band 2", min=-80, max=-0.1, init=-60, rel="lin", unit="dB", col="red1", half=True),
                cslider(name="gain2", label="Gain Band 2", min=-48, max=18, init=0, rel="lin", unit="dB", col="red2", half=True),
                cslider(name="thresh3", label="Threshold Band 3", min=-80, max=-0.1, init=-60, rel="lin", unit="dB", col="green1", half=True),
                cslider(name="gain3", label="Gain Band 3", min=-48, max=18, init=0, rel="lin", unit="dB", col="green2", half=True),
                cslider(name="thresh4", label="Threshold Band 4", min=-80, max=-0.1, init=-60, rel="lin", unit="dB", col="blue1", half=True),
                cslider(name="gain4", label="Gain Band 4", min=-48, max=18, init=0, rel="lin", unit="dB", col="blue2", half=True),
                cslider(name="gaterise", label="Rise Time", min=0.001, max=1, init=0.01, rel="lin", unit="sec", col="orange1"),
                cslider(name="gatefall", label="Fall Time", min=0.001, max=1, init=0.01, rel="lin", unit="sec", col="orange2"),
                cslider(name="drywet", label="Dry / Wet", min=0, max=1, init=1, rel="lin", unit="x", col="blue1"),
                cpoly()
          ]

