def play(voice_id):
    tel = bot.getTel()

    freqs = tune.fromdegrees([ dsp.randchoose([1, 2, 3, 5, 6, 8]) for f in range(dsp.randint(2, 5)) ], root='c', octave=dsp.randint(1, 3), ratios=tune.just)

    out = ''

    for freq in freqs:
        waveform = dsp.randchoose(['tri', 'sine2pi'])
        length = dsp.randint(dsp.mstf(10), dsp.mstf(300))
        #length = dsp.mstf(150)
        pulsewidth = dsp.rand()

        mod = dsp.breakpoint([ dsp.rand() for b in range(int(round(tel['density'])) + 3) ], 512)
        window = dsp.breakpoint([0] + [ dsp.rand() for b in range(int(round(tel['harmonicity'] * 2)) + 3) ] + [0], 512)
        waveform = dsp.breakpoint([0] + [ dsp.rand(-1, 1) for b in range(int(round(tel['roughness'] * 3)) + 3) ] + [0], 512)

        modRange = 0.005
        modFreq = dsp.rand(0.0001, 5)

        volume = dsp.rand(0.2, 0.3)

        if dsp.rand(0, 100) > 50:
            t = dsp.pulsar(freq, length, pulsewidth, waveform, window, mod, modRange, modFreq, volume)
        else:
            t = dsp.tone(length, freq, waveform)

        #t = dsp.pulsar(freq, length, pulsewidth, waveform, window, mod, modRange, modFreq, volume)
        #t = dsp.tone(length, freq, waveform)

        t = dsp.pan(t, dsp.rand())
        t = dsp.alias(t)

        t = dsp.amp(t, dsp.rand(0.5, 5.0))

        t = dsp.pad(t, 0, dsp.randint(dsp.mstf(1), dsp.mstf(500)))
        #t = dsp.pad(t, 0, dsp.mstf(100))
        t = dsp.amp(t, dsp.rand(0.5, 0.75))

        #out += dsp.env(t, 'sine')
        out += t

    #out = dsp.env(t, 'sine')

    dsp.log('')
    dsp.log('boone')
    dsp.log('%s length: %.2f' % (voice_id, dsp.fts(dsp.flen(out))))
    bot.show_telemetry(tel)

    return out
