-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthlab.py
309 lines (264 loc) · 13.3 KB
/
synthlab.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
from __future__ import with_statement
from pymt import *
import os
import osc
host = '127.0.0.1'
port = 4444
class Workspace(MTScatterPlane):
def __init__(self, **kwargs):
super(Workspace,self).__init__( **kwargs)
osc.init()
self.modules = []
def remove_widget(self, widget):
if widget.parents != []:
for parent in widget.parents:
for index, module in enumerate(parent.signal_connections):
if module[0] == widget:
parent.signal_connections.pop(index)
for index, module in enumerate(parent.control_connections):
if module[0] == widget:
parent.control_connections.pop(index)
self.modules.remove(widget)
osc.sendMsg("/delete", [widget.category, widget.instance], host, port)
super(Workspace, self).remove_widget(widget)
class Module(MTSvg):
def __init__(self, **kwargs):
super(Module,self).__init__(**kwargs)
self.category = kwargs.get('category')
self.type = kwargs.get('type')
self.instance = kwargs.get('instance')
self.touchstarts = [] # only react to touch input that originated on this widget
self.mode = 'move'
self.drag_x = 0
self.drag_y = 0
self.control_connections = []
self.signal_connections = []
self.parents = []
def draw(self):
if self.mode == 'draw_connection':
set_color(1,1,1,1)
x1 = self.x + self.width / 2
y1 = self.y + 1
x2 = self.drag_x
y2 = self.drag_y
drawLine([x1, y1, x2, y2], width = 1)
set_color(1,1,1,1)
for module in self.control_connections:
drawLine(self.return_connection_coordinates(module), width = 1)
for module in self.signal_connections:
drawLine(self.return_connection_coordinates(module, 'signal'), width = 1)
super(Module, self).draw()
def return_connection_coordinates(self, m2, type='control'):
x1 = self.x +self.width / 2
y1 = self.y + 1
if m2[0].category == 'source':
x2 = m2[0].x + 16 + (m2[1] - 1) * 16
y2 = m2[0].y + m2[0].height - 1
if m2[0].category == 'effect':
x2 = m2[0].x + m2[0].width - 4
y2 = m2[0].y + m2[0].height - 20 - (m2[1] - 1) * 13
if type == 'signal':
x2 = m2[0].x + self.width / 2.
y2 = (m2[0].y + m2[0].height) - 1
return (x1, y1, x2, y2)
def line_collision_with_point(self, x1, y1, x2, y2, x, y):
# If line is vertical
if x1 == x2:
print 'hey'
print y, y1, y2
_max = max(y1,y2)
_min = min(y1,y2)
if y > _min and y < _max:
return True
else:
return False
m = float((y2 - y1)) / float((x2 - x1))
b = y1 - m*x1
result = abs(int(y) - int(m * x + b))
if result < 10:
return True
else:
return False
def on_touch_down(self, touch):
#Delete connections
if touch.is_double_tap:
for connection in self.signal_connections:
x1,y1,x2,y2 = self.return_connection_coordinates(connection, type='signal')
if self.line_collision_with_point(x1, y1, x2, y2, touch.x, touch.y):
self.signal_connections.remove([connection[0], connection[1]])
osc.sendMsg("/disconnect", [self.category, self.instance, connection[0].category, connection[0].instance], host, port)
for connection in self.control_connections:
x1,y1,x2,y2 = self.return_connection_coordinates(connection, type='control')
if self.line_collision_with_point(x1, y1, x2, y2, touch.x, touch.y):
self.control_connections.remove([connection[0], connection[1]])
osc.sendMsg("/disconnect", [self.category, self.instance, connection[0].category, connection[0].instance, connection[1]], host, port)
if self.collide_point(touch.x,touch.y):
self.touchstarts.append(touch.id)
if touch.is_double_tap:
self.parent.remove_widget(self)
self.first_x = touch.x
self.first_y = touch.y
self.first_pos_x = self.x
self.first_pos_y = self.y
if self.category is not 'output':
# Lower section
if touch.y < self.y + 20:
self.mode = 'draw_connection'
self.drag_x = touch.x
self.drag_y = touch.y
# Middle section
else:
self.mode = 'move'
return True
def on_touch_move(self, touch):
if touch.id in self.touchstarts:
delta_x = touch.x - self.first_x
delta_y = touch.y - self.first_y
if self.mode == 'move':
self.x = self.first_pos_x + delta_x
self.y = self.first_pos_y + delta_y
if self.mode == 'draw_connection':
self.drag_x = touch.x
self.drag_y = touch.y
return True
def on_touch_up(self, touch):
if self.mode == 'draw_connection':
for m in self.parent.modules:
if m.collide_point(touch.x,touch.y) and m != self and m.category != 'controller':
# Control connections
if self.category == 'controller' and m.category != 'output':
if m.category == 'source':
inlet_calc = int(round((touch.x - m.x) / ((m.width - 10) / 4.)))
if inlet_calc >= 1 and inlet_calc <= 4:
inlet = inlet_calc
else: inlet = None
if m.category == 'effect':
inlet_calc = int(round((m.height - (touch.y - m.y)) / (m.height / 5.)))
if inlet_calc >= 1 and inlet_calc <= 4:
inlet = inlet_calc
else: inlet = None
if inlet:
if [m, inlet] not in self.control_connections:
self.control_connections.append([m, inlet])
m.parents.append(self)
osc.sendMsg("/connect", [self.category, self.instance, m.category, m.instance, inlet], host, port)
# Signal connections
if self.category == 'source' or self.category == 'effect':
if m.category != 'source':
inlet = 0
if [m, inlet] not in self.signal_connections:
self.signal_connections.append([m, inlet])
m.parents.append(self)
osc.sendMsg("/connect", [self.category, self.instance, m.category, m.instance], host, port)
if touch.id in self.touchstarts:
self.touchstarts.remove(touch.id)
self.mode = 'move'
return True
class ModulePick(MTSvg):
def __init__(self, **kwargs):
super(ModulePick,self).__init__(**kwargs)
self.workspace_cb = kwargs.get('workspace_cb')
self.category = kwargs.get('category')
self.touchstarts = [] # only react to touch input that originated on this widget
self.drag_x = 0
self.drag_y = 0
self.original_pos = self.pos
self.instance_count = 1
self.icons = {'source':'icons/sl-addSynth+.svg', 'effect':'icons/sl-distort+.svg', 'controller':'icons/sl-lfo+.svg', 'output':'icons/sl-speaker.svg'}
def draw(self):
super(ModulePick, self).draw()
def on_touch_down(self, touch):
if self.collide_point(touch.x,touch.y):
self.touchstarts.append(touch.id)
self.first_x = touch.x
self.first_y = touch.y
self.first_pos_x = self.x
self.first_pos_y = self.y
return True
def on_touch_move(self, touch):
if touch.id in self.touchstarts:
delta_x = touch.x - self.first_x
delta_y = touch.y - self.first_y
self.x = self.first_pos_x + delta_x
self.y = self.first_pos_y + delta_y
return True
def on_touch_up(self, touch):
if touch.id in self.touchstarts:
xoffset = touch.x - self.x
yoffset = touch.y - self.y
xx, yy = self.workspace_cb.to_local(touch.x, touch.y)
self.workspace_cb.modules.append(Module(pos = (xx - xoffset, yy - yoffset) , filename = self.icons[self.category], category = self.category, instance = self.instance_count))
self.workspace_cb.add_widget(self.workspace_cb.modules[len(self.workspace_cb.modules)-1])
osc.sendMsg("/create", [self.category, self.instance_count], host, port)
self.pos = self.original_pos
self.instance_count += 1
self.touchstarts.remove(touch.id)
return True
class MasterControls(MTBoxLayout):
def __init__(self, **kwargs):
super(MasterControls,self).__init__(**kwargs)
self.workspace_cb = kwargs.get('workspace_cb')
self.volume = MTSlider(min = 0, max = 1, height = 200)
self.dspactive = MTToggleButton(label = 'DSP', size = (50,50))
self.clear_modules = MTButton(label = 'Clear', size = (50,50))
@self.volume.event
def on_value_change(value):
osc.sendMsg("/master/volume", [value], host, port)
@self.dspactive.event
def on_press(value):
if self.dspactive.state == 'down':
osc.sendMsg("/master/dspactive", [1], host, port)
else:
osc.sendMsg("/master/dspactive", [0], host, port)
@self.clear_modules.event
def on_press(value):
for module in self.workspace_cb.modules[:]:
self.workspace_cb.remove_widget(module)
self.add_widget(self.volume)
self.add_widget(self.dspactive)
self.add_widget(self.clear_modules)
class ModulePicker(MTWidget):
def __init__(self, **kwargs):
super(ModulePicker,self).__init__(**kwargs)
self.workspace_cb = kwargs.get('workspace_cb')
self.touchstarts = [] # only react to touch input that originated on this widget
self.spacing = 4
self.source_img = MTSvg(filename = 'icons/sl-addSynth+.svg', pos = self.pos)
self.effect_img = MTSvg(filename = 'icons/sl-distort+.svg', pos = (self.x + self.source_img.width + self.spacing, self.y))
self.controller_img = MTSvg(filename = 'icons/sl-lfo+.svg', pos = (self.effect_img.x + self.source_img.width + self.spacing, self.y))
self.output_img = MTSvg(filename = 'icons/sl-speaker.svg', pos = (self.controller_img.x + self.source_img.width + self.spacing, self.y - 10))
self.source = ModulePick(filename = 'icons/sl-addSynth+.svg', pos = self.pos, workspace_cb = self.workspace_cb, category = 'source')
self.effect = ModulePick(filename = 'icons/sl-distort+.svg', pos = (self.x + self.source.width + self.spacing, self.y), workspace_cb = self.workspace_cb, category = 'effect')
self.controller = ModulePick(filename = 'icons/sl-lfo+.svg', pos = (self.effect.x + self.source.width + self.spacing, self.y), workspace_cb = self.workspace_cb, category = 'controller')
self.output = ModulePick(filename = 'icons/sl-speaker.svg', pos = (self.controller.x + self.source.width + self.spacing, self.y - 10), workspace_cb = self.workspace_cb, category = 'output')
self.add_widget(self.source_img)
self.add_widget(self.effect_img)
self.add_widget(self.controller_img)
self.add_widget(self.output_img)
self.add_widget(self.source)
self.add_widget(self.effect)
self.add_widget(self.controller)
self.add_widget(self.output)
class NoteWidget(MTWidget):
def __init__(self, **kwargs):
super(NoteWidget,self).__init__(**kwargs)
self.first_touch = None
def on_touch_down(self, touch):
osc.sendMsg("/note/" + str(touch.id).strip('mouse'), ['on', touch.sx], host, port)
self.first_touch = touch.sx
def on_touch_move(self, touch):
osc.sendMsg("/note/" + str(touch.id).strip('mouse'), ['bend', touch.sx], host, port)
print self.first_touch - touch.sx
def on_touch_up(self, touch):
osc.sendMsg("/note/" + str(touch.id).strip('mouse'), ['off', touch.sx], host, port)
w = MTWindow()
w.style['bg-color'] = (0,0,0,1)
workspace = Workspace(do_rotation = False, auto_bring_to_front = False)
mastercontrols = MasterControls(pos = (2,2), spacing = 4, workspace_cb = workspace)
modulepicker = ModulePicker(pos = (160,2), workspace_cb = workspace)
notewidget = NoteWidget(size = (800,600))
w.add_widget(workspace)
w.add_widget(mastercontrols)
w.add_widget(modulepicker)
w.add_widget(notewidget)
runTouchApp()