-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiRealProAnalysis.py
More file actions
174 lines (142 loc) · 4.95 KB
/
Copy pathiRealProAnalysis.py
File metadata and controls
174 lines (142 loc) · 4.95 KB
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
166
167
168
169
170
171
172
173
from pyRealParser import Tune
from collections import defaultdict, namedtuple
from enum import Enum
import re
import os
from itertools import islice
KEYS = {
"C": "C D E F G A B",
"C-": "C D Eb F G Ab Bb",
"F": "F G A Bb C D E",
"F-": "F G Ab Bb C Db Eb",
"Bb": "Bb C D Eb F G A",
"Bb-": "Bb C Db Eb F Gb Ab",
"Eb": "Eb F G Ab Bb C D",
"Eb-": "Eb F Gb Ab Bb Cb Db",
"Ab": "Ab Bb C Db Eb F G",
"G#-": "G# A# B C# D# E F#",
"Db": "Db Eb F Gb Ab Bb C",
"C#-": "C# D# E F# G# A B",
"Gb": "Gb Ab Bb Cb Db Eb F",
"F#-": "F# G# A B C# D E",
"B": "B C# D# E F# G# A#",
"B-": "B C# D E F# G A",
"E": "E F# G# A B C# D#",
"E-": "E F# G A B C D",
"A": "A B C# D E F# G#",
"A-": "A B C D E F G",
"D": "D E F# G A B C#",
"D-": "D E F G A Bb C",
"G": "G A B C D E F#",
"G-": "G A Bb C D Eb F"
}
JAZZ1350 = "jazz1350.txt"
RHYTHM_CHANGES = "rhythmchanges.txt"
def get_tunes(file_name):
with open(file_name, "r") as file:
contents = file.read()
tunes = Tune.parse_ireal_url(contents)
return tunes
Chord = namedtuple("Chord", "key quality bass_note")
def split_chords_in_measure(bar):
chords = re.findall(r"([A-G][#b]?)(7|6|\^7|\^|-7|-|h7|o7?)?(/[A-G][#b]?)?", bar)
result = []
for (key, extension, slash_chord) in chords:
quality = extension_to_quality(extension)
bass_note = parse_bass_note(slash_chord)
result.append(Chord(key, quality, bass_note))
return result
def extension_to_quality(extension):
lookup = {
"": "Major",
"^": "Major",
"^7": "Major",
"6": "Major",
"7": "Dominant",
"-": "Minor",
"-7": "Minor",
"h7": "Half-Diminished",
"o": "Diminished",
"o7": "Diminished"
}
return lookup[extension]
def parse_bass_note(slash_chord):
if slash_chord[:1] == "/":
return slash_chord[1:]
else:
return None
def parse_chord(chord):
return split_chords_in_measure(chord)[0]
def distance_from_natural(note):
flats = len([x for x in note if x == "b"])
sharps = len([x for x in note if x == "#"])
return sharps - flats
def _strip_sharps_flats(note):
return note.replace("b", "").replace("#", "")
def _distance_to_sharps_flats(distance):
if distance > 0:
return "#" * distance
elif distance < 0:
return "b" * abs(distance)
else:
return ""
def convert_to_roman_numeral(key, chord):
roman_numerals = "I II III IV V VI VII".split(" ")
scale = KEYS[key].split(" ")
scale_notes = [_strip_sharps_flats(x) for x in scale]
scale_position = scale_notes.index(_strip_sharps_flats(chord.key))
distance = distance_from_natural(chord.key) - distance_from_natural(scale[scale_position])
return chord._replace(key=roman_numerals[scale_position] + _distance_to_sharps_flats(distance))
def short_form(chord):
assert isinstance(chord, Chord)
lookup = {
"Major": "M7",
"Dominant": "7",
"Minor": "m7",
"Half-Diminished": "-7b5",
"Diminished": "o7"
}
return chord.key + lookup[chord.quality]
# https://stackoverflow.com/a/6822773
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
if __name__ == "__main__":
tunes = get_tunes(JAZZ1350)
key_frequency = defaultdict(int)
chart_frequency = defaultdict(list)
not_quite_contrafacts = defaultdict(list)
two = defaultdict(list)
three = defaultdict(list)
four = defaultdict(list)
eight = defaultdict(list)
groupings = [(two, 2), (three, 3), (four, 4), (eight, 8)]
for tune in tunes:
assert isinstance(tune, Tune)
key_frequency[tune.key] += 1
chart_frequency[tune.chord_string].append(tune)
chords_in_tune = tuple(convert_to_roman_numeral(tune.key, chord) for measure in tune.measures_as_strings for chord in split_chords_in_measure(measure))
not_quite_contrafacts[chords_in_tune].append(tune)
for (coll, n) in groupings:
for group in window(chords_in_tune, n):
coll[" ".join(short_form(chord) for chord in group)].append(tune)
_ = """
for (k, v) in not_quite_contrafacts.items():
if len(v) > 1:
for tune in v:
print(tune.title + ": " + tune.key)
print()
#"""
for (coll, n) in groupings:
top_20 = islice(sorted(coll.items(), reverse=True, key=lambda x: len(x[1])), 20)
print("top 20 progressions of length: " + str(n))
for (progression, frequency) in top_20:
print(progression + " was found " + str(len(frequency)) + " times")
print()