def play(voice_id):
    tel = bot.getTel()

    bpm = s.config('bpm')

    #if 'gentle' in tel['name'] or 'upbeat' in tel['name'] or 'full' in tel['name']:
        #dsp.log('')
        #dsp.log(voice_id + ' chirps silent')
        #return dsp.pad('', 0, dsp.stf(dsp.rand(1, 10)))

    length = int((1.0 / (tel['pace'] / 10.0)) * dsp.stf(3))

    def makecurve(length):
        # freq, length, pulsewidth, waveform, window, mod, modRange, modFreq, amp

        wf = dsp.breakpoint([0] + [ dsp.rand(-1, 1) for i in range(int(dsp.rand(5, 30))) ] + [0], 1024)
        win = dsp.breakpoint([0] + [ dsp.rand(0, 1) for i in range(4) ] + [0], 1024)
        mod = dsp.breakpoint([ dsp.rand(0, 1) for i in range(int(dsp.rand(4, 8))) ], 1024)

        smaxamp = dsp.rand(0.65, 0.95)
        amp = dsp.rand(0.01, smaxamp)

        pw = dsp.rand(0.1, 1.0)

        if 'upbeat' in tel['name']:
            wf = dsp.breakpoint([0] + [ dsp.rand(-1, 1) for i in range(int(dsp.rand(5, 30))) ] + [0], 1024)
            win = dsp.breakpoint([0] + [ dsp.rand(0, 1) for i in range(4) ] + [0], 1024)
            mod = dsp.breakpoint([ dsp.rand(0, 1) for i in range(int(dsp.rand(5, 80))) ], 1024)


            pw = 1.0

        if 'ballsout' in tel['name']:
            wf = dsp.breakpoint([0] + [ dsp.rand(-1, 1) for i in range(int(dsp.rand(10, 40))) ] + [0], 1024)
            win = dsp.breakpoint([0] + [ dsp.rand(0, 1) for i in range(10) ] + [0], 1024)
            mod = dsp.breakpoint([ dsp.rand(0, 1) for i in range(int(dsp.rand(20, 100))) ], 1024)


        if 'sparse' in tel['name']:
            amp = dsp.rand(0.7, 3)

        freq = tel['register'] * tel['roughness'] * (tel['density'] * tel['pace'] * 0.25)
        #if dsp.rand(0, 100) > 50:
            #freq = dsp.rand(2, 20)

        modR = tel['harmonicity']
        modF= tel['pace'] / 10.0

        c = dsp.pulsar(freq, length, pw, wf, win, mod, modR, modF, amp)

        ngrains = len(c)
        pans = dsp.breakpoint([ dsp.rand(0,1) for i in range(100) ], ngrains)

        maxPad = dsp.randint(2000, 4000)

        if 'sparse' in tel['name']:
            c = dsp.vsplit(c, dsp.mstf(0.5), dsp.mstf(tel['density'] * tel['harmonicity'] * tel['roughness'] * tel['pace']))
            c = [ dsp.randchoose(c) for i in range(int(dsp.rand(3, 10))) ]

        elif 'ballsout' in tel['name']:
            c = dsp.vsplit(c, dsp.mstf(0.1), dsp.mstf(400))
            c = dsp.packet_shuffle(c, dsp.randint(5, 10))

            speeds = dsp.breakpoint([ dsp.rand(tel['register'] * 0.1 + 0.2, tel['register'] + 0.2) for i in range(100) ], ngrains)

            c = [ dsp.transpose(cg, speeds[i]) for i, cg in enumerate(c) ]
            #c = [ dsp.amp(cg, dsp.rand(0.25, 1.25)) for i, cg in enumerate(c) ]

            for ic, cc in enumerate(c):
                if dsp.rand(0, 100) > 70:
                    c[ic] = dsp.tone(dsp.flen(cc), 11000, amp=0.5)

        elif 'upbeat' in tel['name']:
            beat = dsp.bpm2frames(bpm)
            c = dsp.split(c, beat)

            maxPad = 0

            c = [ dsp.pad(cg, 0, dsp.mstf(dsp.rand(10, beat / 4))) for i, cg in enumerate(c) ]

        else:
            c = dsp.vsplit(c, dsp.mstf(10), dsp.mstf(tel['density'] * tel['harmonicity'] * tel['roughness'] * tel['pace'] * dsp.rand(1, 10)))

        c = [ dsp.pan(cg, pans[i]) for i, cg in enumerate(c) ]
        c = [ dsp.env(cg, 'sine') for i, cg in enumerate(c) ]

        if 'sparse' in tel['name'] or 'ballsout' in tel['name']:
            speeds = dsp.breakpoint([ dsp.rand(0.5, 1.99) for i in range(100) ], ngrains)
            
            for ic, cc in enumerate(c):
                if dsp.flen(cc) < dsp.mstf(100):
                    c[ic] = cc + ''.join([ dsp.amp(cc, dsp.rand(0.1, 1.0)) for buh in range(dsp.randint(1, int(tel['density']))) ])

            c = [ dsp.transpose(cg, speeds[i]) for i, cg in enumerate(c) ]
            c = [ dsp.pan(cg, dsp.rand(0.0, 1.0)) for i, cg in enumerate(c) ]

        if 'ballsout' not in tel['name']:
            c = [ dsp.pad(cg, 0, dsp.mstf(dsp.rand(10, maxPad))) for i, cg in enumerate(c) ]

        out = ''.join(c)

        return out

    out = makecurve(length)

    if dsp.flen(out) > dsp.mstf(100) and dsp.rand(0, 100) > 30:
        out = dsp.drift(out, (tel['harmonicity'] - 10.0) * -1 * 0.5, dsp.randint(41, 441))

    if dsp.flen(out) > dsp.stf(10):
        out = dsp.fill(out, dsp.stf(10))

    if dsp.rand(0, 100) > 50:
        out = dsp.vsplit(out, 41, 441)
        for ii, o in enumerate(out):
            if dsp.rand(0, 100) > 50:
                out[ii] = dsp.pad('', 0, dsp.flen(o))
            elif dsp.rand(0, 100) > 50:
                out[ii] = dsp.amp(o, dsp.rand(0.75, 3))

        out = [ dsp.pan(o, dsp.rand()) for o in out ]

        out = ''.join(out)

    dsp.log('')
    dsp.log('chirp')
    dsp.log('%s length: %.2f' % (voice_id, dsp.fts(dsp.flen(out))))
    bot.show_telemetry(tel)

    return out
