-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundHub_nodes.py
264 lines (224 loc) · 8.96 KB
/
SoundHub_nodes.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import os
import hashlib
import numpy as np
import torch
import torchaudio
import folder_paths
import random
from datetime import datetime
class LoadAudio:
@classmethod
def INPUT_TYPES(s):
input_dir = folder_paths.get_input_directory()
audio_extensions = ('.wav', '.mp3', '.ogg', '.flac')
files = [f for f in os.listdir(input_dir)
if os.path.isfile(os.path.join(input_dir, f))
and f.lower().endswith(audio_extensions)]
return {"required": {
"audio": (sorted(files), {
"file_type": "audio",
"file_select": "audio",
"upload": "audio",
"display": "file"
}),
"preview": ("BOOLEAN", {
"default": True,
"label": "Preview after loading"
}),
"channels": (["auto", "mono", "stereo"], ),
"start_time": ("FLOAT", {
"default": 0,
"min": 0,
"max": 10000000,
"step": 0.01,
"display": "number"
}),
"duration": ("FLOAT", {
"default": 0,
"min": 0,
"max": 10000000,
"step": 0.01,
"display": "number"
}),
"volume": ("FLOAT", {
"default": 1.0,
"min": 0.0,
"max": 5.0,
"step": 0.1,
"display": "number"
}),
}}
CATEGORY = "SoundHub"
RETURN_TYPES = ("AUDIO", "SAMPLE_RATE")
RETURN_NAMES = ("audio", "sample_rate")
FUNCTION = "load_audio"
def load_audio(self, audio, preview, channels, start_time, duration, volume):
audio_path = folder_paths.get_annotated_filepath(audio)
waveform, sample_rate = torchaudio.load(audio_path)
if waveform.dtype != torch.float32:
waveform = waveform.to(torch.float32)
if duration > 0:
start_frame = int(start_time * sample_rate)
duration_frames = int(duration * sample_rate)
waveform = waveform[:, start_frame:start_frame + duration_frames]
elif start_time > 0:
start_frame = int(start_time * sample_rate)
waveform = waveform[:, start_frame:]
if channels == "mono":
if waveform.size(0) == 2:
waveform = torch.mean(waveform, dim=0, keepdim=True)
elif channels == "stereo":
if waveform.size(0) == 1:
waveform = waveform.repeat(2, 1)
waveform = waveform * volume
if preview:
preview_data = {
"ui": {
"audio": {
"audio": audio_path,
"type": "input"
}
}
}
return (waveform, sample_rate, preview_data)
return (waveform, sample_rate)
@classmethod
def IS_CHANGED(s, audio, preview, channels, start_time, duration, volume):
audio_path = folder_paths.get_annotated_filepath(audio)
m = hashlib.sha256()
with open(audio_path, 'rb') as f:
m.update(f.read())
m.update(str(start_time).encode())
m.update(str(duration).encode())
m.update(str(channels).encode())
m.update(str(volume).encode())
return m.digest().hex()
class PreviewAudio:
def __init__(self):
self.output_dir = folder_paths.get_temp_directory()
self.type = "temp"
self.prefix_append = "_temp_" + ''.join(random.choice("abcdefghijklmnopqrstupvxyz") for x in range(5))
@classmethod
def INPUT_TYPES(s):
return {"required": {
"audio": ("AUDIO",),
"sample_rate": ("SAMPLE_RATE",),
}}
CATEGORY = "SoundHub"
RETURN_TYPES = ()
FUNCTION = "preview_audio"
OUTPUT_NODE = True
def preview_audio(self, audio, sample_rate):
# Add basic audio validation
if audio is None or not isinstance(audio, torch.Tensor):
raise ValueError("Invalid audio input")
if audio.dim() > 2 or audio.dim() == 0:
raise ValueError("Audio must be mono or stereo (1 or 2 channels)")
os.makedirs(self.output_dir, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
filename = f"preview{self.prefix_append}_{timestamp}.wav"
filepath = os.path.join(self.output_dir, filename)
if isinstance(audio, torch.Tensor):
torchaudio.save(
filepath,
audio,
sample_rate,
format="wav"
)
preview = {
"audio": filepath,
"type": self.type
}
return {"ui": {"audio": preview}}
class SaveAudio:
def __init__(self):
# Initialize output directory and settings
self.output_dir = folder_paths.get_output_directory()
self.type = "output"
self.prefix_append = ""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"audio": ("AUDIO", {"tooltip": "The audio to save."}),
"sample_rate": ("SAMPLE_RATE", {"tooltip": "The sample rate of the audio."}),
"filename_prefix": ("STRING", {
"default": "ComfyUI_Audio",
"tooltip": "The prefix for the file to save. Can include formatting like %date:yyyy-MM-dd%"
}),
"format": (["wav", "mp3", "ogg", "flac"], {
"default": "wav",
"tooltip": "Audio format to save as"
})
},
"hidden": {
"prompt": "PROMPT",
"extra_pnginfo": "EXTRA_PNGINFO"
},
}
RETURN_TYPES = ()
FUNCTION = "save_audio"
OUTPUT_NODE = True
CATEGORY = "SoundHub"
DESCRIPTION = "Saves the input audio to your ComfyUI output directory."
def save_audio(self, audio, sample_rate, filename_prefix="ComfyUI_Audio", format="wav", prompt=None, extra_pnginfo=None):
# Validate audio format
if format not in ["wav", "mp3", "ogg", "flac"]:
raise ValueError(f"Unsupported audio format: {format}")
# Validate sample rate
if sample_rate <= 0:
raise ValueError("Invalid sample rate")
# Add any prefix append to the filename
filename_prefix += self.prefix_append
# Create output directory if it doesn't exist
os.makedirs(self.output_dir, exist_ok=True)
# Get current timestamp for unique filename
current_time = datetime.now().strftime("%Y%m%d-%H%M%S")
# Build base filename
filename = f"{filename_prefix}_{current_time}"
# Handle subfolders in filename_prefix
subfolder = os.path.dirname(filename_prefix) if "/" in filename_prefix else ""
if subfolder != "":
# Create subfolder if specified in prefix
full_output_folder = os.path.join(self.output_dir, subfolder)
os.makedirs(full_output_folder, exist_ok=True)
else:
full_output_folder = self.output_dir
# Ensure unique filename by incrementing counter
counter = 1
while True:
file = f"{filename}_{counter:05}.{format}"
full_path = os.path.join(full_output_folder, file)
if not os.path.exists(full_path):
break
counter += 1
# Save the audio file
if isinstance(audio, torch.Tensor):
# Ensure audio data is in float32 format
if audio.dtype != torch.float32:
audio = audio.to(torch.float32)
# Save audio using torchaudio
torchaudio.save(
full_path,
audio,
sample_rate,
format=format
)
# Prepare results for UI
results = [{
"filename": file,
"subfolder": subfolder,
"type": self.type,
"format": format
}]
# Handle metadata if provided
# Note: Audio metadata handling could be implemented here
if prompt is not None or extra_pnginfo is not None:
# TODO: Add metadata handling for audio files
pass
# Return results for UI display
return {"ui": {"audio": results[0]}}
@classmethod
def IS_CHANGED(s, audio, sample_rate, filename_prefix, format):
# Always save a new file
return float("nan")