-
Notifications
You must be signed in to change notification settings - Fork 0
/
chord.py
161 lines (134 loc) · 6.02 KB
/
chord.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
from .note import Note
from .chord_types import types_of_chords
class Chord:
def __init__(self, chord_type="custom", bass_note="", list_of_notes=None):
if chord_type != "custom":
if bass_note != "":
self.bass_note = Note(bass_note) if isinstance(bass_note, str) else bass_note
else:
raise Exception("base_note must be a Note/str representing the bass note of the chord.")
self.chord_type = chord_type
try:
self.chord_intervals = types_of_chords[chord_type]
except KeyError:
raise Exception("Chord \"{}\" does not exist. Please create a chord that is in the following list"
"of all possible chords:\n{}. Otherwise, create a custom chord with the list_of_notes"
"parameter."
.format(chord_type, types_of_chords.keys())) from None
self.chord = [self.bass_note + interval for interval in self.chord_intervals]
else:
if list_of_notes is None or not isinstance(list_of_notes, list):
raise Exception("list_of_notes must be a list of Note/str objects representing a custom chord.")
self.chord = []
for note in list_of_notes:
if isinstance(note, Note):
self.chord.append(note)
elif isinstance(note, str):
self.chord.append(Note(note))
else:
raise Exception("{} must be a list of note/str objects.".format(list_of_notes))
self.bass_note = self.chord[0]
self.chord_type = chord_type
self.max_inversions = len(self.chord) - 1
self._pop_note = True
def to_n_number_notes(self, number):
assert number > 0, "{} must be > 0.".format(number)
if number <= len(self):
return Chord(list_of_notes=self.chord[:number])
else:
self._pop_note = False
new_chord = self.inversion(number - len(self))
self._pop_note = True
return new_chord
def inversion(self, inversion_num):
# assert inversion_num <= self.max_inversions, "Inversion {} exceeds the maximum " \
# "inversion ({}) for {} {}!". \
# format(inversion_num, self.max_inversions, self.bass_note.note, self.chord_type)
chord_copy = self.chord.copy()
for i in range(inversion_num):
chord_letters = []
for note in chord_copy:
chord_letters.append(note.letter)
index_to_append = (chord_letters.index(chord_letters[-1]) + 1) % len(chord_letters)
chord_copy.append(chord_copy[index_to_append] + "P8")
if self._pop_note: chord_copy.pop(0)
return Chord(list_of_notes=chord_copy)
def __iter__(self):
return iter(self.chord)
def __contains__(self, item):
if isinstance(item, str):
return Note(item) in self.chord
elif isinstance(item, Note):
return item in self.chord
else:
raise Exception("{} is of incorrect type. Must be str or Note, not {}".format(item, type(item)))
def __eq__(self, other):
if isinstance(other, Chord):
return self.chord == other.chord
else:
raise Exception("{} is of incorrect type. Must be a Chord object.".format(other))
def __ne__(self, other):
if isinstance(other, Chord):
return self.chord != other.chord
else:
raise Exception("{} is of incorrect type. Must be a Chord object.".format(other))
def __gt__(self, other):
if isinstance(other, Chord):
return self[-1].number > other[-1].number
else:
raise Exception("{} is of incorrect type. Must be a Chord object.".format(other))
def __lt__(self, other):
if isinstance(other, Chord):
return self[-1].number < other[-1].number
else:
raise Exception("{} is of incorrect type. Must be a Chord object.".format(other))
def __ge__(self, other):
if isinstance(other, Chord):
return self[-1].number >= other[-1].number
else:
raise Exception("{} is of incorrect type. Must be a Chord object.".format(other))
def __le__(self, other):
if isinstance(other, Chord):
return self[-1].number <= other[-1].number
else:
raise Exception("{} is of incorrect type. Must be a Chord object.".format(other))
def __getitem__(self, i):
self.chord_type = "custom"
return self.chord[i]
def __delitem__(self, i):
self.chord_type = "custom"
del self.chord[i]
def __setitem__(self, i, val):
if isinstance(val, Note):
self.chord_type = "custom"
self.chord[i] = val
elif isinstance(val, str):
self.chord_type = "custom"
self.chord[i] = Note(val)
else:
raise Exception("{} is of incorrect type. Must be a Note or str object.".format(val))
def insert(self, i, val):
if isinstance(val, Note):
self.chord_type = "custom"
self.chord.insert(i, val)
elif isinstance(val, str):
self.chord_type = "custom"
self.chord.insert(i, Note(val))
else:
raise Exception("{} is of incorrect type. Must be a Note or str object.".format(val))
def append(self, val):
if isinstance(val, Note):
self.chord_type = "custom"
self.insert(len(self.chord), val)
elif isinstance(val, str):
self.chord_type = "custom"
self.insert(len(self.chord), Note(val))
else:
raise Exception("{} is of incorrect type. Must be a Note or str object.".format(val))
def __len__(self):
return len(self.chord)
def __str__(self):
return "Chord(" + str(self.chord) + ")"
def __repr__(self):
# return "<{} {}>".format(self.__class__.__name__, self.note)
return self.__str__()