generated from NCKU-CCS/TOC-Project-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.py
433 lines (411 loc) · 12.5 KB
/
notes.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import re
note = r"[A-G]#?x?b{0,2}"
noteNumDict = {
"C" : 0,
"B#" : 0,
"Dbb": 0,
"Bx" : 1,
"C#" : 1,
"Db" : 1,
"Cx" : 2,
"D" : 2,
"Ebb": 2,
"D#" : 3,
"Eb" : 3,
"Fbb": 3,
"Dx" : 4,
"E" : 4,
"Fb" : 4,
"E#" : 5,
"F" : 5,
"Gbb": 5,
"Ex" : 6,
"F#" : 6,
"Gb" : 6,
"Fx" : 7,
"G" : 7,
"Abb": 7,
"G#" : 8,
"Ab" : 8,
"Gx" : 9,
"A" : 9,
"Bbb": 9,
"A#" :10,
"Bb" :10,
"Cbb":10,
"Ax" :11,
"B" :11,
"Cb" :11,
}
#chord
diffList = [
#major
[4,3],
[4,3,4],
[4,3,4,3],
#minor
[3,4],
[3,4,3],
[3,4,4],
[3,4,3,4],
#domiant
[4,3,3],
[4,3,3,4],
#half dim
[3,3,4],
#dim
[3,3,3],
#aug
[4,4],
[4,4,2],
[4,4,3],
#6
[4,3,2],
#sus4
[5,2],
#sus2
[2,5],
]
chordList = [
["Major", "大和弦", ""],
["Major 7", "大七和弦", "Maj", "Maj7", "M7", "Δ7"],
["Major 9", "大九和弦", "9", "Maj9", "M9", "Δ9"],
["Minor", "小和弦", "m", "-"],
["Minor 7", "小七和弦", "m7", "-7"],
["Minor Major 7", "小大七和弦", "mmaj7", "mM7", "-Δ7"],
["Minor 9", "小九和弦", "m9", "-9"],
["Dominant 7", "屬七和弦", "7"],
["Dominant 9", "屬九和弦", "9"],
["Half diminished 7", "半減七和弦", "ø", "m7(b5)"],
["Diminished 7", "減七和弦", "dim", "o"],
["Augmented", "增和弦", "aug", "+"],
["Augmented 7", "增屬七和弦", "aug7", "+7"],
["Augmented Major 7", "增大七和弦", "augM7", "+Δ7", "M7(#5)"],
["Sixth", "大六和弦", "6", "M6"],
["Suspended 4", "掛留四和弦", "sus4", "sus"],
["Suspended 2", "掛留二和弦", "sus2"],
]
chordListAlt = [
["major", "大調", "三和弦", "大三和弦", "3", "大三", "大3", "大 3"],
["major 7", "七和弦", "大七", "maj7", "maj 7"],
["major 9", "九和弦", "大九", "maj9", "maj 9"],
["minor", "小調", "小三和弦", "小三", "小3", "小 3"],
["minor 7", "小七", "小7", "小 7"],
["minor major 7", "mmaj7", "mm7", "-大7", "小Δ7", "小大七", "小大7", "mmaj 7", "mm 7", "-大 7" "小大 7"],
["minor 9", "小九", "小9", "小 9"],
["dominant 7", "屬七", "屬7", "屬 7", "七", "7"],
["dominant 9", "屬九", "屬9", "屬 9", "九", "9"],
["half diminished 7", "半減七", "半減7", "半減 7", "小七降五", "小7降5", "小七b5", "小七 b5", "half diminish","半減"],
["diminished 7", "diminish 7", "diminish", "減七", "減7", "減 7"],
["augmented", "增", "增三", "增3", "增 3"],
["augmented 7", "增七和弦", "增屬七", "增七", "增7", "增 7", "增屬7", "增屬 7"],
["augmented major 7", "增大七", "增大7", "增大 7", "大7升5", "大七升五"],
["sixth", "大六", "大6", "大 6", "六", "Δ6"],
["suspended 4", "suspended", "suspend 4", "suspend", "掛留四", "掛留4", "掛四", "掛4", "sus 4", "sus"],
["suspended 2", "suspend 2", "掛留二", "掛留2", "掛二", "掛2", "sus 2"],
]
diffSeven = [
#major
[2,2],
[2,2,2],
[2,2,2,2],
#minor
[2,2],
[2,2,2],
[2,2,2],
[2,2,2,2],
#domiant
[2,2,2],
[2,2,2,2],
#half dim
[2,2,2],
#dim
[2,2,2],
#aug
[2,2],
[2,2,2],
[2,2,2],
#6
[2,2,1],
#sus4
[3,1],
#sus2
[1,3],
]
#convert note to corresponding number, starting from C = 0, B = 11
def noteToNumber(note):
return noteNumDict.get(note, -1)
#return all notes in text
def containNotes(text):
return re.findall(note, text)
def notesToChord(noteList):
numList = []
for i in range(len(noteList)):
numList.append(noteToNumber(noteList[i]))
for i in range(1, len(numList)):
if numList[i] < numList[0]:
numList[i] = numList[i] + 12
numList = numList[0:1] + sorted(numList[1:])
diff = []
for i in range(1, len(numList)):
diff.append(numList[i] - numList[i-1])
whichChord = -100
for i in range(len(diffList)):
if whichChord != -100:
break
if len(diff) != len(diffList[i]):
continue
for j in range(len(diffList[i])):
if diff[j] != diffList[i][j]:
break
if j == len(diffList[i])-1:
whichChord = i
break
print("whichChord = " + str(whichChord) + " diff = " + str(diff))
if whichChord >= len(chordList) or whichChord <= -1:
whichChord = -1
return noteList[0], whichChord
def chordToNote(noteList, whichChord):
sevenRootnote = notesToSevenNum(noteList[0])
sevenList = [sevenRootnote]
for i in range(len(diffSeven[whichChord])):
sevenRootnote = (sevenRootnote + diffSeven[whichChord][i])%7
sevenList.append(sevenRootnote)
rootnote = noteToNumber(noteList[0])
notes = [rootnote]
for i in range(len(diffList[whichChord])):
rootnote = (rootnote + diffList[whichChord][i])%12
notes.append(rootnote)
print("whichChord = " + str(whichChord) + " seven = " + str(sevenList) + " notesnum = " + str(notes))
result = str(sevenNumToNote(sevenList[0], notes[0]))
for i in range(1,len(notes)):
result = result + "、" + str(sevenNumToNote(sevenList[i], notes[i]))
return result
def notesToSevenNum(note):
note = note[0]
return {
"C" : 0,
"D" : 1,
"E" : 2,
"F" : 3,
"G" : 4,
"A" : 5,
"B" : 6,
}.get(note, -1)
def sevenNumToNote(seven, note):
if seven == 0:
if note == 0: return "C"
elif note == 1: return "C#"
elif note == 2: return "Cx"
elif note ==11: return "Cb"
elif note ==10: return "Cbb"
elif seven == 1:
if note == 2: return "D"
elif note == 3: return "D#"
elif note == 4: return "Dx"
elif note == 1: return "Db"
elif note == 0: return "Dbb"
elif seven == 2:
if note == 4: return "E"
elif note == 5: return "E#"
elif note == 6: return "Ex"
elif note == 3: return "Eb"
elif note == 2: return "Ebb"
elif seven == 3:
if note == 5: return "F"
elif note == 6: return "F#"
elif note == 7: return "Fx"
elif note == 4: return "Fb"
elif note == 3: return "Fbb"
elif seven == 4:
if note == 7: return "G"
elif note == 8: return "G#"
elif note == 9: return "Gx"
elif note == 6: return "Gb"
elif note == 5: return "Gbb"
elif seven == 5:
if note == 9: return "A"
elif note ==10: return "A#"
elif note ==11: return "Ax"
elif note == 8: return "Ab"
elif note == 7: return "Abb"
elif seven == 6:
if note ==11: return "B"
elif note == 0: return "B#"
elif note == 1: return "Bx"
elif note ==10: return "Bb"
elif note == 9: return "Bbb"
#scale
scaleDiffList = [
#modern modes
[2,2,1,2,2,2],
[2,1,2,2,2,1],
[1,2,2,2,1,2],
[2,2,2,1,2,2],
[2,2,1,2,2,1],
[2,1,2,2,1,2],
[1,2,2,1,2,2],
#two more minor
[2,1,2,2,1,3],
[2,1,2,2,2,2],
#modern modes with accidentals
[2,1,3,1,2,1],
[1,2,2,2,2,1],
[2,1,2,1,2,2],
[1,2,2,1,3,1],
[3,1,2,1,2,2],
[2,2,2,1,2,1],
[2,2,1,2,1,2],
[1,3,1,2,1,2],
#whole tone
[2,2,2,2,2],
#altered
[1,2,1,2,2,2],
[1,2,1,2,2,1],
#diminished
[1,2,1,2,1,2,1],
[2,1,2,1,2,1,2],
#pentatonic
[2,2,3,2],
[2,3,2,3],
[3,2,3,2],
[2,3,2,2],
[3,2,2,3],
]
scaleList = [
["Ionian", "大調"],
["Dorian", "Dor."],
["Phrygian", "Phry."],
["Lydian", "Lyd."],
["Mixolydian", "Mixo."],
["Aeolian", "自然小音階"],
["Locrian", "Loc."],
["Harmonic minor", "和聲小音階"],
["Melodic minor", "旋律小音階"],
["Dorian #4", "Dor. #4"],
["Dorain b2", "Dor. b2"],
["Locrian 2", "Loc. 2"],
["Locrian 6", "Loc. 6"],
["Lydian #9", "Lyd. #9"],
["Lydian b7", "Lyd. b7"],
["Mixolydian b6", "Mixo. b6"],
["Mixolydian b2, b6", "Mixo. b2 b6"],
["Whole tone", "全音音階"],
["Altered", "alt."],
["Altered bb7", "alt. bb7"],
["Diminished (Half-Whole)", "半全音階"],
["Diminished (Whole-Half)", "全半音階"],
["Major pentatonic", "宮調式"],
["Egyptian", "商調式"],
["Blue minor (pentatonic)", "角調式"],
["Blue major (pentatonic)", "徵調式"],
["Minor pentatonic", "羽調式"],
]
scaleListAlt = [
["major", "maj", "ion.", "ion", "艾奧尼安調式", "艾奧尼安"],
["dorian", "dor.", "dor", "多利安調式", "多利安"],
["phrygian", "phry.", "phry", "弗利吉安調式", "弗利吉安"],
["lydian", "lyd.", "lyd", "利地安調式", "利地安"],
["mixolydian", "mixo.", "mixo", "米克索利地安調式", "米克索利地安", "混和利地安調式", "混和利地安"],
["aeolian", "natural minor", "natural", "小調", "aeo.", "aeo", "自然小調", "自然", "伊奧利安調式", "伊奧利安"],
["locrian", "loc.", "loc", "洛克利安調式", "洛克利安"],
["harmonic minor", "harmonic", "和聲小調", "和聲"],
["melodic minor", "melodic", "旋律小調", "旋律"],
["dorian #4", "dor. #4", "dor #4"],
["dorain b2", "dor. b2", "dor b2"],
["locrian 2", "loc. 2", "loc 2"],
["locrian 6", "loc. 6", "loc 6"],
["lydian #9", "lyd. #9", "lyd #9"],
["lydian b7", "lyd. b7", "lyd b7"],
["mixolydian b6", "mixo. b6", "mixo b6"],
["mixolydian b2 b6", "mixo. b2 b6", "mixo b2 b6"],
["whole tone", "whole tone scale", "全音", "全音階"],
["altered", "alt.", "alt", "altered scale"],
["altered bb7", "alt. bb7", "alt bb7", "altered scale bb7"],
["diminished (half-whole)", "h-w", "h w", "half whole scale", "half whole", "半全"],
["diminished (whole-half)", "w-h", "w h", "whole half scale", "whole half", "全半", "symmetric diminished"],
["major pentatonic", "宮"],
["egyptian", "商"],
["blue minor (pentatonic)", "blue minor pentatonic", "pentatonic blue minor", "角", "慢宮", "慢宮調"],
["blue major (pentatonic)", "blue major pentatonic", "pentatonic blue major", "徵"],
["minor pentatonic", "羽"],
]
scaleDiffSeven = [
#modern modes
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
#two more minor
[1,1,1,1,1,1],
[1,1,1,1,1,1],
#modern modes with accidentals
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
#whole tone
[1,1,1,1,1],
#altered
[1,1,1,1,1,1],
[1,1,1,1,1,1],
#diminished
[0,1,1,1,1,1,1],
[1,0,2,0,2,0,1],
#pentatonic
[1,1,2,1],
[1,2,1,2],
[2,1,2,1],
[1,2,1,1],
[2,1,1,2],
]
def notesToScale(noteList):
numList = []
for i in range(len(noteList)):
numList.append(noteToNumber(noteList[i]))
for i in range(1, len(numList)):
if numList[i] < numList[0]:
numList[i] = numList[i] + 12
numList = numList[0:1] + sorted(numList[1:])
diff = []
for i in range(1, len(numList)):
diff.append(numList[i] - numList[i-1])
whichScale = -100
for i in range(len(scaleDiffList)):
if whichScale != -100:
break
if len(diff) != len(scaleDiffList[i]):
continue
for j in range(len(scaleDiffList[i])):
if diff[j] != scaleDiffList[i][j]:
break
if j == len(scaleDiffList[i])-1:
whichScale = i
break
print("whichScale = " + str(whichScale) + " diff = " + str(diff))
if whichScale >= len(scaleList) or whichScale <= -1:
whichScale = -1
return noteList[0], whichScale
def scaleToNote(noteList, whichScale):
sevenRootnote = notesToSevenNum(noteList[0])
sevenList = [sevenRootnote]
for i in range(len(scaleDiffSeven[whichScale])):
sevenRootnote = (sevenRootnote + scaleDiffSeven[whichScale][i])%7
sevenList.append(sevenRootnote)
rootnote = noteToNumber(noteList[0])
notes = [rootnote]
for i in range(len(scaleDiffList[whichScale])):
rootnote = (rootnote + scaleDiffList[whichScale][i])%12
notes.append(rootnote)
print("whichScale = " + str(whichScale) + " seven = " + str(sevenList) + " notesnum = " + str(notes))
result = str(sevenNumToNote(sevenList[0], notes[0]))
for i in range(1,len(notes)):
result = result + "、" + str(sevenNumToNote(sevenList[i], notes[i]))
return result