stop sound
This commit is contained in:
35
init.py
35
init.py
@@ -50,27 +50,45 @@ def isSunUp() -> bool:
|
|||||||
now = datetime.now(city.tzinfo)
|
now = datetime.now(city.tzinfo)
|
||||||
return s["sunrise"] <= now <= s["sunset"]
|
return s["sunrise"] <= now <= s["sunset"]
|
||||||
|
|
||||||
def playAudio(filePath: str) -> None:
|
def playAudio(filePath: str, loop: bool = False) -> Callable[[], None]:
|
||||||
"""
|
"""
|
||||||
Play an audio file through the Raspberry Pi's standard aux output.
|
Play an audio file through the Raspberry Pi's standard aux output.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filePath: Path to the audio file (.wav, .mp3, .ogg, etc.)
|
filePath: Path to the audio file (.wav, .mp3, .ogg, etc.)
|
||||||
|
loop: Whether to loop the audio playback until stopped (default: False)
|
||||||
|
Returns:
|
||||||
|
A callable that stops the audio playback when invoked.
|
||||||
"""
|
"""
|
||||||
if filePath.endswith(".wav"):
|
if filePath.endswith(".wav"):
|
||||||
subprocess.Popen(["aplay", filePath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
if loop:
|
||||||
|
# Use sox's play command with repeat for looping wav files
|
||||||
|
process = subprocess.Popen(["play", filePath, "repeat", "-"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
else:
|
||||||
|
process = subprocess.Popen(["aplay", filePath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
else:
|
else:
|
||||||
# Use mpg123 for mp3, or ffplay/mpv as fallback for other formats
|
# Use mpg123 for mp3
|
||||||
subprocess.Popen(["mpg123", "-q", filePath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
if loop:
|
||||||
|
process = subprocess.Popen(["mpg123", "-q", "--loop", "-1", filePath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
else:
|
||||||
|
process = subprocess.Popen(["mpg123", "-q", filePath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
def stop():
|
||||||
|
process.terminate()
|
||||||
|
|
||||||
|
return stop
|
||||||
|
|
||||||
|
|
||||||
class WakeUpAction:
|
class WakeUpAction:
|
||||||
offsetSeconds: int # how many seconds before alarm time to trigger
|
offsetSeconds: int # how many seconds before alarm time to trigger
|
||||||
action: Callable[[], None]
|
action: Callable[[], None]
|
||||||
|
dismissAction: Callable[[], None] | None = None
|
||||||
triggered: bool = False
|
triggered: bool = False
|
||||||
|
|
||||||
def __init__(self, offsetSeconds: int, action: Callable[[], None]):
|
def __init__(self, offsetSeconds: int, action: Callable[[], None], dismissAction: Callable[[], None] | None):
|
||||||
self.offsetSeconds = offsetSeconds
|
self.offsetSeconds = offsetSeconds
|
||||||
self.action = action
|
self.action = action
|
||||||
|
self.dismissAction = dismissAction
|
||||||
self.triggered = False
|
self.triggered = False
|
||||||
|
|
||||||
|
|
||||||
@@ -100,9 +118,9 @@ class AlarmClock:
|
|||||||
self.addWakeUpAction(0, lambda: self.triggerAlarm()) # Activate alarm state
|
self.addWakeUpAction(0, lambda: self.triggerAlarm()) # Activate alarm state
|
||||||
self.setLcdText("AlarmClock Init")
|
self.setLcdText("AlarmClock Init")
|
||||||
|
|
||||||
def addWakeUpAction(self, offsetSeconds: int, action: Callable[[], None]):
|
def addWakeUpAction(self, offsetSeconds: int, action: Callable[[], None], dismissAction: Callable[[], None] | None = None):
|
||||||
"""Add a wakeup action to be triggered offsetSeconds before alarm time"""
|
"""Add a wakeup action to be triggered offsetSeconds before alarm time"""
|
||||||
self.wakeupActions.append(WakeUpAction(offsetSeconds, action))
|
self.wakeupActions.append(WakeUpAction(offsetSeconds, action, dismissAction))
|
||||||
|
|
||||||
def resetWakeUpActions(self):
|
def resetWakeUpActions(self):
|
||||||
for action in self.wakeupActions:
|
for action in self.wakeupActions:
|
||||||
@@ -120,6 +138,9 @@ class AlarmClock:
|
|||||||
if not self.alarmActive:
|
if not self.alarmActive:
|
||||||
return
|
return
|
||||||
self.alarmActive = False
|
self.alarmActive = False
|
||||||
|
for action in self.wakeupActions:
|
||||||
|
if action.dismissAction:
|
||||||
|
action.dismissAction()
|
||||||
print("Alarm dismissed")
|
print("Alarm dismissed")
|
||||||
|
|
||||||
def checkWakeUpActions(self):
|
def checkWakeUpActions(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user