diff --git a/__init__.py b/__init__.py index 334761a..dd14aae 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,5 @@ WEB_DIRECTORY = "js" -NODE_CLASS_MAPPINGS = {} +from .nodes import NODE_CLASS_MAPPINGS __all__ = ['NODE_CLASS_MAPPINGS'] from aiohttp import ClientSession, web diff --git a/nodes.py b/nodes.py new file mode 100644 index 0000000..30b6843 --- /dev/null +++ b/nodes.py @@ -0,0 +1,51 @@ +import soundfile as sf +import requests +import io +import numpy as np +import torch + +class AudioLoadPath: + @classmethod + def INPUT_TYPES(s): + return {"required": { "path": ("STRING", {"default": "X://insert/path/here.mp4"}), + "sample_rate": ("INT", {"default": 22050, "min": 6000, "max": 192000, "step": 1}), + "offset": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1e6, "step": 0.001}), + "duration": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1e6, "step": 0.001})}} + + RETURN_TYPES = ("AUDIO", ) + CATEGORY = "Audio Reactor" + FUNCTION = "load" + + def load(self, path: str, sample_rate: int, offset: float, duration: float|None): + if duration == 0.0: + duration = None + + if path.startswith(('http://', 'https://')): + # 对于网络路径,直接从内存加载 + try: + response = requests.get(path) + response.raise_for_status() + audio_data = io.BytesIO(response.content) + + # 使用 soundfile 从内存中读取音频数据 + audio, file_sr = sf.read(audio_data) + + # 如果需要重采样 + if file_sr != sample_rate: + # 这里需要添加重采样逻辑 + # 可以使用 librosa.resample 或其他方法 + pass + + except Exception as e: + raise Exception(f"加载网络音频失败: {str(e)}") + else: + # 本地文件使用原有的 librosa 方式加载 + audio, _ = librosa.load(path, sr=sample_rate, offset=offset, duration=duration) + + # 转换为 torch tensor 并调整维度 + audio = torch.from_numpy(audio)[None,:,None] + return (audio,) + +NODE_CLASS_MAPPINGS = { + "AudioLoadPath": AudioLoadPath, +} \ No newline at end of file