-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
VariationUtils.py
322 lines (270 loc) · 10.5 KB
/
VariationUtils.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import torch
import torchaudio
import os
import soundfile as sf
import numpy as np
from comfy.model_management import get_torch_device
from SampleDiffusion import AudioInference
from libs.diffusion_library.sampler import SamplerType
from libs.diffusion_library.scheduler import SchedulerType
# -------------
# LIST CREATION
# -------------
# Split up audio into a sequence of smaller clips (represented as a 4d tensor) to be processed individually
class SliceAudio:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"audio": ("AUDIO",),
"clip_size": ("INT", {"default": 1, "min": 1, "max": 1e9, "step": 1}),
},
"optional": {
"sample_rate": ("INT", {"default": 44100, "min": 1, "max": 1e9, "step": 1, "forceInput": True}),
},
}
RETURN_TYPES = ("AUDIO_LIST", "INT")
RETURN_NAMES = ("audio_list", "sample_rate")
FUNCTION = "slice_audio"
CATEGORY = "🎙️Jags_Audio/VariationUtils"
def slice_audio(self, audio, clip_size, sample_rate):
return list(torch.split(audio.clone(), clip_size, 2)), sample_rate
class BatchToList:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"audio": ("AUDIO",),
},
"optional": {
"sample_rate": ("INT", {"default": 44100, "min": 1, "max": 1e9, "step": 1, "forceInput": True}),
},
}
RETURN_TYPES = ("AUDIO_LIST", "INT")
RETURN_NAMES = ("audio_list", "sample_rate")
FUNCTION = "batch_to_list"
CATEGORY = "🎙️Jags_Audio/VariationUtils"
def batch_to_list(self, audio, sample_rate):
return list(torch.split(audio.clone(), 1, 0)), sample_rate
class LoadAudioDir:
@classmethod
def INPUT_TYPES(cls):
"""
Input Types
"""
return {
"required": {
"dir_path": ("STRING", {}),
}
}
RETURN_TYPES = ("STRING", "AUDIO_LIST", "INT")
RETURN_NAMES = ("dir_path", "audio_list", "sample_rate")
FUNCTION = "load_audio_dir"
OUTPUT_NODE = True
CATEGORY = "🎙️Jags_Audio/VariationUtils"
def load_audio_dir(self, dir_path):
tensor_list = []
sample_rate = None
if dir_path == '':
return dir_path, tensor_list, sample_rate
for file_path in os.listdir(dir_path):
if os.path.isfile(f'{dir_path}/{file_path}'):
if file_path.endswith('.mp3'):
if os.path.exists(file_path.replace('.mp3', '') + '.wav'):
file_path = file_path.replace('.mp3', '') + '.wav'
else:
data, sample_rate = sf.read(file_path)
sf.write(file_path.replace('.mp3', '') + '.wav', data, sample_rate)
os.remove(file_path.replace('.wav', '.mp3'))
waveform, sample_rate = torchaudio.load(f'{dir_path}/{file_path}')
waveform = waveform.to(get_torch_device())
waveform = waveform.unsqueeze(0)
tensor_list.append(waveform)
return dir_path, tensor_list, sample_rate
# ----------------
# LIST AGGREGATION
# ----------------
class ListToBatch:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"audio_list": ("AUDIO_LIST",),
},
"optional": {
"sample_rate": ("INT", {"default": 44100, "min": 1, "max": 1e9, "step": 1, "forceInput": True}),
},
}
RETURN_TYPES = ("AUDIO", "INT")
RETURN_NAMES = ("🎙️audio", "sample_rate")
FUNCTION = "list_to_batch"
CATEGORY = "🎙️Jags_Audio/VariationUtils"
def list_to_batch(self, audio_list, sample_rate):
max_len = 0
batch_size = 0
for tensor in audio_list:
max_len = max(tensor.size(2), max_len)
batch_size += tensor.size(0)
tensor_out = torch.zeros((batch_size, 2, max_len), device=audio_list[0].device)
batch_start = 0
for tensor in audio_list:
batch_end = batch_start + tensor.size(0)
tensor_out[batch_start:batch_end, :, :tensor.size(2)] += tensor
batch_start = batch_end
return tensor_out, sample_rate
class ConcatAudioList:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"audio_list": ("AUDIO_LIST",)
},
"optional": {
"sample_rate": ("INT", {"default": 44100, "min": 1, "max": 1e9, "step": 1, "forceInput": True}),
},
}
RETURN_TYPES = ("AUDIO", "INT")
RETURN_NAMES = ("🎙️audio", "sample_rate")
FUNCTION = "concat_audio"
CATEGORY = "🎙️Jags_Audio/VariationUtils"
def concat_audio(self, audio_list, sample_rate):
return torch.cat(audio_list, 2), sample_rate
# Perform variation on each audio tensor in a list
class SequenceVariation:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
"""
Input Types
"""
return {
"required": {
"audio_model": ("DD_MODEL",),
"batch_size": ("INT", {"default": 1, "min": 1, "max": 1e9, "step": 1}),
"steps": ("INT", {"default": 50, "min": 1, "max": 1e9, "step": 1}),
"sampler": (SamplerType._member_names_, {"default": "V_IPLMS"}),
"sigma_min": ("FLOAT", {"default": 0.1, "min": 0.0, "max": 1280, "step": 0.01}),
"sigma_max": ("FLOAT", {"default": 50, "min": 0.0, "max": 1280, "step": 0.01}),
"rho": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 128.0, "step": 0.01}),
"scheduler": (SchedulerType._member_names_, {"default": "V_CRASH"}),
"noise_level": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}),
"seed": ("INT", {"default": -1}),
"audio_list": ("AUDIO_LIST",)
},
"optional": {},
}
RETURN_TYPES = ("AUDIO_LIST", "INT")
RETURN_NAMES = ("audio_list", "sample_rate")
FUNCTION = "do_variation"
CATEGORY = "🎙️Jags_Audio/VariationUtils"
def do_variation(self, audio_model, batch_size, steps, sampler, sigma_min, sigma_max, rho, scheduler, audio_list, noise_level=0.7, seed=-1):
audio_inference = AudioInference()
tensor_list_out = []
sample_rate = 44100
for tensor in audio_list:
_, tensor_out, sample_rate = audio_inference.do_sample(
audio_model=audio_model,
mode='Variation',
batch_size=batch_size,
steps=steps,
sampler=sampler,
sigma_min=sigma_min,
sigma_max=sigma_max,
rho=rho,
scheduler=scheduler,
input_tensor=tensor,
noise_level=noise_level,
seed=seed)
tensor_list_out.append(tensor_out)
return tensor_list_out, sample_rate
class GetSingle:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"audio_list": ("AUDIO_LIST",),
"index": ("INT", {"default": 1, "min": 1, "max": 1e9, "step": 1})
},
"optional": {
"sample_rate": ("INT", {"default": 44100, "min": 1, "max": 1e9, "step": 1, "forceInput": True}),
},
}
RETURN_TYPES = ("AUDIO", "INT")
RETURN_NAMES = ("🎙️audio", "sample_rate")
FUNCTION = "get_single"
CATEGORY = "🎙️Jags_Audio/VariationUtils"
def get_single(self, audio_list, index, sample_rate):
return audio_list[index], sample_rate
# ----------
# PROCESSING
# ----------
# Perform variation on each audio tensor in a list
class BulkVariation:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
"""
Input Types
"""
return {
"required": {
"audio_model": ("DD_MODEL",),
"batch_size": ("INT", {"default": 1, "min": 1, "max": 1e9, "step": 1}),
"steps": ("INT", {"default": 50, "min": 1, "max": 1e9, "step": 1}),
"sampler": (SamplerType._member_names_, {"default": "V_IPLMS"}),
"sigma_min": ("FLOAT", {"default": 0.1, "min": 0.0, "max": 1280, "step": 0.01}),
"sigma_max": ("FLOAT", {"default": 50, "min": 0.0, "max": 1280, "step": 0.01}),
"rho": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 128.0, "step": 0.01}),
"scheduler": (SchedulerType._member_names_, {"default": "V_CRASH"}),
"noise_level": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}),
"seed": ("INT", {"default": -1}),
"audio_list": ("AUDIO_LIST",)
},
"optional": {},
}
RETURN_TYPES = ("AUDIO_LIST", "INT")
RETURN_NAMES = ("audio_list", "sample_rate")
FUNCTION = "do_variation"
CATEGORY = "🎙️Jags_Audio/VariationUtils"
def do_variation(self, audio_model, batch_size, steps, sampler, sigma_min, sigma_max, rho, scheduler, audio_list, noise_level=0.7, seed=-1):
audio_inference = AudioInference()
tensor_list_out = []
sample_rate = 44100
for tensor in audio_list:
_, tensor_out, sample_rate = audio_inference.do_sample(
audio_model=audio_model,
mode='Variation',
batch_size=batch_size,
steps=steps,
sampler=sampler,
sigma_min=sigma_min,
sigma_max=sigma_max,
rho=rho,
scheduler=scheduler,
input_tensor=tensor,
noise_level=noise_level,
seed=seed)
tensor_list_out.append(tensor_out)
return tensor_list_out, sample_rate
NODE_CLASS_MAPPINGS = {
'SliceAudio': SliceAudio,
'BatchToList': BatchToList,
'LoadAudioDir': LoadAudioDir,
'ListToBatch': ListToBatch,
'ConcatAudioList': ConcatAudioList,
'GetSingle': GetSingle,
'SequenceVariation': SequenceVariation,
'BulkVariation': BulkVariation
}
NODE_DISPLAY_NAME_MAPPINGS = {
'SliceAudio': 'Jags_SliceAudio',
'BatchToList': 'Jags_BatchToList',
'LoadAudioDir': 'Jags_LoadAudioDir',
'ListToBatch': 'Jags_ListToBatch',
'ConcatAudioList': 'Jags_ConcatAudioList',
'SequenceVariation': 'Jags_SequenceVariation',
'GetSingle': 'Jags_GetSingle',
'BulkVariation': 'Jags_BulkVariation'
}