segLibB40.py
author wim
Mon, 25 May 2020 10:39:18 +0200
changeset 1 7fd6cac1a69d
child 6 193999e56a90
permissions -rw-r--r--
- hard link to segLibB40 added

'''B40 versie
bevat B40lib, b40ana en b40krumm
zie ook b40krumm.py voor uitleg van b40 keyalgoritme.
'''
import re, math

major = [6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88]
minor = [6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17]
base_templates = [(0,12,23),(0,12,23,34),(0,11,23),(0,11,22,33),(0,11,22,34),(0,11,22)]
base_names = ['maj','dom7','min','fdim','hdim','dim']

def avg (xs):
    return sum (xs) / len (xs)

tab = [1,10,11,0,1,2,3,0,1,2,3,4,1,2,3,4,5,6,3,4,5,6,7,4,5,6,7,8,9,6,7,8,9,10,11,8,9,10,11,0]
acs = [2,-2,-1,0,1,2,0,-2,-1,0,1,2,0,-2,-1,0,1,2,-2,-1,0,1,2,0,-2,-1,0,1,2,0,-2,-1,0,1,2,0,-2,-1,0,1]
def b40pcHst (b40Hst):
    pcHst = [0.0 for i in range (12)]
    nacc = 0
    for ix, b40 in enumerate (b40Hst):
        if b40 == 0: continue
        pcHst [tab [ix]] += float (b40) # soms valt een Dbb op een C b.v. ex14, maat 5, 3e tel
        nacc += acs [ix] * b40
    return pcHst, nacc

# de b40 nummers van de 12 pitches [3,[4,8],9,[10,14],15,20,[21,25],26,[27,31],32,[33,37],38]
b12b40 = [3,4,9,10,15,20,21,26,27,32,33,38]
dkeus = {}.fromkeys ([1,3,6,8,10], 1)

def keyCor (b40Hst):
    pcHst, nacc = b40pcHst (b40Hst)
    pcHst [0] += 0.0001
    pc_mean = avg (pcHst)
    maj_mean = avg (major)
    min_mean = avg (minor)
    maj_dif_sq = sum ([(m - maj_mean)**2 for m in major])
    min_dif_sq = sum ([(m - min_mean)**2 for m in minor])
    pc_dif_sq  = sum ([(m -  pc_mean)**2 for m in pcHst])
    maj_noemer = math.sqrt (maj_dif_sq * pc_dif_sq)
    min_noemer = math.sqrt (min_dif_sq * pc_dif_sq)

    cor_maj = []; cor_min = []
    for i in range (12):
        maj_cor = min_cor = 0
        for j in range (12):
            k = (i + j) % 12
            maj_cor += (major[j] - maj_mean) * (pcHst[k] - pc_mean)
            min_cor += (minor[j] - min_mean) * (pcHst[k] - pc_mean)
        cor_maj.append (maj_cor / maj_noemer)
        cor_min.append (min_cor / min_noemer)

    ks = [(c,i,0) for i,c in enumerate (cor_maj)] + [(c,i,1) for i,c in enumerate (cor_min)]
    ks.sort ()
    ks.reverse ()
    ksb40 = []
    for c,b12,mnr in ks:
        b40 = b12b40 [b12]
        if b12 in dkeus and nacc < 0: # er zijn meer mollen dan kruisen
            b40 += 4                  # C# => Db (4 => 8) etc.
        ksb40.append ((c, b40, mnr))
    return ksb40

# the note names on the line of fifth in order of sharpness. The sharpness of the notes is:
# -13 (Fbb), -12, -11, ... 0 (Bb), 1, 2 (=C) , ... 21 (B##)
Lof = ['Fbb','Cbb','Gbb','Dbb','Abb','Ebb','Bbb','Fb','Cb','Gb','Db','Ab','Eb','Bb',
       'F','C','G','D','A','E','B','F#','C#','G#','D#','A#','E#','B#',
       'F##','C##','G##','D##','A##','E##','B##']
       
# returns the base40 number of a note (between 1 and 40) given the sharpness (between -13 and 21)
def loftobase40 (lof):
    return (lof + 12) * 23 % 40 + 1

# returns the sharpness of a note given the base40 number
def base40tolof (b40):
    return (b40 * 7 - 1) % 40 - 18

# returns the name of a b40 note number
def b40nm (b40):
    if b40 in [6,12,23,29,35]: name = '' # no note on these positions
    else:
        ix = base40tolof (b40) # the sharpness
        name = Lof [ix+13]     # +13 -> index in the line of fifth table
    return name

rc = re.compile (r'([-A-GX][b#]*)(.*)')
def splitCh (chnm):
    ro = rc.match (chnm)
    if not ro: raise '%s matcht niet' % chnm
    return ro.group (1), ro.group (2)

