Skip to content

Commit ecc7b78

Browse files
Merge pull request #17 from IsakTheHacker/dev
2 parents b439f3d + c972f8e commit ecc7b78

File tree

2 files changed

+100
-906
lines changed

2 files changed

+100
-906
lines changed

Diff for: Python AT Audio Player/Python_AT_Audio_Player.py

+100-24
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import os
55
import time
66
import random
7-
import msvcrt
87
import json
8+
import keyboard
9+
import msvcrt
910
import threading as thread
1011

1112
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide"
@@ -16,6 +17,7 @@
1617
scriptDir = os.path.dirname(os.path.realpath(__file__))
1718
shouldRun = True
1819
loopSong = False
20+
loadingAnimation = False
1921
songLog = []
2022
standardOptions = {
2123
"shuffle": False,
@@ -30,6 +32,11 @@
3032
pygame.mixer_music.set_endevent(SONG_END)
3133

3234
#Classes
35+
class PressedCharacter:
36+
def __init__(self):
37+
self.char = None
38+
self.globalChar = keyboard.KeyboardEvent(None, None)
39+
pressedCharacter = PressedCharacter()
3340
class PlaylistData:
3441
def __init__(self):
3542
self.shuffleIndefinitely = False
@@ -41,6 +48,7 @@ def __init__(self, name, path, paused):
4148
self.paused = paused
4249
self.positionOffset = 0
4350
self.playlist = None
51+
self.logged = False
4452
if self.path != None:
4553
self.length = int(pygame.mixer.Sound(self.path).get_length())
4654
else:
@@ -49,6 +57,23 @@ def __init__(self, name, path, paused):
4957
nowPlaying = NowPlaying(None, None, None) #Standard object for the current playing song
5058

5159
#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)
5277
def play(files):
5378
if len(files) == 1:
5479
type = "song"
@@ -82,6 +107,7 @@ def play(files):
82107
nowPlaying.paused = False
83108
nowPlaying.length = files[index].length
84109
nowPlaying.positionOffset = files[index].positionOffset
110+
nowPlaying.logged = False
85111

86112
#Add to log
87113
songLog.append(files[index].path)
@@ -109,6 +135,7 @@ def play(files):
109135
nowPlaying.length = None
110136
nowPlaying.positionOffset = 0
111137
nowPlaying.playlist = None
138+
nowPlaying.logged = False
112139

113140
sys.exit(0) #Exit thread
114141

@@ -129,12 +156,20 @@ def play(files):
129156
print("Options file could not be found...")
130157
pygame.mixer_music.set_volume(options["volume"])
131158

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+
132163
#Start standard song
133164
if options["onStartSong"] != None:
134165
files = []
166+
loadingAnimation = True
167+
thread._start_new_thread(loading_animation, ("Loading playlist!",))
135168
for entry in os.scandir(options["onStartSong"]["path"]):
136169
if entry.is_file() and entry.name.endswith(".mp3"):
137170
files.append(NowPlaying(entry.name, entry.path, False))
171+
loadingAnimation = False
172+
print("\r", end="")
138173
if len(files) > 0:
139174
thread._start_new_thread(play, (files,))
140175
print("Standard playlist loaded!")
@@ -150,38 +185,65 @@ def saveFiles():
150185

151186
#Append played songs to log
152187
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)
157196
while shouldRun:
158-
char = msvcrt.getwch()
159197

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():
161215
pygame.mixer_music.pause()
162216
nowPlaying.paused = True
163217
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():
165220
pygame.mixer_music.unpause()
166221
nowPlaying.paused = False
167222
print("Unpaused!")
223+
wait()
168224

169-
if char == "s":
225+
if s_event:
170226
pygame.mixer_music.stop()
171227
pygame.mixer_music.unload()
172228
nowPlaying.paused = None
173229
print("Stopped/Next!")
230+
wait()
174231

175-
if char == "l":
232+
if l_event:
176233
new_song = input("Type the new song here: ")
177234
if os.path.exists(new_song):
235+
loadingAnimation = True
236+
thread._start_new_thread(loading_animation, ("Loading song!",))
178237
song = [NowPlaying(os.path.basename(new_song), os.path.abspath(new_song), False)]
238+
loadingAnimation = False
239+
print("\r", end="")
179240
thread._start_new_thread(play, (song,))
180-
print("Song loaded!")
241+
print("Song was loaded!")
181242
else:
182243
print("Song doesn't exist.")
244+
wait()
183245

