xmlChordAna.py
author wim
Mon, 25 May 2020 10:39:18 +0200
changeset 1 7fd6cac1a69d
parent 0 4896b49e870a
child 2 775a1d0d3be6
permissions -rw-r--r--
- hard link to segLibB40 added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
4896b49e870a initial commit
wim
parents:
diff changeset
     1
import fractions as F
4896b49e870a initial commit
wim
parents:
diff changeset
     2
import xml2b40
4896b49e870a initial commit
wim
parents:
diff changeset
     3
import segLibB40 as S
4896b49e870a initial commit
wim
parents:
diff changeset
     4
4896b49e870a initial commit
wim
parents:
diff changeset
     5
def keySeg (iseg,jseg):
4896b49e870a initial commit
wim
parents:
diff changeset
     6
    hist = S.getSegment (iseg, jseg)
4896b49e870a initial commit
wim
parents:
diff changeset
     7
    ks = S.keyCor (hist)
4896b49e870a initial commit
wim
parents:
diff changeset
     8
    c1, key, m = ks [0] # correlation coeff, key, major/minor
4896b49e870a initial commit
wim
parents:
diff changeset
     9
    c2, _, _ = ks [1]   # second best key
4896b49e870a initial commit
wim
parents:
diff changeset
    10
    conf = 300 * (c1 - c2) / c2
4896b49e870a initial commit
wim
parents:
diff changeset
    11
    if conf > 100: conf = 10
4896b49e870a initial commit
wim
parents:
diff changeset
    12
    return conf, S.b40nm (key) + (m and 'm' or ' ')
4896b49e870a initial commit
wim
parents:
diff changeset
    13
4896b49e870a initial commit
wim
parents:
diff changeset
    14
def mkAna (events, fnm):
4896b49e870a initial commit
wim
parents:
diff changeset
    15
    kgroep = S.readEvents (events)
4896b49e870a initial commit
wim
parents:
diff changeset
    16
    tikkenPerKwart = 384
4896b49e870a initial commit
wim
parents:
diff changeset
    17
    S.mkWeights (kgroep)
4896b49e870a initial commit
wim
parents:
diff changeset
    18
    conf, key = keySeg (0, len (kgroep)-1)
4896b49e870a initial commit
wim
parents:
diff changeset
    19
    tonalcentre = key[:2] if key[1] in 'b#' else key[:1]  # cut minor from key
4896b49e870a initial commit
wim
parents:
diff changeset
    20
    S.mkTemplates ()
4896b49e870a initial commit
wim
parents:
diff changeset
    21
    segs, kgroep = S.analyseK (kgroep, debug=0)
4896b49e870a initial commit
wim
parents:
diff changeset
    22
    regels = []
4896b49e870a initial commit
wim
parents:
diff changeset
    23
    regels.append ('File: %s, ticks per quarternote: %d' % (fnm, tikkenPerKwart))
4896b49e870a initial commit
wim
parents:
diff changeset
    24
    regels.append ('%d note groups, %d chord segments' % (len (kgroep), len (segs)))
4896b49e870a initial commit
wim
parents:
diff changeset
    25
    regels.append ('Key: %s, confidence: %.0f%%' % (key, conf))
4896b49e870a initial commit
wim
parents:
diff changeset
    26
    for (kb, ke, score, chord, _) in segs:
4896b49e870a initial commit
wim
parents:
diff changeset
    27
        grpsInSeg = kgroep [kb:ke]
4896b49e870a initial commit
wim
parents:
diff changeset
    28
        totNotes = sum ([len (ns) for ts, ns in grpsInSeg])
4896b49e870a initial commit
wim
parents:
diff changeset
    29
        percnt = 100. * score / totNotes
4896b49e870a initial commit
wim
parents:
diff changeset
    30
        regels.append ('---- chord %s, score: %.0f%%' % (chord, percnt))
4896b49e870a initial commit
wim
parents:
diff changeset
    31
        for tikken, noten in grpsInSeg:
4896b49e870a initial commit
wim
parents:
diff changeset
    32
            noten = ', '.join ([S.b40nm (n) + str(n / 40) for n in noten])
4896b49e870a initial commit
wim
parents:
diff changeset
    33
            x = F.Fraction (tikken, tikkenPerKwart)
4896b49e870a initial commit
wim
parents:
diff changeset
    34
            n, d = x.numerator, x.denominator
4896b49e870a initial commit
wim
parents:
diff changeset
    35
            t = n / d
4896b49e870a initial commit
wim
parents:
diff changeset
    36
            r = x - t
4896b49e870a initial commit
wim
parents:
diff changeset
    37
            if r == 0: r = ''
4896b49e870a initial commit
wim
parents:
diff changeset
    38
            regels.append ('%4d %3s %s' % (t, r, noten))
4896b49e870a initial commit
wim
parents:
diff changeset
    39
    return regels    
4896b49e870a initial commit
wim
parents:
diff changeset
    40
4896b49e870a initial commit
wim
parents:
diff changeset
    41
if __name__ == '__main__':
4896b49e870a initial commit
wim
parents:
diff changeset
    42
    fnm = 'mids/bwv539p_arr.xml'
4896b49e870a initial commit
wim
parents:
diff changeset
    43
    noten = xml2b40.xml2b40 (fnm)
4896b49e870a initial commit
wim
parents:
diff changeset
    44
    regels = mkAna (noten, fnm)
4896b49e870a initial commit
wim
parents:
diff changeset
    45
    print '\n'.join (regels)