-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPySound.py
More file actions
120 lines (100 loc) · 3.66 KB
/
PySound.py
File metadata and controls
120 lines (100 loc) · 3.66 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
from just_playback import Playback
import threading
import os
import time
class PySound:
def __init__(self):
self.Playback = Playback()
self.Path = ""
# --- Play Music ---
def Play(self, FilePath, Background=False):
self.Playback.load_file(FilePath)
self.Path = FilePath
if Background:
thread = threading.Thread(target=self.Playback.play)
thread.start()
else:
self.Playback.play()
# --- Pause / Resume / Stop ---
def Pause(self):
self.Playback.pause()
def Resume(self):
self.Playback.resume()
def Stop(self):
self.Playback.stop()
# --- Volume Control ---
def SetVolume(self, Volume, Unit="%"):
if Unit == "%":
Volume = Volume / 100.0
elif Unit == "float":
Volume = float(Volume)
else:
raise ValueError("Unit must be '%' or 'float'")
Volume = max(0.0, min(1.0, Volume))
self.Playback.set_volume(volume=Volume)
# --- Fade In / Fade Out ---
def FadeIn(self, Duration=2000, TargetVolume=1.0, Steps=20, freezeProcess=False):
def _fade_in():
step_time = Duration / Steps / 1000.0
for i in range(Steps + 1):
vol = (i / Steps) * TargetVolume
self.Playback.set_volume(volume=vol)
time.sleep(step_time)
if freezeProcess:
_fade_in()
else:
thread = threading.Thread(target=_fade_in)
thread.start()
def FadeOut(self, Duration=2000, Steps=20, freezeProcess=False):
def _fade_out():
start_vol = self.Playback.volume
step_time = Duration / Steps / 1000.0
for i in range(Steps + 1):
vol = start_vol * (1 - i / Steps)
self.Playback.set_volume(volume=vol)
time.sleep(step_time)
self.Stop()
if freezeProcess:
_fade_out()
else:
thread = threading.Thread(target=_fade_out)
thread.start()
def Mute(self):
self.Playback.set_volume(0.0)
def Unmute(self, volume=1.0):
self.Playback.set_volume(volume)
def Seek(self, seconds):
self.Playback.seek(seconds)
def Loop(self, entity, restart=False):
# Start initial playback
if restart:
self.Playback.play()
else:
self.Playback.resume()
# Define a background task so the main program doesn't freeze
def playback_watcher():
i = 0
limit = float('inf') if entity == "inf" else entity
while i < limit:
# We only act if the playback has stopped/finished
if not self.Playback.active:
self.Playback.play()
if entity != "inf":
i += 1
# Sleep briefly to keep CPU usage near 0%
time.sleep(0.1)
# Launch the watcher as a daemon thread
threading.Thread(target=playback_watcher, daemon=True).start()
# --- Info ---
def Info(self):
if self.Path:
return {
"Playing": self.Playback.playing,
"Paused": self.Playback.paused,
"Duration": self.Playback.duration,
"CurrentPos": self.Playback.curr_pos,
"Volume": self.Playback.volume,
"Path": self.Path,
"Filename": os.path.basename(self.Path)
}
return {}