Ejemplo n.º 1
0
def centMel(phr):
    
    keystr = lv.bestkey(phr)
    root = int(phrase.roots[string.lower(keystr.split(" ")[0])])
    keyl = phrase.key(keystr)
    ind = keyl.index(root)
    top3 = [keyl[ind]%12, keyl[ind+4]%12, keyl[ind+2]]
    
    ns = []
    ts = []
    for i in range(len(phr)):
        if phr.n[i] < 0:
            continue
        ns.append(phr.n[i])
        ts.append(phr.t[i])
        if i < len(phr.t)-1 and phr.n[i+1] == -1:
            t = ts.pop()
            ts.append(t + phr.t[i+1])
    
    inbest = 0
    ttot = 0
    for i in range(len(ns)):
        if ns[i] in top3:
            inbest += ts[i]
        ttot += ts[i]
    
    return inbest/ttot
Ejemplo n.º 2
0
def makeprog(keystr, *args): #untested
    prog = phrase.Progression()
    
    rootN = phrase.roots[keystr.split(" ")[0]]
    keynotes = phrase.key(keystr)
    rooti = keynotes.index(rootN) - 1  #-1 because then, root + 1 (for first degree) gives index of first degree
    
    for i in args:
        c = phrase.Chord([keynotes[rooti+i],keynotes[rooti+i+2], keynotes[rooti+i+4]])
        prog.append((c, 4))
    
    return prog        
Ejemplo n.º 3
0
def closestrandinkey(n, keystr, prob=.5): #untested
    vals = phrase.key(keystr)
    #print "find it in key", n, vals
    if(n in vals):
        return n
    else:
        m = 0
        while not(vals[m] < n and n < vals[m+1]):
            m += 1
        if(random.uniform(0, 1) < prob):
            return vals[m]
        else:
            return vals[m+1]
Ejemplo n.º 4
0
def keymorph(phr, keystr, n, *args): #untested
    keyN = phrase.key(keystr)
    p = phrase.Phrase(phr)
    if(len(args) != 0):
        spread = args[0]
    else: spread = 3
    for i in range(n):
        l = random.randint(0, len(phr)-1)
        #print "i,", l
        ind = keyN.index(closestrandinkey(p.n[l], keystr))
        new = random.choice(keyN[ind-spread:ind] + keyN[ind+1:spread+1]) #base spread range on p or phr? p allows for some crazy deviation at high n
        #print p.n[l], new
        p.n[l] = new
    return p 
Ejemplo n.º 5
0
def chordvary(c, keystring):
    chord = phrase.Chord(c)
    
    keynotes = phrase.key(keystring)
    rootI = keynotes.index(chord.root)
    addons = [7, 9, 11, 13]
    numremove = random.randint(2,3)
    for i in range(numremove): #removes 2-3 elements from addons 
        addons.remove(random.choice(addons))
    for i in addons:
        chord.append(keynotes[rootI-1+i])
    inverts = random.randint[-2, 2]
    chord.invert(inverts)
    
    return chord
Ejemplo n.º 6
0
def supermorph(phr, n, keystr=None, nORr=1.0, keyORno=0, stableL=1, addP=.5, flatspread=5, keyspread=3): #untested
    if(keystr == None):
        keystr = string.lower(bestkey(phr)) + " major" 
    keyN = phrase.key(keystr)
    ts = [2, 4, 4, 8, 8, 16, 16]
    p = phrase.Phrase(phr)
    for i in range(n):
        ind = random.randint(0, len(p)-1)
        #print "i,", i, ind
        if(random.uniform(0, 1) < nORr):
            #notes
                if(random.uniform(0, 1) < keyORno):
                    #key
                    if(random.uniform(0, 1) < stableL):
                        #stable 
                        p = keymorph(p, keystr, 1, keyspread)
                    else:
                        #add/remove
                        if(random.uniform(0, 1) < addP):
                            #add
                            keyind = keyN.index(closestrandinkey(p.n[ind], keystr))
                            note = random.choice(keyN[keyind-keyspread:keyind+keyspread])
                            time = random.choice(ts)
                            p.insert(ind, (note, time))
                        else:
                            #subtract
                            p.pop(ind)
                else:
                    #nonkey
                    if(random.uniform(0, 1) < stableL):
                        #stable
                        p = morphN(p, 1, flatspread)
                    else:
                        #add/remove
                        if(random.uniform(0, 1) < addP):
                            #add
                            note = random.randint(p.n[ind]-flatspread, p.n[ind]+flatspread)
                            time = random.choice(ts)
                            p.insert(ind, (note, time))
                        else:
                            #subtract
                            p.pop(ind)
        else:
            p = morphT(p, 1)
    return p
Ejemplo n.º 7
0
def randfittokey(phrCrd, keystr):
    if(phrCrd.type == "phrase"):
        p = phrase.Phrase(phrCrd)
    if(phrCrd.type == "chord"):
        p = phrase.Chord(phrCrd)
    
    vals = phrase.key(keystr)
    
    for i in range(len(p.n)):
        if(not p.n[i] in vals):
            m = 0
            while not(vals[m] < p.n[i] and p.n[i] < vals[m+1]):
                m += 1
            if(random.choice([True, False])):
                p.n[i] = vals[m]
            else:
                p.n[i] = vals[m+1]
    return p
