4
4
import os
5
5
import time
6
6
import random
7
- import msvcrt
8
7
import json
8
+ import keyboard
9
+ import msvcrt
9
10
import threading as thread
10
11
11
12
os .environ ["PYGAME_HIDE_SUPPORT_PROMPT" ] = "hide"
16
17
scriptDir = os .path .dirname (os .path .realpath (__file__ ))
17
18
shouldRun = True
18
19
loopSong = False
20
+ loadingAnimation = False
19
21
songLog = []
20
22
standardOptions = {
21
23
"shuffle" : False ,
30
32
pygame .mixer_music .set_endevent (SONG_END )
31
33
32
34
#Classes
35
+ class PressedCharacter :
36
+ def __init__ (self ):
37
+ self .char = None
38
+ self .globalChar = keyboard .KeyboardEvent (None , None )
39
+ pressedCharacter = PressedCharacter ()
33
40
class PlaylistData :
34
41
def __init__ (self ):
35
42
self .shuffleIndefinitely = False
@@ -41,6 +48,7 @@ def __init__(self, name, path, paused):
41
48
self .paused = paused
42
49
self .positionOffset = 0
43
50
self .playlist = None
51
+ self .logged = False
44
52
if self .path != None :
45
53
self .length = int (pygame .mixer .Sound (self .path ).get_length ())
46
54
else :
@@ -49,6 +57,23 @@ def __init__(self, name, path, paused):
49
57
nowPlaying = NowPlaying (None , None , None ) #Standard object for the current playing song
50
58
51
59
#Functions
60
+ def getChar ():
61
+ while shouldRun :
62
+ pressedCharacter .char = msvcrt .getwch ()
63
+ def loading_animation (str ):
64
+ array = ["/" , "—" , "\\ " , "|" ]
65
+ n = 0
66
+ while loadingAnimation :
67
+ print ("\r " + array [n ] + " " + str , end = "" )
68
+ time .sleep (0.15 )
69
+ if n == 3 :
70
+ n = 0
71
+ else :
72
+ n += 1
73
+ sys .exit (0 )
74
+ def wait ():
75
+ pressedCharacter .char = ""
76
+ time .sleep (0.2 )
52
77
def play (files ):
53
78
if len (files ) == 1 :
54
79
type = "song"
@@ -82,6 +107,7 @@ def play(files):
82
107
nowPlaying .paused = False
83
108
nowPlaying .length = files [index ].length
84
109
nowPlaying .positionOffset = files [index ].positionOffset
110
+ nowPlaying .logged = False
85
111
86
112
#Add to log
87
113
songLog .append (files [index ].path )
@@ -109,6 +135,7 @@ def play(files):
109
135
nowPlaying .length = None
110
136
nowPlaying .positionOffset = 0
111
137
nowPlaying .playlist = None
138
+ nowPlaying .logged = False
112
139
113
140
sys .exit (0 ) #Exit thread
114
141
@@ -129,12 +156,20 @@ def play(files):
129
156
print ("Options file could not be found..." )
130
157
pygame .mixer_music .set_volume (options ["volume" ])
131
158
159
+ #Prepare some things
160
+ with open (scriptDir + "/play.log" ,"a" ) as file :
161
+ file .write (time .strftime ("#time: %Y-%m-%d %H:%M:%S\n " , time .localtime (time .time ())))
162
+
132
163
#Start standard song
133
164
if options ["onStartSong" ] != None :
134
165
files = []
166
+ loadingAnimation = True
167
+ thread ._start_new_thread (loading_animation , ("Loading playlist!" ,))
135
168
for entry in os .scandir (options ["onStartSong" ]["path" ]):
136
169
if entry .is_file () and entry .name .endswith (".mp3" ):
137
170
files .append (NowPlaying (entry .name , entry .path , False ))
171
+ loadingAnimation = False
172
+ print ("\r " , end = "" )
138
173
if len (files ) > 0 :
139
174
thread ._start_new_thread (play , (files ,))
140
175
print ("Standard playlist loaded!" )
@@ -150,38 +185,65 @@ def saveFiles():
150
185
151
186
#Append played songs to log
152
187
with open (scriptDir + "/play.log" ,"a" ) as file :
153
- file .write (time .strftime ("#time: %Y-%m-%d %H:%M:%S\n " , time .localtime (time .time ())))
154
- for song in songLog :
155
- file .write (song + "\n " )
156
-
188
+ if not nowPlaying .logged :
189
+ file .write (nowPlaying .path + "\n " )
190
+ nowPlaying .logged = True
191
+
192
+ thread ._start_new_thread (getChar , ())
193
+ def setGlobalChar (event ):
194
+ pressedCharacter .globalChar = event
195
+ keyboard .on_press (setGlobalChar )
157
196
while shouldRun :
158
- char = msvcrt .getwch ()
159
197
160
- if char == "p" and pygame .mixer_music .get_busy ():
198
+ p_event = (pressedCharacter .char == "p" ) or ((keyboard .is_pressed (76 )) and (pressedCharacter .globalChar .is_keypad )) #Trigger on "p" inside console or "5" on numpad (globally)
199
+ s_event = (pressedCharacter .char == "s" ) or ((keyboard .is_pressed (77 )) and (pressedCharacter .globalChar .is_keypad )) #Trigger on "s" inside console or "6" on numpad (globally)
200
+ l_event = pressedCharacter .char == "l"
201
+ i_event = pressedCharacter .char == "i"
202
+ u_event = pressedCharacter .char == "u"
203
+ r_event = (pressedCharacter .char == "r" ) or ((keyboard .is_pressed (75 )) and (pressedCharacter .globalChar .is_keypad )) #Trigger on "r" inside console or "4" on numpad (globally)
204
+ t_event = pressedCharacter .char == "t"
205
+ g_event = pressedCharacter .char == "g"
206
+ j_event = pressedCharacter .char == "j"
207
+ n_event = pressedCharacter .char == "n"
208
+ h_event = pressedCharacter .char == "h"
209
+ c_event = pressedCharacter .char == "c"
210
+ o_event = pressedCharacter .char == "o"
211
+ e_event = (pressedCharacter .char == "e" ) or ((keyboard .is_pressed (79 )) and (pressedCharacter .globalChar .is_keypad )) #Trigger on "e" inside console or "1" on numpad (globally)
212
+ q_event = pressedCharacter .char == "q"
213
+
214
+ if p_event and pygame .mixer_music .get_busy ():
161
215
pygame .mixer_music .pause ()
162
216
nowPlaying .paused = True
163
217
print ("Paused!" )
164
- elif char == "p" and not pygame .mixer_music .get_busy ():
218
+ wait ()
219
+ elif p_event and not pygame .mixer_music .get_busy ():
165
220
pygame .mixer_music .unpause ()
166
221
nowPlaying .paused = False
167
222
print ("Unpaused!" )
223
+ wait ()
168
224
169
- if char == "s" :
225
+ if s_event :
170
226
pygame .mixer_music .stop ()
171
227
pygame .mixer_music .unload ()
172
228
nowPlaying .paused = None
173
229
print ("Stopped/Next!" )
230
+ wait ()
174
231
175
- if char == "l" :
232
+ if l_event :
176
233
new_song = input ("Type the new song here: " )
177
234
if os .path .exists (new_song ):
235
+ loadingAnimation = True
236
+ thread ._start_new_thread (loading_animation , ("Loading song!" ,))
178
237
song = [NowPlaying (os .path .basename (new_song ), os .path .abspath (new_song ), False )]
238
+ loadingAnimation = False
239
+ print ("\r " , end = "" )
179
240
thread ._start_new_thread (play , (song ,))
180
- print ("Song loaded!" )
241
+ print ("Song was loaded!" )
181
242
else :
182
243
print ("Song doesn't exist." )
244
+ wait ()
183
245
184
- if char == "i" :
246
+ if i_event :
185
247
playlist = input ("Type the playlist here: " )
186
248
if os .path .exists (playlist ):
187
249
#if os.path.isfile(playlist):
@@ -193,18 +255,23 @@ def saveFiles():
193
255
# pygame.mixer_music.play()
194
256
if os .path .isdir (playlist ):
195
257
files = []
258
+ loadingAnimation = True
259
+ thread ._start_new_thread (loading_animation , ("Loading playlist!" ,))
196
260
for entry in os .scandir (playlist ):
197
261
if entry .is_file () and entry .name .endswith (".mp3" ):
198
262
files .append (NowPlaying (entry .name , entry .path , False ))
263
+ loadingAnimation = False
264
+ print ("\r " , end = "" )
199
265
if len (files ) > 0 :
200
266
thread ._start_new_thread (play , (files ,))
201
- print ("Playlist loaded!" )
267
+ print ("Playlist was loaded!" )
202
268
else :
203
269
print ("Directory does not contain any mp3-files" )
204
270
else :
205
271
print ("Playlist doesn't exist." )
272
+ wait ()
206
273
207
- if char == "u" :
274
+ if u_event :
208
275
print ("\n REMAINING SONGS:" )
209
276
if not nowPlaying .path :
210
277
print ("No song is currently loaded!" )
@@ -219,23 +286,26 @@ def saveFiles():
219
286
print (song .name )
220
287
else :
221
288
print ("The current playing song is the last one." )
289
+ wait ()
222
290
223
- if char == "r" :
291
+ if r_event :
224
292
pygame .mixer_music .play ()
225
293
if nowPlaying .paused :
226
294
pygame .mixer_music .pause ()
227
295
nowPlaying .paused = True
228
296
print ("Rewind!" )
297
+ wait ()
229
298
230
- if char == "t" :
299
+ if t_event :
231
300
if loopSong :
232
301
print ("Stop looping current track." )
233
302
loopSong = False
234
303
else :
235
304
print ("Looping current track!" )
236
305
loopSong = True
306
+ wait ()
237
307
238
- if char == "g" :
308
+ if g_event :
239
309
print ("\n GOTO POSITION:" )
240
310
seconds = int (pygame .mixer_music .get_pos () / 1000 ) + nowPlaying .positionOffset
241
311
print ("Position is currently: {}:{}{}. " .format (seconds // 60 , (lambda int : "0" if int < 10 else "" )(seconds % 60 ), seconds % 60 ), end = "" )
@@ -258,13 +328,15 @@ def saveFiles():
258
328
pygame .mixer_music .pause ()
259
329
nowPlaying .paused = True
260
330
print ("Go to {} seconds!" .format (position ))
331
+ wait ()
261
332
262
- if char == "j" :
333
+ if j_event :
263
334
print ("\n JOURNAL:" )
264
335
for song in songLog :
265
336
print (song )
337
+ wait ()
266
338
267
- if char == "n" :
339
+ if n_event :
268
340
print ("\n NOW PLAYING:" )
269
341
if not nowPlaying .path :
270
342
print ("No song is currently loaded!" )
@@ -276,8 +348,9 @@ def saveFiles():
276
348
print ("Length: {}:{}{}" .format (nowPlaying .length // 60 , (lambda int : "0" if int < 10 else "" )(nowPlaying .length % 60 ), nowPlaying .length % 60 ))
277
349
seconds = int (pygame .mixer_music .get_pos () / 1000 ) + nowPlaying .positionOffset
278
350
print ("Position: {}:{}{}" .format (seconds // 60 , (lambda int : "0" if int < 10 else "" )(seconds % 60 ), seconds % 60 ))
351
+ wait ()
279
352
280
- if char == "h" :
353
+ if h_event :
281
354
print ("\n HELP:" )
282
355
print ("p - Pause/Unpause" )
283
356
print ("s - Stop" )
@@ -291,15 +364,17 @@ def saveFiles():
291
364
print ("h - Access this menu" )
292
365
print ("e/q - Quit program" )
293
366
print ()
367
+ wait ()
294
368
295
- if char == "c" :
369
+ if c_event :
296
370
print ("\n CREDITS:" )
297
371
print ("Author: Isak Brynielsson Neri" )
298
372
print ("Libs:" )
299
373
print (" Pygame: {}" .format (pygame .version .ver ))
300
374
print ()
375
+ wait ()
301
376
302
- if char == "o" :
377
+ if o_event :
303
378
print ("\n OPTIONS:" )
304
379
print ("v - Change volume!" )
305
380
print ("s - Change shuffle setting." )
@@ -357,11 +432,12 @@ def saveFiles():
357
432
options ["shuffleIndefinitely" ] = newShuffleIndefinitely
358
433
print ("New shuffle indefinitely settings: {}!\n " .format (options ["shuffleIndefinitely" ]))
359
434
saveFiles ()
435
+ wait ()
360
436
361
- if char == "e" :
437
+ if e_event :
362
438
print ("Bye!\n " )
363
439
shouldRun = False
364
- if char == "q" :
440
+ if q_event :
365
441
print ("Goodbye!\n " )
366
442
shouldRun = False
367
443
0 commit comments