51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
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,
|
|
} |