mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-20 21:37:00 +08:00
Add audio widget
This commit is contained in:
parent
175fe02522
commit
c224291553
@ -93,11 +93,18 @@ class SaveAudio:
|
|||||||
return { "ui": { "audio": results } }
|
return { "ui": { "audio": results } }
|
||||||
|
|
||||||
class LoadAudio:
|
class LoadAudio:
|
||||||
|
SUPPORTED_FORMATS = ('.wav', '.mp3', '.ogg', '.flac', '.aiff', '.aif')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def INPUT_TYPES(s):
|
def INPUT_TYPES(s):
|
||||||
input_dir = folder_paths.get_input_directory()
|
input_dir = folder_paths.get_input_directory()
|
||||||
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
|
files = [
|
||||||
return {"required": {"audio": [sorted(files), ]}, }
|
f for f in os.listdir(input_dir)
|
||||||
|
if (os.path.isfile(os.path.join(input_dir, f))
|
||||||
|
and f.endswith(LoadAudio.SUPPORTED_FORMATS)
|
||||||
|
)
|
||||||
|
]
|
||||||
|
return {"required": {"audio": (sorted(files), {"audio_upload": True})}}
|
||||||
|
|
||||||
CATEGORY = "_for_testing/audio"
|
CATEGORY = "_for_testing/audio"
|
||||||
|
|
||||||
|
|||||||
156
web/extensions/core/uploadAudio.js
Normal file
156
web/extensions/core/uploadAudio.js
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
import { app } from "../../scripts/app"
|
||||||
|
import { api } from "../../scripts/api"
|
||||||
|
|
||||||
|
function splitFilePath(path) {
|
||||||
|
const folder_separator = path.lastIndexOf("/")
|
||||||
|
if (folder_separator === -1) {
|
||||||
|
return ["", path]
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
path.substring(0, folder_separator),
|
||||||
|
path.substring(folder_separator + 1)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
function getResourceURL(filename, subfolder, type = "input") {
|
||||||
|
const params = [
|
||||||
|
"filename=" + encodeURIComponent(filename),
|
||||||
|
"type=" + type,
|
||||||
|
"subfolder=" + subfolder,
|
||||||
|
app.getPreviewFormatParam().substring(1),
|
||||||
|
app.getRandParam().substring(1)
|
||||||
|
].join("&")
|
||||||
|
|
||||||
|
return `/view?${params}`
|
||||||
|
}
|
||||||
|
|
||||||
|
async function uploadFile(
|
||||||
|
audioWidget,
|
||||||
|
audioUIWidget,
|
||||||
|
file,
|
||||||
|
updateNode,
|
||||||
|
pasted = false
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
// Wrap file in formdata so it includes filename
|
||||||
|
const body = new FormData()
|
||||||
|
body.append("image", file)
|
||||||
|
if (pasted) body.append("subfolder", "pasted")
|
||||||
|
const resp = await api.fetchApi("/upload/image", {
|
||||||
|
method: "POST",
|
||||||
|
body
|
||||||
|
})
|
||||||
|
|
||||||
|
if (resp.status === 200) {
|
||||||
|
const data = await resp.json()
|
||||||
|
// Add the file to the dropdown list and update the widget value
|
||||||
|
let path = data.name
|
||||||
|
if (data.subfolder) path = data.subfolder + "/" + path
|
||||||
|
|
||||||
|
if (!audioWidget.options.values.includes(path)) {
|
||||||
|
audioWidget.options.values.push(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updateNode) {
|
||||||
|
audioUIWidget.element.src = api.apiURL(
|
||||||
|
getResourceURL(...splitFilePath(path))
|
||||||
|
)
|
||||||
|
audioWidget.value = path
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
alert(resp.status + " - " + resp.statusText)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
alert(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AudioWidget MUST be registered first, as AUDIOUPLOAD depends on AUDIO_UI to be
|
||||||
|
// present.
|
||||||
|
app.registerExtension({
|
||||||
|
name: "Comfy.AudioWidget",
|
||||||
|
async beforeRegisterNodeDef(nodeType, nodeData) {
|
||||||
|
if (["LoadAudio", "SaveAudio"].includes(nodeType.comfyClass)) {
|
||||||
|
nodeData.input.required.audioUI = ["AUDIO_UI"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getCustomWidgets() {
|
||||||
|
return {
|
||||||
|
AUDIO_UI(node, inputName) {
|
||||||
|
const audio = document.createElement("audio")
|
||||||
|
audio.controls = true
|
||||||
|
audio.classList.add("comfy-audio")
|
||||||
|
audio.setAttribute("name", "media")
|
||||||
|
|
||||||
|
const audioUIWidget = node.addDOMWidget(
|
||||||
|
inputName,
|
||||||
|
/* name=*/ "audioUI",
|
||||||
|
audio
|
||||||
|
)
|
||||||
|
audioUIWidget.serialize = false
|
||||||
|
|
||||||
|
const isOutputNode = node.constructor.nodeData.output_node
|
||||||
|
if (isOutputNode) {
|
||||||
|
const onExecuted = node.onExecuted
|
||||||
|
node.onExecuted = function(message) {
|
||||||
|
onExecuted?.apply(this, arguments)
|
||||||
|
const audios = message.audio
|
||||||
|
if (!audios) return
|
||||||
|
const audio = audios[0]
|
||||||
|
audioUIWidget.element.src = api.apiURL(
|
||||||
|
getResourceURL(audio.filename, audio.subfolder, "output")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { widget: audioUIWidget }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
app.registerExtension({
|
||||||
|
name: "Comfy.UploadAudio",
|
||||||
|
async beforeRegisterNodeDef(nodeType, nodeData) {
|
||||||
|
if (nodeData?.input?.required?.audio?.[1]?.audio_upload === true) {
|
||||||
|
nodeData.input.required.upload = ["AUDIOUPLOAD"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getCustomWidgets() {
|
||||||
|
return {
|
||||||
|
AUDIOUPLOAD(node, inputName) {
|
||||||
|
// The widget that allows user to select file.
|
||||||
|
const audioWidget = node.widgets.find(w => w.name === "audio")
|
||||||
|
const audioUIWidget = node.widgets.find(w => w.name === "audioUI")
|
||||||
|
|
||||||
|
audioWidget.callback = function() {
|
||||||
|
audioUIWidget.element.src = api.apiURL(
|
||||||
|
getResourceURL(...splitFilePath(audioWidget.value))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileInput = document.createElement("input")
|
||||||
|
fileInput.type = "file"
|
||||||
|
fileInput.accept = "audio/*"
|
||||||
|
fileInput.style.display = "none"
|
||||||
|
fileInput.onchange = () => {
|
||||||
|
if (fileInput.files.length) {
|
||||||
|
uploadFile(audioWidget, audioUIWidget, fileInput.files[0], true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The widget to pop up the upload dialog.
|
||||||
|
const uploadWidget = node.addWidget(
|
||||||
|
"button",
|
||||||
|
inputName,
|
||||||
|
/* value=*/ "",
|
||||||
|
() => {
|
||||||
|
fileInput.click()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
uploadWidget.label = "choose file to upload"
|
||||||
|
uploadWidget.serialize = false
|
||||||
|
|
||||||
|
return { widget: uploadWidget }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
Loading…
x
Reference in New Issue
Block a user