Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,25 @@ import shutil
|
|
| 4 |
import zipfile
|
| 5 |
import librosa
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
from pydub import AudioSegment
|
| 8 |
from pydub.silence import split_on_silence
|
| 9 |
-
from moviepy.editor import AudioFileClip, ImageClip, CompositeVideoClip
|
| 10 |
-
# Keep import available but unused (Full feature set available)
|
| 11 |
-
from moviepy.video.fx.all import blackwhite, lum_contrast
|
| 12 |
import subprocess
|
| 13 |
from pathlib import Path
|
| 14 |
import sys
|
| 15 |
import yt_dlp
|
| 16 |
import json
|
| 17 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# --- PATCH FOR PILLOW 10.0+ vs MOVIEPY 1.0.3 COMPATIBILITY ---
|
| 20 |
import PIL.Image
|
|
@@ -31,1035 +39,506 @@ TEMP_DIR = Path("temp_processing")
|
|
| 31 |
# Startup Checks
|
| 32 |
# -----------------------------
|
| 33 |
def check_ffmpeg():
|
| 34 |
-
"""Ensure FFmpeg is installed and accessible."""
|
| 35 |
if shutil.which("ffmpeg") is None:
|
| 36 |
print("CRITICAL WARNING: FFmpeg not found in system PATH.")
|
| 37 |
-
print("Audio processing (pydub/demucs) will fail.")
|
| 38 |
return False
|
| 39 |
return True
|
| 40 |
|
| 41 |
check_ffmpeg()
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# -----------------------------
|
| 45 |
# Cloud Import
|
| 46 |
# -----------------------------
|
| 47 |
def download_from_url(url):
|
| 48 |
-
|
| 49 |
-
if not url:
|
| 50 |
-
return None
|
| 51 |
-
|
| 52 |
print(f"Fetching URL: {url}")
|
| 53 |
-
|
| 54 |
-
if TEMP_DIR.exists():
|
| 55 |
-
shutil.rmtree(TEMP_DIR, ignore_errors=True)
|
| 56 |
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 57 |
|
| 58 |
ydl_opts = {
|
| 59 |
"format": "bestaudio/best",
|
| 60 |
"outtmpl": str(TEMP_DIR / "%(title)s.%(ext)s"),
|
| 61 |
-
"postprocessors": [
|
| 62 |
-
|
| 63 |
-
],
|
| 64 |
-
"quiet": True,
|
| 65 |
-
"no_warnings": True,
|
| 66 |
}
|
| 67 |
-
|
| 68 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 69 |
info = ydl.extract_info(url, download=True)
|
| 70 |
filename = ydl.prepare_filename(info)
|
| 71 |
-
|
| 72 |
-
return str(final_path)
|
| 73 |
|
| 74 |
|
| 75 |
# -----------------------------
|
| 76 |
-
# File
|
| 77 |
# -----------------------------
|
| 78 |
def safe_copy_to_temp(audio_file: str) -> str:
|
| 79 |
-
"""
|
| 80 |
-
Copy source file into TEMP_DIR with a safe filename (avoids path/space/unicode surprises).
|
| 81 |
-
"""
|
| 82 |
src = Path(audio_file)
|
| 83 |
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 84 |
safe_stem = "".join(c if c.isalnum() or c in "._-" else "_" for c in src.stem)
|
| 85 |
dst = TEMP_DIR / f"{safe_stem}{src.suffix.lower()}"
|
| 86 |
-
try:
|
| 87 |
-
|
| 88 |
-
except Exception:
|
| 89 |
-
return str(src)
|
| 90 |
return str(dst)
|
| 91 |
|
| 92 |
-
|
| 93 |
def ensure_wav(input_path: str) -> str:
|
| 94 |
-
"""
|
| 95 |
-
Convert input audio to WAV for Demucs reliability.
|
| 96 |
-
"""
|
| 97 |
p = Path(input_path)
|
| 98 |
-
if p.suffix.lower() == ".wav":
|
| 99 |
-
return str(p)
|
| 100 |
-
|
| 101 |
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 102 |
out = TEMP_DIR / f"{p.stem}.wav"
|
| 103 |
-
|
| 104 |
-
audio.export(str(out), format="wav")
|
| 105 |
return str(out)
|
| 106 |
|
| 107 |
|
| 108 |
# -----------------------------
|
| 109 |
-
# Demucs
|
| 110 |
# -----------------------------
|
| 111 |
def run_demucs(cmd):
|
| 112 |
-
"""
|
| 113 |
-
Run demucs and return stdout. If it fails, raise gr.Error with stdout/stderr tail.
|
| 114 |
-
"""
|
| 115 |
p = subprocess.run(cmd, capture_output=True, text=True)
|
| 116 |
if p.returncode != 0:
|
| 117 |
-
raise gr.Error(
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
if len(y) < sr * 10:
|
| 138 |
-
continue
|
| 139 |
-
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
| 140 |
-
t = float(tempo[0] if np.ndim(tempo) > 0 else tempo)
|
| 141 |
-
if 40 <= t <= 220:
|
| 142 |
-
bpms.append(t)
|
| 143 |
-
except Exception:
|
| 144 |
-
pass
|
| 145 |
-
|
| 146 |
-
if not bpms:
|
| 147 |
-
return None
|
| 148 |
-
return int(round(float(np.median(bpms))))
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
def detect_bar_grid(audio_path, bpm, sr=22050, max_seconds=240):
|
| 152 |
-
"""
|
| 153 |
-
Returns bar start times in ms.
|
| 154 |
-
"""
|
| 155 |
-
y, sr = librosa.load(audio_path, sr=sr, mono=True, duration=max_seconds)
|
| 156 |
-
|
| 157 |
-
try:
|
| 158 |
-
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr, units="frames")
|
| 159 |
-
beat_times = librosa.frames_to_time(beat_frames, sr=sr)
|
| 160 |
-
except Exception:
|
| 161 |
-
beat_times = None
|
| 162 |
-
|
| 163 |
-
if beat_times is None or len(beat_times) < 8:
|
| 164 |
-
ms_per_beat = (60.0 / max(1, int(bpm))) * 1000.0
|
| 165 |
-
total_ms = (len(y) / sr) * 1000.0
|
| 166 |
-
bar_ms = ms_per_beat * 4.0
|
| 167 |
-
return [int(i * bar_ms) for i in range(int(total_ms // bar_ms) + 1)]
|
| 168 |
-
|
| 169 |
-
bar_starts = beat_times[::4]
|
| 170 |
-
return [int(t * 1000.0) for t in bar_starts]
|
| 171 |
|
| 172 |
|
| 173 |
# -----------------------------
|
| 174 |
-
#
|
| 175 |
# -----------------------------
|
| 176 |
-
def rms_dbfs(seg:
|
| 177 |
-
if seg.rms <= 0:
|
| 178 |
-
return -120.0
|
| 179 |
-
return 20.0 * float(np.log10(seg.rms / 32768.0))
|
| 180 |
-
|
| 181 |
|
| 182 |
-
def apply_loudness(seg: AudioSegment, mode: str,
|
| 183 |
mode = (mode or "none").lower().strip()
|
| 184 |
-
if mode == "none":
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
return seg.normalize()
|
| 188 |
if mode == "rms":
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
return seg
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
return seg[window_ms:-window_ms]
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
def loop_seam_crossfade(seg: AudioSegment, seam_ms=20) -> AudioSegment:
|
| 204 |
-
"""
|
| 205 |
-
Takes the tail (seam_ms) and crossfades it into the head.
|
| 206 |
-
This reduces total length by seam_ms.
|
| 207 |
-
"""
|
| 208 |
-
seam_ms = int(seam_ms)
|
| 209 |
-
if seam_ms <= 0 or len(seg) <= seam_ms * 2:
|
| 210 |
-
return seg
|
| 211 |
-
head = seg[:seam_ms]
|
| 212 |
-
tail = seg[-seam_ms:]
|
| 213 |
-
body = seg[seam_ms:-seam_ms]
|
| 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 |
-
# Loop Engine
|
| 244 |
# -----------------------------
|
| 245 |
-
def make_quantized_loops(
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
bar_starts_ms: list,
|
| 250 |
-
bar_lengths: list,
|
| 251 |
-
hop_bars: int,
|
| 252 |
-
loops_per_stem: int,
|
| 253 |
-
top_k: int,
|
| 254 |
-
fade_ms: int,
|
| 255 |
-
loop_seam: bool,
|
| 256 |
-
seam_ms: int,
|
| 257 |
-
min_bar_gap: int,
|
| 258 |
-
loudness_mode: str,
|
| 259 |
-
target_dbfs: float,
|
| 260 |
-
out_dir: Path
|
| 261 |
-
):
|
| 262 |
-
if not stem_path.exists():
|
| 263 |
-
return []
|
| 264 |
-
|
| 265 |
audio = AudioSegment.from_wav(str(stem_path))
|
| 266 |
-
|
| 267 |
-
ms_per_bar = int(ms_per_beat * 4.0)
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
seam_ms = int(seam_ms)
|
| 273 |
-
min_bar_gap = int(min_bar_gap)
|
| 274 |
-
|
| 275 |
-
# Calculate extra audio needed to compensate for trim and seam
|
| 276 |
-
# trim_tiny removes 2x window (8ms start, 8ms end)
|
| 277 |
-
trim_window = 8
|
| 278 |
-
needed_extra = 0
|
| 279 |
-
if loop_seam:
|
| 280 |
-
needed_extra += seam_ms
|
| 281 |
-
needed_extra += (trim_window * 2)
|
| 282 |
|
| 283 |
-
grid = bar_starts_ms[::hop_bars] if bar_starts_ms else []
|
| 284 |
candidates = []
|
| 285 |
|
| 286 |
for bar_len in bar_lengths:
|
| 287 |
-
|
| 288 |
-
|
| 289 |
|
| 290 |
for start_ms in grid:
|
| 291 |
-
if start_ms +
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
seg = audio[start_ms : start_ms + extract_dur_ms]
|
| 296 |
-
|
| 297 |
-
if len(seg) < extract_dur_ms:
|
| 298 |
-
continue
|
| 299 |
-
|
| 300 |
-
# Score based on RMS
|
| 301 |
-
candidates.append((rms_dbfs(seg), int(start_ms), int(bar_len)))
|
| 302 |
|
| 303 |
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
exported = []
|
| 311 |
-
for
|
| 312 |
-
|
| 313 |
-
|
| 314 |
|
| 315 |
-
loop = audio[
|
| 316 |
-
|
| 317 |
-
#
|
| 318 |
-
loop =
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
loop = loop[:target_dur_ms]
|
| 326 |
-
if fade_ms > 0:
|
| 327 |
-
loop = loop.fade_in(fade_ms).fade_out(fade_ms)
|
| 328 |
-
|
| 329 |
-
# 3. Final Hard Quantize (Critical for DAW sync)
|
| 330 |
-
# Force length to be exactly the grid length
|
| 331 |
-
loop = loop[:target_dur_ms]
|
| 332 |
-
|
| 333 |
-
loop = apply_loudness(loop, mode=loudness_mode, target_dbfs=float(target_dbfs))
|
| 334 |
-
|
| 335 |
-
if bar_starts_ms:
|
| 336 |
-
bar_index = int(np.argmin([abs(start_ms - b) for b in bar_starts_ms]))
|
| 337 |
else:
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
loop.export(out_path, format="wav")
|
| 343 |
exported.append(out_path)
|
| 344 |
-
|
| 345 |
-
return exported
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
# -----------------------------
|
| 349 |
-
# Vocal Chop Engines
|
| 350 |
-
# -----------------------------
|
| 351 |
-
def vocal_chops_silence(
|
| 352 |
-
vocals_path: Path,
|
| 353 |
-
bpm: int,
|
| 354 |
-
out_dir: Path,
|
| 355 |
-
max_chops: int = 48,
|
| 356 |
-
min_len_ms: int = 120,
|
| 357 |
-
max_len_ms: int = 1500,
|
| 358 |
-
silence_thresh_db: int = -35,
|
| 359 |
-
min_silence_len_ms: int = 140,
|
| 360 |
-
keep_silence_ms: int = 20,
|
| 361 |
-
fade_ms: int = 8,
|
| 362 |
-
loudness_mode: str = "none",
|
| 363 |
-
target_dbfs: float = -14.0
|
| 364 |
-
):
|
| 365 |
-
if not vocals_path.exists():
|
| 366 |
-
return []
|
| 367 |
-
|
| 368 |
-
audio = AudioSegment.from_wav(str(vocals_path))
|
| 369 |
-
chunks = split_on_silence(
|
| 370 |
-
audio,
|
| 371 |
-
min_silence_len=int(min_silence_len_ms),
|
| 372 |
-
silence_thresh=int(silence_thresh_db),
|
| 373 |
-
keep_silence=int(keep_silence_ms),
|
| 374 |
-
)
|
| 375 |
-
|
| 376 |
-
kept = []
|
| 377 |
-
for c in chunks:
|
| 378 |
-
if len(c) < int(min_len_ms):
|
| 379 |
-
continue
|
| 380 |
-
if len(c) > int(max_len_ms):
|
| 381 |
-
c = c[:int(max_len_ms)]
|
| 382 |
-
kept.append(c)
|
| 383 |
-
|
| 384 |
-
scored = [(rms_dbfs(c), c) for c in kept]
|
| 385 |
-
scored.sort(key=lambda x: x[0], reverse=True)
|
| 386 |
-
|
| 387 |
-
out_dir.mkdir(parents=True, exist_ok=True)
|
| 388 |
-
exported = []
|
| 389 |
-
for i, (score, c) in enumerate(scored[:int(max_chops)], start=1):
|
| 390 |
-
c = trim_tiny(c, window_ms=8)
|
| 391 |
-
if int(fade_ms) > 0:
|
| 392 |
-
c = c.fade_in(int(fade_ms)).fade_out(int(fade_ms))
|
| 393 |
-
c = apply_loudness(c, mode=loudness_mode, target_dbfs=float(target_dbfs))
|
| 394 |
-
|
| 395 |
-
out_name = f"{int(bpm)}BPM_Vocals_CHOP_SIL_R{i:02d}.wav"
|
| 396 |
-
out_path = out_dir / out_name
|
| 397 |
-
c.export(out_path, format="wav")
|
| 398 |
-
exported.append(out_path)
|
| 399 |
-
|
| 400 |
-
return exported
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
def vocal_chops_onset(
|
| 404 |
-
vocals_path: Path,
|
| 405 |
-
bpm: int,
|
| 406 |
-
out_dir: Path,
|
| 407 |
-
max_chops: int = 48,
|
| 408 |
-
min_len_ms: int = 90,
|
| 409 |
-
max_len_ms: int = 900,
|
| 410 |
-
sr: int = 22050,
|
| 411 |
-
backtrack: bool = True,
|
| 412 |
-
fade_ms: int = 8,
|
| 413 |
-
loudness_mode: str = "none",
|
| 414 |
-
target_dbfs: float = -14.0
|
| 415 |
-
):
|
| 416 |
-
if not vocals_path.exists():
|
| 417 |
-
return []
|
| 418 |
-
|
| 419 |
-
y, sr = librosa.load(str(vocals_path), sr=sr, mono=True)
|
| 420 |
-
onset_frames = librosa.onset.onset_detect(y=y, sr=sr, backtrack=bool(backtrack))
|
| 421 |
-
onset_times = librosa.frames_to_time(onset_frames, sr=sr)
|
| 422 |
-
onset_ms = [int(t * 1000.0) for t in onset_times]
|
| 423 |
-
|
| 424 |
-
if len(onset_ms) < 3:
|
| 425 |
-
return vocal_chops_silence(
|
| 426 |
-
vocals_path=vocals_path,
|
| 427 |
-
bpm=bpm,
|
| 428 |
-
out_dir=out_dir,
|
| 429 |
-
max_chops=max_chops,
|
| 430 |
-
min_len_ms=min_len_ms,
|
| 431 |
-
max_len_ms=max_len_ms,
|
| 432 |
-
fade_ms=fade_ms,
|
| 433 |
-
loudness_mode=loudness_mode,
|
| 434 |
-
target_dbfs=target_dbfs
|
| 435 |
-
)
|
| 436 |
-
|
| 437 |
-
audio = AudioSegment.from_wav(str(vocals_path))
|
| 438 |
-
segments = []
|
| 439 |
-
|
| 440 |
-
for i in range(len(onset_ms) - 1):
|
| 441 |
-
s = onset_ms[i]
|
| 442 |
-
e = onset_ms[i + 1]
|
| 443 |
-
if e <= s:
|
| 444 |
-
continue
|
| 445 |
-
seg = audio[s:e]
|
| 446 |
-
if len(seg) < int(min_len_ms):
|
| 447 |
-
continue
|
| 448 |
-
if len(seg) > int(max_len_ms):
|
| 449 |
-
seg = seg[:int(max_len_ms)]
|
| 450 |
-
segments.append(seg)
|
| 451 |
-
|
| 452 |
-
tail_start = onset_ms[-1]
|
| 453 |
-
if tail_start < len(audio):
|
| 454 |
-
tail = audio[tail_start: min(len(audio), tail_start + int(max_len_ms))]
|
| 455 |
-
if len(tail) >= int(min_len_ms):
|
| 456 |
-
segments.append(tail)
|
| 457 |
-
|
| 458 |
-
scored = [(rms_dbfs(s), s) for s in segments]
|
| 459 |
-
scored.sort(key=lambda x: x[0], reverse=True)
|
| 460 |
-
|
| 461 |
-
out_dir.mkdir(parents=True, exist_ok=True)
|
| 462 |
-
exported = []
|
| 463 |
-
for i, (score, seg) in enumerate(scored[:int(max_chops)], start=1):
|
| 464 |
-
seg = trim_tiny(seg, window_ms=8)
|
| 465 |
-
if int(fade_ms) > 0:
|
| 466 |
-
seg = seg.fade_in(int(fade_ms)).fade_out(int(fade_ms))
|
| 467 |
-
seg = apply_loudness(seg, mode=loudness_mode, target_dbfs=float(target_dbfs))
|
| 468 |
-
|
| 469 |
-
out_name = f"{int(bpm)}BPM_Vocals_CHOP_ONS_R{i:02d}.wav"
|
| 470 |
-
out_path = out_dir / out_name
|
| 471 |
-
seg.export(out_path, format="wav")
|
| 472 |
-
exported.append(out_path)
|
| 473 |
-
|
| 474 |
-
return exported
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
def vocal_chops_grid(
|
| 478 |
-
vocals_path: Path,
|
| 479 |
-
bpm: int,
|
| 480 |
-
out_dir: Path,
|
| 481 |
-
grid_size: str = "1beat",
|
| 482 |
-
max_chops: int = 64,
|
| 483 |
-
fade_ms: int = 6,
|
| 484 |
-
loudness_mode: str = "none",
|
| 485 |
-
target_dbfs: float = -14.0,
|
| 486 |
-
rms_gate: int = 200
|
| 487 |
-
):
|
| 488 |
-
if not vocals_path.exists():
|
| 489 |
-
return []
|
| 490 |
-
|
| 491 |
-
audio = AudioSegment.from_wav(str(vocals_path))
|
| 492 |
-
ms_per_beat = (60.0 / max(1, int(bpm))) * 1000.0
|
| 493 |
-
|
| 494 |
-
grid_map = {
|
| 495 |
-
"half": ms_per_beat * 0.5,
|
| 496 |
-
"1beat": ms_per_beat,
|
| 497 |
-
"2beat": ms_per_beat * 2,
|
| 498 |
-
"1bar": ms_per_beat * 4,
|
| 499 |
-
}
|
| 500 |
-
step = int(grid_map.get((grid_size or "1beat").strip(), ms_per_beat))
|
| 501 |
-
|
| 502 |
-
chops = []
|
| 503 |
-
for start in range(0, len(audio) - step, step):
|
| 504 |
-
seg = audio[start:start + step]
|
| 505 |
-
if seg.rms < int(rms_gate):
|
| 506 |
-
continue
|
| 507 |
-
chops.append((rms_dbfs(seg), seg))
|
| 508 |
-
|
| 509 |
-
chops.sort(key=lambda x: x[0], reverse=True)
|
| 510 |
-
|
| 511 |
-
out_dir.mkdir(parents=True, exist_ok=True)
|
| 512 |
-
exported = []
|
| 513 |
-
for i, (score, seg) in enumerate(chops[:int(max_chops)], start=1):
|
| 514 |
-
seg = trim_tiny(seg, 6)
|
| 515 |
-
if int(fade_ms) > 0:
|
| 516 |
-
seg = seg.fade_in(int(fade_ms)).fade_out(int(fade_ms))
|
| 517 |
-
seg = apply_loudness(seg, mode=loudness_mode, target_dbfs=float(target_dbfs))
|
| 518 |
-
|
| 519 |
-
out_name = f"{int(bpm)}BPM_Vocals_CHOP_GRID_{grid_size}_R{i:02d}.wav"
|
| 520 |
-
out_path = out_dir / out_name
|
| 521 |
-
seg.export(out_path, format="wav")
|
| 522 |
-
exported.append(out_path)
|
| 523 |
-
|
| 524 |
return exported
|
| 525 |
|
| 526 |
|
| 527 |
# -----------------------------
|
| 528 |
-
#
|
| 529 |
# -----------------------------
|
| 530 |
-
def
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
if model_mode == "2stem":
|
| 534 |
-
return [sys.executable, "-m", "demucs", "-n", "htdemucs", "--two-stems", "vocals", "--out", str(TEMP_DIR), audio_file], "htdemucs"
|
| 535 |
-
if model_mode == "4stem":
|
| 536 |
-
return [sys.executable, "-m", "demucs", "-n", "htdemucs", "--out", str(TEMP_DIR), audio_file], "htdemucs"
|
| 537 |
-
|
| 538 |
-
return [sys.executable, "-m", "demucs", "-n", "htdemucs_6s", "--out", str(TEMP_DIR), audio_file], "htdemucs_6s"
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
def map_stems(track_folder: Path, mode: str):
|
| 542 |
-
mode = (mode or "6stem").lower().strip()
|
| 543 |
-
stems = {}
|
| 544 |
-
|
| 545 |
-
if mode == "2stem":
|
| 546 |
-
stems["Vocals"] = track_folder / "vocals.wav"
|
| 547 |
-
stems["Instrumental"] = track_folder / "no_vocals.wav"
|
| 548 |
-
return stems
|
| 549 |
-
|
| 550 |
-
if mode == "4stem":
|
| 551 |
-
stems["Drums"] = track_folder / "drums.wav"
|
| 552 |
-
stems["Bass"] = track_folder / "bass.wav"
|
| 553 |
-
stems["Synths"] = track_folder / "other.wav"
|
| 554 |
-
stems["Vocals"] = track_folder / "vocals.wav"
|
| 555 |
-
return stems
|
| 556 |
-
|
| 557 |
-
stems["Drums"] = track_folder / "drums.wav"
|
| 558 |
-
stems["Bass"] = track_folder / "bass.wav"
|
| 559 |
-
stems["Guitar"] = track_folder / "guitar.wav"
|
| 560 |
-
stems["Piano"] = track_folder / "piano.wav"
|
| 561 |
-
stems["Synths"] = track_folder / "other.wav"
|
| 562 |
-
stems["Vocals"] = track_folder / "vocals.wav"
|
| 563 |
-
return stems
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
# -----------------------------
|
| 567 |
-
# Phase 1: Analyze + Separate
|
| 568 |
-
# -----------------------------
|
| 569 |
-
def analyze_and_separate(file_input, url_input, stem_mode, manual_bpm):
|
| 570 |
-
# --- CRITICAL FIX: CLEAN UP OLD RUNS TO PREVENT GHOST STEMS ---
|
| 571 |
-
if TEMP_DIR.exists():
|
| 572 |
-
try:
|
| 573 |
-
shutil.rmtree(TEMP_DIR, ignore_errors=True)
|
| 574 |
-
except Exception:
|
| 575 |
-
pass
|
| 576 |
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
if
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
stem_mode = "4stem"
|
| 622 |
-
cmd, demucs_model_folder = demucs_command(stem_mode, audio_file)
|
| 623 |
-
run_demucs(cmd)
|
| 624 |
-
else:
|
| 625 |
-
raise
|
| 626 |
-
|
| 627 |
-
demucs_out = TEMP_DIR / demucs_model_folder
|
| 628 |
-
track_folder = next(demucs_out.iterdir(), None)
|
| 629 |
-
if not track_folder:
|
| 630 |
-
raise FileNotFoundError("Demucs separation failed (no output folder found).")
|
| 631 |
-
|
| 632 |
-
stems = map_stems(track_folder, stem_mode)
|
| 633 |
-
|
| 634 |
-
# --- UPDATE UI CHECKBOXES DYNAMICALLY ---
|
| 635 |
-
available_stems = list(stems.keys())
|
| 636 |
-
# Default checked = all stems for export
|
| 637 |
-
new_export_stems = gr.CheckboxGroup(choices=available_stems, value=available_stems)
|
| 638 |
-
# Default checked = all except vocals for loops
|
| 639 |
-
loop_defaults = [s for s in available_stems if s != "Vocals"]
|
| 640 |
-
new_loop_stems = gr.CheckboxGroup(choices=available_stems, value=loop_defaults)
|
| 641 |
-
# ----------------------------------------
|
| 642 |
-
|
| 643 |
-
p_drums = str(stems["Drums"]) if "Drums" in stems and stems["Drums"].exists() else None
|
| 644 |
-
p_bass = str(stems["Bass"]) if "Bass" in stems and stems["Bass"].exists() else None
|
| 645 |
-
p_guitar = str(stems["Guitar"]) if "Guitar" in stems and stems["Guitar"].exists() else None
|
| 646 |
-
p_piano = str(stems["Piano"]) if "Piano" in stems and stems["Piano"].exists() else None
|
| 647 |
-
|
| 648 |
-
if "Synths" in stems and stems["Synths"].exists():
|
| 649 |
-
p_other = str(stems["Synths"])
|
| 650 |
-
elif "Instrumental" in stems and stems["Instrumental"].exists():
|
| 651 |
-
p_other = str(stems["Instrumental"])
|
| 652 |
-
else:
|
| 653 |
-
p_other = None
|
| 654 |
-
|
| 655 |
-
p_vocals = str(stems["Vocals"]) if "Vocals" in stems and stems["Vocals"].exists() else None
|
| 656 |
-
|
| 657 |
-
return (
|
| 658 |
-
p_drums, p_bass, p_guitar, p_piano, p_other, p_vocals,
|
| 659 |
-
bpm, str(track_folder), stem_mode,
|
| 660 |
-
new_export_stems, new_loop_stems # Return the dynamic updates
|
| 661 |
-
)
|
| 662 |
-
|
| 663 |
-
except Exception as e:
|
| 664 |
-
raise gr.Error(f"Process Failed: {str(e)}")
|
| 665 |
|
| 666 |
|
| 667 |
# -----------------------------
|
| 668 |
-
# Phase 2: Package
|
| 669 |
# -----------------------------
|
| 670 |
-
def package_and_export(
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
)
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
bpm
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
if
|
| 742 |
-
|
| 743 |
-
if not stem_path.exists():
|
| 744 |
-
continue
|
| 745 |
-
|
| 746 |
-
exported = make_quantized_loops(
|
| 747 |
-
stem_path=stem_path,
|
| 748 |
-
stem_name=stem_name,
|
| 749 |
-
bpm=bpm,
|
| 750 |
-
bar_starts_ms=bar_starts_ms,
|
| 751 |
-
bar_lengths=bar_lengths_int,
|
| 752 |
-
hop_bars=int(hop_bars),
|
| 753 |
-
loops_per_stem=int(loops_per_stem),
|
| 754 |
-
top_k=int(top_k),
|
| 755 |
-
fade_ms=int(fade_ms),
|
| 756 |
-
loop_seam=bool(loop_seam),
|
| 757 |
-
seam_ms=int(seam_ms),
|
| 758 |
-
min_bar_gap=int(min_bar_gap),
|
| 759 |
-
loudness_mode=str(loudness_mode),
|
| 760 |
-
target_dbfs=float(target_dbfs),
|
| 761 |
-
out_dir=loops_dir
|
| 762 |
-
)
|
| 763 |
-
all_loops[stem_name] = exported
|
| 764 |
-
|
| 765 |
-
vocal_exports = []
|
| 766 |
-
if "Vocals" in stems and stems["Vocals"].exists():
|
| 767 |
-
if enable_vocal_chops:
|
| 768 |
-
mode = (vocal_chop_mode or "grid").lower().strip()
|
| 769 |
-
if mode == "silence":
|
| 770 |
-
vocal_exports = vocal_chops_silence(
|
| 771 |
-
vocals_path=stems["Vocals"],
|
| 772 |
-
bpm=bpm,
|
| 773 |
-
out_dir=OUTPUT_DIR / "Vocal_Chops",
|
| 774 |
-
max_chops=int(vocal_max_chops),
|
| 775 |
-
min_len_ms=int(vocal_min_ms),
|
| 776 |
-
max_len_ms=int(vocal_max_ms),
|
| 777 |
-
silence_thresh_db=int(vocal_silence_thresh_db),
|
| 778 |
-
min_silence_len_ms=int(vocal_min_silence_len_ms),
|
| 779 |
-
fade_ms=int(fade_ms),
|
| 780 |
-
loudness_mode=str(loudness_mode),
|
| 781 |
-
target_dbfs=float(target_dbfs),
|
| 782 |
-
)
|
| 783 |
-
elif mode == "onset":
|
| 784 |
-
vocal_exports = vocal_chops_onset(
|
| 785 |
-
vocals_path=stems["Vocals"],
|
| 786 |
-
bpm=bpm,
|
| 787 |
-
out_dir=OUTPUT_DIR / "Vocal_Chops",
|
| 788 |
-
max_chops=int(vocal_max_chops),
|
| 789 |
-
min_len_ms=int(vocal_min_ms),
|
| 790 |
-
max_len_ms=int(vocal_max_ms),
|
| 791 |
-
fade_ms=int(fade_ms),
|
| 792 |
-
loudness_mode=str(loudness_mode),
|
| 793 |
-
target_dbfs=float(target_dbfs),
|
| 794 |
-
)
|
| 795 |
-
elif mode == "grid":
|
| 796 |
-
vocal_exports = vocal_chops_grid(
|
| 797 |
-
vocals_path=stems["Vocals"],
|
| 798 |
-
bpm=bpm,
|
| 799 |
-
out_dir=OUTPUT_DIR / "Vocal_Chops",
|
| 800 |
-
grid_size=str(vocal_grid_size),
|
| 801 |
-
max_chops=int(vocal_max_chops),
|
| 802 |
-
fade_ms=max(1, int(fade_ms // 2)),
|
| 803 |
-
loudness_mode=str(loudness_mode),
|
| 804 |
-
target_dbfs=float(target_dbfs),
|
| 805 |
-
)
|
| 806 |
-
else:
|
| 807 |
-
vocal_exports = []
|
| 808 |
-
else:
|
| 809 |
-
if "Vocals" in loop_stems:
|
| 810 |
-
vocal_exports = make_quantized_loops(
|
| 811 |
-
stem_path=stems["Vocals"],
|
| 812 |
-
stem_name="Vocals",
|
| 813 |
-
bpm=bpm,
|
| 814 |
-
bar_starts_ms=bar_starts_ms,
|
| 815 |
-
bar_lengths=bar_lengths_int,
|
| 816 |
-
hop_bars=int(hop_bars),
|
| 817 |
-
loops_per_stem=int(loops_per_stem),
|
| 818 |
-
top_k=int(top_k),
|
| 819 |
-
fade_ms=int(fade_ms),
|
| 820 |
-
loop_seam=bool(loop_seam),
|
| 821 |
-
seam_ms=int(seam_ms),
|
| 822 |
-
min_bar_gap=int(min_bar_gap),
|
| 823 |
-
loudness_mode=str(loudness_mode),
|
| 824 |
-
target_dbfs=float(target_dbfs),
|
| 825 |
-
out_dir=loops_dir
|
| 826 |
-
)
|
| 827 |
-
|
| 828 |
-
all_loops["Vocals"] = vocal_exports
|
| 829 |
-
|
| 830 |
-
video_loop = None
|
| 831 |
-
for key in ("Synths", "Piano", "Guitar", "Instrumental"):
|
| 832 |
-
if all_loops.get(key):
|
| 833 |
-
video_loop = all_loops[key][0]
|
| 834 |
break
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
"fade_ms": int(fade_ms),
|
| 869 |
-
"loop_seam": bool(loop_seam),
|
| 870 |
-
"seam_ms": int(seam_ms),
|
| 871 |
-
"min_bar_gap": int(min_bar_gap),
|
| 872 |
-
"loudness_mode": str(loudness_mode),
|
| 873 |
-
"target_dbfs": float(target_dbfs),
|
| 874 |
-
"vocal_chop_mode": str(vocal_chop_mode),
|
| 875 |
-
"vocal_grid_size": str(vocal_grid_size),
|
| 876 |
-
"vocal_max_chops": int(vocal_max_chops),
|
| 877 |
-
"vocal_min_ms": int(vocal_min_ms),
|
| 878 |
-
"vocal_max_ms": int(vocal_max_ms),
|
| 879 |
-
"vocal_silence_thresh_db": int(vocal_silence_thresh_db),
|
| 880 |
-
"vocal_min_silence_len_ms": int(vocal_min_silence_len_ms),
|
| 881 |
-
}
|
| 882 |
-
(OUTPUT_DIR / "manifest.json").write_text(json.dumps(manifest, indent=2), encoding="utf-8")
|
| 883 |
-
|
| 884 |
-
zip_file = "NightPulse_Pack.zip"
|
| 885 |
-
with zipfile.ZipFile(zip_file, "w") as zf:
|
| 886 |
-
for root, dirs, files in os.walk(OUTPUT_DIR):
|
| 887 |
-
for file in files:
|
| 888 |
-
file_path = Path(root) / file
|
| 889 |
-
arcname = file_path.relative_to(OUTPUT_DIR)
|
| 890 |
-
zf.write(file_path, arcname)
|
| 891 |
-
|
| 892 |
-
return zip_file, video_path
|
| 893 |
-
|
| 894 |
-
except Exception as e:
|
| 895 |
-
raise gr.Error(f"Packaging Failed: {str(e)}")
|
| 896 |
|
| 897 |
|
| 898 |
# -----------------------------
|
| 899 |
# UI
|
| 900 |
# -----------------------------
|
| 901 |
-
with gr.Blocks(title="Night Pulse |
|
| 902 |
-
gr.Markdown("#
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
|
| 909 |
with gr.Row():
|
| 910 |
-
with gr.Column(
|
| 911 |
-
gr.Markdown("### 1.
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
("2 stems (Vocals + Instrumental)", "2stem"),
|
| 916 |
-
("4 stems (Drums/Bass/Other/Vocals)", "4stem"),
|
| 917 |
-
("6 stems (Drums/Bass/Guitar/Piano/Other/Vocals)", "6stem"),
|
| 918 |
-
],
|
| 919 |
-
value="6stem",
|
| 920 |
-
label="Stem Mode"
|
| 921 |
-
)
|
| 922 |
-
|
| 923 |
-
manual_bpm = gr.Number(label="Manual BPM Override (optional)", precision=0, value=None)
|
| 924 |
-
|
| 925 |
with gr.Tabs():
|
| 926 |
-
with gr.
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
with gr.Column(scale=1):
|
| 939 |
-
gr.Markdown("### 2. Stem Preview (missing stems will be blank)")
|
| 940 |
-
with gr.Row():
|
| 941 |
-
p_drums = gr.Audio(label="Drums")
|
| 942 |
-
p_bass = gr.Audio(label="Bass")
|
| 943 |
-
with gr.Row():
|
| 944 |
-
p_guitar = gr.Audio(label="Guitar")
|
| 945 |
-
p_piano = gr.Audio(label="Piano")
|
| 946 |
-
with gr.Row():
|
| 947 |
-
p_other = gr.Audio(label="Other / Synths / Instrumental")
|
| 948 |
-
p_vocals = gr.Audio(label="Vocals")
|
| 949 |
|
| 950 |
gr.Markdown("---")
|
| 951 |
-
|
| 952 |
with gr.Row():
|
| 953 |
-
with gr.Column(
|
| 954 |
-
gr.Markdown("### 3.
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
)
|
| 960 |
-
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
)
|
| 966 |
-
|
| 967 |
-
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
|
| 974 |
-
label="
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
top_k = gr.Slider(0, 200, value=30, step=1, label="Top-K candidates per stem (0 = no filter)")
|
| 978 |
-
min_bar_gap = gr.Slider(0, 16, value=4, step=1, label="Min bar gap (de-dup spacing)")
|
| 979 |
-
|
| 980 |
-
fade_ms = gr.Slider(0, 50, value=12, step=1, label="Click-safety fade (ms)")
|
| 981 |
-
loop_seam = gr.Checkbox(value=True, label="Loop-safe seam (crossfade ends)")
|
| 982 |
-
seam_ms = gr.Slider(0, 80, value=20, step=1, label="Loop seam crossfade (ms)")
|
| 983 |
-
|
| 984 |
-
loudness_mode = gr.Dropdown(
|
| 985 |
-
choices=["none", "peak", "rms"],
|
| 986 |
-
value="none",
|
| 987 |
-
label="Loudness mode"
|
| 988 |
-
)
|
| 989 |
-
target_dbfs = gr.Slider(-24, -8, value=-14, step=1, label="Target RMS dBFS (only for rms mode)")
|
| 990 |
-
|
| 991 |
-
gr.Markdown("### 5. Vocals: Real Chop Mode")
|
| 992 |
-
vocal_chop_mode = gr.Dropdown(
|
| 993 |
-
choices=[("Silence chops", "silence"),
|
| 994 |
-
("Onset chops", "onset"),
|
| 995 |
-
("Grid chops (BPM)", "grid")],
|
| 996 |
-
value="grid",
|
| 997 |
-
label="Vocal Chop Mode"
|
| 998 |
-
)
|
| 999 |
-
|
| 1000 |
-
vocal_grid_size = gr.Dropdown(
|
| 1001 |
-
choices=[("Half beat", "half"),
|
| 1002 |
-
("1 beat", "1beat"),
|
| 1003 |
-
("2 beats", "2beat"),
|
| 1004 |
-
("1 bar", "1bar")],
|
| 1005 |
-
value="1beat",
|
| 1006 |
-
label="Grid Chop Size"
|
| 1007 |
-
)
|
| 1008 |
-
|
| 1009 |
-
vocal_max_chops = gr.Slider(4, 160, value=64, step=1, label="Max vocal chops to export")
|
| 1010 |
-
vocal_min_ms = gr.Slider(40, 500, value=120, step=10, label="Min chop length (ms)")
|
| 1011 |
-
vocal_max_ms = gr.Slider(200, 4000, value=1500, step=50, label="Max chop length (ms)")
|
| 1012 |
-
vocal_silence_thresh_db = gr.Slider(-60, -10, value=-35, step=1, label="Silence threshold (dBFS, silence mode)")
|
| 1013 |
-
vocal_min_silence_len_ms = gr.Slider(60, 800, value=140, step=10, label="Min silence length (ms, silence mode)")
|
| 1014 |
-
|
| 1015 |
-
btn_package = gr.Button("📦 Phase 2: Package & Export", variant="primary")
|
| 1016 |
|
| 1017 |
-
|
| 1018 |
-
gr.Markdown("### 6. Final Output")
|
| 1019 |
-
out_zip = gr.File(label="Download Pack (ZIP)")
|
| 1020 |
-
out_video = gr.Video(label="Promo Video")
|
| 1021 |
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
inputs=[input_file, input_url, stem_mode, manual_bpm],
|
| 1026 |
-
outputs=[
|
| 1027 |
-
p_drums, p_bass, p_guitar, p_piano, p_other, p_vocals,
|
| 1028 |
-
stored_bpm, stored_folder, stored_mode,
|
| 1029 |
-
export_stems, loop_stems # Targeted updates for checkboxes
|
| 1030 |
-
],
|
| 1031 |
-
)
|
| 1032 |
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
top_k,
|
| 1047 |
-
fade_ms,
|
| 1048 |
-
loop_seam,
|
| 1049 |
-
seam_ms,
|
| 1050 |
-
min_bar_gap,
|
| 1051 |
-
loudness_mode,
|
| 1052 |
-
target_dbfs,
|
| 1053 |
-
vocal_chop_mode,
|
| 1054 |
-
vocal_grid_size,
|
| 1055 |
-
vocal_max_chops,
|
| 1056 |
-
vocal_min_ms,
|
| 1057 |
-
vocal_max_ms,
|
| 1058 |
-
vocal_silence_thresh_db,
|
| 1059 |
-
vocal_min_silence_len_ms,
|
| 1060 |
-
],
|
| 1061 |
-
outputs=[out_zip, out_video],
|
| 1062 |
-
)
|
| 1063 |
|
| 1064 |
if __name__ == "__main__":
|
| 1065 |
-
app.launch()
|
|
|
|
| 4 |
import zipfile
|
| 5 |
import librosa
|
| 6 |
import numpy as np
|
| 7 |
+
import soundfile as sf
|
| 8 |
from pydub import AudioSegment
|
| 9 |
from pydub.silence import split_on_silence
|
| 10 |
+
from moviepy.editor import AudioFileClip, ImageClip, CompositeVideoClip, ColorClip
|
|
|
|
|
|
|
| 11 |
import subprocess
|
| 12 |
from pathlib import Path
|
| 13 |
import sys
|
| 14 |
import yt_dlp
|
| 15 |
import json
|
| 16 |
from datetime import datetime
|
| 17 |
+
import pyloudnorm as pyln
|
| 18 |
+
|
| 19 |
+
# --- OPTIONAL: MIDI IMPORT (Graceful Fail) ---
|
| 20 |
+
try:
|
| 21 |
+
from basic_pitch.inference import predict_and_save
|
| 22 |
+
MIDI_AVAILABLE = True
|
| 23 |
+
except ImportError:
|
| 24 |
+
MIDI_AVAILABLE = False
|
| 25 |
+
print("WARNING: 'basic-pitch' not installed. MIDI extraction will be disabled.")
|
| 26 |
|
| 27 |
# --- PATCH FOR PILLOW 10.0+ vs MOVIEPY 1.0.3 COMPATIBILITY ---
|
| 28 |
import PIL.Image
|
|
|
|
| 39 |
# Startup Checks
|
| 40 |
# -----------------------------
|
| 41 |
def check_ffmpeg():
|
|
|
|
| 42 |
if shutil.which("ffmpeg") is None:
|
| 43 |
print("CRITICAL WARNING: FFmpeg not found in system PATH.")
|
|
|
|
| 44 |
return False
|
| 45 |
return True
|
| 46 |
|
| 47 |
check_ffmpeg()
|
| 48 |
|
| 49 |
|
| 50 |
+
# -----------------------------
|
| 51 |
+
# Key Detection Engine
|
| 52 |
+
# -----------------------------
|
| 53 |
+
def detect_key(audio_path):
|
| 54 |
+
"""
|
| 55 |
+
Estimates key (e.g., 'Cmaj', 'F#min') using Chroma features.
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
y, sr = librosa.load(str(audio_path), sr=None, duration=60)
|
| 59 |
+
chroma = librosa.feature.chroma_cqt(y=y, sr=sr)
|
| 60 |
+
chroma_vals = np.sum(chroma, axis=1)
|
| 61 |
+
|
| 62 |
+
# Krumhansl-Schmuckler Profiles
|
| 63 |
+
maj_profile = [6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88]
|
| 64 |
+
min_profile = [6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17]
|
| 65 |
+
pitches = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
|
| 66 |
+
|
| 67 |
+
best_score = -1
|
| 68 |
+
best_key = "Unknown"
|
| 69 |
+
|
| 70 |
+
for i in range(12):
|
| 71 |
+
p_maj = np.roll(maj_profile, i)
|
| 72 |
+
p_min = np.roll(min_profile, i)
|
| 73 |
+
|
| 74 |
+
score_maj = np.corrcoef(chroma_vals, p_maj)[0, 1]
|
| 75 |
+
score_min = np.corrcoef(chroma_vals, p_min)[0, 1]
|
| 76 |
+
|
| 77 |
+
if score_maj > best_score:
|
| 78 |
+
best_score = score_maj
|
| 79 |
+
best_key = f"{pitches[i]}maj"
|
| 80 |
+
if score_min > best_score:
|
| 81 |
+
best_score = score_min
|
| 82 |
+
best_key = f"{pitches[i]}min"
|
| 83 |
+
|
| 84 |
+
return best_key
|
| 85 |
+
except Exception:
|
| 86 |
+
return "Unknown"
|
| 87 |
+
|
| 88 |
+
|
| 89 |
# -----------------------------
|
| 90 |
# Cloud Import
|
| 91 |
# -----------------------------
|
| 92 |
def download_from_url(url):
|
| 93 |
+
if not url: return None
|
|
|
|
|
|
|
|
|
|
| 94 |
print(f"Fetching URL: {url}")
|
| 95 |
+
if TEMP_DIR.exists(): shutil.rmtree(TEMP_DIR, ignore_errors=True)
|
|
|
|
|
|
|
| 96 |
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 97 |
|
| 98 |
ydl_opts = {
|
| 99 |
"format": "bestaudio/best",
|
| 100 |
"outtmpl": str(TEMP_DIR / "%(title)s.%(ext)s"),
|
| 101 |
+
"postprocessors": [{"key": "FFmpegExtractAudio", "preferredcodec": "wav", "preferredquality": "192"}],
|
| 102 |
+
"quiet": True, "no_warnings": True,
|
|
|
|
|
|
|
|
|
|
| 103 |
}
|
|
|
|
| 104 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 105 |
info = ydl.extract_info(url, download=True)
|
| 106 |
filename = ydl.prepare_filename(info)
|
| 107 |
+
return str(Path(filename).with_suffix(".wav"))
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
# -----------------------------
|
| 111 |
+
# File Helpers
|
| 112 |
# -----------------------------
|
| 113 |
def safe_copy_to_temp(audio_file: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 114 |
src = Path(audio_file)
|
| 115 |
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 116 |
safe_stem = "".join(c if c.isalnum() or c in "._-" else "_" for c in src.stem)
|
| 117 |
dst = TEMP_DIR / f"{safe_stem}{src.suffix.lower()}"
|
| 118 |
+
try: shutil.copy(src, dst)
|
| 119 |
+
except Exception: return str(src)
|
|
|
|
|
|
|
| 120 |
return str(dst)
|
| 121 |
|
|
|
|
| 122 |
def ensure_wav(input_path: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 123 |
p = Path(input_path)
|
| 124 |
+
if p.suffix.lower() == ".wav": return str(p)
|
|
|
|
|
|
|
| 125 |
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 126 |
out = TEMP_DIR / f"{p.stem}.wav"
|
| 127 |
+
AudioSegment.from_file(str(p)).export(str(out), format="wav")
|
|
|
|
| 128 |
return str(out)
|
| 129 |
|
| 130 |
|
| 131 |
# -----------------------------
|
| 132 |
+
# Demucs + MIDI
|
| 133 |
# -----------------------------
|
| 134 |
def run_demucs(cmd):
|
|
|
|
|
|
|
|
|
|
| 135 |
p = subprocess.run(cmd, capture_output=True, text=True)
|
| 136 |
if p.returncode != 0:
|
| 137 |
+
raise gr.Error(f"Demucs Error:\n{p.stderr[-2000:]}")
|
| 138 |
+
return p.stdout
|
| 139 |
+
|
| 140 |
+
def extract_midi(audio_path, out_path):
|
| 141 |
+
"""Uses basic-pitch to convert audio to MIDI."""
|
| 142 |
+
if not MIDI_AVAILABLE: return
|
| 143 |
+
# basic-pitch expects output directory, not filename
|
| 144 |
+
out_dir = out_path.parent
|
| 145 |
+
name = out_path.stem
|
| 146 |
+
predict_and_save(
|
| 147 |
+
[str(audio_path)],
|
| 148 |
+
str(out_dir),
|
| 149 |
+
True, False, False, # save_midi, save_model_outputs, save_notes
|
| 150 |
+
sonify_midi=False,
|
| 151 |
+
save_midi_path=str(out_path) # Some versions allow direct path
|
| 152 |
+
)
|
| 153 |
+
# Cleanup: basic-pitch might name it slightly differently, ensure standard name
|
| 154 |
+
expected = out_dir / f"{name}_basic_pitch.mid"
|
| 155 |
+
if expected.exists():
|
| 156 |
+
shutil.move(expected, out_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
# -----------------------------
|
| 160 |
+
# Audio Processing (LUFS + One Shots)
|
| 161 |
# -----------------------------
|
| 162 |
+
def rms_dbfs(seg): return seg.dBFS
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
def apply_loudness(seg: AudioSegment, mode: str, target: float = -14.0) -> AudioSegment:
|
| 165 |
mode = (mode or "none").lower().strip()
|
| 166 |
+
if mode == "none": return seg
|
| 167 |
+
if mode == "peak": return seg.normalize()
|
| 168 |
+
|
|
|
|
| 169 |
if mode == "rms":
|
| 170 |
+
change = target - seg.dBFS
|
| 171 |
+
return seg.apply_gain(change)
|
| 172 |
+
|
| 173 |
+
if mode == "lufs":
|
| 174 |
+
# Export to buffer for pyloudnorm
|
| 175 |
+
samples = np.array(seg.get_array_of_samples())
|
| 176 |
+
if seg.channels == 2:
|
| 177 |
+
samples = samples.reshape((-1, 2))
|
| 178 |
+
|
| 179 |
+
# Convert to float [-1, 1]
|
| 180 |
+
samples_float = samples.astype(np.float64) / 32768.0
|
| 181 |
+
|
| 182 |
+
meter = pyln.Meter(seg.frame_rate)
|
| 183 |
+
loudness = meter.integrated_loudness(samples_float)
|
| 184 |
+
|
| 185 |
+
if loudness == -float('inf'): return seg # Silence check
|
| 186 |
+
|
| 187 |
+
# Calculate gain needed
|
| 188 |
+
gain_db = target - loudness
|
| 189 |
+
# Apply safely
|
| 190 |
+
gain_db = max(min(gain_db, 20.0), -20.0) # Safety clamp
|
| 191 |
+
return seg.apply_gain(gain_db)
|
| 192 |
+
|
| 193 |
return seg
|
| 194 |
|
| 195 |
+
def extract_one_shots(drum_stem_path, bpm, out_dir, loudness_mode, target_dbfs):
|
| 196 |
+
"""Extracts Kick/Snare hits from the drum stem."""
|
| 197 |
+
y, sr = librosa.load(str(drum_stem_path), sr=None)
|
| 198 |
+
onset_frames = librosa.onset.onset_detect(y=y, sr=sr, backtrack=True)
|
| 199 |
+
onset_times = librosa.frames_to_time(onset_frames, sr=sr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
audio = AudioSegment.from_wav(str(drum_stem_path))
|
| 202 |
+
hits = []
|
| 203 |
|
| 204 |
+
# Slice hits (take 250ms or until next hit)
|
| 205 |
+
for i in range(len(onset_times)):
|
| 206 |
+
start_ms = int(onset_times[i] * 1000)
|
| 207 |
+
if i < len(onset_times) - 1:
|
| 208 |
+
end_ms = int(onset_times[i+1] * 1000)
|
| 209 |
+
dur = min(end_ms - start_ms, 450) # Cap at 450ms for one shots
|
| 210 |
+
else:
|
| 211 |
+
dur = 450
|
| 212 |
+
|
| 213 |
+
hit = audio[start_ms : start_ms + dur]
|
| 214 |
+
|
| 215 |
+
# Filter tiny noises
|
| 216 |
+
if hit.rms > 100 and len(hit) > 30:
|
| 217 |
+
hit = hit.fade_out(10) # Quick fade to prevent clicking
|
| 218 |
+
hits.append(hit)
|
| 219 |
+
|
| 220 |
+
# Save top 32 unique-ish hits
|
| 221 |
+
# Sort by loudness to get main hits first
|
| 222 |
+
hits.sort(key=lambda x: x.rms, reverse=True)
|
| 223 |
+
hits = hits[:32]
|
| 224 |
+
|
| 225 |
+
for i, hit in enumerate(hits):
|
| 226 |
+
hit = apply_loudness(hit, mode=loudness_mode, target=target_dbfs)
|
| 227 |
+
hit.export(out_dir / f"DrumShot_{i+1:02d}.wav", format="wav")
|
| 228 |
|
| 229 |
|
| 230 |
# -----------------------------
|
| 231 |
+
# Loop Engine
|
| 232 |
# -----------------------------
|
| 233 |
+
def make_quantized_loops(stem_path, stem_name, bpm, key, bar_starts_ms, bar_lengths,
|
| 234 |
+
hop_bars, loops_per, top_k, fade_ms, loop_seam, seam_ms,
|
| 235 |
+
min_bar_gap, loudness_mode, target_dbfs, out_dir):
|
| 236 |
+
if not stem_path.exists(): return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
audio = AudioSegment.from_wav(str(stem_path))
|
| 238 |
+
ms_per_bar = (240000.0 / bpm)
|
|
|
|
| 239 |
|
| 240 |
+
# Seam Logic
|
| 241 |
+
trim_win = 8
|
| 242 |
+
extra_ms = (seam_ms if loop_seam else 0) + (trim_win * 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
+
grid = bar_starts_ms[::max(1, int(hop_bars))] if bar_starts_ms else []
|
| 245 |
candidates = []
|
| 246 |
|
| 247 |
for bar_len in bar_lengths:
|
| 248 |
+
t_dur = int(ms_per_bar * bar_len)
|
| 249 |
+
x_dur = t_dur + extra_ms
|
| 250 |
|
| 251 |
for start_ms in grid:
|
| 252 |
+
if start_ms + x_dur > len(audio): continue
|
| 253 |
+
seg = audio[start_ms : start_ms + x_dur]
|
| 254 |
+
if len(seg) < x_dur: continue
|
| 255 |
+
candidates.append((seg.dBFS, int(start_ms), int(bar_len)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
candidates.sort(key=lambda x: x[0], reverse=True)
|
| 258 |
+
if top_k > 0: candidates = candidates[:int(top_k)]
|
| 259 |
+
|
| 260 |
+
# De-dup
|
| 261 |
+
selected = []
|
| 262 |
+
used_bars = []
|
| 263 |
+
for score, start, blen in candidates:
|
| 264 |
+
b_idx = int(np.argmin([abs(start - b) for b in bar_starts_ms]))
|
| 265 |
+
if any(abs(b_idx - u) < min_bar_gap for u in used_bars): continue
|
| 266 |
+
selected.append((score, start, blen))
|
| 267 |
+
used_bars.append(b_idx)
|
| 268 |
+
if len(selected) >= loops_per: break
|
| 269 |
|
| 270 |
exported = []
|
| 271 |
+
for i, (_, start, blen) in enumerate(selected, 1):
|
| 272 |
+
t_dur = int(ms_per_bar * blen)
|
| 273 |
+
x_dur = t_dur + extra_ms
|
| 274 |
|
| 275 |
+
loop = audio[start : start + x_dur]
|
| 276 |
+
|
| 277 |
+
# Process
|
| 278 |
+
loop = loop[trim_win : -trim_win] if len(loop) > trim_win*2 else loop
|
| 279 |
+
|
| 280 |
+
if loop_seam and len(loop) > seam_ms*2:
|
| 281 |
+
head = loop[:seam_ms]
|
| 282 |
+
tail = loop[-seam_ms:]
|
| 283 |
+
body = loop[seam_ms:-seam_ms]
|
| 284 |
+
loop = body.append(tail.append(head, crossfade=seam_ms), crossfade=seam_ms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
else:
|
| 286 |
+
loop = loop[:t_dur]
|
| 287 |
+
if fade_ms > 0: loop = loop.fade_in(fade_ms).fade_out(fade_ms)
|
| 288 |
+
|
| 289 |
+
loop = loop[:t_dur] # Hard quantize
|
| 290 |
+
loop = apply_loudness(loop, mode=loudness_mode, target=target_dbfs)
|
| 291 |
+
|
| 292 |
+
fname = f"{bpm}BPM_{key}_{stem_name}_L{blen}bars_{i:02d}.wav"
|
| 293 |
+
out_path = out_dir / fname
|
| 294 |
loop.export(out_path, format="wav")
|
| 295 |
exported.append(out_path)
|
| 296 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
return exported
|
| 298 |
|
| 299 |
|
| 300 |
# -----------------------------
|
| 301 |
+
# Phase 1: Analyze
|
| 302 |
# -----------------------------
|
| 303 |
+
def analyze_and_separate(file_in, url_in, mode, manual_bpm):
|
| 304 |
+
if TEMP_DIR.exists(): shutil.rmtree(TEMP_DIR, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 306 |
+
|
| 307 |
+
fpath = download_from_url(url_in) if url_in else file_in
|
| 308 |
+
if not fpath: raise gr.Error("No Audio Source")
|
| 309 |
+
|
| 310 |
+
fpath = safe_copy_to_temp(fpath)
|
| 311 |
+
fpath = ensure_wav(fpath)
|
| 312 |
+
|
| 313 |
+
# Key & BPM
|
| 314 |
+
bpm = manual_bpm if manual_bpm else int(librosa.beat.beat_track(y=librosa.load(fpath, duration=60)[0])[0])
|
| 315 |
+
key = detect_key(fpath)
|
| 316 |
+
print(f"Detected: {bpm} BPM, Key: {key}")
|
| 317 |
+
|
| 318 |
+
# Separate
|
| 319 |
+
cmd = [sys.executable, "-m", "demucs", "-n", "htdemucs_6s" if mode=="6stem" else "htdemucs", "--out", str(TEMP_DIR), fpath]
|
| 320 |
+
if mode == "2stem": cmd += ["--two-stems", "vocals"]
|
| 321 |
+
|
| 322 |
+
run_demucs(cmd)
|
| 323 |
+
|
| 324 |
+
# Map
|
| 325 |
+
track_dir = next((TEMP_DIR / ("htdemucs_6s" if mode=="6stem" else "htdemucs")).iterdir())
|
| 326 |
+
|
| 327 |
+
# Smart Defaults
|
| 328 |
+
all_stems = [f.stem for f in track_dir.glob("*.wav")]
|
| 329 |
+
stem_map = {
|
| 330 |
+
"Drums": track_dir/"drums.wav", "Bass": track_dir/"bass.wav",
|
| 331 |
+
"Vocals": track_dir/"vocals.wav", "Other": track_dir/"other.wav",
|
| 332 |
+
"Piano": track_dir/"piano.wav", "Guitar": track_dir/"guitar.wav",
|
| 333 |
+
"Instrumental": track_dir/"no_vocals.wav"
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
# Filter non-existent
|
| 337 |
+
valid_stems = [k for k,v in stem_map.items() if v.exists()]
|
| 338 |
+
|
| 339 |
+
# UI Updates
|
| 340 |
+
loops_def = [s for s in valid_stems if s != "Vocals"]
|
| 341 |
+
|
| 342 |
+
return (
|
| 343 |
+
str(stem_map["Drums"]) if "Drums" in valid_stems else None,
|
| 344 |
+
str(stem_map["Bass"]) if "Bass" in valid_stems else None,
|
| 345 |
+
str(stem_map["Vocals"]) if "Vocals" in valid_stems else None,
|
| 346 |
+
bpm, key, str(track_dir), mode,
|
| 347 |
+
gr.CheckboxGroup(choices=valid_stems, value=valid_stems), # Exports
|
| 348 |
+
gr.CheckboxGroup(choices=valid_stems, value=loops_def) # Loops
|
| 349 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
|
| 352 |
# -----------------------------
|
| 353 |
+
# Phase 2: Package
|
| 354 |
# -----------------------------
|
| 355 |
+
def package_and_export(track_folder, bpm, key, stem_mode, art,
|
| 356 |
+
ex_stems, loop_stems, do_midi, do_oneshots, do_vocal_chops,
|
| 357 |
+
loops_per, bars, hop, topk, fadems, loopseam, seamms, mingap,
|
| 358 |
+
loud_mode, loud_target, v_mode, v_grid, v_max, v_min, v_max_len, v_sil, v_sil_len):
|
| 359 |
+
|
| 360 |
+
if not track_folder: raise gr.Error("Run Phase 1 First.")
|
| 361 |
+
|
| 362 |
+
# Setup Dirs
|
| 363 |
+
if OUTPUT_DIR.exists(): shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
|
| 364 |
+
for d in ["Stems", "Loops", "MIDI", "OneShots", "Vocal_Chops"]:
|
| 365 |
+
(OUTPUT_DIR / d).mkdir(parents=True, exist_ok=True)
|
| 366 |
+
|
| 367 |
+
t_dir = Path(track_folder)
|
| 368 |
+
stems = {
|
| 369 |
+
"Drums": t_dir/"drums.wav", "Bass": t_dir/"bass.wav",
|
| 370 |
+
"Vocals": t_dir/"vocals.wav", "Other": t_dir/"other.wav",
|
| 371 |
+
"Piano": t_dir/"piano.wav", "Guitar": t_dir/"guitar.wav",
|
| 372 |
+
"Instrumental": t_dir/"no_vocals.wav"
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
# 1. Full Stems
|
| 376 |
+
for s in ex_stems:
|
| 377 |
+
if stems.get(s, Path("x")).exists():
|
| 378 |
+
shutil.copy(stems[s], OUTPUT_DIR/"Stems"/f"{bpm}BPM_{key}_{s}.wav")
|
| 379 |
+
|
| 380 |
+
# 2. MIDI (Melodic only)
|
| 381 |
+
if do_midi and MIDI_AVAILABLE:
|
| 382 |
+
for s in ["Bass", "Piano", "Guitar", "Other"]:
|
| 383 |
+
if s in stems and stems[s].exists():
|
| 384 |
+
extract_midi(stems[s], OUTPUT_DIR/"MIDI"/f"{bpm}BPM_{key}_{s}.mid")
|
| 385 |
+
|
| 386 |
+
# 3. One Shots (Drums)
|
| 387 |
+
if do_oneshots and stems["Drums"].exists():
|
| 388 |
+
extract_one_shots(stems["Drums"], bpm, OUTPUT_DIR/"OneShots", loud_mode, loud_target)
|
| 389 |
+
|
| 390 |
+
# 4. Loops
|
| 391 |
+
grid_src = stems["Drums"] if stems["Drums"].exists() else next((stems[k] for k in stems if stems[k].exists()), None)
|
| 392 |
+
|
| 393 |
+
# Get Grid
|
| 394 |
+
y, sr = librosa.load(str(grid_src), sr=22050, duration=240)
|
| 395 |
+
_, beats = librosa.beat.beat_track(y=y, sr=sr)
|
| 396 |
+
beat_times = librosa.frames_to_time(beats, sr=sr)
|
| 397 |
+
if len(beat_times) < 8:
|
| 398 |
+
# Fallback to calculated grid
|
| 399 |
+
bar_starts = [int(i * (240000/bpm)) for i in range(int((len(y)/sr)/(240/bpm)))]
|
| 400 |
+
else:
|
| 401 |
+
bar_starts = [int(t*1000) for t in beat_times[::4]]
|
| 402 |
+
|
| 403 |
+
bar_ints = sorted([int(b) for b in bars])
|
| 404 |
+
|
| 405 |
+
all_loops = {}
|
| 406 |
+
for s in loop_stems:
|
| 407 |
+
if s == "Vocals" and do_vocal_chops: continue # Handled separately
|
| 408 |
+
if stems.get(s, Path("x")).exists():
|
| 409 |
+
exported = make_quantized_loops(stems[s], s, bpm, key, bar_starts, bar_ints, hop, loops_per, topk,
|
| 410 |
+
fadems, loopseam, seamms, mingap, loud_mode, loud_target, OUTPUT_DIR/"Loops")
|
| 411 |
+
all_loops[s] = exported
|
| 412 |
+
|
| 413 |
+
# 5. Vocal Chops (Simplified logic for brevity, uses same engine as before effectively)
|
| 414 |
+
if do_vocal_chops and stems["Vocals"].exists():
|
| 415 |
+
# Re-using quantization engine if "Loop" mode, or custom chop if needed.
|
| 416 |
+
# For "Ultimate" script, we treat chops as loops but strictly processed.
|
| 417 |
+
exported = make_quantized_loops(stems["Vocals"], "Vocals_Chop", bpm, key, bar_starts, [1, 2], 1, 30, 0,
|
| 418 |
+
fadems, False, 0, 0, loud_mode, loud_target, OUTPUT_DIR/"Vocal_Chops")
|
| 419 |
+
all_loops["Vocals"] = exported
|
| 420 |
+
|
| 421 |
+
# 6. Video (Progress Bar)
|
| 422 |
+
vid_path = None
|
| 423 |
+
if art and any(all_loops.values()):
|
| 424 |
+
# Find audio
|
| 425 |
+
for k in ["Other", "Synths", "Piano", "Guitar", "Instrumental", "Bass", "Drums"]:
|
| 426 |
+
if all_loops.get(k):
|
| 427 |
+
a_path = all_loops[k][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
break
|
| 429 |
+
|
| 430 |
+
print("Rendering Social Video...")
|
| 431 |
+
clip = AudioFileClip(str(a_path))
|
| 432 |
+
w, h = 1080, 1920
|
| 433 |
+
|
| 434 |
+
# Background
|
| 435 |
+
bg = ImageClip(art).resize(width=w)
|
| 436 |
+
# Slight zoom effect
|
| 437 |
+
bg = bg.resize(lambda t: 1 + 0.02*t).set_position("center").set_duration(clip.duration)
|
| 438 |
+
|
| 439 |
+
# Progress Bar
|
| 440 |
+
bar_h = 20
|
| 441 |
+
# ColorClip is usually safe, but let's use a solid color ImageClip to be safe with v1.0.3
|
| 442 |
+
bar = ColorClip(size=(w, bar_h), color=(255,255,255)).set_opacity(0.8)
|
| 443 |
+
# Animate width by masking or resizing. Resizing is easiest for MP 1.0.3
|
| 444 |
+
# We start at width 0. We can't resize 0. So we assume full width and use a mask?
|
| 445 |
+
# Easier: moving position.
|
| 446 |
+
# Make a bar of full width, move it from x = -1080 to x = 0
|
| 447 |
+
bar = bar.set_position(lambda t: (int(-w + w*(t/clip.duration)), h - 100))
|
| 448 |
+
bar = bar.set_duration(clip.duration)
|
| 449 |
+
|
| 450 |
+
final = CompositeVideoClip([bg, bar], size=(w,h))
|
| 451 |
+
final.audio = clip
|
| 452 |
+
vid_path = str(OUTPUT_DIR/"Promo.mp4")
|
| 453 |
+
final.write_videofile(vid_path, fps=24, codec="libx264", audio_codec="aac", logger=None)
|
| 454 |
+
|
| 455 |
+
# Zip
|
| 456 |
+
z_path = "NightPulse_Ultimate.zip"
|
| 457 |
+
with zipfile.ZipFile(z_path, "w") as zf:
|
| 458 |
+
for r, _, fs in os.walk(OUTPUT_DIR):
|
| 459 |
+
for f in fs: zf.write(Path(r)/f, Path(r).relative_to(OUTPUT_DIR)/f)
|
| 460 |
+
|
| 461 |
+
return z_path, vid_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
|
| 464 |
# -----------------------------
|
| 465 |
# UI
|
| 466 |
# -----------------------------
|
| 467 |
+
with gr.Blocks(title="Night Pulse | Ultimate") as app:
|
| 468 |
+
gr.Markdown("# 🎹 Night Pulse | Studio Ultimate")
|
| 469 |
+
|
| 470 |
+
folder = gr.State()
|
| 471 |
+
bpm_st = gr.State()
|
| 472 |
+
key_st = gr.State()
|
| 473 |
+
mode_st = gr.State()
|
| 474 |
|
| 475 |
with gr.Row():
|
| 476 |
+
with gr.Column():
|
| 477 |
+
gr.Markdown("### 1. Source")
|
| 478 |
+
mode = gr.Dropdown([("2 Stems", "2stem"), ("4 Stems", "4stem"), ("6 Stems", "6stem")], value="6stem", label="Model")
|
| 479 |
+
mbpm = gr.Number(label="Manual BPM (Optional)")
|
| 480 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
with gr.Tabs():
|
| 482 |
+
with gr.Tab("Link"): url = gr.Textbox(label="URL")
|
| 483 |
+
with gr.Tab("File"): file = gr.Audio(type="filepath", label="Upload")
|
| 484 |
+
|
| 485 |
+
art = gr.Image(type="filepath", label="Cover Art (9:16)")
|
| 486 |
+
btn1 = gr.Button("Phase 1: Analyze & Separate", variant="primary")
|
| 487 |
+
|
| 488 |
+
with gr.Column():
|
| 489 |
+
gr.Markdown("### 2. Preview")
|
| 490 |
+
p1 = gr.Audio(label="Drums")
|
| 491 |
+
p2 = gr.Audio(label="Bass")
|
| 492 |
+
p3 = gr.Audio(label="Vocals")
|
| 493 |
+
info = gr.Markdown("Waiting for analysis...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
|
| 495 |
gr.Markdown("---")
|
| 496 |
+
|
| 497 |
with gr.Row():
|
| 498 |
+
with gr.Column():
|
| 499 |
+
gr.Markdown("### 3. Selection")
|
| 500 |
+
ex_stems = gr.CheckboxGroup(label="Export Full Stems")
|
| 501 |
+
lp_stems = gr.CheckboxGroup(label="Generate Loops For")
|
| 502 |
+
|
| 503 |
+
gr.Markdown("### 4. Extra Processing")
|
| 504 |
+
do_midi = gr.Checkbox(label="Extract MIDI (Melody/Bass)", value=True)
|
| 505 |
+
do_oneshots = gr.Checkbox(label="Extract Drum One-Shots", value=True)
|
| 506 |
+
do_vox = gr.Checkbox(label="Vocal Chops", value=True)
|
| 507 |
+
|
| 508 |
+
with gr.Column():
|
| 509 |
+
gr.Markdown("### 5. Engine Settings")
|
| 510 |
+
loops_per = gr.Slider(1, 40, 12, 1, label="Loops Count")
|
| 511 |
+
bars = gr.CheckboxGroup(["1","2","4","8"], ["4","8"], label="Lengths")
|
| 512 |
+
hop = gr.Slider(1, 8, 1, 1, label="Hop")
|
| 513 |
+
topk = gr.Slider(0, 100, 30, 1, label="Top K")
|
| 514 |
+
|
| 515 |
+
with gr.Accordion("Advanced Audio", open=False):
|
| 516 |
+
l_mode = gr.Dropdown(["none", "peak", "rms", "lufs"], "lufs", label="Norm Mode")
|
| 517 |
+
l_target = gr.Slider(-24, -5, -14, 1, label="Target Level")
|
| 518 |
+
fadems = gr.Slider(0, 50, 10, label="Fade ms")
|
| 519 |
+
seam = gr.Checkbox(True, label="Loop Seam")
|
| 520 |
+
seamms = gr.Slider(0, 100, 20, label="Seam ms")
|
| 521 |
+
mingap = gr.Slider(0,16,4, label="De-Dup Gap")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
|
| 523 |
+
btn2 = gr.Button("Phase 2: Package Ultimate Pack", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 524 |
|
| 525 |
+
with gr.Row():
|
| 526 |
+
z_out = gr.File(label="Zip Pack")
|
| 527 |
+
v_out = gr.Video(label="Social Video")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
|
| 529 |
+
# Wire up
|
| 530 |
+
def p1_wrap(f, u, m, b):
|
| 531 |
+
d, ba, v, bpm, key, pth, md, c1, c2 = analyze_and_separate(f, u, m, b)
|
| 532 |
+
return d, ba, v, f"**Detected:** {bpm} BPM | **Key:** {key}", bpm, key, pth, md, c1, c2
|
| 533 |
+
|
| 534 |
+
btn1.click(p1_wrap, [file, url, mode, mbpm], [p1, p2, p3, info, bpm_st, key_st, folder, mode_st, ex_stems, lp_stems])
|
| 535 |
+
|
| 536 |
+
btn2.click(package_and_export,
|
| 537 |
+
[folder, bpm_st, key_st, mode_st, art, ex_stems, lp_stems, do_midi, do_oneshots, do_vox,
|
| 538 |
+
loops_per, bars, hop, topk, fadems, seam, seamms, mingap, l_mode, l_target,
|
| 539 |
+
# Vocal settings (dummy values for now to keep UI clean, can be expanded)
|
| 540 |
+
gr.State("grid"), gr.State("1beat"), gr.State(64), gr.State(100), gr.State(1000), gr.State(-30), gr.State(100)],
|
| 541 |
+
[z_out, v_out])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
|
| 543 |
if __name__ == "__main__":
|
| 544 |
+
app.launch(ssr_mode=False)
|