class Module(BaseModule):
    """
    "Arctangent distortion module with pre and post filters"
    
    Description
    
    This module applies an arctangent distortion with control on the amount
    of drive and pre/post filtering.

    Sliders
    
        # Pre Filter Freq : 
            Center frequency of the filter applied before distortion
        # Pre Filter Q : 
            Q factor of the filter applied before distortion
        # Pre Gain : 
            Gain control applied before the distortion
        # Drive : 
            Amount of distortion applied on the signal
        # Post Filter Freq : 
            Center frequency of the filter applied after distortion
        # Post Filter Q : 
            Q factor of the filter applied after distortion
        # Dry / Wet : 
            Mix between the original signal and the degraded signal

    Graph Only
    
        # Overall Amplitude : 
            The amplitude curve applied on the total duration of the performance
    
    Popups & Toggles
    
        # Pre Filter Type : 
            Type of filter used before distortion
        # Post Filter Type : 
            Type of filter used after distortion
        # Balance :
            Compression mode. Off, balanced with a fixed signal
            or balanced with the input source.
        # 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")
        self.snd_filt = Biquadx(self.snd, freq=self.prefiltf, q=self.prefiltq, type=self.prefilttype_index, stages=2)
        self.input = Sig(self.snd_filt, mul=DBToA(self.pregain))
        self.disto = Disto(self.input, drive=self.drv, slope=0, mul=.2)
        self.disto_filt = Biquadx(self.disto, freq=self.cut, q=self.q, stages=2, type=self.postfilttype_index)
        self.deg = Interp(self.snd, self.disto_filt, self.drywet, mul=self.env)

        self.osc = Sine(10000,mul=.1)
        self.balanced = Balance(self.deg, self.osc, freq=10)
        self.out = Interp(self.deg, self.balanced)

        #INIT
        self.balance(self.balance_index, self.balance_value)

    def balance(self,index,value):
       if index == 0:
           self.out.interp  = 0
       elif index == 1:
           self.out.interp  = 1
           self.balanced.input2 = self.osc
       elif index == 2:
           self.out.interp = 1
           self.balanced.input2 = self.snd

    def prefilttype(self, index, value):
        self.snd_filt.type = index

    def postfilttype(self, index, value):
        self.out.type = index

Interface = [   csampler(name="snd"), 
                cgraph(name="env", label="Overall Amplitude", func=[(0,1),(1,1)], col="blue1"),
                cslider(name="prefiltf", label="Pre Filter Freq", min=100, max=18000, init=250, rel="log", unit="Hz", col="green1"),
                cslider(name="prefiltq", label="Pre Filter Q", min=.5, max=10, init=0.707, rel="log", col="green2"),
                cslider(name="pregain", label="Pre Gain", min=-48, max=18, init=0, rel="lin", col="blue2"),
                cslider(name="drv", label="Drive", min=0.5, max=1, init=.9, rel="lin", col="purple1"),
                cslider(name="cut", label="Post Filter Freq", min=100, max=18000, init=5000, rel="log", col="green3"),
                cslider(name="q", label="Post Filter Q", min=.5, max=10, init=0.707, rel="log", col="green4"),
                cslider(name="drywet", label="Dry / Wet", min=0, max=1, init=1, rel="lin", unit="x", col="blue1"),
                cpopup(name="prefilttype", label="Pre Filter Type", init="Highpass", col="green1", value=["Lowpass","Highpass","Bandpass","Bandstop"]),
                cpopup(name="postfilttype", label="Post Filter Type", init="Lowpass", col="green3", value=["Lowpass","Highpass","Bandpass","Bandstop"]),
                cpopup(name="balance", label = "Balance", init= "Off", col="blue", value=["Off","Compress", "Source"]),
                cpoly()
            ]