def mkTemplates ():
    global templates, accNames, freqTab
    templates = []
    accNames = []
    freqTab = [] # template index -> frequentie index = (5..0), 5 meest voorkomend type, 0 minst voorkomend
    for sh in range (-6,15):   # sharpness of 'Fb','Cb', Gb, ... F, C, G, ... D#, A#, E#, B#
        b40 = loftobase40 (sh) # base40 number = roots of chords
        for it, t in enumerate (base_templates):
            tx = tuple( [(ival + b40) % 40 for ival in t] )      # the b40 notes of the chord
            ex = tuple( [n for n in range (40) if not n in tx] ) # the notes notes not in the chord
            templates.append ((tx, ex))
            accNames.append (b40nm (b40) + base_names[it])
    for acc in accNames:
        chroot, type = splitCh (acc)
        freqIx = len (base_names) - base_names.index (type) # hoogste index voor meestvoorkomende chordtype
        freqTab.append (freqIx)

def score (iseg, jseg):
    w = getSegment (iseg, jseg)
    scores = []
    nseg = jseg - iseg + 1
    for it, (tx, ex) in enumerate (templates):
        notes = [w[i] for i in tx]
        fout = [w[i] for i in ex]
        mis = sum ([1 for n in notes if n == 0])
        s = sum (notes) - sum (fout) - mis
        #~ ps = [n for n in notes if n > 0]  # noot histogram van segment
        #~ if ps and min (ps) < nseg * 0.3: s -= 2 # niet gewogen (oud), bevoordeelt s01 t.o.v. s0 + s1
        #~ if ps and min (ps) * 4 < nseg: s -= int (round (0.1 * nseg)) # nieuw, gewogen
        rootw = notes[0] # root weight voor tie-break
        acc = accNames [it]
        freqIx = freqTab [it]
        scores.append ((s, rootw, freqIx, acc))
    scores.sort ()
    scores.reverse () # hoogste score, hoogste root-weight, hoogste freqIx komt bovenaan
    highest, _, _, acc = scores[0]
    return highest, acc, scores[:5] # score, acc-name

def readEvents (events, resolution=2): # list of [time, +/- midi note number]
    #~ events.sort ()
    tgroep = events[0][0]
    groep = []
    merged = []
    for t, p in events:
        if t - tgroep < resolution:
            groep.append (p)
            tgroep = t
        else:
            merged.append ((tgroep, groep))
            tgroep = t
            groep = [p]
    merged.append ((tgroep, groep))
    klinkt = []
    kgroep = []
    for t, g in merged:
        for p in g:
            if p > 0: klinkt.append (p)
            elif -p in klinkt: klinkt.remove (-p)
            else: print 'unmatched off-message'
        kgroep.append ((t, klinkt[:]))
    return kgroep

def mkWeights (kgroep):
    global weights
    weights = [[] for i in range (len (kgroep))]
    for ix, (t, g) in enumerate (kgroep):
        w = 40 * [0]
        for n in g: w [n % 40] += 1
        weights [ix] = w
    return weights

def getSegment (i, j):
    wtot = 40 * [0]
    for ws in weights [i:j+1]:
        for i, w in enumerate (ws):
            wtot [i] += w
    return wtot

def analyseK (kgroep, debug=0):
    if debug: print 'aantal segmenten', len (kgroep)

    #~ mkWeights (kgroep)
    #~ mkTemplates ()

    j0, j1, j2 = 0,1,2
    s0, acc0, xs0 = score (j0, j0)
    s1, acc1, xs1 = score (j1, j1)
    s01, acc01, xs01 = score (j0, j1)
    segs = []
    while j1 < len (weights):
        s2, acc2, xs2 = score (j2, j2)
        s12, acc12, xs12 = score (j1, j2)
        s012, acc012, xs012 = score (j0, j2)
        left = max ([s0+s12, s0+s1+s2])
        right = max ([s012, s01+s2])
        if left <= right:
            j1, j2 = j2, j2 + 1
            s0, acc0, xs0 = s01, acc01, xs01
            s01, acc01, xs01 = s012, acc012, xs012
        else:
            segs.append ((j0,j1,s0,acc0,xs0))
            if debug: print j0, j1, s0, acc0
            j0, j1, j2 = j1, j2, j2 + 1
            s0, acc0, xs0 = s1, acc1, xs1
            s01, acc01, xs01 = s12, acc12, xs12
        s1, acc1, xs1 = s2, acc2, xs2
    if j0 < len (weights):
        segs.append ((j0,j1,s0,acc0,xs0))
        if debug: print j0, j1, s0, acc0

    return segs, kgroep

if __name__ == '__main__':
    f = open ('mids/bwv539p.b40')
    xs = f.readlines ()
    f.close ()
    def toint (xs): return map (lambda x: int (x), xs)
    events = map (lambda x: toint (x.strip().split()), xs)
    kgroep = readEvents (events)
    mkWeights (kgroep)
    mkTemplates ()
    segs, kgroep = analyseK (kgroep, debug=0)
    print '%d segs, %d groepen' % (len (segs), len (kgroep))
    for iseg, jseg, score, acc, rest in segs:
        print '---', acc, score
        for t, ns in kgroep [iseg:jseg]:
            print t, ', '.join (map (b40nm, ns))