-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformations.py
350 lines (275 loc) · 9.95 KB
/
transformations.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
"""
NAME:
===============================
Transformations (transformations.py)
BY:
===============================
Mark Gotham, 2021-
LICENCE:
===============================
Creative Commons Attribution-ShareAlike 4.0 International License
https://creativecommons.org/licenses/by-sa/4.0/
ABOUT:
===============================
Functions for transforming pitch lists:
e.g., transposition, inversion, retrograde, rotation.
Most apply equally to any pitch class sequence,
some are more specific to tone rows.
"""
from typing import Union, List, Tuple
import unittest
# ------------------------------------------------------------------------------
# Basic operations first: transpose, retrograde, invert.
def transposeBy(
row: Union[List, Tuple],
semitones: int = 0
) -> list:
"""
Transposes a list of pitch classes by an interval of size
set by the value of `semitones`.
"""
zeroList = []
for x in range(len(row)):
zeroList.append((row[x] + semitones) % 12)
return zeroList
def transposeTo(
row: Union[List, Tuple],
start: int = 0
) -> list:
"""
Transpose a list of pitch classes to start on 0 (by default), or
any another number from 0-11 set by the value of `start`.
"""
firstPC = row[0]
zeroList = []
for y in range(len(row)):
zeroList.append((row[y] - firstPC + start) % 12)
return zeroList
def retrograde(
row: Union[List, Tuple]
) -> list:
"""
Retrograde a list of pitch classes (simply reverse the pitch list).
"""
return row[::-1]
def invert(
row: Union[List, Tuple]
) -> list:
"""
Invert a list of pitch classes around its starting pitch.
"""
startingPitch = row[0]
return [(startingPitch - x) % 12 for x in row]
def pitchesToIntervals(
row: Union[List, Tuple],
wrap: bool = False
) -> list:
"""
Retrieve the interval succession of a list of pitch classes (mod 12).
By default (`wrap = False`) this function returns 11 intervals for a 12 tone row.
Setting wrap to True gives the '12th' interval: that between the last and the first pitch.
"""
intervals = []
if wrap:
row += row[0]
for i in range(1, len(row)):
intervals.append((row[i] - row[i - 1]) % 12)
return intervals
def rotate(
row: Union[List, Tuple],
steps: int = 1
) -> list:
"""
Rotates a list of pitch classes through N steps (i.e. starts on the Nth element).
Should be called on an integer < 12.
If called on a larger integer, the value modulo 12 will be taken
(e.g. 15 becomes 3).
"""
if steps > 12:
steps = steps % 12
return row[steps:] + row[:steps]
# ------------------------------------------------------------------------------
# Further rotations and swaps operations that are: row specific, and niche (e.g., from Krenek 1960)
def rotateHexachords(
row: Union[List, Tuple],
transposeIterations: bool = False
) -> list:
"""
Implements a set of hexachord rotations of the kind described in Krenek 1960, p.212.
Splits the row into two hexachords and iteratively rotates each.
This function returns a list of lists with each iteration until
the cycle is complete and come full circle.
The transposeIterations option (default False) transposes each iteration to
start on the original pitch of the hexachord, also as described by Krenek.
Note this often converts a 12-tone row into one with repeated pitches.
"""
rows = [row]
hexachord1note1 = row[0]
hexachord2note1 = row[6]
for i in range(1, 6):
firstHexachord = row[i:6] + row[0:i]
secondHexachord = row[6+i:] + row[6:6+i]
if transposeIterations:
firstHexachord = transposeTo(firstHexachord, start=hexachord1note1)
secondHexachord = transposeTo(secondHexachord, start=hexachord2note1)
newRow = firstHexachord + secondHexachord
rows.append(newRow)
rows.append(row) # completes the cycle
return rows
def pairSwapKrenek(
row: Union[List, Tuple]
) -> list:
"""
Iteratively swaps pairs of adjacent notes in a row
with a two-step process as described in Krenek 1960, p.213.
Returns a list of 13 rows of which the last is the retrograde of the first.
As such, calling this twice brings you back to the original row.
"""
rows = [row]
for pair in range(6):
# First swap type, starting at position 1 (2nd pitch)
row = [x for x in row]
for x in range(1, 11, 2):
row[x], row[x + 1] = row[x + 1], row[x]
rows.append(row)
# Second swap type, starting at position 0 (1st pitch)
row = [x for x in row]
for x in range(0, 12, 2):
row[x], row[x + 1] = row[x + 1], row[x]
rows.append(row)
return rows
# ------------------------------------------------------------------------------
def lumsdaine_4x(
row: Union[list, None] = None,
) -> list[list]:
"""
A multipart
rotation and re-combination
method as reported in
Hopper's "The Music of David Lumsdaine", p.21.
>>> for x in lumsdaine_4x():
... print(x)
[11, 6, 5, 0, 3, 2, 9, 8, 10, 4, 1, 7]
[11, 9, 6, 8, 5, 10, 0, 4, 3, 1, 2, 7]
[11, 10, 2, 8, 3, 9, 0, 7, 5, 1, 6, 4]
[11, 0, 10, 7, 2, 5, 8, 1, 3, 6, 9, 4]
[2, 6, 10, 1, 11, 5, 9, 7, 3, 0, 8, 4]
[2, 9, 6, 7, 10, 3, 1, 0, 11, 8, 5, 4]
[2, 3, 5, 7, 11, 9, 1, 4, 10, 8, 6, 0]
[2, 1, 3, 4, 5, 10, 7, 8, 11, 6, 9, 0]
[5, 6, 3, 8, 2, 10, 9, 4, 11, 1, 7, 0]
[5, 9, 6, 4, 3, 11, 8, 1, 2, 7, 10, 0]
[5, 11, 10, 4, 2, 9, 8, 0, 3, 7, 6, 1]
[5, 8, 11, 0, 10, 3, 4, 7, 2, 6, 9, 1]
[10, 6, 11, 7, 5, 3, 9, 0, 2, 8, 4, 1]
[10, 9, 6, 0, 11, 2, 7, 8, 5, 4, 3, 1]
[10, 2, 3, 0, 5, 9, 7, 1, 11, 4, 6, 8]
[10, 7, 2, 1, 3, 11, 0, 4, 5, 6, 9, 8]
[3, 6, 2, 4, 10, 11, 9, 1, 5, 7, 0, 8]
[3, 9, 6, 1, 2, 5, 4, 7, 10, 0, 11, 8]
[3, 5, 11, 1, 10, 9, 4, 8, 2, 0, 6, 7]
[3, 4, 5, 8, 11, 2, 1, 0, 10, 6, 9, 7]
"""
if row is None:
row = [11, 6, 5, 0, 3, 2, 9, 8, 10, 4, 1, 7]
out = []
for i in range(5): # run 4, update to new starting row, and run again
out += lumsdaine_4(row)
row = every_nth(out[-1], start_index=4, n=5)
return out
def lumsdaine_4(
row1: list | None = None,
) -> list[list]:
"""
One phase of `lumsdaine_4x()`.
>>> for x in lumsdaine_4([11, 6, 5, 0, 3, 2, 9, 8, 10, 4, 1, 7]):
... print(x)
[11, 6, 5, 0, 3, 2, 9, 8, 10, 4, 1, 7]
[11, 9, 6, 8, 5, 10, 0, 4, 3, 1, 2, 7]
[11, 10, 2, 8, 3, 9, 0, 7, 5, 1, 6, 4]
[11, 0, 10, 7, 2, 5, 8, 1, 3, 6, 9, 4]
>>> for x in lumsdaine_4([5, 9, 7, 3, 0, 8, 4, 2, 6, 10, 1, 11]):
... print(x)
[5, 9, 7, 3, 0, 8, 4, 2, 6, 10, 1, 11]
[5, 4, 9, 2, 7, 6, 3, 10, 0, 1, 8, 11]
[5, 6, 8, 2, 0, 4, 3, 11, 7, 1, 9, 10]
[5, 3, 6, 11, 8, 7, 2, 1, 0, 9, 4, 10]
"""
if row1 is None:
row1 = [11, 6, 5, 0, 3, 2, 9, 8, 10, 4, 1, 7]
row2 = l_hexachord_pairs(row1)
row3 = every_nth(row2)
row4 = l_hexachord_pairs(row3)
return [row1, row2, row3, row4]
def l_hexachord_pairs(this_row: list):
"""
Re-arrange 0, 6, 1, 7 etc.
>>> l_hexachord_pairs([11, 6, 5, 0, 3, 2, 9, 8, 10, 4, 1, 7])
[11, 9, 6, 8, 5, 10, 0, 4, 3, 1, 2, 7]
"""
out_row = []
for i in range(6):
out_row += [this_row[i], this_row[i + 6]]
return out_row
def every_nth(
row: list,
start_index: int = 0,
n: int = 5,
ran: range = range(12)
) -> list:
"""
Cycle through the row (mod 12) with a step size of n.
>>> row = [11, 9, 6, 8, 5, 10, 0, 4, 3, 1, 2, 7]
>>> every_nth(row)
[11, 10, 2, 8, 3, 9, 0, 7, 5, 1, 6, 4]
By default,
start at index 0 and iterate 12 times (0-12),
though both the start index and the range() are settable arguments.
Hence this equivlance:
>>> every_nth(row, ran=range(1, 13))
[10, 2, 8, 3, 9, 0, 7, 5, 1, 6, 4, 11]
>>> every_nth(row, start_index=5)
[10, 2, 8, 3, 9, 0, 7, 5, 1, 6, 4, 11]
"""
out_row = []
for i in ran:
out_row.append(row[(start_index + i * n) % 12])
return out_row
# ------------------------------------------------------------------------------
class RowTester(unittest.TestCase):
def testTranspose(self):
rowBoulez = [3, 2, 9, 8, 7, 6, 4, 1, 0, 10, 5, 11]
zeroBoulez = transposeTo(rowBoulez, start=0)
self.assertEqual(zeroBoulez, [0, 11, 6, 5, 4, 3, 1, 10, 9, 7, 2, 8])
transBoulez = transposeTo(zeroBoulez, start=3)
self.assertEqual(transBoulez, [3, 2, 9, 8, 7, 6, 4, 1, 0, 10, 5, 11])
byByBoulez = transposeBy(transBoulez, semitones=2)
self.assertEqual(byByBoulez, [5, 4, 11, 10, 9, 8, 6, 3, 2, 0, 7, 1])
def testRotate(self):
luto = [0, 6, 5, 11, 10, 4, 3, 9, 8, 2, 1, 7]
for i in range(12):
row = rotate(luto, i)
self.assertEqual(row[0], luto[i])
def testInvert(self):
testSet = [0, 1, 4, 6]
self.assertEqual(invert(testSet), [0, 11, 8, 6])
def testPitchesToIntervals(self):
testRowUp = [x for x in range(12)]
self.assertEqual(pitchesToIntervals(testRowUp), [1]*11)
testRowDown = testRowUp[::-1]
self.assertEqual(pitchesToIntervals(testRowDown), [11]*11)
def testRotateHexachords(self):
"""Using Krenek's example"""
rowKrenek = [5, 7, 9, 10, 1, 3, 11, 0, 2, 4, 6, 8]
rotatedKrenek = rotateHexachords(rowKrenek)
self.assertEqual(len(rotatedKrenek), 7)
self.assertEqual(rotatedKrenek[-1], rowKrenek)
def testPairSwapAndRetrograde(self):
"""Using Krenek's example"""
testRow = [9, 2, 3, 6, 5, 1, 7, 4, 8, 0, 10, 11]
testPairSwapKrenek = pairSwapKrenek(testRow)
self.assertEqual(len(testPairSwapKrenek), 13)
self.assertEqual(testPairSwapKrenek[-1], retrograde(testRow))
# ------------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()