forked from HughTebby/ASSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.py
279 lines (222 loc) · 10.4 KB
/
UI.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
#!/bin/false
# this file takes care of the UI definition for the MASSS
import sys
import os
import os.path
import re
import tkinter as tk
import tkinter.ttk as ttk
import asyncio as aio
from math import floor, ceil
skip_detector = re.compile(r'(?P<core>.*?)\#'+
r'(?P<seconds>[0-9]*)'+
r'\.(?P<ext>[a-zA-Z0-9]*)')
# This class create a gtk.ToggleButton linked to a sound file.
# Send "toggle", it plays the sound. Send "toggle" again, it stops.
class SndButton(tk.Frame): # ## a button associated to a file. sends playback commands
def __init__(self, master, fullname, interface, parent_scrollableframe):
tk.Frame.__init__(self, master)
self.filename = fullname
self.name = os.path.split(fullname)[1]
self.interface = interface
self.vol_modifier = 1.0
self.instance_id = None
has_skip = skip_detector.fullmatch(self.name)
if has_skip:
self.skip = has_skip.groupdict()['seconds'] [:-1]
# the [:-1] is because tenths of seconds cannot be skipped yet.
else:
self.skip = None
self.state = tk.IntVar(master)
self.btn = tk.Checkbutton(self, text=self.name,
command=self.onPress, indicatoron=0, variable=self.state, compound=tk.BOTTOM)
self.vol = tk.Scale(self, from_=0, to=2, orient=tk.HORIZONTAL, resolution=0.05, length=150, command=self.onUpdate)
self.vol.set(1)
self.vol.pack(side=tk.LEFT)
self.btn.pack(side=tk.LEFT, fill=tk.X)
# ensure correct scrolling for parent...
for binded in (self, self.btn, self.vol):
binded.bind("<MouseWheel>", parent_scrollableframe.onMousewheel)
binded.bind("<Button-4>", parent_scrollableframe.onMousewheel)
binded.bind("<Button-5>", parent_scrollableframe.onMousewheel)
def onPress(self, event=None):
if self.state.get()==1: # at this point, the state is already changed!
# this will have to be done in two parts:
# the polling and the callback (because play() is a coroutine)
# callback is another method
playtask = self.interface.loop.create_task(
self.interface.play(self.filename, self.skip, self.onStop)
)
playtask.add_done_callback(self._onPress_activate_part2)
else:
if self.instance_id is None:
# it didn't start playing completely
print("warning: tried to stop track before full activation")
inst_id = self.instance_id
self.instance_id = None
self.interface.stop(inst_id)
def _onPress_activate_part2(self, future):
"""the callback for when the VLC instance started playing"""
result = future.result()
self.instance_id = result
self.onUpdate()
def onUpdate(self, event=None):
if self.instance_id is not None:
self.interface.set_vol_mod(self.instance_id, self.vol.get())
def onStop(self):
self.state.set(0)
self.btn.deselect()
class MainFileChooser(ttk.Notebook): # the main panel to load audio files
def __init__(self, master, interface, directory):
ttk.Notebook.__init__(self, master)
dirs = os.listdir(directory)
subfiles=[]
for subdir in dirs:
fullname = os.path.join(directory, subdir)
if os.path.isdir(fullname):
frame = FileChooserFrame(self, interface, fullname)
self.add(frame, text=subdir)
else:
subfiles.append(fullname)
if subfiles:
frame = FileChooserFrame(self, interface, directory, is_root=True)
self.add(frame, text='SOUNDS_ROOT')
class FileChooserFrame(tk.Frame): # one of the 'tabs' of the file panel
def __init__(self, master, interface, directory, is_root=False):
# ## basic UI configuration (heavier because we need a scrollable frame)
tk.Frame.__init__(self, master)
self.inner = tk.Canvas(self)
self.inframe = tk.Frame(self.inner)
self.bar = tk.Scrollbar(self, command=self.inner.yview)
self.bar.pack(side = tk.RIGHT, fill = tk.Y)
self.inner.configure(yscrollcommand = self.bar.set)
self.inframe_id = self.inner.create_window((0,0), window=self.inframe, anchor="nw")
self.inframe.bind('<Configure>', self.onInnerConfigure)
self.inner.pack(fill=tk.BOTH, expand=1)
self.inner.bind("<MouseWheel>", self.onMousewheel)
self.inner.bind("<Button-4>", self.onMousewheel)
self.inner.bind("<Button-5>", self.onMousewheel)
# subdir (and subfile) configuration
subdirs = os.listdir(directory)
subfiles = []
i=0
for i, subdir in enumerate(subdirs):
is_dir = os.path.isdir(os.path.join(directory, subdir))
if is_dir and not is_root:
frame = tk.LabelFrame(self.inframe, text=subdir, labelanchor='n')
#frame.columnconfigure(0, weight=1)
# manually bind children too to ensure correct scrolling...
frame.bind("<MouseWheel>", self.onMousewheel)
frame.bind("<Button-4>", self.onMousewheel)
frame.bind("<Button-5>", self.onMousewheel)
files = os.listdir(os.path.join(directory, subdir))
for j, file in enumerate(files):
button = SndButton(frame, os.path.join(directory, subdir, file), interface, self)
button.pack(fill=tk.X)
frame.grid(column=0,row=i, sticky='W')
elif not is_dir: # check that only files end up here
subfiles.append(subdir)
if subfiles:
frame = tk.LabelFrame(self.inframe, text='DIR_ROOT', labelanchor='n')
for j, file in enumerate(subfiles):
button = SndButton(frame, os.path.join(directory, file), interface)
button.pack(fill=tk.X)
frame.grid(column=0,row=i+1, align='W')
def onInnerConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
self.inner.configure(scrollregion=self.inner.bbox("all"))
# change canvas width. somehow only height is done automatically
self.inframe.update_idletasks()
self.inner.configure(width=self.inframe.winfo_width())
self.configure(width=self.inner.winfo_width()+self.bar.winfo_width())
def onMousewheel(self, event):
if event.delta != 0:
if event.delta < 0:
delta = floor(event.delta/120)
else:
delta = ceil(event.delta/120)
elif event.num in (4,5):
delta = int(2*(4.5-event.num))
else:
print('scrolling unimplemented here')
return
self.inner.yview("scroll", delta, "units")
return "break"
class EqFrame(tk.LabelFrame):
"""frame with the equaliser bars. knows when to call the VlcInterface to adjust its equalizer."""
def __init__(self, master, interface):
tk.LabelFrame.__init__(self, master, text='Equalizer (values in dB)', labelanchor = 'n')
self.rowconfigure(1, weight=1) # UI stretching configuration...
for i in range(10):
self.columnconfigure(i, weight=1)
self.master = master
self.interface = interface
self.bars = [tk.Scale(self, from_=20, to=-20, label = str(i+1), command=self.onUpdate)
for i in range(10)]
for i, bar in enumerate(self.bars):
bar.grid(row=1, column = i, sticky='news')
#bar.bind('<ButtonPress-1>', self.interface.deactivate)
#bar.bind('<ButtonRelease-1>', self.interface.reactivate)
bar.bind('<ButtonRelease-1>', self.sendUpdate)
def getstr(self):
ret = ' '
for x in self.bars:
ret += (str(x.get()) + ' ')
return ret
def onUpdate(self, event=None):
return
self.sendUpdate()
def sendUpdate(self, event=None):
print("EqFrame-sendUpdate")
self.interface.eq(self.getstr())
class VolFrame(tk.LabelFrame): # ## a widget to control volume
def __init__(self, master, interface):
tk.LabelFrame.__init__(self, master, text='Master\nvolume\n(0-125%)', labelanchor = 'n')
self.rowconfigure(1, weight=1) # UI stretching configuration...
self.columnconfigure(0, weight=1)
self.master = master
self.interface = interface
self.bar = tk.Scale(self, from_=125, to=0, command=self.onUpdate)
self.bar.set(50)
self.bar.grid(row=1, column = 0, sticky='news')
def onUpdate(self, event=None):
self.interface.vol(self.bar.get()/100)
class OverrideFrame(tk.LabelFrame): # for the volume override
def __init__(self, master, interface):
self.interface = interface
tk.LabelFrame.__init__(self, master, text='skipping override (seconds)')
self.enable = tk.BooleanVar()
self.check = tk.Checkbutton(self, text='enable', command=self.onPress,
variable=self.enable)
self.entry = tk.Entry(self, width=6)
self.entry.bind('<Return>', self.onPress)
self.entry.bind('<KP_Enter>', self.onPress)
self.check.pack(side=tk.TOP)
self.entry.pack()
def onPress(self, event=None):
if self.enable.get():
if re.fullmatch('[0-9]+', self.entry.get()):
self.interface.skip_override = self.entry.get()
else:
print('skip override must be an int')
else:
self.interface.skip_override = None
def create_ui(vlc_interface):
win = tk.Tk()
win.rowconfigure(0, weight=1) # UI stratching configuration...
win.columnconfigure(0, minsize=100, weight=0)
win.columnconfigure(1, weight=100)
win.columnconfigure(2, weight=1)
a = MainFileChooser(win, vlc_interface, './sounds')
a.grid(row=0, column=0, sticky='wnes', rowspan=2)
b = EqFrame(win, vlc_interface)
b.grid(row=0, column=1, sticky='news', rowspan=2)
v = VolFrame(win, vlc_interface)
v.grid(row=0, column=2, sticky='nes')
s = OverrideFrame(win, vlc_interface)
s.grid(row=1, column=2, sticky='es')
if sys.platform!='linux':
# this is windows. add the 'foreground' option
win.wm_attributes("-topmost", 1)
win.bind('<Destroy>', vlc_interface.startFinalization)
return win