always test audio because on startup mpv won't immediately work

This commit is contained in:
JJJHolscher 2023-03-29 18:08:43 +02:00
parent a5c1a49248
commit dfdc92a98b

50
main.py
View File

@ -38,7 +38,6 @@ def generate_interval(work_time: int, break_time: int):
yield 60 * 25
yield 60 * 30
def play_playlist(playlist):
"""
Play the playlist.
@ -47,6 +46,18 @@ def play_playlist(playlist):
["mpv", "--shuffle", "--no-video", playlist], stdout=DEVNULL, stderr=DEVNULL
)
def test_playlist(playlist, duration=None):
print(
"testing playlist, press enter to start if you hear the playlist",
end="",
flush=True,
)
if duration is None:
duration = float("inf")
play = play_playlist(playlist)
focus=bool(WINDOW_TITLE) and duration is None
wait_or_enter(duration, i3_focus=focus)
play.terminate()
def wait_or_enter(duration, i3_focus=False):
"""
@ -80,10 +91,10 @@ def wait_or_enter(duration, i3_focus=False):
if stop:
return sys.stdin.readline().strip()
else:
print()
tcflush(sys.stdin, TCIOFLUSH)
return
def write_break_message(msg):
today = datetime.now().strftime("%Y-%m-%d")
minute = datetime.now().strftime("%H:%M")
@ -95,16 +106,6 @@ def write_break_message(msg):
def main(playlist, work_time, break_time):
interval = generate_interval(work_time, break_time)
print(
"testing playlist, press enter to start if you hear the playlist",
end="",
flush=True,
)
play = play_playlist(playlist)
wait_or_enter(float("inf"), i3_focus=True)
play.terminate()
while True:
# work
i = next(interval)
@ -127,17 +128,8 @@ def main(playlist, work_time, break_time):
def timer(playlist, duration):
print(
"testing playlist, press enter to start if you hear the playlist",
end="",
flush=True,
)
play = play_playlist(playlist)
wait_or_enter(float("inf"), i3_focus=bool(WINDOW_TITLE))
play.terminate()
# work
print("wait for", duration, "seconds")
second = datetime.now().strftime("%H:%M:%S")
print(second, "wait for", duration, "seconds")
msg = wait_or_enter(duration, i3_focus=bool(WINDOW_TITLE))
# start music to signal the end of a break.
@ -147,7 +139,6 @@ def timer(playlist, duration):
wait_or_enter(float("inf"), i3_focus=bool(WINDOW_TITLE))
play.terminate()
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
@ -179,9 +170,20 @@ if __name__ == "__main__":
type=int,
help="set true for this to be a single timer",
)
parser.add_argument(
"--test-sound",
action="store_true",
help="test the sound before starting the timer",
)
args = parser.parse_args()
WINDOW_TITLE = args.window_title
if args.test_sound:
test_playlist(args.playlist)
else:
# For some reason the first time I play the playlist it doesn't work.
# This is a workaround.
test_playlist(args.playlist, 10)
if args.once:
timer(args.playlist, args.once)
else: