-
Notifications
You must be signed in to change notification settings - Fork 2
/
seq_widget.py
322 lines (274 loc) · 11.9 KB
/
seq_widget.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
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line, Rectangle
from random import randint, uniform
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.clock import Clock
from functools import partial
from kivy.graphics.instructions import InstructionGroup
from kivy.utils import get_color_from_hex
from kivy.core.window import Window
from pyo import *
import multiprocessing
import time
from aengine_thread import AudioItem
stress_test = False
#TODO: select sound from sound palette
#TODO: loop bar dragging
#TODO: snap to grid lines - done
# -- fix snapping to center of cursor
#TODO: play sounds from thread
#TODO: audio mixer
#TODO: effects
#TODO: synth sounds
#TODO: waveform view
#TODO: zoom gridlines
def paint_stress_test(width, height):
# stress test
for x in range(1000):
Rectangle(pos=(randint(1,width),randint(0, height)), size=(5, 5))
class PlayHead(Widget):
def __init__(self, height, location=40):
self.height = height
self.playhead_line = Line(points=[location, self.height, location, 0])
self.playhead_line.width = 2
def moveX(self, increment):
self.playhead_line.points = [30+increment,self.height,30+increment,0]
Line.posX= (40)
print("moveX called")
class GridLines(object):
def __init__(self):
self.lines = []
self.space = 8
self.snap_thresh = self.space -1
self.start = 0
# self.amt = screen_width / self.space
# self.screen = screen
# for x in range(0,int(self.amt)):
# l = pygame.Rect(self.start,0,1,pygame.Surface.get_height(screen))
# self.lines.append(l)
# self.start+=self.space
def drawlines(self):
c = 4
v = 16
for x in range(0,len(self.lines)):
if c==0:
# pygame.draw.rect(self.screen, (44,44,44), self.lines[x], 2)
c = 4
if v == 0:
pygame.draw.rect(self.screen, (44,44,44), self.lines[x], 4)
v = 16
else:
pygame.draw.rect(self.screen, (29,29,29), self.lines[x], 1)
c-=1
v-=1
class SeqGridWidget(Widget):
def __init__(self, **kwargs):
super(SeqGridWidget, self).__init__(**kwargs)
# Change main widget background color
Window.clearcolor=get_color_from_hex("#444444")
# main widget
self.main_lines = []
self.audio_items = []
self.d=5.
self.size_hint=(None,None)
self.size=(11100,11100)
self.width = self.size[0]
self.height = self.size[1]
# grid
self.space = 32
self.start = 0
self.amt = (self.width / self.space) * 2
self.drag = False
self.selected_item = None
# Playhead
self.ph = Line(points=[30,self.height,30, self.height])
self.ph.width = 6
self.inc = 0
self.playhead_increment = 0
self.isPlayheadAdjust = False
# loop bars
self.loop = False
self.loops = InstructionGroup()
# self.items = []
# self.grid_lines_main = []
def draw_grid(amt, start, width, height, space):
Color(1, 1, 1)
# lines that are added to main_lines could be put in
# an InstructionGroup and edited later for line spacing
for x in range(int(amt)):
if x % 4 == 0:
# every 4th line is darker
# vertical line - thick width
Color(.2,.2,.2)
L = Line(points=[0+start, height, 0+start, 0])
L.width = 2
self.main_lines.append(L)
else:
Color(.2,.2,.2)
# vertical line - normal width
L = Line(points=[0+start, height, 0+start, 0])
self.main_lines.append(L)
start+=space
Color(.2,.2,.2)
# horizontal line
L = Line(points=[0, start, width, start])
self.main_lines.append(L)
with self.canvas:
draw_grid(self.amt,self.start,self.width,self.height, self.space)
# Change playhead color to blue
# Color(0.25, 0.95, 0.87, 1)
# x = get_color_from_hex("#F685BD")
Color(0.96, 0.52, 0.74)
self.canvas.add(self.ph)
with self.canvas:
# top grey horizontal bar
# controls playhead skipping / looping markers
Color(0.43, 0.43, 0.43, 1)
Rectangle(pos=(0,self.height-20), size=(self.width, 20))
def adjust_playhead(self, touch):
if touch.y > (self.height-20):
self.isPlayheadAdjust = True
self.playhead_increment = touch.x - self.space * 2
p = [touch.x, self.height, touch.x, 0]
with self.canvas:
self.ph.points = p
else:
self.isPlayheadAdjust = False
# temporarily moves playhead on screen
def move_playhead(self):
self.playhead_increment += self.space
# if playhead reaches end of window width, loop back to beginning
# if self.playhead_increment > Window.size[0]:
# self.playhead_increment=0
# self.size is for testing inside the widget, Window.size[0]
# is normal use
if self.playhead_increment > self.size[0]:
self.playhead_increment=0
p = [30+self.playhead_increment,self.height,30+self.playhead_increment,0]
# print(p)
with self.canvas:
self.ph.width = 2
self.ph.points = p
# Line(points=p)
# self.playhead.playhead_line
# self.playhead.playhead_line.width=300
# print(self.playhead.playhead_line)
# self.playhead.moveX(self.playhead_incremen)
def loop_func(self, loop):
if loop:
Color(0,1,1)
loopL = Line(points=[30+20,self.height,30+20,0])
loopR = Line(points=[30+70,self.height,30+70,0])
handle_size=(20,20)
top_padding = 20
loopHandleL = Rectangle(pos=(loopL.points[0]-(handle_size[0]/2),self.height-top_padding), size=handle_size)
loopHandleR = Rectangle(pos=(loopR.points[0]-(handle_size[0]/2),self.height-top_padding), size=handle_size)
self.loops.add(loopL)
self.loops.add(loopR)
self.loops.add(loopHandleL)
self.loops.add(loopHandleR)
self.canvas.add(self.loops)
else:
self.canvas.remove(self.loops)
self.loops.clear()
def show_audio_items_stats(self):
print("*"*20)
for item in self.audio_items:
print("Block pos", item.shape.pos)
print("Block size", item.shape.size)
print("Audio item count: ", len(self.audio_items))
print("*"*20)
def check_click(self, touch, box, button_type):
# checks that we're in bounds when a button/rect is pressed
# check for right or left buttons
res = touch.x >=box.shape.pos[0] and touch.x <= box.shape.pos[0]+box.shape.size[0] \
and touch.y >=box.shape.pos[1] and touch.y <= box.shape.pos[1]+box.shape.size[1] and \
touch.button == button_type
return res
def on_touch_down(self, touch):
super(SeqGridWidget, self).on_touch_down(touch)
# enables dragging of playhead
self.adjust_playhead(touch)
with self.canvas:
# if playhead is being moved, don't place a block/rect
if self.isPlayheadAdjust == False:
for box in self.audio_items:
if self.check_click(touch, box, 'left'):
self.drag = True
self.selected_item = box
print("selected item",self.selected_item)
print("in bounds")
print("left click")
if self.check_click(touch, box, 'right'):
print("right click")
self.selected_item = box
idx = self.audio_items.index(self.selected_item)
print("IS LABEL",self.audio_items[idx].text)
#next(widget for widget in app.root.walk if widget is ...).parent.remove_widget(...)
app = App.get_running_app()
self.remove_widget(self.audio_items[idx].text)
self.remove_widget(self.audio_items[idx])
self.audio_items[idx].clear_widgets()
# remove audio item (shape)
self.canvas.remove(self.audio_items[idx].shape)
# remove audio item (text)
print("remove text here")
# self.clear_widgets(self.audio_items)
# remove audio item
self.audio_items.remove(self.audio_items[idx])
# if not dragging and not right button, add new audio item
if self.drag == False and touch.button != 'right':
box_size = self.space
ai = AudioItem("sounds/snare1.wav", 100, 100, 100, [touch.x - box_size/2, touch.y - box_size/2], [box_size, box_size])
# add to audio_item list
self.audio_items.append(ai)
self.new_check_snap_to_grid(ai, touch)
if stress_test:
paint_stress_test(self.width, self.height)
self.show_audio_items_stats()
def on_touch_up(self, touch):
self.drag = False
def new_check_snap_to_grid(self, item, touch):
for line in self.main_lines:
lineX = line.points[0]
lineY = line.points[1]
selShapeX = item.shape.pos[0]
selShapeY = item.shape.pos[1]
if touch.x > lineX and touch.x <= lineX + self.space:
item.shape.pos = (lineX, selShapeY)
item.text.pos = (item.shape.pos[0], item.shape.pos[1])
if touch.y > lineY and touch.y <= lineY + self.space:
item.shape.pos = (selShapeX, lineY)
item.text.pos = (item.shape.pos[0], item.shape.pos[1])
# item.text.size = (32,32)
# if selShapeY > lineY + self.space/2 and selShapeY <= lineY + self.space:
# item.shape.pos = (selShapeX, lineY)
# item.text.pos = (item.shape.pos[0], item.shape.pos[1])
# # snap to vertical line within spacing px amount
# if selShapeX > lineX and selShapeX <= lineX + self.space-1:
# item.shape.pos = (lineX, selShapeY)
# item.text.pos = (item.shape.pos[0], item.shape.pos[1])
# # snap to horizontal line within spacing px amount
# elif selShapeY > lineY and selShapeY <= lineY + self.space-1:
# item.shape.pos = (selShapeX, lineY)
# item.text.pos = (item.shape.pos[0], item.shape.pos[1])
def on_touch_move(self, touch):
# if mouse is less than grid height-20, change pos of playhead
# on touch drag
# if touch.y > (self.height-20):
# self.playhead_increment = touch.x - self.space*2
self.adjust_playhead(touch)
if self.drag == True:
self.selected_item.shape.pos = (touch.x - self.selected_item.shape.size[0]/2, touch.y-self.selected_item.shape.size[1]/2)
self.selected_item.text.pos = (touch.x - self.selected_item.shape.size[0]/2, touch.y-self.selected_item.shape.size[1]/2)
print("drag " + str(self.selected_item.shape.pos))
self.new_check_snap_to_grid(self.selected_item, touch)
else:
self.drag = False
class SeqGridWidgetApp(App):
def build(self):
return SeqGridWidget()
if __name__ == '__main__':
SeqGridWidgetApp().run()