class Module(BaseModule):
    """
    "Phase vocoder additive resynthesis"
    
    Description

    This module uses the magnitudes and true frequencies of a
    phase vocoder analysis to control amplitudes and frequencies 
    envelopes of an oscillator bank.

    Sliders
    
        # Transpo : 
            Transposition factor
        # Num of Oscs : 
            Number of oscillators used to synthesize the sound
        # First bin : 
            First synthesized bin
        # Increment : 
            Step between synthesized bins
        # Dry / Wet : 
            Mix between the original signal and the processed signal
    
    Graph Only
    
        # Overall Amplitude : 
            The amplitude curve applied on the total duration of the performance

    Popups & Toggles

        # FFT Size : 
            Size, in samples, of the FFT
        # FFT Envelope : 
            Windowing shape of the FFT
        # FFT Overlaps : 
            Number of FFT overlaping analysis
        # 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")

        size = int(self.fftsize_value)
        olaps = int(self.overlaps_value)
        wintype = self.wtype_index
        self.oneOverSr = 1.0 / self.sr

        self.delsrc = Delay(self.snd, delay=size*self.oneOverSr)

        self.fin = PVAnal(self.snd, size=size, overlaps=olaps, wintype=wintype)
        self.fout = PVAddSynth(self.fin, pitch=self.transpo, num=int(self.num.get()), 
                            first=int(self.first.get()), inc=int(self.inc.get()))
        self.fade = SigTo(value=1, time=.05, init=1)
        self.out = Interp(self.delsrc*0.7, self.fout, self.mix, mul=self.fade*self.env)

    def fftsize(self, index, value):
        newsize = int(value)
        self.fade.value = 0
        time.sleep(.05)
        self.delsrc.delay = newsize*self.oneOverSr
        self.fin.size = newsize
        time.sleep(.05)
        self.fade.value = 1

    def overlaps(self, index, value):
        olaps = int(value)
        self.fade.value = 0
        time.sleep(.05)
        self.fin.overlaps = olaps
        time.sleep(.05)
        self.fade.value = 1

    def wtype(self, index, value):
        self.fin.wintype = index

    def num_up(self, value):
        self.fout.num = int(value)

    def first_up(self, value):
        self.fout.first = int(value)

    def inc_up(self, value):
        self.fout.inc = int(value)

Interface = [   csampler(name="snd"), 
                cgraph(name="env", label="Overall Amplitude", func=[(0,1),(1,1)], col="blue"),
                cslider(name="transpo", label="Transpo", min=0.25, max=4, init=1, rel="lin", unit="x", col="green1"),
                cslider(name="num", label="Num of Oscs", min=1, max=500, init=100, rel="lin", res="int", unit="x", up=True),
                cslider(name="first", label="First bin", min=0, max=50, init=0, rel="lin", res="int", unit="x", up=True),
                cslider(name="inc", label="Increment", min=1, max=25, init=1, rel="lin", res="int", unit="x", up=True),
                cslider(name="mix", label="Dry / Wet", min=0, max=1, init=1, rel="lin", unit="x", col="blue"),
                cpopup(name="fftsize", label="FFT Size", init="1024", value=["16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"], col="red"),
                cpopup(name="wtype", label="FFT Envelope", init="Hanning", col="red", value=["Rectangular", "Hamming", "Hanning", "Bartlett",
                            "Blackman 3", "Blackman 4", "Blackman 7", "Tuckey", "Sine"]),
                cpopup(name="overlaps", label="FFT Overlaps", init="4", col="red", value=["1", "2", "4", "8", "16"]),
                cpoly()
          ]