Ejemplo n.º 8
0
def macroMel(phr):
    
    key = phrase.key(lv.bestkey(phr))
    ns = []
    ts = []
    for i in range(len(phr)):
        if phr.n[i] < 0:
            continue
        ns.append(phr.n[i])
        ts.append(phr.t[i])
        if i < len(phr.t)-1 and phr.n[i+1] == -1:
            t = ts.pop()
            ts.append(t + phr.t[i+1])
    inkey = 0
    ttot = 0
    for i in range(len(ns)):
        if ns[i] in key:
            inkey += ts[i]
        ttot += ts[i]
    ratio = 1.0 * inkey/ttot
    
    tpair = {}

    for i in range(len(ns)):
        if ns[i] not in tpair:
            tpair[ns[i]] = ts[i]
        else:
             tpair[ns[i]] += ts[i]
    
    ctot = 0
    for i in tpair.values():
        ctot += i
    cavg = 1.0 * ctot / len(tpair)
    
    varn = 0
    for i in tpair.values():
        varn += (i-cavg)**2
    if varn == 0:
        return 2 * ratio / (1.0 / len(phr))
    return ratio / varn
Ejemplo n.º 9
0
def supermorph(phr, n, keystr=None, nORr=.5, keyORno=.8, stableL=.7, addP=.5, flatspread=5, keyspread=3): #untested
    if(keystr == None):
        keystr = string.lower(bestkey(phr)) + " major" 
    keyN = phrase.key(keystr)
    ts = [2, 1, 1, .5, .5, .25, .25]
    p = phrase.Phrase(phr)
    for i in range(n):
        ind = random.randint(0, len(p)-1)
        #print "i,", i, ind
        if(random.uniform(0, 1) < nORr):
            #notes
                if(random.uniform(0, 1) < keyORno):
                    #key
                    if(random.uniform(0, 1) < stableL):
                        #stable 
                        p = keymorph(p, keystr, 1, keyspread)
                        er = [i > 2 for i in p.t]
                        if True in er:
                            print "err hurr1"
                            return p
                    else:
                        #add/remove
                        if(random.uniform(0, 1) < addP):
                            #add
                            keyind = keyN.index(closestrandinkey(p.n[ind], keystr))
                            #print keyind, "keyind"
                            note = random.choice(keyN[max(keyind-keyspread,0):max(keyind+keyspread,len(keyN))])
                            time = random.choice(ts)
                            p.insert(ind, (note, time))
                            er = [i > 2 for i in p.t]
                            if True in er:
                                print "err hurr2"
                                return p
                        else:
                            #subtract
                            p.pop(ind)
                else:
                    #nonkey
                    if(random.uniform(0, 1) < stableL):
                        #stable
                        p = morphN(p, 1, flatspread)
                        er = [i > 2 for i in p.t]
                        if True in er:
                            print "err hurr3"
                            return p 
                    else:
                        #add/remove
                        if(random.uniform(0, 1) < addP):
                            #add
                            note = random.randint(p.n[ind]-flatspread, p.n[ind]+flatspread)
                            time = random.choice(ts)
                            p.insert(ind, (note, time))
                            er = [i > 2 for i in p.t]
                            if True in er:
                                print "err hurr4"
                                return p
                        else:
                            #subtract
                            p.pop(ind)
        else:
            p = morphT(p, 1)
    return p
Ejemplo n.º 10
0
def makechrdmel(keystr, prog, *args):  #untested
    
    times = [2, 4, 4, 8, 8, 16, 16]
    t = []
    
    if(len(args) == 0):
        sum = 0
        for i in prog.t:
            sum += 1.0/i
        #print sum, "total prog sum"
        while(sum >= 1.0/2):
            t.append(times[random.randint(0, 6)])
            sum -= 1.0/t[len(t)-1]
            #print sum, "sum2"
        while(sum >= 1.0/4):
            t.append(times[random.randint(1, 6)])
            sum -= 1.0/t[len(t)-1]
            #print sum, "sum4"
        while(sum >= 1.0/8):
            t.append(times[random.randint(3, 6)])
            sum -= 1.0/t[len(t)-1]
            #print sum, "sum8"
        while(sum >= 1.0/16):
            t.append(times[random.randint(5, 6)])
            sum -= 1.0/t[len(t)-1]
            #print sum, "sum16"
        n = [i for i in t]
        p = phrase.Phrase(n, t)
    if(len(args) == 1):
        t = args[0].t
        p = args[0]
    
    tsum = [1.0/i for i in t]
    print sum, tsum, "sum compare"
    
    divs = findsets(prog, p)
    keynotes = phrase.key(keystr)
    phsum = 0
    prsum = 0
    n = []
    print keynotes
    print
    for i in range(len(divs)-1):
        root = prog.c[i].root
        rootI = keynotes.index(root) #what if notes in prog really high/low, out of keynotes range?
        #print rootI, "rootI", keynotes[rootI], keynotes[rootI+4]
        prsum += 1.0/prog.t[i]
        for k in range(divs[i], divs[i+1]):
            r = random.randint(rootI, rootI+4)
            #print r, "randIndex", keynotes[r]
            n.append(keynotes[r])
            phsum += 1.0/t[k] 
        if(phsum > prsum): # does not "mix" last note in section if it doesnt overlap, can add that condition here to fix it
            old15 = [j%12 for j in keynotes[rootI:rootI+5]]
            oldnotes = keynotes[rootI:rootI+5]
            root = prog.c[i+1].root
            rootI = keynotes.index(root)
            new15 = [j%12 for j in keynotes[rootI:rootI+5]]
            intersect = []
            for j in range(len(old15)):
                if old15[j] in new15:
                    intersect.append(oldnotes[j]) 
            print "intersect", intersect
            n[len(n)-1] = intersect[random.randint(0, len(intersect)-1)]
    
    p = phrase.Phrase(n, t)
    return p