class Module(BaseModule):
    """
    "Table-based transposition module with multiple voices"
    
    Description

    This module implements five sound loopers playing in parallel.
    Loopers are table-based so a change in pitch will produce a 
    change in sound duration. This can be useful to create rhythm
    effects as well as harmonic effects.

    Sliders
    
        # Transpo Voice 1 : 
            Playback speed of the first voice
        # Gain Voice 1 : 
            Gain of the transposed first voice
        # Transpo Voice 2 : 
            Playback speed of the second voice
        # Gain Voice 2 : 
            Gain of the transposed second voice
        # Transpo Voice 3 : 
            Playback speed of the third voice
        # Gain Voice 3 : 
            Gain of the transposed third voice
        # Transpo Voice 4 : 
            Playback speed of the fourth voice
        # Gain Voice 4 : 
            Gain of the transposed fourth voice
        # Transpo Voice 5 : 
            Playback speed of the fifth voice
        # Gain Voice 5 : 
            Gain of the transposed fifth voice
    
    Graph Only
    
        # Overall Amplitude : 
            The amplitude curve applied on the total duration of the performance

    Popups & Toggles
    
        # Voice Activation 1 --> 5 :
            Mute or unmute each voice independently
        # 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.addFilein("snd")
        self.factor1 = CentsToTranspo(self.transpo1)
        self.factor2 = CentsToTranspo(self.transpo2)
        self.factor3 = CentsToTranspo(self.transpo3)
        self.factor4 = CentsToTranspo(self.transpo4)
        self.factor5 = CentsToTranspo(self.transpo5)
        self.phasor1 = Phasor(self.snd.getRate()*self.factor1)
        self.phasor2 = Phasor(self.snd.getRate()*self.factor2)
        self.phasor3 = Phasor(self.snd.getRate()*self.factor3)
        self.phasor4 = Phasor(self.snd.getRate()*self.factor4)
        self.phasor5 = Phasor(self.snd.getRate()*self.factor5)
        self.mul1 = DBToA(self.gain1, mul=self.onoffv1_value)
        self.mul2 = DBToA(self.gain2, mul=self.onoffv2_value)
        self.mul3 = DBToA(self.gain3, mul=self.onoffv3_value)
        self.mul4 = DBToA(self.gain4, mul=self.onoffv4_value)
        self.mul5 = DBToA(self.gain5, mul=self.onoffv5_value)
        self.voice1 = Pointer(self.snd, self.phasor1, mul=self.mul1*0.2)
        self.voice2 = Pointer(self.snd, self.phasor2, mul=self.mul2*0.2)
        self.voice3 = Pointer(self.snd, self.phasor3, mul=self.mul3*0.2)
        self.voice4 = Pointer(self.snd, self.phasor4, mul=self.mul4*0.2)
        self.voice5 = Pointer(self.snd, self.phasor5, mul=self.mul5*0.2)
        self.mixxx = self.voice1+self.voice2+self.voice3+self.voice4+self.voice5
        self.out = self.mixxx*self.env

    def onoffv1(self, value):
        self.mul1.mul = value
    
    def onoffv2(self, value):
        self.mul2.mul = value
    
    def onoffv3(self, value):
        self.mul3.mul = value
    
    def onoffv4(self, value):
        self.mul4.mul = value
    
    def onoffv5(self, value):
        self.mul5.mul = value
            
Interface = [   cfilein(name="snd"), 
                cgraph(name="env", label="Overall Amplitude", func=[(0,1),(1,1)], col="blue"),
                cslider(name="transpo1", label="Transpo Voice 1", min=-4800, max=4800, init=0, rel="lin", unit="cnts", col="green",half=True),
                cslider(name="gain1", label="Gain Voice 1", min=-48, max=18, init=0, rel="lin", unit="dB", col="green",half=True),
                cslider(name="transpo2", label="Transpo Voice 2", min=-4800, max=4800, init=10, rel="lin", unit="cnts", col="blue",half=True),
                cslider(name="gain2", label="Gain Voice 2", min=-48, max=18, init=0, rel="lin", unit="dB", col="blue",half=True),
                cslider(name="transpo3", label="Transpo Voice 3", min=-4800, max=4800, init=400, rel="lin", unit="cnts", col="orange",half=True),
                cslider(name="gain3", label="Gain Voice 3", min=-48, max=18, init=0, rel="lin", unit="dB", col="orange",half=True),
                cslider(name="transpo4", label="Transpo Voice 4", min=-4800, max=4800, init=-300, rel="lin", unit="cnts", col="blue4",half=True),
                cslider(name="gain4", label="Gain Voice 4", min=-48, max=18, init=0, rel="lin", unit="dB", col="blue4",half=True),
                cslider(name="transpo5", label="Transpo Voice 5", min=-4800, max=4800, init=-800, rel="lin", unit="cnts", col="green4",half=True),
                cslider(name="gain5", label="Gain Voice 5", min=-48, max=18, init=0, rel="lin", unit="dB", col="green4",half=True),
                ctoggle(name="onoffv1", label="Voice Activation 1 --> 5", init=1, stack=True, col="green"),
                ctoggle(name="onoffv2", label="", init=1, stack=True, col="green"),
                ctoggle(name="onoffv3", label="", init=1, stack=True, col="green"),
                ctoggle(name="onoffv4", label="", init=1, stack=True, col="green"),
                ctoggle(name="onoffv5", label="", init=1, stack=True, col="green"),
          ]