184-
if char == "i":
246+
if i_event:
185247
playlist = input("Type the playlist here: ")
186248
if os.path.exists(playlist):
187249
#if os.path.isfile(playlist):
@@ -193,18 +255,23 @@ def saveFiles():
193255
# pygame.mixer_music.play()
194256
if os.path.isdir(playlist):
195257
files = []
258+
loadingAnimation = True
259+
thread._start_new_thread(loading_animation, ("Loading playlist!",))
196260
for entry in os.scandir(playlist):
197261
if entry.is_file() and entry.name.endswith(".mp3"):
198262
files.append(NowPlaying(entry.name, entry.path, False))
263+
loadingAnimation = False
264+
print("\r", end="")
199265
if len(files) > 0:
200266
thread._start_new_thread(play, (files,))
201-
print("Playlist loaded!")
267+
print("Playlist was loaded!")
202268
else:
203269
print("Directory does not contain any mp3-files")
204270
else:
205271
print("Playlist doesn't exist.")
272+
wait()
206273

207-
if char == "u":
274+
if u_event:
208275
print("\nREMAINING SONGS:")
209276
if not nowPlaying.path:
210277
print("No song is currently loaded!")
@@ -219,23 +286,26 @@ def saveFiles():
219286
print(song.name)
220287
else:
221288
print("The current playing song is the last one.")
289+
wait()
222290

223-
if char == "r":
291+
if r_event:
224292
pygame.mixer_music.play()
225293
if nowPlaying.paused:
226294
pygame.mixer_music.pause()
227295
nowPlaying.paused = True
228296
print("Rewind!")
297+
wait()
229298

230-
if char == "t":
299+
if t_event:
231300
if loopSong:
232301
print("Stop looping current track.")
233302
loopSong = False
234303
else:
235304
print("Looping current track!")
236305
loopSong = True
306+
wait()
237307

238-
if char == "g":
308+
if g_event:
239309
print("\nGOTO POSITION:")
240310
seconds = int(pygame.mixer_music.get_pos() / 1000) + nowPlaying.positionOffset
241311
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():
258328
pygame.mixer_music.pause()
259329
nowPlaying.paused = True
260330
print("Go to {} seconds!".format(position))
331+
wait()
261332

262-
if char == "j":
333+
if j_event:
263334
print("\nJOURNAL:")
264335
for song in songLog:
265336
print(song)
337+
wait()
266338

267-
if char == "n":
339+
if n_event:
268340
print("\nNOW PLAYING:")
269341
if not nowPlaying.path:
270342
print("No song is currently loaded!")
@@ -276,8 +348,9 @@ def saveFiles():
276348
print("Length: {}:{}{}".format(nowPlaying.length // 60, (lambda int: "0" if int < 10 else "")(nowPlaying.length % 60), nowPlaying.length % 60))
277349
seconds = int(pygame.mixer_music.get_pos() / 1000) + nowPlaying.positionOffset
278350
print("Position: {}:{}{}".format(seconds // 60, (lambda int: "0" if int < 10 else "")(seconds % 60), seconds % 60))
351+
wait()
279352

280-
if char == "h":
353+
if h_event:
281354
print("\nHELP:")
282355
print("p - Pause/Unpause")
283356
print("s - Stop")
@@ -291,15 +364,17 @@ def saveFiles():
291364
print("h - Access this menu")
292365
print("e/q - Quit program")
293366
print()
367+
wait()
294368

295-
if char == "c":
369+
if c_event:
296370
print("\nCREDITS:")
297371
print("Author: Isak Brynielsson Neri")
298372
print("Libs:")
299373
print(" Pygame: {}".format(pygame.version.ver))
300374
print()
375+
wait()
301376

302-
if char == "o":
377+
if o_event:
303378
print("\nOPTIONS:")
304379
print("v - Change volume!")
305380
print("s - Change shuffle setting.")
@@ -357,11 +432,12 @@ def saveFiles():
357432
options["shuffleIndefinitely"] = newShuffleIndefinitely
358433
print("New shuffle indefinitely settings: {}!\n".format(options["shuffleIndefinitely"]))
359434
saveFiles()
435+
wait()
360436

361-
if char == "e":
437+
if e_event:
362438
print("Bye!\n")
363439
shouldRun = False
364-
if char == "q":
440+
if q_event:
365441
print("Goodbye!\n")
366442
shouldRun = False
367443

0 commit comments

Comments
 (0)