-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateseq_patterns.py
267 lines (229 loc) · 8.69 KB
/
gateseq_patterns.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
import struct
from viatools.via_resource import ViaResource, ViaResourceSet
class GateseqPattern(ViaResource):
def load(self, json_path):
self.pattern_size = 16
if not super().load(json_path):
return False
self.sorted = True
if 'sorted' in self.data:
if self.data['sorted'] is False:
self.sorted = False
self.sort()
return True
def save(self, json_path):
self.pad_to_length()
super().save(json_path)
def add_data(self, recipe, idx=None):
if idx:
self.data['data'].insert(idx, recipe)
self.sort()
else:
self.data['data'].append(recipe)
self.sort()
insert_idx = len(self.data['data']) - 1
return insert_idx
def clear_data(self):
old_data = self.data['data']
self.data['data'] = [[1,1]]
self.sort()
return old_data
def reorder_data(self, idx_to_move, destination):
self.data['data'].insert(destination, self.data['data'].pop(idx_to_move))
self.sort()
def reload_data(self, data_copy):
self.data['data'] = []
for ratio in data_copy:
self.data['data'].append(ratio)
self.sort()
def remove_data(self, index):
self.data['data'].pop(index)
self.sort()
def get_length(self, seq_idx):
self.bake()
return len(self.baked)
def get_data(self):
data = []
for recipe in self.data['data']:
data.append(recipe)
return data
def get_recipe(self, seq_idx):
return self.data['data'][seq_idx]
def update_length(self, seq_idx, length):
if self.is_euclidean_recipe(self.data['data'][seq_idx]):
self.data['data'][seq_idx] = self.euclidean_to_bool(self.data['data'][seq_idx])
current_length = len(self.data['data'][seq_idx])
if current_length >= length:
self.data['data'][seq_idx] = self.data['data'][seq_idx][0:length]
else:
for i in range(current_length, length):
self.data['data'][seq_idx].append(False)
save_me = self.data['data'][seq_idx]
#TODO sorted/unsorted
self.sort()
return self.data['data'].index(save_me)
def update_step(self, seq_idx, step_idx, state):
if self.is_euclidean_recipe(self.data['data'][seq_idx]):
self.data['data'][seq_idx] = self.euclidean_to_bool(self.data['data'][seq_idx])
self.data['data'][seq_idx][step_idx] = state
save_me = self.data['data'][seq_idx]
#TODO sorted/unsorted
self.sort()
return self.data['data'].index(save_me)
def reload_recipe(self, seq_idx, recipe):
self.data['data'][seq_idx] = recipe
self.sort()
def bake(self):
self.pad_to_length()
self.baked = []
for recipe in self.data['data']:
self.baked.append(self.expand_sequence(recipe))
def update_sorted(self, is_sorted):
self.data['sorted'] = is_sorted
self.sorted = is_sorted
old_order = []
for pattern in self.data['data']:
old_order.append(pattern)
self.sort()
return old_order
def sort(self):
pattern_sort = []
for idx, ratio in enumerate(self.data['data']):
pattern_sort.append((ratio, idx))
if self.sorted:
pattern_sort.sort(key=self.get_density)
self.data['sorted_patterns'] = []
self.data['unsorted_indices'] = []
for pattern in pattern_sort:
self.data['sorted_patterns'].append(pattern[0])
self.data['unsorted_indices'].append(pattern[1])
def get_density(self, recipe):
pattern = self.expand_sequence(recipe[0])
return pattern.count(1)/len(pattern)
def pad_to_length(self):
self.data['expanded_data'] = []
if self.data['sorted_patterns'] == []:
self.data['sorted_patterns'] = [[0,1],[1,1]]
if len(self.data['sorted_patterns']) < self.pattern_size:
out_size = self.pattern_size
#TODO copied from sync3_scales.py
relative_indices = []
for idx in range(0, len(self.data['sorted_patterns'])):
rel = idx * (out_size/len(self.data['sorted_patterns']))
rel += (out_size - 1) - (len(self.data['sorted_patterns']) - 1) * (out_size/len(self.data['sorted_patterns']))
relative_indices.append(rel)
to_add = 0
for notch in range(0, int(out_size)):
self.data['expanded_data'].append(self.data['sorted_patterns'][to_add])
if notch >= relative_indices[to_add]:
to_add += 1
else:
for pattern in self.data['sorted_patterns']:
self.data['expanded_data'].append(pattern)
def get_name(self, idx):
sequence = self.data['sorted_patterns'][idx]
if self.is_euclidean_recipe(sequence):
return '%s/%s' % (sequence[0], sequence[1])
else:
return '!%s/%s!' % (str(sequence.count(True)), str(len(sequence)))
def is_euclidean_recipe(self, recipe):
if str(recipe[0]).isdigit():
return True
else:
return False
def expand_sequence(self, recipe):
if self.is_euclidean_recipe(recipe):
sequence = self.expand_euclidean(recipe[0], recipe[1])
else:
sequence = self.expand_bool(recipe)
int_sequence = []
for step in sequence:
int_sequence.append(int(step))
return int_sequence
# from https://github.com/brianhouse/bjorklund
def expand_euclidean(self, pulses, steps):
steps = int(steps)
pulses = int(pulses)
if pulses > steps:
raise ValueError
if pulses == 0:
zero_pad = []
for i in range(0, steps):
zero_pad.append(0)
return zero_pad
sequence = []
counts = []
remainders = []
divisor = steps - pulses
remainders.append(pulses)
level = 0
while True:
counts.append(divisor / remainders[level])
remainders.append(divisor % remainders[level])
divisor = remainders[level]
level = level + 1
if remainders[level] <= 1:
break
counts.append(divisor)
def build(level):
if level == -1:
sequence.append(0)
elif level == -2:
sequence.append(1)
else:
for i in range(0, int(counts[level])):
build(level - 1)
if remainders[level] != 0:
build(level - 2)
build(level)
i = sequence.index(1)
sequence = sequence[i:] + sequence[0:i]
return sequence
def euclidean_to_bool(self, recipe):
expanded = self.expand_euclidean(recipe[0], recipe[1])
new_seq = []
for step in expanded:
if step == 1:
new_seq.append(True)
else:
new_seq.append(False)
return new_seq
def expand_bool(self, recipe):
sequence = []
for step in recipe:
if step is True:
sequence.append(1)
else:
sequence.append(0)
return sequence
class GateseqPatternSet(ViaResourceSet):
def __init__(self, resource_dir, slug):
super().__init__(GateseqPattern, slug, resource_dir, resource_dir + 'patterns/')
self.output_dir = resource_dir + 'binaries/'
self.pattern_size = 16
def pack_binary(self, write_dir=None, title=None):
if not title:
title=self.slug
if not write_dir:
write_dir = self.output_dir
packer = struct.Struct('<%dI%dI' % (self.pattern_size, self.pattern_size))
compiled_structs = []
binary_offset = self.pattern_size * 2 * 8
for pattern in self.resources:
pattern.bake()
pack = []
for sequence in pattern.baked:
pack.append(binary_offset)
binary_offset += len(sequence)
for sequence in pattern.baked:
pack.append(len(sequence))
compiled_structs.append(packer.pack(*pack))
for pattern in self.resources:
for sequence in pattern.baked:
packer = struct.Struct('<%dI' % len(sequence))
compiled_structs.append(packer.pack(*sequence))
resource_path = write_dir + title + '.gateseq'
with open(resource_path, 'wb') as outfile:
for chunk in compiled_structs:
outfile.write(chunk)
return resource_path