import random
class Module(BaseModule):
    """
    "Additive synthesis module"
    
    Description

    An all featured additive synthesis module.

    Sliders
    
        # Base Frequency : 
            Base pitch of the synthesis
        # Partials Spread : 
            Distance between partials
        # Partials Freq Rand Amp : 
            Amplitude of the jitter applied on the partials pitch
        # Partials Freq Rand Speed : 
            Frequency of the jitter applied on the partials pitch
        # Partials Amp Rand Amp : 
            Amplitude of the jitter applied on the partials amplitude
        # Partials Amp Rand Speed : 
            Frequency of the jitter applied on the partials amplitude
        # Amplitude Factor : 
            Spread of amplitude between partials
        # Chorus Depth : 
            Amplitude of the chorus
        # Chorus Feedback : 
            Amount of chorused signal fed back to the chorus
        # Chorus Dry / Wet : 
            Mix between the original synthesis and the chorused signal
    
    Graph Only
    
        # Overall Amplitude : 
            The amplitude curve applied on the total duration of the performance

    Popups & Toggles
    
        # Num of Partials : 
            Number of partials present
        # Wave Shape : 
            Shape used for the synthesis
        # Custom Wave : 
            Define a custom wave shape by entering amplitude values

    """
    def __init__(self):
        BaseModule.__init__(self)
        self.customtable = self.custom_value
        self.wavetable = HarmTable(size=8192)
        self.polyfreqs = self.polyphony_spread
        self.ply = [i*self.freq for i in self.polyfreqs for j in range(self.nchnls)]
        self.out = OscBank(table=self.wavetable, freq=self.ply, spread=self.spread, slope=self.ampfactor, 
                            fjit=True, frndf=self.rndampspeedf, frnda=self.rndampf, arndf=self.rndampspeed, 
                            arnda=self.rndamp, num=int(self.num_value), mul=self.polyphony_scaling*self.env)

        #INIT
        self.wavedict = {'Sine':[1], 'Sawtooth':[1, 0.5, 0.333, 0.25, 0.2, 0.167, 0.143, 0.111, 0.1], 'Square':[1, 0, 0.333, 0, 0.2, 0, 0.143, 0, 0.111],
                            'Complex1':[1, 0, 0, 0, 0.3, 0, 0, 0, 0.2, 0, 0, 0.05], 'Complex2':[1, 0, 0, 0.3, 0, 0, 0.2, 0, 0, 0, 0, 0.1, 0, 0, 0.05, 0, 0, 0.02],
                            'Complex3':[1, 0, 0, 0.2, 0, 0.1, 0, 0, 0, 0.3, 0, 0.1, 0, 0, 0.05, 0, 0, 0.1, 0, 0.05, 0, 0, 0, 0.05, 0, 0, 0.02],
                            'Custom':self.customtable}

        self.shape(self.shape_index, self.shape_value)

    def shape(self, index, value):
        self.wavetable.replace(self.wavedict[value])

    def custom(self, value):
        self.wavedict['Custom'] = value

Interface = [   cgraph(name="env", label="Overall Amplitude", func=[(0,1),(1,1)], col="blue"),
                cslider(name="freq", label="Base Frequency", min=10, max=10000, init=150, rel="log", unit="Hz", col="blue"),
                cslider(name="spread", label="Partials Spread", min=0.0001, max=4, init=0.003, rel="log", unit="x", col="red3"),
                cslider(name="rndampspeedf", label=" Freq Rand Speed", min=0.0001, max=100, init=1, rel="log", unit="Hz", col="blue3",half=True),
                cslider(name="rndampspeed", label=" Amp Rand Speed", min=0.0001, max=20, init=1, rel="log", unit="Hz", col="green3",half=True),
                cslider(name="rndampf", label=" Freq Rand Amp", min=0.0001, max=1, init=0.02, rel="log", unit="x", col="blue3",half=True),
                cslider(name="rndamp", label=" Amp Rand Amp", min=0.0001, max=1, init=0.01, rel="log", unit="x", col="green3",half=True),
                cslider(name="ampfactor", label="Amplitude Factor", min=0.5, max=1, init=0.85, rel="lin", unit="x", col="green"),
                cpopup(name="num", label="Num of Partials", init="20", col="grey", rate="i", value=["5","10","20","40","80","160","320","640"]),
                cpopup(name="shape", label="Wave Shape", init="Square", col="green", value=["Sine","Sawtooth","Square","Complex1", "Complex2", "Complex3", "Custom"]),
                cgen(name="custom", label="Custom Wave", init=[1,0,0.5,0.3,0,0,0.2,0,0.1,0,0.09,0,0.05], popup=("shape", 6), col="green2"),
                cpoly()
          ]
