-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpause_resume_stop.py
87 lines (74 loc) · 2.91 KB
/
pause_resume_stop.py
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
import time
from aiohttp import web
from server import PromptServer
# import logging
from pydub import AudioSegment
from pydub.playback import play
import os
import io
import sys
import random
class Everything(str):
def __ne__(self, __value: object) -> bool:
return False
class PauseResume:
is_paused = True
should_stop = False
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input": (Everything("*"), {"forceInput": True}),
"seed": ("INT", {"default": 1}),
}
}
RETURN_TYPES = (Everything("*"),)
RETURN_NAMES = ("output",)
FUNCTION = "loop_resume_or_stop"
CATEGORY = "Bjornulf"
def play_audio(self):
# Check if the operating system is Windows
if sys.platform.startswith('win'):
try:
# Load the audio file into memory
audio_file = os.path.join(os.path.dirname(__file__), 'bell.m4a')
# Load the audio segment without writing to any temp files
sound = AudioSegment.from_file(audio_file, format="m4a")
# Export the AudioSegment to a WAV file in memory
wav_io = io.BytesIO()
sound.export(wav_io, format='wav')
wav_data = wav_io.getvalue()
# Play the WAV data using winsound
import winsound
winsound.PlaySound(wav_data, winsound.SND_MEMORY)
except Exception as e:
print(f"An error occurred: {e}")
else:
audio_file = os.path.join(os.path.dirname(__file__), 'bell.m4a')
sound = AudioSegment.from_file(audio_file, format="m4a")
play(sound)
def loop_resume_or_stop(self, input, seed):
random.seed(seed)
self.play_audio()
self.input = input
while PauseResume.is_paused and not PauseResume.should_stop:
# logging.info(f"PauseResume.is_paused: {PauseResume.is_paused}, PauseResume.should_stop: {PauseResume.should_stop}")
time.sleep(1) # Sleep to prevent busy waiting
if PauseResume.should_stop:
PauseResume.should_stop = False # Reset for next run
PauseResume.is_paused = True
raise Exception("Workflow stopped by user")
PauseResume.is_paused = True
PauseResume.should_stop = False
return (self.input,)
@PromptServer.instance.routes.get("/bjornulf_resume")
async def resume_node(request):
# logging.info("Resume node called")
PauseResume.is_paused = False
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_stop")
async def stop_node(request):
# logging.info("Stop node called")
PauseResume.should_stop = True
PauseResume.is_paused = False # Ensure the loop exits
return web.Response(text="Workflow stopped")