def play(voice_id):
    tel = bot.getTel()

    if 'sparse' in tel['name'] or 'ballsout' in tel['name'] and dsp.rand(0, 100) > 5:
        dsp.log('')
        dsp.log(voice_id + ' pulsar silent')
        bot.show_telemetry(tel)
        #return dsp.pad('', 0, dsp.stf(dsp.rand(1, 10)))

    #####################
    # PARAMS 
    ##################### 
    volume = dsp.rand(0.4, 0.7)

    melodies = [[dsp.randchoose([1, 5, 6])]]

    if tel['density'] >= 4:
        melodies += [ [ dsp.randchoose([1, 2, 9, 5, 6]) for i in range(2) ] for m in range(dsp.randint(2, 4)) ]

    if tel['density'] >= 6:
        melodies += [ [ dsp.randchoose([1, 2, 9, 3, 4, 5, 6, 7]) for i in range(dsp.randint(3, 6)) ] for m in range(dsp.randint(2, 5)) ]

    try:
        notes = dsp.randchoose(melodies)
    except IndexError:
        dsp.log(melodies)
        notes = [1]

    if tel['harmonicity'] <= 5:
        octave = (tel['register'] / 10.0) * dsp.randint(1, 3) + 1
    else:
        octave = int(round((tel['register'] / 10.0) * dsp.randint(1, 3) + 1))

    bpm         = p(voice_id, 'bpm', 80.0)

    if 'ballsout' in tel['name']:
        length = int((1.0 / (tel['pace'] / 10.0)) * dsp.stf(3))
    else:
        length = int((1.0 / (tel['pace'] / 10.0)) * dsp.stf(4)) + dsp.stf(dsp.rand(0.25, 1))

    # Cap voice length at 60 secs
    if length > dsp.stf(60):
        length = dsp.stf(60)

    env         = 'sine'

    mod         = p(voice_id, 'mod', 'random')
    modFreq     = p(voice_id, 'modfreq', dsp.rand(1.0, 2.5) / dsp.fts(length))

    modRange    = dsp.rand(0.01, 0.04)
    modRange    = dsp.rand(0, modRange)

    pulsewidth = 1.0 / (tel['roughness'] / 10.0)
    pulsewidth -= dsp.rand(0, 0.09)
    if pulsewidth < 0.1:
        pulsewidth = 0.1

    beat = dsp.bpm2frames(bpm)
 
    try:
        freqs   = tune.fromdegrees(notes, octave=octave, ratios=tune.just, root='c')
    except TypeError:
        print 'hm', notes, octave
        freqs = tune.fromdegrees([1,5], octave=octave, ratios=tune.just, root='c')

    #####################
    # NORMAL 
    ##################### 
    numgrains = int(dsp.rand(50, 500))

    if tel['roughness'] <= 2:
        window      = 'sine'
        waveform    = 'sine2pi'
        minplen     = dsp.ftms(length / 4)
        maxplen     = 30000

    elif tel['roughness'] <= 4:
        window      = 'sine'
        waveform    = 'sine2pi'
        minplen     = 10
        maxplen     = 350

    elif tel['roughness'] > 4:
        window      = 'tri'
        waveform    = 'tri'
        minplen     = 10 
        maxplen     = 120

    elif tel['roughness'] >= 7:
        window      = 'vary'
        waveform    = 'tri'
        minplen     = 5
        maxplen     = 80

    #mod = dsp.wavetable(mod, 512)
    mod = dsp.breakpoint([ dsp.rand() for b in range(int(round(tel['density'])) + 3) ], 512)

    #window = dsp.wavetable(window, 512)
    window = dsp.breakpoint([0] + [ dsp.rand() for b in range(int(round(tel['harmonicity'] * 2)) + 3) ] + [0], 512)

    #waveform = dsp.wavetable(waveform, 512)
    waveform = dsp.breakpoint([0] + [ dsp.rand(-1, 1) for b in range(int(round(tel['roughness'] * 3)) + 3) ] + [0], 512)

    pc = dsp.breakpoint([ dsp.rand(0, 1) for i in range(int(dsp.rand(5, numgrains))) ], numgrains)

    out = ''
    outlen = 0
    count = 0

    bar = dsp.randint(4, 8)

    dobeats = tel['density'] > 5 and dsp.rand(0, 100) > 20
    dobeats = False

    if dobeats:
        numbeats = bar * dsp.randint(3, 7)

    while outlen < length:
        layers = []

        if dobeats:
            plen = beat / dsp.randint(1, 10)
        else:
            plen = dsp.mstf(dsp.rand(minplen, maxplen))

        if tel['register'] >= 6 and tel['density'] >= 4:
            maxo = dsp.randint(2, 6)
        else:
            maxo = 1

        if dobeats:
            freqs = dsp.randshuffle(freqs)
            for b in range(numbeats):
                f = freqs[b % len(freqs)] 
                if dsp.rand(0, 100) > 70:
                    f *= 2**dsp.randint(0, maxo)

                b = dsp.pulsar(f, plen, pulsewidth, waveform, window, mod, modRange, modFreq, volume)
                b = dsp.pan(b, dsp.rand())

                out += b
                outlen += dsp.flen(b)

        else:
            for iff, freq in enumerate(freqs):
                if 'gentle' in tel['name']:
                    volume = dsp.rand(0.5, 0.6)
                elif 'sparse' in tel['name']:
                    volume = dsp.rand(0.3, 0.6)
                elif 'upbeat' in tel['name'] or 'ballsout' in tel['name']:
                    volume = dsp.rand(0.5, 0.6)
                else:
                    volume = dsp.rand(0.4, 0.6)

                if dsp.rand(0, 100) > 60:
                    freq *= 2**dsp.randint(0, maxo)

                layer = dsp.pulsar(freq, plen, pulsewidth, waveform, window, mod, modRange, modFreq, volume)

                layer = dsp.env(layer, 'sine')
                layer = dsp.pan(layer, dsp.rand())

                layers += [ layer ]

            layer = dsp.mix(layers)
            out += layer
            outlen += dsp.flen(layer)

            count += 1

    out = dsp.env(out, 'sine')

    #if dsp.flen(out) > dsp.mstf(100) and dsp.rand(0, 100) > 50:
        #out = dsp.drift(out, (tel['harmonicity'] - 10.0) * -1 * 0.1, dsp.randint(4, 441))

    dsp.log('')
    dsp.log('pulsar')
    dsp.log('%s length: %.2f' % (voice_id, dsp.fts(dsp.flen(out))))
    bot.show_telemetry(tel)

    return out