-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz_manager.py
283 lines (247 loc) · 10.5 KB
/
viz_manager.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
from dataclasses import dataclass, field
from GUI import print_table
import sig_gen as sg
import kaleidoscope as kal
import numpy as np
import json
import os
from midi_controls import MIDIKNOBS
PRESETFILE = "preset_lib.json"
REFLECTIONFILE = "kal_rotations_lib.json"
@dataclass(unsafe_hash=True)
class Parameters:
'''Class for storing parameters before the Manager adopts them'''
r_freq: int = 1
r_state: int = 0
r_waveform_ix: int = 0
g_freq: int = 1
g_state: int = 0
g_waveform_ix: int = 0
b_freq: int = 1
b_state: int = 0
b_waveform_ix: int = 0
px_scan_speed: int = 50
increments: int = 0
kaleidoscope_ix: int = 0
k_n_segments: int = 0
k_flip: int = 1
k_alternate_flip: int = 1
k_manual_rot_curr: tuple = (None, None)
k_manual_rot: tuple = ((120, True), (240, True), (360, True))
winw: int = 600
winh: int = 600
w: int = 600
h: int = 600
need_update: tuple = ()
update_state: bool = False
current_preset_num: int = 0
current_refl_num: int = 0
def __setattr__(self, name, value):
if name == "need_update":
self.__dict__["need_update"] = value
else:
#print("set", name, value)
super().__setattr__(name, value)
self.need_update = self.need_update + (name,)
@classmethod
def from_dict(cls, d):
instance = cls(**d)
return instance
def to_dict(self):
return self.__dict__
class Vizard():
def __init__(self):
self.interface_mode = True
self.debugmode = True
self.increments = 1
self.params = Parameters()
self.w = self.params.w
self.h = self.params.h
self.winh = self.params.winh
self.winw = self.params.winw
self._numpix = self.params.w * self.params.h
self.px_scan_speed = int(self.params.px_scan_speed)
self.waveforms = [sg.sin, sg.square_wave,
sg.noise, sg.constant, sg.triangle]
self.rchannel = sg.SignalGenerator(self.waveforms, self._numpix,
self.params.r_freq, id="red")
self.gchannel = sg.SignalGenerator(self.waveforms, self._numpix,
self.params.r_freq, id="green")
self.bchannel = sg.SignalGenerator(self.waveforms, self._numpix,
self.params.r_freq, id="blue")
self.k_ix = 0
self.kal1 = kal.Multiscope(self.params.k_flip, self.params.w,
self.params.k_n_segments,
self.params.k_alternate_flip)
self.kal2 = kal.Recurseoscope(self.params.k_flip, self.params.w,
self.params.k_n_segments)
self.kal3 = kal.KaleidoscopeMultiMirror(self.params.k_flip,
self.params.w)
self.kaleidoscopes = [None, self.kal1, self.kal2, self.kal3]
self.win_canvas = np.empty((self.params.winh, self.params.winw, 3),
dtype=np.float32)
self.presets = {}
#self.refl_presets = {}
self.load_preset_lib()
self.load_reflection_lib()
self.extra_message = ""
self.debug_message = ""
self.midi_msg_str = ""
self.error_message = ""
def load_reflection_lib(self):
print("Load Reflection Lib")
with open(REFLECTIONFILE, 'r') as f:
self.refl_presets = json.load(f)
def load_reflection_from_preset(self, number):
self.load_reflection_lib()
p = self.refl_presets[str(number)]
p = tuplify(p)
self.params.k_manual_rot = p
return f"Loaded Reflections from {number}"
def load_preset_lib(self):
print("Load Preset Lib")
with open(PRESETFILE, 'r') as f:
self.presets = json.load(f)
def save_preset_lib(self):
print("Save Preset Lib")
with open(PRESETFILE, 'w') as f:
json.dump(self.presets, f)
def add_params_as_preset(self, number):
p = self.params.to_dict().copy()
p["need_update"] = ()
print(p)
self.presets[str(number)] = p
self.save_preset_lib()
return f"Added new Preset in {number}"
def load_params_from_preset(self, number):
stay_at_n = self.params.current_preset_num
p = self.presets[str(number)]
p = tuplify(p)
print(p)
self.params = Parameters.from_dict(p)
update_names = (tuplify(list(self.presets[str(number)].keys())))
self.params.need_update = update_names
self.params.update_state = True
self.params.current_preset_num = stay_at_n
return f"Loaded Preset from {number}"
def update_params(self):
""" Deploys the parameter values to the correct places """
for name in self.params.need_update:
match name:
case "w":
self.w = self.params.w
self._numpix = self.w * self.h
# Signal Generators
self.rchannel.numpix = self._numpix
self.gchannel.numpix = self._numpix
self.bchannel.numpix = self._numpix
self.kaleidoscopes[1].full_size = self.params.w
self.kaleidoscopes[2].size = self.params.w
self.kaleidoscopes[3].size = self.params.w
case "h":
self.h = self.params.h
self._numpix = self.w * self.h
# Signal Generators
self.rchannel.numpix = self._numpix
self.gchannel.numpix = self._numpix
self.bchannel.numpix = self._numpix
case "winh":
self.winh = self.params.winh
print("set", self.winh)
case "winw":
self.winw = self.params.winw
print("set", self.winw)
case "r_freq":
self.rchannel.freq = self.params.r_freq
case "g_freq":
self.gchannel.freq = self.params.g_freq
case "b_freq":
self.bchannel.freq =self.params.b_freq
case "r_waveform_ix":
self.rchannel.waveform_ix = self.params.r_waveform_ix
case "g_waveform_ix":
self.gchannel.waveform_ix = self.params.g_waveform_ix
case "b_waveform_ix":
self.bchannel.waveform_ix = self.params.b_waveform_ix
case "kaleidoscope_ix":
self.k_ix = (self.params.kaleidoscope_ix)
case "k_flip":
# the first one is always None
for k in self.kaleidoscopes[1:4]:
k.flipped = self.params.k_flip
case "k_alternate_flip":
for k in self.kaleidoscopes[1:3]:
k.alternate_flip = self.params.k_alternate_flip
case "k_n_segments":
for k in self.kaleidoscopes[1:3]:
k.n_segments = self.params.k_n_segments
case "k_manual_rot":
if not len(self.params.k_manual_rot) == 0:
self.kaleidoscopes[3].rotations = \
[i[0] for i in self.params.k_manual_rot]
self.kaleidoscopes[3].rotations_dir = \
[i[1] for i in self.params.k_manual_rot]
else:
self.kaleidoscopes[3].rotations = []
self.kaleidoscopes[3].rotations_dir = []
case _:
# in the default case, take the value for name from params
# and write it into the manager
value = self.params.__getattribute__(name)
self.__setattr__(name, value)
if self.win_canvas.shape != (self.winh, self.winw, 3):
self.win_canvas = np.zeros((self.winh, self.winw, 3),
dtype=np.float32)
if self.params.update_state:
self.rchannel.state = self.params.r_state
self.gchannel.state = self.params.g_state
self.bchannel.state = self.params.b_state
self.params.update_state = False
if len(self.params.need_update) != 0:
if self.interface_mode:
print_table(MIDIKNOBS, self.params.to_dict(), self.extra_message)
print("Width, Height: " + str([self.w, self.h,]) +
"; Window Size: " + str([self.winw, self.winh]))
print("Rotations List: " + str(self.params.k_manual_rot))
print(str(self.error_message))
if self.debugmode:
print(self.midi_msg_str)
print(self.debug_message)
self.params.need_update = ()
def get_RGB(self, n_scanned_px):
r = self.rchannel.get_series(n_scanned_px)
g = self.gchannel.get_series(n_scanned_px)
b = self.bchannel.get_series(n_scanned_px)
rgb = np.array([r, g, b]).T.reshape(self.w, self.h, 3)
return rgb
def apply_kaleidoscope(self, rgb):
if self.k_ix > 0:
rgb = self.kaleidoscopes[self.k_ix].kaleide(rgb)
return rgb
def embed_rgb_in_window(self, rgb):
leftover_w = self.winw - self.w
left = int(leftover_w / 2)
right = leftover_w - left
leftover_h = self.winh - self.h
top = int(leftover_h / 2)
bottom = leftover_h - top
reflect = False
if not reflect:
self.win_canvas[top:(top + self.h),
left:(left + self.w), :] = rgb
else:
if left > 1:
# print(rgb[:, 0:left, :].shape)
# print(rgb[:, 0:left:-1, :].shape)
self.win_canvas[:, 0:left, :] = rgb[:, 0:left, :][:, ::-1, :]
self.win_canvas[:, (self.winw - right):self.winw, :] = \
rgb[:, (self.w - right):self.w, :][:, ::-1, :]
if top > 1:
self.win_canvas[0:top, :, :] = rgb[0:top, :, :][::-1, :, :]
self.win_canvas[(self.winh - bottom):self.winh, :, :] = \
rgb[(self.h - bottom):self.h, :, :][::-1, :, :]
return self.win_canvas
def tuplify(listything):
if isinstance(listything, list): return tuple(map(tuplify, listything))
if isinstance(listything, dict): return {k:tuplify(v) for k,v in listything.items()}
return listything