-
Notifications
You must be signed in to change notification settings - Fork 1
/
chordparser.py
165 lines (137 loc) · 3.66 KB
/
chordparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""This module contains functions for parsing tab files,
determining the key, and
converting to universal representation."""
import re
import sys
from operator import add, and_
from itertools import takewhile, imap
chord_re = re.compile(r">([A-Ga-g][b#]?)(m)?((?:maj)?[0-9])?(sus[0-9]|add[0-9])?(/[A-Ga-g][b#]?)?<")
text_line = re.compile(r"(^[aA] \w|\w a \w)")
def parse(file):
"""Search lyric/tab file for things tha look like chords.
Return a sequence of them."""
for line in file:
if not text_line.search(line):
for chord in chord_re.findall(line):
chord = list(chord)
chord[0] = chord[0].capitalize()
yield chord
number = {
"A":0,
"A#":1,
"Bb":1,
"B":2,
"B#":3,
"Cb":2,
"C":3,
"C#":4,
"Db":4,
"D":5,
"D#":6,
"Eb":6,
"E":7,
"E#":8,
"Fb":7,
"F":8,
"F#":9,
"Gb":9,
"G":10,
"G#":11,
"Ab":11,
}
roman = {
0:"I",
2:"ii",
4:"iii",
5:"IV",
7:"V",
9:"vi",
11:"vii",
}
chords = dict((v,k) for k, v in number.iteritems())
def minor(ch):
"""Transform ch to a minor chord"""
return ch[0] - 1, ch[1]
def major7th(ch):
"""Transform ch to a major sevent chord"""
return [ch[1], 11]
def minor7th(ch):
"""Transform ch to a minor sevent chord"""
return [ch[1], 10]
def noteify(chord):
"""Split chord into individual notes
Returns a triad."""
root = number[ chord[0] ]
quality = [4, 7]
if chord[1] == 'm':
quality = minor( quality )
if chord[2] == 'maj7':
quality = major7th( quality )
if chord[2] == '7':
quality = minor7th( quality )
return root, root+quality[0], root+quality[1]
def track_notes(pressed, ch):
"""Record in pressed the notes played in ch.
pressed is a list of notes"""
for n in ch:
pressed[n%12] += 1
major_scale = [2,2,1,2,2,2,1]
def reductions(f, init, l):
"""Like reduce,
returns all intermediate values."""
yield init
for v in l:
init = f(init, v)
yield init
scales = []
for root in range(12):
scale = reductions(add, root, major_scale)
scale = set([n%12 for n in scale])
scales.append(scale)
def convert_scale(pressed):
"""convert from a list of note counts
to a sequence with the id of the pressed keys"""
for i, n in enumerate(pressed):
if n:
yield i
def convert_universal( key, ch ):
"""translate chord from key to universal representation"""
return (number[ ch[0] ] - key) % 12, ch[2]
def ask_key(default=''):
"""if we can't figure out the key, we ask the user."""
i = raw_input("In which key?[%s]: " % default)
if i == '':
i = default
if i == '':
raise KeyError("no key")
return number[i]
def get_key(pressed):
"""Get the key from all the pressed keys.
If non-diatonic chords are found
or multiple possible keys are found, we ask the user"""
keys = []
s = set( convert_scale( pressed ))
for i, scale in enumerate(scales):
if s.issubset(scale):
keys.append(i)
if not keys:
key = ask_key()
elif len(keys) > 1:
key = ask_key(chords[keys[0]])
else:
key = keys[0]
return key
def get_universal(f):
"""Get all chords from file f
in universal format."""
pressed = [0] * 12
with open(f) as source:
chords = list(parse(source))
for n in chords:
track_notes(pressed, noteify(n))
key = get_key(pressed)
for n in chords:
yield convert_universal(key, n)
if __name__ == "__main__":
for uni in get_universal(sys.argv[1]):
print roman[uni[0]], uni[1]