-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
452 lines (407 loc) · 13.7 KB
/
interface.py
File metadata and controls
452 lines (407 loc) · 13.7 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"""Interface for the application
This file can be run, and will properly initialize interface (and rest of the
app) in your terminal.
Battle-tested on Solarized color scheme and under tmux.
"""
from contextlib import contextmanager
from datetime import datetime
import os
import os.path
import random
import sys
import npyscreen
from pyaudio_fix import fix_pyaudio
import audio
import config
import filters
import utils
@contextmanager
def use_xterm():
"""Helper setting proper TERM value
Required for colors to work under 16-color tmux.
"""
old_value = os.environ.get('TERM')
os.environ['TERM'] = 'xterm'
yield
if old_value is not None:
os.environ['TERM'] = old_value
def quit():
"""Close application gracefully"""
audio.stop()
sys.exit(0)
def show_quit_popup(key=None):
"""Display popup asking whether to quit application"""
result = npyscreen.notify_yes_no(
message='Do you really want to quit?',
title='Quit',
editw=1, # select No button by default
)
if result:
quit()
class TracksListWidget(npyscreen.TitleSelectOne):
"""Widget displaying list of tracks
Properly loads the track after selecting.
"""
def get_additional_filenames(self, filename):
"""Loads up to 2 additional tracks
Returns list of filenames.
"""
app = self.parent.parentApp
filenames = [filename]
if random.random() >= config.LOAD_MULTIPLE_THRESHOLD:
return filenames
app.notify('Multiple tracks selected!')
if random.random() < config.LOAD_TRIPLE_THRESHOLD:
count = 2
else:
count = 1
for _ in range(count):
for __ in range(10):
selected = random.choice(self.values)
if selected not in filenames:
filenames.append(selected)
break
return filenames
def load_tracks(self, filenames):
"""Loads files as pydub tracks"""
app = self.parent.parentApp
tracks = []
for filename in filenames:
app.notify('Loading {title}...'.format(title=filename))
track = audio.load(filename)
if not app._already_cut:
track = audio.cut(track, app._track_length * 2)
tracks.append(track)
return tracks
def get_infos(self, filenames):
"""Obtains infos about filenames"""
app = self.parent.parentApp
infos = []
for filename in filenames:
info = audio.get_info(filename)
no = app._filenames_list.index(filename) + 1
infos.append('No. {no}'.format(no=no),)
infos += info
infos.append('\n')
return infos
def when_value_edited(self):
"""Loads the track to parent app after selecting
Also cuts it to proper length, if requested.
"""
if not self.value:
return
app = self.parent.parentApp
filename = self.values[self.value[0]]
filenames = self.get_additional_filenames(filename)
song_info = self.parent.get_widget('song-info')
# Load everything
filenames = [app.filenames.get(v) for v in filenames]
infos = self.get_infos(filenames)
tracks = self.load_tracks(filenames)
self.parent.set_status('Loading')
song_info.values = infos
song_info.display()
# Mix 'em up!
track = filters.multiple_tracks(tracks)
app.current_track = track
app.current_track_nos = filenames
app.notify('Loaded!')
# Also, clear filters
self.parent.h_reset_filters()
self.parent.set_status('Ready to play')
self.parent.calculate_points()
self.value = []
self.display()
class MainForm(npyscreen.FormBaseNew):
"""Main form of the application"""
def update_slider(self, value):
"""Sets value of position slider"""
self.get_widget('position').value = value / 1000
self.get_widget('position').display()
def h_play(self, key):
"""Plays currently selected track
Also applies filters, if any are selected.
"""
app = self.parentApp
if not app.current_track:
app.notify('No track selected')
return
for filename in app.current_track_nos:
track_no = app._filenames_list.index(filename) + 1
try:
self.get_widget('track-list').values.remove(track_no)
except ValueError:
pass
self.get_widget('track-list').value = []
app.notify('Applying filters...')
track = filters.apply(app.current_track, self.parentApp.filters)
track = track[:app._track_length]
self.get_widget('position').entry_widget.out_of = len(track) / 1000
self.get_widget('position').display()
audio.play(track, notifier=self.update_slider)
app.notify('Playing!')
self.set_status('Playing')
def h_stop(self, key):
"""Stops currently played track"""
audio.stop()
self.parentApp.notify('Stopped.')
self.set_status('Ready to play')
def h_select_filters(self, key):
"""Randomly selects filters"""
selected = filters.get_random_filters()
self.parentApp.filters = selected
values = [filters.FILTERS_LIST.index(f) for f in selected]
widget = self.get_widget('filters')
widget.value = values
widget.display()
self.parentApp.notify('Filters randomized.')
self.calculate_points()
def h_reset_filters(self, key=None):
"""Clears filters selection"""
widget = self.get_widget('filters')
widget.value = []
self.parentApp.filters = []
widget.display()
self.parentApp.notify('Filters cleared.')
def h_toggle_filter(self, key):
"""Toggles single filter on the filters list"""
index = int(chr(key)) - 1
widget = self.get_widget('filters')
try:
self.parentApp.filters.remove(filters.FILTERS_LIST[index])
widget.value.remove(index)
except ValueError:
self.parentApp.filters.append(filters.FILTERS_LIST[index])
widget.value.append(index)
widget.display()
def set_status(self, message):
"""Sets value for the status widget
This is kind of useful, because statusbar displays only the last
message, and it's important to know whether song is playing, stopped
or loaded.
"""
song_status = self.get_widget('song-status')
song_status.value = message
song_status.display()
def calculate_points(self):
"""Sets proper amount of points in Points widget"""
widget = self.get_widget('points')
# Filters
points = config.FILTER_POINTS[len(self.parentApp.filters)]
# Multiple songs
points *= config.TRACKS_MULTIPLIER[
len(self.parentApp.current_track_nos)
]
widget.value = int(round(points))
widget.display()
def set_up_handlers(self):
"""Sets up handlers for keypresses
Bonus: upper keys are also supported, meaning you don't need to worry
about capslock!
"""
super(MainForm, self).set_up_handlers()
keys = {
'^q': show_quit_popup,
'a': self.h_play,
's': self.h_stop,
'f': self.h_select_filters,
'r': self.h_reset_filters,
}
# Make upperkeys available, too!
for key, func in list(keys.items()):
keys[key.upper()] = func
# Add filter toggling
for i, filter_name in enumerate(filters.FILTERS_LIST, start=1):
keys[str(i)] = self.h_toggle_filter
self.handlers.update(keys)
class SettingsForm(npyscreen.Form):
"""Form with settings
Mainly used to get working directory path. You can also customize some
other things here.
Should be displayed before main form.
"""
def afterEditing(self):
"""Sets proper values in the parent app after pressing OK button"""
app = self.parentApp
path = self.get_widget('path').value
status = self.get_widget('status')
track_length = self.get_widget('track_length').value
already_cut = self.get_widget('track_cut').value
seed = self.get_widget('seed').value
if not path:
status.value = 'Enter something'
return
if not os.path.isdir(path):
status.value = 'That is not a directory'
return
app._path = path
app._track_length = int(track_length) * 1000
app._seed = seed
app._already_cut = already_cut
app.load_filenames(path)
app.initialize_filters()
app.setNextForm('MAIN')
class App(npyscreen.NPSAppManaged):
"""Main application class"""
def __init__(self, *args, **kwargs):
super(App, self).__init__(*args, **kwargs)
self._filenames = {}
self._filenames_list = []
self._path = None
self.current_track = None
self.current_track_nos = []
self._track_length = 35000 # in ms
self._seed = None
self._already_cut = False
self.filters = []
@property
def filenames(self):
return self._filenames
@filenames.setter
def filenames(self, value):
self._filenames = value
self._filenames_list = [v for k, v in sorted(value.items())]
track_number = self.getForm('MAIN').get_widget('track-list')
track_number.values = list(self._filenames.keys())
track_number.display()
def onStart(self):
"""Initializes all forms and populates it with widgets
TODO: maybe put each form code into:
1) separate method in App?
2) init method in each form?
Either of these would increase readability.
"""
# Directory form
directory_form = self.addForm(
'directory',
SettingsForm,
name='Settings',
)
directory_form.add_widget(
npyscreen.TitleText,
name='Path',
w_id='path',
)
directory_form.nextrely += 1
directory_form.add_widget(
npyscreen.FixedText,
w_id='status',
editable=False,
)
directory_form.add_widget(
npyscreen.TitleText,
name='Track length',
value='35',
w_id='track_length',
)
directory_form.add_widget(
npyscreen.Checkbox,
name='Already cut?',
value=True,
w_id='track_cut',
relx=18,
)
directory_form.add_widget(
npyscreen.TitleText,
name='Random seed',
value='this is some random seed',
w_id='seed',
)
# Main form
form = self.addForm('MAIN', MainForm, name='EKOiE')
form.add_widget(
TracksListWidget,
name='Track number',
values=[],
w_id='track-list',
max_height=form.lines-7,
width=int(form.columns/2),
)
form.add_widget(
npyscreen.TitleFixedText,
height=2,
name='Status',
rely=-3,
editable=False,
w_id='status',
)
# Split screen vertically in half
form.nextrely = 2
form.nextrelx = int(form.columns/2) + 2
form.add_widget(
npyscreen.MultiLineEditableTitle,
height=18,
name='Songs info',
editable=False,
values=[],
w_id='song-info',
)
# Song status
form.add_widget(
npyscreen.TitleFixedText,
height=1,
editable=False,
name='Song status',
w_id='song-status',
)
form.nextrely += 2
# Slider
form.add_widget(
npyscreen.TitleSlider,
name='Position',
out_of=35,
lowest=0,
value=0,
label=True,
w_id='position',
)
form.nextrely += 2
form.add_widget(
npyscreen.TitleMultiSelect,
editable=False,
height=8,
name='Filters',
w_id='filters',
values=filters.FILTERS_LIST,
)
form.nextrely += 2
form.add_widget(
npyscreen.TitleFixedText,
height=1,
editable=False,
name='Points',
w_id='points',
)
self.setNextForm('directory')
def notify(self, message):
"""Displays notification in the bottom of the screen"""
status = self.getForm('MAIN').get_widget('status')
status.value = '[{time}] {message}'.format(
time=datetime.now().strftime('%H:%M:%S'),
message=message,
)
status.display()
def load_filenames(self, path):
"""Loads filenames of tracks from working directory"""
self.notify(
'Loading files from {path}...'.format(path=path),
)
self.filenames = utils.shuffle(
utils.get_filenames(path),
seed=self._seed,
)
self.notify('{count} files loaded.'.format(count=len(self.filenames)))
def initialize_filters(self):
self.notify('Initializing panzerfaust filter...')
filters.initialize_panzer_tracks()
self.notify('Initializing overlay filter...')
filters.initialize_overlay_tracks()
self.notify('Filters initialized.')
if __name__ == '__main__':
with use_xterm():
fix_pyaudio()
app = App()
try:
app.run()
except KeyboardInterrupt:
quit()