-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenmidi.py
More file actions
321 lines (303 loc) · 11.8 KB
/
genmidi.py
File metadata and controls
321 lines (303 loc) · 11.8 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from midiutil.MidiFile import MIDIFile
class ChordProgressionGenerator:
def __init__(self, scale_root, LOWEST_OCTAVE):
OCTAVES = {1: 60 - 36, 2: 60 - 24, 3: 60 - 12, 4: 60, 5: 60 + 12}
self.scale_root = scale_root
self.LOWEST_OCTAVE = LOWEST_OCTAVE
self.rootlookup = {
"A": -3,
"A#": -2,
"B": -1,
"C": 0,
"C#": 1,
"D": 2,
"D#": 3,
"E": 4,
"F": 5,
"F#": 6,
"G#": 7,
}
i = self.rootlookup[self.scale_root]
c_dim = {
"C": OCTAVES[self.LOWEST_OCTAVE] - 1 + i,
"D": OCTAVES[self.LOWEST_OCTAVE] + 1 + i,
"E": OCTAVES[self.LOWEST_OCTAVE] + 2 + i,
"F": OCTAVES[self.LOWEST_OCTAVE] + 4 + i,
"G": OCTAVES[self.LOWEST_OCTAVE] + 6 + i,
"A": OCTAVES[self.LOWEST_OCTAVE] + 7 + i,
"B": OCTAVES[self.LOWEST_OCTAVE] + 9 + i,
}
c_aug = {
"C": OCTAVES[self.LOWEST_OCTAVE] + 1 + i,
"D": OCTAVES[self.LOWEST_OCTAVE] + 3 + i,
"E": OCTAVES[self.LOWEST_OCTAVE] + 5 + i,
"F": OCTAVES[self.LOWEST_OCTAVE] + 6 + i,
"G": OCTAVES[self.LOWEST_OCTAVE] + 7 + i,
"A": OCTAVES[self.LOWEST_OCTAVE] + 10 + i,
"B": OCTAVES[self.LOWEST_OCTAVE] + 12 + i,
}
self.c_min = {
"C": OCTAVES[self.LOWEST_OCTAVE] + i,
"D": OCTAVES[self.LOWEST_OCTAVE] + 2 + i,
"E": OCTAVES[self.LOWEST_OCTAVE] + 3 + i,
"F": OCTAVES[self.LOWEST_OCTAVE] + 5 + i,
"G": OCTAVES[self.LOWEST_OCTAVE] + 7 + i,
"A": OCTAVES[self.LOWEST_OCTAVE] + 8 + i,
"B": OCTAVES[self.LOWEST_OCTAVE] + 10 + i,
}
self.c_maj = {
"C": OCTAVES[self.LOWEST_OCTAVE] + i,
"D": OCTAVES[self.LOWEST_OCTAVE] + 2 + i,
"E": OCTAVES[self.LOWEST_OCTAVE] + 4 + i,
"F": OCTAVES[self.LOWEST_OCTAVE] + 5 + i,
"G": OCTAVES[self.LOWEST_OCTAVE] + 7 + i,
"A": OCTAVES[self.LOWEST_OCTAVE] + 9 + i,
"B": OCTAVES[self.LOWEST_OCTAVE] + 11 + i,
}
self.R_c_maj = {midi: note for note, midi in self.c_maj.items()}
self.c_maj_list = {1: "C", 2: "D", 3: "E", 4: "F", 5: "G", 6: "A", 7: "B"}
self.R_c_maj_list = {note: deg for deg, note in self.c_maj_list.items()}
# self.c_maj_low = {note: midi - 12 for note, midi in self.c_maj.items()}
# self.c_maj_hi = {note: midi + 12 for note, midi in self.c_maj.items()}
# c_maj_midi = {"C": 0, "D": 2, "E": 4, "F": 5, "G": 7, "A": 9, "B": 11}
# R_c_maj_midi = {midi: note for note, midi in c_maj_midi.items()}
def get_add_maj_min_sus_interval(self, midi, interval, majmin="maj"):
if interval == 2:
return midi + 2
elif interval == 4:
return midi + 4
elif interval == 7:
if majmin == "maj":
return midi + 11
if majmin == "min":
return midi + 10
elif interval == 9:
if majmin == "maj":
return midi + 14
if majmin == "min":
return midi + 13
else:
return midi
def get_maj_scale_chords(
self, scale_root, inversion="closed", add=[], dom=[], majmin="maj"
):
simple_maj_chords = {
"i": [self.c_min["C"], self.c_min["E"], self.c_min["G"]],
"I": [self.c_maj["C"], self.c_maj["E"], self.c_maj["G"]],
"ii": [self.c_maj["D"], self.c_maj["F"], self.c_maj["A"]],
"II": [self.c_maj["D"], self.c_maj["F"] + 1, self.c_maj["A"]],
"iii": [self.c_maj["E"], self.c_maj["G"], self.c_maj["B"]],
"III": [self.c_maj["E"], self.c_maj["G"] + 1, self.c_maj["B"]],
"iv": [self.c_maj["F"], self.c_maj["A"] - 1, self.c_maj["C"]],
"IV": [self.c_maj["F"], self.c_maj["A"], self.c_maj["C"]],
"v": [self.c_maj["G"], self.c_maj["B"] - 1, self.c_maj["D"]],
"V": [self.c_maj["G"], self.c_maj["B"], self.c_maj["D"]],
"vi": [self.c_maj["A"], self.c_maj["C"], self.c_maj["E"]],
"VI": [self.c_maj["A"], self.c_maj["C"] + 1, self.c_maj["E"]],
"viio": [self.c_maj["B"], self.c_maj["D"], self.c_maj["F"]],
"VIIaug": [self.c_maj["B"], self.c_maj["D"] + 1, self.c_maj["F"] + 1],
}
extended_maj_chords = {numeral: [] for numeral in simple_maj_chords.keys()}
for numeral, notes in simple_maj_chords.items():
extendednotes = [n for n in notes]
for note in add:
extendednotes.append(
self.get_add_maj_min_sus_interval(
extendednotes[0], note, majmin=majmin
)
)
for note in dom:
extendednotes.append(extendednotes[0] + 10) # only works for dominant7
extended_maj_chords.update({numeral: extendednotes})
inversions = {
"I": [0, 0, 0],
"i": [0, 0, 0],
"ii": [0, 0, 0],
"II": [0, 0, 0],
"iii": [0, 0, 0],
"III": [0, 0, 0],
"IV": [0, 0, 0],
"iv": [0, 0, 0],
"V": [0, 0, 0],
"v": [0, 0, 0],
"vi": [0, 0, 0],
"VI": [0, 0, 0],
"viio": [0, 0, 0],
"VIIaug": [0, 0, 0],
}
if inversion == "open":
inversions = {
"I": [-12, 12, 0],
"i": [-12, 12, 0],
"ii": [-12, 12, 0],
"II": [-12, 12, 0],
"iii": [-12, 0, 0],
"III": [-12, 0, 0],
"IV": [-12, 0, 0],
"iv": [-12, 0, 0],
"V": [-12, 0, -12],
"v": [-12, 0, -12],
"vi": [-12, 12, -12],
"VI": [-12, 12, -12],
"viio": [-12, 0, 12],
"VIIaug": [-12, 0, 12],
}
elif inversion == "first":
inversions = {
"I": [12, 0, 0],
"i": [12, 0, 0],
"ii": [12, 0, 0],
"II": [12, 0, 0],
"iii": [12, 0, 0],
"III": [12, 0, 0],
"IV": [12, 0, 0],
"iv": [12, 0, 0],
"V": [12, 0, 0],
"v": [12, 0, 0],
"vi": [12, 0, 0],
"VI": [12, 0, 0],
"viio": [12, 0, 0],
"VIIaug": [12, 0, 0],
}
elif inversion == "second":
inversions = {
"I": [12, 12, 0],
"i": [12, 12, 0],
"ii": [12, 12, 0],
"II": [12, 12, 0],
"iii": [12, 12, 0],
"III": [12, 12, 0],
"IV": [12, 12, 0],
"iv": [12, 12, 0],
"V": [12, 12, 0],
"v": [12, 12, 0],
"vi": [12, 12, 0],
"VI": [12, 12, 0],
"viio": [12, 12, 0],
"VIIaug": [12, 12, 0],
}
if dom or add:
extended_inv = {numeral: [] for numeral in inversions.keys()}
for numeral, change in inversions.items():
extended_chages = [c for c in change]
for d in dom:
if inversion == "first" or inversion == "second":
extended_chages.append(12)
elif inversion == "cluster":
extended_chages.append(-12)
else:
extended_chages.append(0)
for a in add:
if inversion == "first" or inversion == "second":
extended_chages.append(12)
elif inversion == "cluster":
extended_chages.append(-12)
else:
extended_chages.append(0)
extended_inv.update({numeral: extended_chages})
if inversion == "cluster":
cluster_voicings = {}
for numeral, chords in extended_maj_chords.items():
chord = []
for note, inv in zip(chords, extended_inv[numeral]):
chord.append(note + inv)
cluster_voicings.update(
{numeral: [chord[0] - 12, chord[1], chord[-1]]}
)
return cluster_voicings
else:
return {
numeral: [
note + inv for note, inv in zip(chords, extended_inv[numeral])
]
for numeral, chords in extended_maj_chords.items()
}
return {
numeral: [note + inv for note, inv in zip(chords, inversions[numeral])]
for numeral, chords in simple_maj_chords.items()
}
# c_maj_chords = {
# 'I':[c_maj["C"],c_maj['E'],c_maj['G']],
# "ii":[c_maj["C"]+2,c_maj['E']+2,c_maj['G']+2],
# 'iii':[c_maj["C"]+4,c_maj['E']+4,c_maj['G']+4],
# 'IV':[c_maj["C"]+5,c_maj['E']+5,c_maj['G']+5],
# 'V':[c_maj["C"]+7,c_maj['E']+7,c_maj['G']+7],
# 'vi':[c_maj["C"]+9,c_maj['E']+9,c_maj['G']+9],
# 'viio':[c_maj["C"]+11,c_maj['E']+11,c_maj['G']+11],
# }
def create_major_progression(
self, interval_list, key, inversion="closed", filename=""
):
# create your MIDI object
mf = MIDIFile(1) # only 1 track
track = 0 # the only track
time = 0 # start at the beginning
mf.addTrackName(track, time, "Sample Track")
mf.addTempo(track, time, 120)
# add some notes
channel = 0
volume = 100
duration = 4 # whole measure chords
maj_chords = self.get_maj_scale_chords(key, inversion)
interval_list = interval_list.split("-")
for i in interval_list:
print(i)
if "maj" in i and not "min" in i:
numeral, ext = i.split("maj")
chord = self.get_maj_scale_chords(
key, inversion, add=[int(ext)], majmin="maj"
)[numeral]
elif "min" in i and not "maj" in i:
numeral, ext = i.split("min")
chord = self.get_maj_scale_chords(
key, inversion, add=[int(ext)], majmin="min"
)[numeral]
elif "maj" in i and "min" in i:
chord = maj_chords[i] # minmaj majmin not supported.
elif "dom" in i:
numeral, ext = i.split("dom")
chord = self.get_maj_scale_chords(key, inversion, dom=[int(ext)])[
numeral
]
else:
chord = maj_chords[i]
print(chord)
for note in chord:
mf.addNote(track, channel, note, time, duration, volume)
time += 4
if not filename:
filename = "-".join(interval_list) + "output.mid"
with open(filename, "wb") as outf:
mf.writeFile(outf)
print(f"wrote to {filename}")
commonprogressions = [
"I-IV-V-I",
"I-I-I-I-IVdom7-IVdom7-I-I-Vdom7-IVdom7-I-Vdom7",
"I-V-vi-IV",
"iimin7-Vdom7-Imaj7-Imaj7",
"I-V-vi-iii-IV-I-IV-V",
"I-vi-IV-V",
"I-IV-V-IV",
"vi-IV-I-V",
"I-IV-ii-V",
"I-IV-I-V",
"I-ii-iii-IV-V",
"I-III-IV-iv",
"vi-V-IV-III",
"iimin7-Vdom7-imin7-imin7",
"Imaj7-vimin7-iimin7-Vdom7",
"Imaj9-vimin9-iimin9-Vdom7",
"Imaj7-VImaj7-iimin7-Vdom7",
"imin7-vimin7-iimin7-Vdom7",
"iiimin7-VImaj7-iimin7-Vdom7",
"Imaj7-IVdom7-iiimin7-VIdom7",
]
for p in commonprogressions:
key = "E"
cpg = ChordProgressionGenerator(scale_root=key, LOWEST_OCTAVE=3)
cpg.create_major_progression(
p,
key,
inversion="open",
filename=f"{p}.mid",
)