-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyap.py
More file actions
102 lines (79 loc) · 3.13 KB
/
Copy pathpyap.py
File metadata and controls
102 lines (79 loc) · 3.13 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
import os, datetime, random, json
from logging import info, error, warning, debug, critical, exception, log
import logging
try:
import audioplayer
except:
os.system("python -m pip install audioplayer")
import audioplayer
try:
import pyttsx3
except:
os.system("python -m pip install pyttsx3 pywintypes")
import pyttsx3
musicdir = os.path.expanduser("~/Music")
datadir = os.path.expanduser("~/PyAP")
os.makedirs(musicdir, exist_ok=True)
os.makedirs(datadir, exist_ok=True)
config_path = os.path.join(datadir, 'config.json')
if not os.path.exists(config_path):
f = open(config_path, 'w')
f.write('{"playlist":"play.list"}')
f.close()
if os.path.exists(datadir+"\\"+'config.json'):
config = open(datadir+"\\"+'config.json', 'r+')
options = json.load(config)
logging.basicConfig(filename=datadir+"\\"+f"player-{datetime.date.today()}.log",level=logging.INFO, filemode="a", format='%(asctime)s - %(levelname)s - %(message)s')
info("Player started")
engine = pyttsx3.init()
engine.setProperty('rate', 125)
def readout(text):
engine.say(text)
engine.runAndWait()
def play_audio_files(musicdir, audio_list=None):
if audio_list is None:
audio_files = [f for f in os.listdir(musicdir) if f.endswith('.mp3') or f.endswith('.wav')]
else:
audio_files = [f for f in audio_list if f.endswith('.mp3') or f.endswith('.wav') and os.path.exists(os.path.join(musicdir, f))]
auxfr = [f for f in os.listdir(musicdir) if f.endswith('.mp3') or f.endswith('.wav')]
audionum = 0
for audio_file in audio_files:
ls = lister()
audio_file = ls[audionum]
if "random" in audio_file.lower():
adf = audio_file.removesuffix(".mp3").split("-")
if adf[1] == "all":
random.shuffle(auxfr)
for auxf in auxfr:
info(f"Started playing random audio ({auxf})")
file_path = os.path.join(musicdir, auxf)
player = audioplayer.AudioPlayer(file_path)
player.play(block=True)
info(f"Finished playing random audio ({auxf})")
else:
auxfs = random.choices(population=auxfr, k=int(adf[1]))
for auxf in auxfs:
info(f"Started playing random audio ({auxf})")
file_path = os.path.join(musicdir, auxf)
player = audioplayer.AudioPlayer(file_path)
player.play(block=True)
info(f"Finished playing random audio ({auxf})")
audionum += 1
continue
info(f"Started playing {audio_file}")
file_path = os.path.join(musicdir, audio_file)
player = audioplayer.AudioPlayer(file_path)
player.play(block=True)
info(f"Finished playing {audio_file}")
audionum += 1
audio_list=ls
def lister():
file = open(datadir+"\\"+options["playlist"], 'r')
audio_list = [line.strip()+".mp3" for line in file.readlines()]
file.close()
return audio_list
x = 1
while True:
play_audio_files(musicdir, lister())
info(f"Played {x}th loop")
x+=1