class Module(BaseModule):
    """
    "State Variable Filter, version 2 !"
    
    Description

    This module implements a second-order state variable filter (SVF) which allow to
    interpolate over various filter types, organized in a user-defined order (see
    `Filter Order` parameter below).

    This 2-pole multimode filter is described in the book "The Art of VA Filter Design"
    by Vadim Zavalishin (version 2.1.0).

    Sliders
    
        # Filter Freq : 
                Cutoff or center frequency of the filter.
        # Filter Q :
                Q factor (inverse of bandwidth) of the filter.
        # Filter Shelf :
                Gain, in dB, used by shelving filters.
        # Type : 
                Interpolating factor between filters
        # Dry / Wet : 
                Mix between the original and the filtered signals

    Graph Only
    
        # Overall Amplitude : 
                The amplitude curve applied on the total duration of the performance
    
    Popups & Toggles
    
        # Balance :
            Compression mode. Off, balanced with a fixed signal
            or balanced with the input source.
        # Filter Order :
            The ordering is a list of one to ten integers indicating the filter
            types order used by the `Type` parameter to crossfade between them.
            Types, as integer, are:
                0 = lowpass
                1 = bandpass
                2 = highpass
                3 = highshelf
                4 = bandshelf
                5 = lowshelf
                6 = notch
                7 = peak
                8 = allpass
        # Polyphony Voices : 
                Number of voices played simultaneously (polyphony), 
                only available at initialization time
        # Polyphony Chords : 
                Pitch interval between voices (chords), 
                only available at initialization time

    """
    def __init__(self):
        BaseModule.__init__(self)
        self.snd = self.addSampler("snd")
        self.filtOrder = [0, 1, 2, 3, 4, 5, 6, 7, 8]
        self.filterType = Sig(self.type, mul=len(self.filtOrder))
        self.dsp = SVF2(self.snd, freq=self.freq, q=self.q, shelf=self.shelf, type=self.filterType)
        self.dsp.order = self.filtOrder
        self.deg = Interp(self.snd, self.dsp, self.drywet, mul=self.env)

#BALANCE
        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 order(self, value):
        self.filtOrder = value[:]
        self.dsp.order = self.filtOrder
        self.filterType.mul = len(self.filtOrder)
        
Interface = [
    csampler(name="snd"),
    cgraph(name="env", label="Overall Amplitude", func=[(0,1),(1,1)], col="blue1"),
    cslider(name="freq", label="Filter Freq", min=20, max=20000, init=1000, rel="log", unit="Hz", col="green1"),
    cslider(name="q", label="Filter Q", min=0.5, max=25, init=1, rel="log", unit="x", col="green2"),
    cslider(name="shelf", label="Filter Shelf", min=-12, max=12, init=-6, rel="lin", unit="dB", col="green3"),
    cslider(name="type", label="Type", min=0, max=1, init=0.5, rel="lin", unit="x", col="green4"),
    cslider(name="drywet", label="Dry / Wet", min=0, max=1, init=1, rel="lin", unit="x", col="blue1"),
    cgen(name="order", label="Filter Order", init=[0,1,2,3,4,5,6,7,8], col="green1"),
    cpopup(name="balance", label = "Balance", init= "Off", col="blue1", value=["Off","Compress", "Source"]),
    cpoly()
]

