-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
327 lines (257 loc) · 8.98 KB
/
utilities.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
__author__ = 'mpevans'
import os
import sys
import pickle
import inspect
import math
import numpy as np
import bisect
import copy
def enum(**named_values):
return type('Enum', (), named_values)
# decorator that saves function output
def save_answers(func):
func.answers = {}
def func_wrapper(*args):
t_args = tuple(args)
if t_args in func.answers:
return func.answers[t_args]
else:
ans = func(*args)
func.answers[t_args] = ans
return ans
return func_wrapper
# determines if application is a script file or frozen exe
def get_relative_file_path(file_name):
if getattr(sys, 'frozen', False):
application_path = os.path.join(os.path.dirname(sys.executable), "..")
else:
frm = inspect.stack()[1]
mod = inspect.getmodule(frm[0])
application_path = os.path.dirname(mod.__file__)
return os.path.join(application_path, file_name)
def save_object(obj, filename):
with open(filename, 'wb') as output:
pickle.dump(copy.deepcopy(obj), output, pickle.HIGHEST_PROTOCOL)
def load_object(filename):
out = None
with open(filename, 'rb') as input_:
out = pickle.load(input_)
return out
pc_number_to_name = {
0: "C",
1: "C#/Db",
2: "D",
3: "D#/Eb",
4: "E",
5: "F",
6: "F#/Gb",
7: "G",
8: "G#/Ab",
9: "A",
10: "A#/Bb",
11: "B"
}
def get_pitch_description(pitch, accidental_type="standard"):
pitch = round(pitch)
octave = int(pitch/12) - 1
pc_name = pc_number_to_name[pitch % 12]
if "/" in pc_name and accidental_type != "both":
# unless accidental type is "both", we need to pick sharp or flat
# let's have the standard choice be C#, Eb, F#, Ab, Bb
if accidental_type == "standard":
accidental_type = "flat" if pitch % 12 in (3, 8, 10) else "sharp"
# though by setting accidental_type to "flat" or "sharp", the user can choose
if accidental_type.lower() == "flat":
pc_name = pc_name.split("/")[1]
elif accidental_type.lower() == "sharp":
pc_name = pc_name.split("/")[0]
return pc_name + str(octave)
def get_pitch_class_description(pitch_class, accidental_type="standard"):
pc_name = pc_number_to_name[pitch_class]
if "/" in pc_name and accidental_type != "both":
# unless accidental type is "both", we need to pick sharp or flat
# let's have the standard choice be C#, Eb, F#, Ab, Bb
if accidental_type == "standard":
accidental_type = "flat" if pitch_class in (3, 8, 10) else "sharp"
# though by setting accidental_type to "flat" or "sharp", the user can choose
if accidental_type.lower() == "flat":
pc_name = pc_name.split("/")[1]
elif accidental_type.lower() == "sharp":
pc_name = pc_name.split("/")[0]
return pc_name
def get_interval_cycle_length(cycle_size):
x = cycle_size
length = 1
while x % 12 != 0:
x += cycle_size
length += 1
return length
def get_interval_cycle_distance(pc1, pc2, cycle_size):
cycle_length = get_interval_cycle_length(cycle_size)
distance = 0
while (pc1 - pc2) % 12 != 0:
pc2 += cycle_size
distance += 1
if distance > cycle_length:
return - 1
if distance > cycle_length/2:
return cycle_length-distance
return distance
# took this disgustingly unreadable function from somewhere
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(int(n/10)%10!=1)*(n%10<4)*n%10::4])
def is_x_pow_of_y(x, y):
a = math.log(x, y)
if a == int(a):
return True
else:
return False
def floor_x_to_pow_of_y(x, y):
a = math.log(x, y)
return y ** math.floor(a)
def round_x_to_pow_of_y(x, y):
a = math.log(x, y)
return y ** (int(round(a)) if isinstance(y, int) else round(a))
def midi_to_freq(midi_val):
return 440.0 * 2**((midi_val - 69.0)/12)
def freq_to_midi(frequency):
return math.log(frequency/440.0, 2.0) * 12 + 69.0
def cents_to_ratio(cents):
return 2**(cents/1200.0)
def ratio_to_cents(ratio):
return math.log(ratio)/math.log(2) * 1200.0
def round_to_multiple(x, factor):
return round(x/factor)*factor
def is_multiple(x, y):
return round_to_multiple(x, y) == x
def get_bracketed_text(string, start_bracket, end_bracket):
out = []
next_start_bracket = string.find(start_bracket)
while next_start_bracket != -1:
string = string[next_start_bracket+len(start_bracket):]
next_end_bracket = string.find(end_bracket)
if next_end_bracket == -1:
break
else:
out.append(string[:next_end_bracket])
string = string[next_end_bracket:]
next_start_bracket = string.find(start_bracket)
return out
# Color stuff
def get_luminance(r, g, b):
return 0.2126 * r + 0.7152 * g + 0.0722 * g
# Geometric shit
def get_projection_and_rejection_of_a_onto_b(a, b):
assert isinstance(a, np.ndarray) and a.ndim == 1
assert isinstance(b, np.ndarray) and a.ndim == 1
projection = np.dot(a, b) * b / np.linalg.norm(b)**2
rejection = a-projection
return projection, rejection
def get_projection_a_onto_b(a, b):
assert isinstance(a, np.ndarray) and a.ndim == 1
assert isinstance(b, np.ndarray) and a.ndim == 1
return np.dot(a, b) * b / np.linalg.norm(b)**2
def get_rejection_of_a_onto_b(a, b):
assert isinstance(a, np.ndarray) and a.ndim == 1
assert isinstance(b, np.ndarray) and a.ndim == 1
return a - np.dot(a, b) * b / np.linalg.norm(b)**2
# --------------------- list stuff ---------------------------
def make_flat_list(l, indivisible_type=None):
# indivisible_type is a type that we don't want to divide,
new_list = list(l)
i = 0
while i < len(new_list):
if hasattr(new_list[i], "__len__"):
if indivisible_type is None or not isinstance(new_list[i], indivisible_type):
new_list = new_list[:i] + new_list[i] + new_list[i+1:]
else:
i += 1
return new_list
def get_interpolated_array_value(array, index, power=1, cyclic=False, normalized=False):
if normalized:
index *= len(array)
index = round(index, 10)
# some special cases
if index < 0:
if cyclic:
index = index % len(array)
else:
return array[0]
elif index >= len(array)-1:
if cyclic:
index = index % len(array)
else:
return array[-1]
elif index == int(index):
return array[int(index)]
lower_index_value = array[int(index)]
upper_index_value = array[(int(index)+1) % len(array)]
progress_to_next = (index - int(index))**power
return lower_index_value * (1 - progress_to_next) + upper_index_value * progress_to_next
def get_closest_index(myList, value):
"""
Assumes myList is sorted. Returns closest value to myNumber.
If two numbers are equally close, return the index of the smallest number.
"""
pos = bisect.bisect_left(myList, value)
if pos == 0:
return 0
if pos == len(myList):
return len(myList) - 1
before = myList[pos - 1]
after = myList[pos]
if after - value < value - before:
return pos
else:
return pos - 1
def cyclic_slice(l, start, end):
# m
"""
takes a slice that loops back to the beginning if end is before start
:param l(list): the list to slice
:param start(int): start index
:param end(int): end index
:return: list
"""
if end >= start:
# start by making both indices positive, since that's easier to handle
while start < 0 or end < 0:
start += len(l)
end += len(l)
if end >= start + len(l):
out = []
while end - start >= len(l):
out.extend(l[start:] + l[:start])
end -= len(l)
out += cyclic_slice(l, start, end)
return out
else:
end = end % len(l)
start = start % len(l)
if end >= start:
return l[start:end]
else:
return l[start:] + l[:end]
else:
# if the end is before the beginning, we do a backwards slice
# basically this means we reverse the list, and recalculate the start and end
new_start = len(l)-start-1
new_end = len(l)-end-1
new_list = list(l)
new_list.reverse()
return cyclic_slice(new_list, new_start, new_end)
class CyclingTuple:
def __init__(self, *tuples_that_cycle):
self.tuples = tuples_that_cycle
self.num_versions = len(tuples_that_cycle)
self.current_version = 0
def __getitem__(self, item):
out = self.tuples[self.current_version][item]
return out
def __getslice__(self, i, j):
out = self.tuples[self.current_version][i: j]
return out
def __len__(self):
return len(self.tuples)
def next_cycle(self):
self.current_version = (self.current_version + 1) % self.num_versions