-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitchclasses.py
412 lines (326 loc) · 13.9 KB
/
pitchclasses.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
from math import ceil, floor, gcd, lcm
PC_UNIVERSE = 12 # default is 12 tone equal temperament
class PitchClasses:
def __init__(self, pcs, univ=0):
if univ == 0:
self.univ = PC_UNIVERSE
else:
self.univ = univ
self.set_pcs(pcs)
def set_pcs(self):
raise NotImplementedError()
def _transposed(self, transposition):
"return list of pitch classes, transposed by transposition"
return [(pc + transposition) % self.univ for pc in self.pcs]
def _inverted(self, axis):
"return list of pitch classes, inverted across axis"
return [(axis - pc) % self.univ for pc in self.pcs]
def _m_transformed(self, multiplier):
"return list of pitch classes, multiplied by multiplier"
return [(pc * multiplier) % self.univ for pc in self.pcs]
def _retrograded(self):
"return list of pitch classes in reversed order"
return list(reversed(self.pcs))
def _as_univ(self, new_univ, mode="e"):
"return list of pitch classes, scaled by new_univ"
multiplier = new_univ / self.univ
new_pcs = [x * multiplier for x in self.pcs]
# different options for what to do with pitch classes that do not fit cleanly when pitch class universe is resized
if mode == "e" or mode == "exception":
for pc in new_pcs:
if int(pc) != pc:
err = round(pc / multiplier)
raise ValueError(
"pitch class {} does not exist in a universe of size {}.".format(
err, new_univ
)
)
return [int(x) for x in new_pcs]
elif mode == "d" or mode == "drop":
return [int(x) for x in new_pcs if x == int(x)]
elif mode == "c" or mode == "ceil" or mode == "ceiling":
return [ceil(x) for x in new_pcs]
elif mode == "r" or mode == "round":
return [round(x) for x in new_pcs]
elif mode == "f" or mode == "floor":
return [floor(x) for x in new_pcs]
else:
raise ValueError("invalid mode")
def _minimized_univ(self):
divisor = gcd(*self.pcs, self.univ)
new_univ = self.univ // divisor
return self._as_univ(new_univ, mode="e"), new_univ
class PitchClassSet(PitchClasses):
def set_pcs(self, pcs):
pcs = [pc % self.univ for pc in pcs]
self.pcs = sorted(set(pcs))
self.cardinality = len(self.pcs)
def __repr__(self):
return "PitchClassSet {}{}".format(self.univ, self.pcs)
def _pcs_in_normalized_univ(self, *pc_sets):
"""This function allows comparison of multiple pc_sets with
different pc universe sizes. Returns a tuple consisting of:
- lists of pcs in the new pc universe
- the size of the new pc universe"""
univs = [pc_set.univ for pc_set in pc_sets]
comp_univ = lcm(*univs)
return (pc_set._as_univ(comp_univ) for pc_set in pc_sets), comp_univ
def __lt__(self, pc_set):
(self_pcs, arg_pcs), _ = self._pcs_in_normalized_univ(self, pc_set)
return self_pcs < arg_pcs
def __le__(self, pc_set):
(self_pcs, arg_pcs), _ = self._pcs_in_normalized_univ(self, pc_set)
return self_pcs <= arg_pcs
def __eq__(self, pc_set):
(self_pcs, arg_pcs), _ = self._pcs_in_normalized_univ(self, pc_set)
return self_pcs == arg_pcs
def __ne__(self, pc_set):
(self_pcs, arg_pcs), _ = self._pcs_in_normalized_univ(self, pc_set)
return self_pcs != arg_pcs
def __gt__(self, pc_set):
(self_pcs, arg_pcs), _ = self._pcs_in_normalized_univ(self, pc_set)
return self_pcs > arg_pcs
def __ge__(self, pc_set):
(self_pcs, arg_pcs), _ = self._pcs_in_normalized_univ(self, pc_set)
return self_pcs >= arg_pcs
def __sub__(self, pc_set):
(self_pcs, arg_pcs), univ = self._pcs_in_normalized_univ(self, pc_set)
difference = set(self_pcs) - set(arg_pcs)
return PitchClassSet(difference, univ)
def __and__(self, pc_set):
(self_pcs, arg_pcs), univ = self._pcs_in_normalized_univ(self, pc_set)
sum_ = set(self_pcs) & set(arg_pcs)
return PitchClassSet(sum_, univ)
def __xor__(self, pc_set):
(self_pcs, arg_pcs), univ = self._pcs_in_normalized_univ(self, pc_set)
sym_diff = set(self_pcs) ^ set(arg_pcs)
return PitchClassSet(sym_diff, univ)
def __or__(self, pc_set):
(self_pcs, arg_pcs), univ = self._pcs_in_normalized_univ(self, pc_set)
union = set(self_pcs) | set(arg_pcs)
return PitchClassSet(union, univ)
def _check_valid_pitch_class_set(self, pc_set):
if not isinstance(pc_set, PitchClassSet):
return TypeError("Only a PitchClassSet can be compared to a PitchClassSet")
elif pc_set.univ != self.univ:
return NotImplementedError(
"Cannot compare PitchClassSets with different values of .univ in this way"
)
return None
def transposed(self, transposition):
return PitchClassSet(self._transposed(transposition), univ=self.univ)
def transpose(self, transposition):
self.set_pcs(self._transposed(transposition))
def inverted(self, axis):
return PitchClassSet(self._inverted(axis), univ=self.univ)
def invert(self, axis):
self.set_pcs(self._inverted(axis))
def m_transformed(self, multiplier):
return PitchClassSet(self._m_transformed(multiplier), univ=self.univ)
def m_transform(self, multiplier):
self.set_pcs(self._m_transformed(multiplier))
def as_univ(self, new_univ, mode="e"):
return PitchClassSet(self._as_univ(new_univ, mode=mode), univ=new_univ)
def set_univ(self, new_univ, mode="e"):
new_pcs = self._as_univ(new_univ, mode=mode)
self.univ = new_univ
self.set_pcs(new_pcs)
def complement(self):
comp = [pc for pc in range(self.univ) if pc not in self.pcs]
return PitchClassSet(comp, univ=self.univ)
def vector(self):
vec = [0] * (self.univ - 1)
# calculate interval for each pair of notes
for i, note1 in enumerate(self.pcs):
for j, note2 in enumerate(self.pcs):
if j <= i:
continue
else:
interval = (note1 - note2) % self.univ
vec[interval - 1] += 1
# invert large intervals
for i in range(len(vec) // 2):
vec[i] += vec.pop()
return IntervalVector(vec, univ=self.univ)
def copy(self):
return PitchClassSet(pcs=self.pcs, univ=self.univ)
def minimized_univ(self):
new_pcs, new_univ = self._minimized_univ()
return PitchClassSet(new_pcs, univ=new_univ)
def minimize_univ(self):
new_pcs, new_univ = self._minimized_univ()
self.univ = new_univ
self.pcs = new_pcs
class PitchClassSequence(PitchClasses):
def set_pcs(self, pcs):
self.pcs = [pc % self.univ for pc in pcs]
self.length = len(self.pcs)
def __repr__(self):
return "PitchClassSequence {}{}".format(self.univ, self.pcs)
def __add__(self, pc_sequence):
exception = self._check_valid_pitch_class_sequence(pc_sequence)
if exception is not None:
raise exception
else:
combined_sequence = self.pcs + pc_sequence.pcs
return PitchClassSequence(combined_sequence, univ=self.univ)
def extend(self, pc_sequence):
exception = self._check_valid_pitch_class_sequence(pc_sequence)
if exception is not None:
raise exception
else:
combined_sequence = self.pcs + pc_sequence.pcs
self.set_pcs(combined_sequence)
def append(self, pc):
if not isinstance(pc, int):
raise TypeError("Only an int can be added to a PitchClassSequence")
else:
pc_sequence = self.pcs
pc_sequence.append(pc)
self.set_pcs(pc_sequence)
def _check_valid_pitch_class_sequence(self, pc_sequence):
if not isinstance(pc_sequence, PitchClassSequence):
return TypeError(
"Only a PitchClassSequence can be compared to a PitchClassSequence"
)
elif pc_sequence.univ != self.univ:
return NotImplementedError(
"Cannot compare PitchClassSequences with different values of .univ in this way"
)
return None
def transposed(self, transposition):
return PitchClassSequence(self._transposed(transposition), univ=self.univ)
def transpose(self, transposition):
self.set_pcs(self._transposed(transposition))
def inverted(self, axis):
return PitchClassSequence(self._inverted(axis), univ=self.univ)
def invert(self, axis):
self.set_pcs(self._inverted(axis))
def m_transformed(self, multiplier):
return PitchClassSequence(self._m_transformed(multiplier), univ=self.univ)
def m_transform(self, multiplier):
self.set_pcs(self._m_transformed(multiplier))
def retrograded(self):
return PitchClassSequence(self._retrograded(), univ=self.univ)
def retrograde(self):
self.set_pcs(self._retrograded())
def as_univ(self, new_univ, mode="e"):
return PitchClassSequence(self._as_univ(new_univ, mode=mode), univ=new_univ)
def set_univ(self, new_univ, mode="e"):
new_pcs = self._as_univ(new_univ, mode=mode)
self.univ = new_univ
self.set_pcs(new_pcs)
def pc_inventory(self):
inventory = set(self.pcs)
return PitchClassSet(inventory, self.univ)
def intervals(self):
ivals = []
for i in range(1, self.length):
ivals.append((self.pcs[i] - self.pcs[i - 1]) % self.univ)
return IntervalSequence(ivals, self.univ)
def copy(self):
return PitchClassSequence(pcs=self.pcs, univ=self.univ)
def minimized_univ(self):
new_pcs, new_univ = self._minimized_univ()
return PitchClassSequence(new_pcs, univ=new_univ)
def minimize_univ(self):
new_pcs, new_univ = self._minimized_univ()
self.univ = new_univ
self.pcs = new_pcs
class IntervalVector:
def __init__(self, intervals, univ=0):
if univ == 0:
self.univ = PC_UNIVERSE
else:
self.univ = univ
self.intervals = intervals
def __repr__(self):
return "IntervalVector {}{}".format(self.univ, self.intervals)
class IntervalSequence:
def __init__(self, intervals, univ=0):
if univ == 0:
self.univ = PC_UNIVERSE
else:
self.univ = univ
self.intervals = intervals
def __repr__(self):
return "IntervalSequence {}{}".format(self.univ, self.intervals)
def melody(self, starting_pc):
mel = [starting_pc]
for i in self.intervals:
mel.append((mel[-1] + i) % self.univ)
return PitchClassSequence(mel, self.univ)
def _inverted(self):
return [(0 - i) % self.univ for i in self.intervals]
def inverted(self):
return IntervalSequence(self._inverted(), univ=self.univ)
def invert(self):
self.intervals = self._inverted()
def _retrograded(self):
inverted = self._inverted() # intervals flip when reversed
return list(reversed(inverted))
def retrograded(self):
return IntervalSequence(self._retrograded(), univ=self.univ)
def retrograde(self):
self.intervals = self._retrograded()
def _as_univ(self, new_univ):
multiplier = new_univ / self.univ
new_intervals = [x * multiplier for x in self.intervals]
for i in new_intervals:
if int(i) != i:
err = round(i / multiplier)
raise ValueError(
"interval {} does not exist in a universe of size {}.".format(
err, new_univ
)
)
return [int(x) for x in new_intervals]
def as_univ(self, new_univ):
return IntervalSequence(self._as_univ(new_univ), new_univ)
def set_univ(self, new_univ):
self.intervals = self._as_univ(new_univ)
self.univ = new_univ
def copy(self):
return IntervalSequence(intervals=self.intervals, univ=self.univ)
class SetSequence:
def __init__(self, pc_sets, univ=12):
self.univ = univ
self.pc_sets = self._parse_sets(pc_sets)
def __repr__(self):
sets_repr = []
for s in self.pc_sets:
if s.cardinality == 1:
sets_repr.append(s.pcs[0])
else:
sets_repr.append(s.pcs)
return "SetSequence {}{}".format(sets_repr, self.univ)
def _parse_sets(self, inp):
sequence = []
if not (isinstance(inp, list) or isinstance(inp, tuple)):
t = type(inp)
message = "pc_sets must be of type list or tuple; received {}".format(t)
raise TypeError(message)
for element in inp:
if isinstance(element, PitchClassSet):
sequence.append(element.copy())
elif (
isinstance(element, list)
or isinstance(element, tuple)
or isinstance(element, set)
):
sequence.append(PitchClassSet(element, univ=self.univ))
elif isinstance(element, int):
sequence.append(PitchClassSet([element], univ=self.univ))
else:
t = type(element)
message = "All elements of pc_sets must be of type PitchClassSet, list, tuple, set, or int; received {}".format(
t
)
raise TypeError(message)
return sequence
def aggregate(univ=PC_UNIVERSE):
return PitchClassSet([x for x in range(univ)], univ=univ)
def maximally_distributed(i, univ=PC_UNIVERSE):
pc_set = aggregate(i)
pc_set.set_univ(univ, "floor")
return pc_set