class Module(BaseModule):
    """
    "Multi-band harmonizer module"

    Description

    MultiBandHarmonizer implements four separated spectral band 
    harmonizers with independent transposition, feedback and gain.
    
    Sliders
    
        # Frequency Splitter : 
            Split points for multi-band processing
        # Transpo Band 1 : 
            Pitch shift for the first band
        # Feedback Band 1 : 
            Amount of harmonized signal fed back into the first band harmonizer
        # Gain Band 1 : 
            Gain of the harmonized first band
        # Transpo Band 2 : 
            Pitch shift for the second band
        # Feedback Band 2 : 
            Amount of harmonized signal fed back into the second band harmonizer
        # Gain Band 2 : 
            Gain of the harmonized second band
        # Transpo Band 3 : 
            Pitch shift for the third band
        # Feedback Band 3 : 
            Amount of harmonized signal fed back into the third band harmonizer
        # Gain Band 3 : 
            Gain of the harmonized third band
        # Transpo Band 4 : 
            Pitch shift for the fourth band
        # Feedback Band 4 : 
            Amount of harmonized signal fed back into the fourth band harmonizer
        # Gain Band 4 : 
            Gain of the harmonized fourth band
        # Dry / Wet : 
            Mix between the original signal and the harmonized signals

    Graph Only
    
        # Overall Amplitude : 
            The amplitude curve applied on the total duration of the performance
    
    Popups & Toggles
    
        # Win Size : 
            Harmonizer window size (delay) in seconds
        # 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.transps = self.duplicate([self.transp1,self.transp2,self.transp3,self.transp4], 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.fbs = self.duplicate([self.fb1, self.fb2, self.fb3, self.fb4], len(self.snd))
        self.harm = Harmonizer(input=self.split, transpo=self.transps, feedback=self.fbs, winsize=float(self.winsize_value), mul=self.muls)
        self.harms = self.harm.mix(self.nchnls)
        self.out = Interp(self.snd, self.harms, self.drywet, mul=self.env*0.5)
        
    def winsize(self, index, value):
        self.harm.winsize = float(value)
    
    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="blue1"),
                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="transp1", label="Transpo Band 1", min=-24, max=24, init=2, rel="lin", unit="semi", col="purple1", half=True),
                cslider(name="transp2", label="Transpo Band 2", min=-24, max=24, init=4, rel="lin", unit="semi", col="red1", half=True),
                cslider(name="fb1", label="Feedback Band 1", min=0, max=0.999, init=0.6, rel="lin", unit="x", col="purple2", half=True),
                cslider(name="fb2", label="Feedback Band 2", min=0, max=0.999, init=0.5, rel="lin", unit="x", col="red2", half=True),
                cslider(name="gain1", label="Gain Band 1", min=-48, max=18, init=0, rel="lin", unit="dB", col="purple3", half=True),
                cslider(name="gain2", label="Gain Band 2", min=-48, max=18, init=0, rel="lin", unit="dB", col="red3", half=True),
                cslider(name="transp3", label="Transpo Band 3", min=-24, max=24, init=-2, rel="lin", unit="semi", col="green1", half=True),
                cslider(name="transp4", label="Transpo Band 4", min=-24, max=24, init=-4, rel="lin", unit="semi", col="blue1", half=True),
                cslider(name="fb3", label="Feedback Band 3", min=0, max=0.999, init=0.5, rel="lin", unit="x", col="green2", half=True),
                cslider(name="fb4", label="Feedback Band 4", min=0, max=0.999, init=0.6, rel="lin", unit="x", col="blue2", half=True),
                cslider(name="gain3", label="Gain Band 3", min=-48, max=18, init=0, rel="lin", unit="dB", col="green3", half=True),
                cslider(name="gain4", label="Gain Band 4", min=-48, max=18, init=0, rel="lin", unit="dB", col="blue3", half=True),
                cslider(name="drywet", label="Dry / Wet", min=0, max=1, init=1, rel="lin", unit="x", col="blue1"),
                cpopup(name="winsize", label="Win Size", init="0.1", col="orange1", value=["0.025","0.05","0.1","0.15","0.2","0.25","0.5","0.75","1"]),
                cpoly()
          ]

