perf: reduce prompt relay and audio ui overhead
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
import folder_paths
|
||||
import numpy as np
|
||||
import os
|
||||
import torch
|
||||
import av
|
||||
|
||||
DEFAULT_SAMPLE_RATE = 44100
|
||||
DEFAULT_SILENCE_SECONDS = 1
|
||||
|
||||
|
||||
def build_silence(sample_rate: int = DEFAULT_SAMPLE_RATE, seconds: int = DEFAULT_SILENCE_SECONDS, channels: int = 2) -> torch.Tensor:
|
||||
return torch.zeros((channels, int(sample_rate * seconds)), dtype=torch.float32)
|
||||
|
||||
def f32_pcm(wav: torch.Tensor) -> torch.Tensor:
|
||||
"""Convert audio to float 32 bits PCM format."""
|
||||
if wav.dtype.is_floating_point:
|
||||
@@ -23,18 +31,17 @@ def load_audio_file(filepath: str) -> tuple[torch.Tensor, int]:
|
||||
sr = stream.codec_context.sample_rate
|
||||
n_channels = stream.channels
|
||||
|
||||
frames = []
|
||||
frame_arrays = []
|
||||
for frame in af.decode(streams=stream.index):
|
||||
buf = torch.from_numpy(frame.to_ndarray())
|
||||
if buf.shape[0] != n_channels:
|
||||
buf = buf.view(-1, n_channels).t()
|
||||
array = frame.to_ndarray()
|
||||
if array.shape[0] != n_channels:
|
||||
array = array.reshape(-1, n_channels).T
|
||||
frame_arrays.append(array)
|
||||
|
||||
frames.append(buf)
|
||||
|
||||
if not frames:
|
||||
if not frame_arrays:
|
||||
raise ValueError("No audio frames decoded.")
|
||||
|
||||
wav = torch.cat(frames, dim=1)
|
||||
wav = torch.from_numpy(np.concatenate(frame_arrays, axis=1))
|
||||
wav = f32_pcm(wav)
|
||||
return wav, sr
|
||||
|
||||
@@ -99,17 +106,16 @@ class LoadAudioUI:
|
||||
missing_info = audio if audio != "none" else "None selected"
|
||||
print(f"!!! [LoadAudioUI] Warning: Audio file '{missing_info}' not found. Outputting 1 second of silence.")
|
||||
|
||||
sample_rate = 44100
|
||||
# 1 second of silence (stereo) -> shape [channels, time]
|
||||
waveform = torch.zeros((2, 44100))
|
||||
sample_rate = DEFAULT_SAMPLE_RATE
|
||||
waveform = build_silence(sample_rate)
|
||||
else:
|
||||
try:
|
||||
waveform, sample_rate = load_audio_file(audio_path)
|
||||
except Exception as e:
|
||||
# If decoding fails for any reason, fallback to silence rather than crashing the workflow
|
||||
print(f"!!! [LoadAudioUI] Error decoding {audio}: {e}. Falling back to silence.")
|
||||
sample_rate = 44100
|
||||
waveform = torch.zeros((2, 44100))
|
||||
sample_rate = DEFAULT_SAMPLE_RATE
|
||||
waveform = build_silence(sample_rate)
|
||||
|
||||
# Convert seconds to frames
|
||||
start_frame = int(start_time * sample_rate)
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import logging
|
||||
import os
|
||||
import types
|
||||
|
||||
import comfy.ldm.modules.attention
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
_DEBUG_PROMPT_RELAY = os.environ.get("PROMPT_RELAY_DEBUG", "").strip().lower() in {"1", "true", "yes", "on"}
|
||||
_DEBUG_LOG_PATH = os.path.join(os.path.dirname(__file__), "debug_prompt_relay.log")
|
||||
|
||||
|
||||
def _masked_attention(q, k, v, heads, mask, transformer_options={}, **kwargs):
|
||||
@@ -73,10 +76,10 @@ def _make_masked_override(prev_override):
|
||||
|
||||
|
||||
def debug_log(msg):
|
||||
if not _DEBUG_PROMPT_RELAY:
|
||||
return
|
||||
try:
|
||||
import os
|
||||
log_path = os.path.join(os.path.dirname(__file__), "debug_prompt_relay.log")
|
||||
with open(log_path, "a", encoding="utf-8") as f:
|
||||
with open(_DEBUG_LOG_PATH, "a", encoding="utf-8") as f:
|
||||
f.write(msg + "\n")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user