-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpick_input.py
176 lines (148 loc) · 6.37 KB
/
pick_input.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import time
from aiohttp import web
from server import PromptServer
# import logging
from pydub import AudioSegment
from pydub.playback import play
import os
import sys
import io
import random
class Everything(str):
def __ne__(self, __value: object) -> bool:
return False
class PickInput:
is_paused = True
should_stop = False
selected_input = None
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"number_of_inputs": ("INT", {"default": 2, "min": 1, "max": 10, "step": 1}),
"seed": ("INT", {"default": 1}),
},
"hidden": {
**{f"input_{i}": (Everything("*"), {"forceInput": "True"}) for i in range(1, 11)}
}
}
RETURN_TYPES = (Everything("*"),)
RETURN_NAMES = ("output",)
FUNCTION = "pick_input"
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 pick_input(self, seed, **kwargs):
random.seed(seed)
# logging.info(f"Selected input at the start: {PickInput.selected_input}")
self.play_audio()
while PickInput.is_paused and not PickInput.should_stop:
# logging.info(f"PickInput.is_paused: {PickInput.is_paused}, PickInput.should_stop: {PickInput.should_stop}")
time.sleep(1) # Sleep to prevent busy waiting
if PickInput.should_stop:
PickInput.should_stop = False # Reset for next run
PickInput.is_paused = True
raise Exception("Workflow stopped by user")
PickInput.is_paused = True
PickInput.should_stop = False
# Check if the selected input exists in kwargs
if PickInput.selected_input not in kwargs:
# logging.error(f"Selected input '{PickInput.selected_input}' not found in kwargs")
# logging.info(f"Available kwargs: {list(kwargs.keys())}")
return (None,) # or handle this error as appropriate
selected_value = kwargs.get(PickInput.selected_input)
# logging.info(f"Value of selected input '{PickInput.selected_input}': {selected_value}")
# Store the value in self.target if needed
self.target = selected_value
return (selected_value,)
def create_select_handler(self, index):
async def select_input(request):
self.selected_input = index
self.is_waiting = False
return web.Response(text=f"Input {index + 1} selected")
return select_input
@PromptServer.instance.routes.get("/bjornulf_stop_pick")
async def stop_node_pick(request):
# logging.info("Stop node pick called")
PickInput.should_stop = True
PickInput.is_paused = False # Ensure the loop exits
return web.Response(text="Workflow stopped")
@PromptServer.instance.routes.get("/bjornulf_select_input_1")
async def bjornulf_select_input_1(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_1"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_2")
async def bjornulf_select_input_2(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_2"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_3")
async def bjornulf_select_input_3(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_3"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_4")
async def bjornulf_select_input_4(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_4"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_5")
async def bjornulf_select_input_5(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_5"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_6")
async def bjornulf_select_input_6(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_6"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_7")
async def bjornulf_select_input_7(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_7"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_8")
async def bjornulf_select_input_8(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_8"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_9")
async def bjornulf_select_input_9(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_9"
return web.Response(text="Node resumed")
@PromptServer.instance.routes.get("/bjornulf_select_input_10")
async def bjornulf_select_input_10(request):
# logging.info("Resume node called")
PickInput.is_paused = False
PickInput.selected_input="input_10"
return web.Response(text="Node resumed")