xmlChordAna.py
author wim
Mon, 25 May 2020 12:34:01 +0200
changeset 2 775a1d0d3be6
parent 0 4896b49e870a
child 6 193999e56a90
permissions -rwxr-xr-x
- add commandline interface
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
     1
#!/usr/bin/env python
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
     2
# coding: latin-1
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
     3
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
     4
import fractions, os, xml2b40
0
4896b49e870a initial commit
wim
parents:
diff changeset
     5
import segLibB40 as S
4896b49e870a initial commit
wim
parents:
diff changeset
     6
4896b49e870a initial commit
wim
parents:
diff changeset
     7
def keySeg (iseg,jseg):
4896b49e870a initial commit
wim
parents:
diff changeset
     8
    hist = S.getSegment (iseg, jseg)
4896b49e870a initial commit
wim
parents:
diff changeset
     9
    ks = S.keyCor (hist)
4896b49e870a initial commit
wim
parents:
diff changeset
    10
    c1, key, m = ks [0] # correlation coeff, key, major/minor
4896b49e870a initial commit
wim
parents:
diff changeset
    11
    c2, _, _ = ks [1]   # second best key
4896b49e870a initial commit
wim
parents:
diff changeset
    12
    conf = 300 * (c1 - c2) / c2
4896b49e870a initial commit
wim
parents:
diff changeset
    13
    if conf > 100: conf = 10
4896b49e870a initial commit
wim
parents:
diff changeset
    14
    return conf, S.b40nm (key) + (m and 'm' or ' ')
4896b49e870a initial commit
wim
parents:
diff changeset
    15
4896b49e870a initial commit
wim
parents:
diff changeset
    16
def mkAna (events, fnm):
4896b49e870a initial commit
wim
parents:
diff changeset
    17
    kgroep = S.readEvents (events)
4896b49e870a initial commit
wim
parents:
diff changeset
    18
    tikkenPerKwart = 384
4896b49e870a initial commit
wim
parents:
diff changeset
    19
    S.mkWeights (kgroep)
4896b49e870a initial commit
wim
parents:
diff changeset
    20
    conf, key = keySeg (0, len (kgroep)-1)
4896b49e870a initial commit
wim
parents:
diff changeset
    21
    tonalcentre = key[:2] if key[1] in 'b#' else key[:1]  # cut minor from key
4896b49e870a initial commit
wim
parents:
diff changeset
    22
    S.mkTemplates ()
4896b49e870a initial commit
wim
parents:
diff changeset
    23
    segs, kgroep = S.analyseK (kgroep, debug=0)
4896b49e870a initial commit
wim
parents:
diff changeset
    24
    regels = []
4896b49e870a initial commit
wim
parents:
diff changeset
    25
    regels.append ('File: %s, ticks per quarternote: %d' % (fnm, tikkenPerKwart))
4896b49e870a initial commit
wim
parents:
diff changeset
    26
    regels.append ('%d note groups, %d chord segments' % (len (kgroep), len (segs)))
4896b49e870a initial commit
wim
parents:
diff changeset
    27
    regels.append ('Key: %s, confidence: %.0f%%' % (key, conf))
4896b49e870a initial commit
wim
parents:
diff changeset
    28
    for (kb, ke, score, chord, _) in segs:
4896b49e870a initial commit
wim
parents:
diff changeset
    29
        grpsInSeg = kgroep [kb:ke]
4896b49e870a initial commit
wim
parents:
diff changeset
    30
        totNotes = sum ([len (ns) for ts, ns in grpsInSeg])
4896b49e870a initial commit
wim
parents:
diff changeset
    31
        percnt = 100. * score / totNotes
4896b49e870a initial commit
wim
parents:
diff changeset
    32
        regels.append ('---- chord %s, score: %.0f%%' % (chord, percnt))
4896b49e870a initial commit
wim
parents:
diff changeset
    33
        for tikken, noten in grpsInSeg:
4896b49e870a initial commit
wim
parents:
diff changeset
    34
            noten = ', '.join ([S.b40nm (n) + str(n / 40) for n in noten])
2
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    35
            x = fractions.Fraction (tikken, tikkenPerKwart)
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    36
            t = x.numerator / x.denominator
0
4896b49e870a initial commit
wim
parents:
diff changeset
    37
            r = x - t
4896b49e870a initial commit
wim
parents:
diff changeset
    38
            if r == 0: r = ''
4896b49e870a initial commit
wim
parents:
diff changeset
    39
            regels.append ('%4d %3s %s' % (t, r, noten))
4896b49e870a initial commit
wim
parents:
diff changeset
    40
    return regels    
4896b49e870a initial commit
wim
parents:
diff changeset
    41
4896b49e870a initial commit
wim
parents:
diff changeset
    42
if __name__ == '__main__':
2
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    43
    from optparse import OptionParser
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    44
    parser = OptionParser (usage='%prog [-h] [-g TPQ] [--C5] <file1>')
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    45
    parser.add_option ("-t", action="store", type="int", help="ticks per quarternote", default=384, metavar='TPQ')
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    46
    parser.add_option ("--C5", action="store_true", help="central C is C5", default=False)
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    47
    options, args = parser.parse_args ()
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    48
    if len (args) < 1: parser.error ('file argument needed')
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    49
    fnmext = args [0]
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    50
    fnm, ext = os.path.splitext (fnmext)
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    51
    if ext != '.xml': parser.error ('.xml file needed file needed')
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    52
    if not os.path.exists (fnmext): parser.error ('%s does not exist' % fnmext)
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    53
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    54
    noten = xml2b40.xml2b40 (fnmext, options.t, options.C5)
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    55
    regels = mkAna (noten, fnmext)
775a1d0d3be6 - add commandline interface
wim
parents: 0
diff changeset
    56
    print '\n'.join (regels)