-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
364 lines (292 loc) · 9.18 KB
/
app.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
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
import sounddevice
import threading
import soundfile as sf
import json
import os
import time
import pygame
import queue
import sounddevice as sd
import numpy
from os import listdir
from os.path import isfile, join
clear = lambda: os.system('cls')
DATA_TYPE = os.getenv('DATA_TYPE')
DATA_PATH = os.getenv('DATA_PATH')
CONSOLE_LYRICS = int(os.getenv('CONSOLE_LYRICS'))==1
data = None
song1 = None
song2 = None
songIndex = -1
printingLyrics = True
out1 = None
out2 = None
# Config
BLOCKSIZE = int(os.getenv('BLOCKSIZE'))
BUFFERSIZE = int(os.getenv('BUFFERSIZE'))
# Begin: PyGame
pygame.init()
black = (0, 0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
grey = (200,200,200)
infoScreen = pygame.display.Info()
X = infoScreen.current_w
Y = infoScreen.current_h
display_surface = pygame.display.set_mode((X, Y), flags=pygame.HIDDEN)
pygame.display.set_caption('Lyrics')
font = pygame.font.Font('freesansbold.ttf', 32)
# End: PyGame
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class myThread(threading.Thread):
def __init__(self,filename,device):
threading.Thread.__init__(self)
self.filename = filename
self.device = device
self.isPaused = False
self.isFinsished = False
self.isReady = False
self.q = queue.Queue(maxsize=BUFFERSIZE)
self.event = threading.Event()
def callback(self,outdata, frames, time, status):
assert frames == BLOCKSIZE
if status.output_underflow:
#print('Output underflow: increase blocksize?', file=sys.stderr)
raise sd.CallbackAbort
assert not status
try:
data = self.q.get_nowait()
except queue.Empty as e:
#print('Buffer is empty: increase buffersize?', file=sys.stderr)
raise sd.CallbackAbort from e
if len(data) < len(outdata):
outdata[:len(data)] = data
outdata[len(data):].fill(0)
raise sd.CallbackStop
else:
outdata[:] = data
def pause(self):
self.isPaused = not self.isPaused
def finish(self):
self.isFinsished = not self.isFinsished
def run(self):
try:
with sf.SoundFile(self.filename) as f:
for _ in range(BUFFERSIZE):
data = f.read(BLOCKSIZE)
if not len(data):
break
self.q.put_nowait(data) # Pre-fill queue
stream = sd.OutputStream(
samplerate=f.samplerate, blocksize=BLOCKSIZE,
device=self.device, channels=f.channels,
callback=self.callback, finished_callback=self.event.set)
with stream:
timeout = BLOCKSIZE * BUFFERSIZE / f.samplerate
self.isReady = True
while len(data) and not self.isFinsished:
if self.isPaused:
self.q.put(numpy.zeros((BLOCKSIZE,2)), timeout=timeout)
else:
data = f.read(BLOCKSIZE)
self.q.put(data, timeout=timeout)
self.event.wait() # Wait until playback is finished
except Exception as e:
#print(type(e).__name__ + ': ' + str(e))
quit()
def get_devices():
devices = sounddevice.query_devices()
index = 1
realIndex = 0
realIndexList = []
for device in devices:
if device['hostapi'] == 0:
if device['max_input_channels'] == 0:
print(str(index)+'.',device["name"])
realIndexList.append(realIndex)
index+=1
realIndex += 1
return realIndexList
def loadData():
global data
data = {
"songs":[]
}
onlyfiles = [f for f in listdir(DATA_PATH) if isfile(join(DATA_PATH, f))]
for elem in onlyfiles:
namePath = DATA_PATH + '\\' + elem
f = open(namePath, mode="r", encoding='utf-8')
miDat = json.load(f)
data["songs"].append(miDat)
f.close()
def selectOutput():
realIndexList = get_devices()
print()
global out1,out2
while out1 == None or out2 == None:
if out1 == None:
out1a = input("Select output for singer: ")
out1a = int(out1a)-1
if out1a >= 0 and out1a < len(realIndexList):
out1 = realIndexList[out1a]
else:
out1 = None
if out2 == None:
out2a = input("Select output for instrumental: ")
out2a = int(out2a)-1
if out2a >= 0 and out2a < len(realIndexList):
out2 = realIndexList[out2a]
else:
out2 = None
clear()
def selectSong():
selection = None
while selection is None:
index = 1
for cancion in data["songs"]:
print(str(index)+'.',cancion["title"],"-",cancion["artist"])
index += 1
print()
try:
selection = int(input("Select a song (0 to exit): "))-1
except:
selection = -1
if selection == -1:
quit()
if selection+1 <= 0 or selection+1 >= index:
selection = None
global songIndex
songIndex = selection
clear()
global song1,song2
song1 = data["songs"][songIndex]["songpath"]
song2 = data["songs"][songIndex]["karaokepath"]
def loadLyrics():
letras = []
f = open(data["songs"][songIndex]["lyricspath"], mode="r", encoding="utf-8")
for x in f:
line = x.split("[")[1].split("]")
if line[1] != "\n":
minutes = int(line[0].split(":")[0])
seconds = float(line[0].split(":")[1])
total = 60*minutes+seconds
letras.append([total,line[1].encode('utf-8')])
f.close()
return letras
def writeText(line,pos):
text = font.render(line[:-1], True, green if pos==2 else white, black)
textRect = text.get_rect()
textRect.center = (X // 2, (Y * pos) // 4)
display_surface.blit(text, textRect)
def writeTitle(title,pos):
text = font.render(title, True, grey, black)
textRect = text.get_rect()
textRect.center = (X // 2, (Y * pos) // 4)
display_surface.blit(text, textRect)
def pyGameUpdate():
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
global printingLyrics
if printingLyrics and event.key == pygame.K_ESCAPE:
stopAll()
printingLyrics = False
def printLyrics():
lyrics = loadLyrics()
index = 0
interval = 0
if CONSOLE_LYRICS:
clear()
print('\n\n\n')
firstLine = lyrics[0][1].decode('utf-8')
if CONSOLE_LYRICS:
print(firstLine)
songTitle = data["songs"][songIndex]["title"]+" - "+data["songs"][songIndex]["artist"]
# PyGame
display_surface = pygame.display.set_mode((X, Y), flags=pygame.SHOWN|pygame.FULLSCREEN|pygame.NOFRAME)
pyGameUpdate()
display_surface.fill(black)
writeText(firstLine,3)
writeTitle(songTitle,0.2)
pyGameUpdate()
global printingLyrics
printingLyrics = True
playAll()
while not thread1.isReady and not thread2.isReady:
pass
currentTime = time.perf_counter()
lastLine1 = None
nextLine = firstLine
while index < len(lyrics) and printingLyrics:
interval = time.perf_counter() - currentTime
if lyrics[index][0] < interval:
if CONSOLE_LYRICS:
clear()
display_surface.fill(black)
writeTitle(songTitle,0.2)
if index != 0:
if CONSOLE_LYRICS:
print(lastLine1)
writeText(lastLine1,1)
elif CONSOLE_LYRICS:
print('\n')
mainLine = nextLine
if CONSOLE_LYRICS:
print(bcolors.OKGREEN + mainLine + bcolors.ENDC)
writeText(mainLine,2)
lastLine1 = mainLine
if index < len(lyrics)-1:
lastLine = lyrics[index+1][1].decode('utf-8')
if CONSOLE_LYRICS:
print(lastLine)
writeText(lastLine,3)
nextLine = lastLine
index += 1
pyGameUpdate()
def playAll():
play1()
play2()
def play1():
global thread1
thread1 = myThread(song1,out1)
thread1.start()
def play2():
global thread2
thread2 = myThread(song2,out2)
thread2.start()
def stopAll():
stop1()
stop2()
def stop1():
thread1.finish()
def stop2():
thread2.finish()
def pauseAll():
pause1()
pause2()
def pause1():
thread1.pause()
def pause2():
thread2.pause()
loadData()
selectOutput()
while True:
selectSong()
printLyrics()
while thread1.is_alive() or thread2.is_alive():
time.sleep(0.5)
display_surface = pygame.display.set_mode((X, Y), flags=pygame.HIDDEN)
input()