Spaces:
Running
Running
File size: 9,080 Bytes
a0cee5d b7f1453 0f24c2f cad5bf4 0f24c2f a0cee5d b7f1453 a0cee5d b7f1453 a0cee5d 56f2ba5 4369b06 56f2ba5 a0cee5d 3b1b3cb a0cee5d 27df8ab 4369b06 27df8ab 3b1b3cb 27df8ab 56f2ba5 3b1b3cb 56f2ba5 4369b06 56f2ba5 0f24c2f 27df8ab 4369b06 27df8ab 4369b06 27df8ab 0f24c2f 4369b06 b7f1453 0f24c2f 4369b06 27df8ab 0f24c2f 27df8ab 0f24c2f 27df8ab b7f1453 0f24c2f 27df8ab 0f24c2f 4369b06 27df8ab 0f24c2f 2bfa333 0f24c2f 4369b06 0f24c2f 4369b06 b7f1453 63f26c4 b7f1453 4369b06 0f24c2f 4369b06 0f24c2f 27df8ab 0f24c2f 2bfa333 4369b06 0f24c2f 4369b06 0f24c2f 27df8ab 4369b06 27df8ab 0f24c2f 4369b06 0f24c2f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | import asyncio
import os
import shutil
import sys
from pathlib import Path
from typing import Dict
from audio_analysis import analyze_audio_file
from models import JobState, JobStatus
def _get_device() -> str:
"""Return the best available device for Demucs inference."""
device = os.environ.get("DEMUCS_DEVICE")
if device:
return device
try:
import torch
if torch.cuda.is_available():
return "cuda"
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return "mps"
except ImportError:
pass
return "cpu"
_jobs: Dict[str, JobStatus] = {}
_queue: asyncio.Queue | None = None
STEM_NAMES = ("vocals", "drums", "bass", "other")
def init_queue() -> None:
"""Call this from inside a running event loop (FastAPI startup)."""
global _queue
_queue = asyncio.Queue()
def get_job_status(job_id: str) -> JobStatus | None:
return _jobs.get(job_id)
def create_job(job_id: str, original_filename: str | None = None) -> JobStatus:
status = JobStatus(
job_id=job_id,
state=JobState.pending,
progress=0,
original_filename=original_filename,
)
_jobs[job_id] = status
return status
async def enqueue_job(
job_id: str,
input_path: str,
output_dir: str,
trim_start_sec: float | None = None,
trim_end_sec: float | None = None,
original_filename: str | None = None,
) -> None:
if _queue is None:
raise RuntimeError("Queue not initialized. Call init_queue() in startup.")
create_job(job_id, original_filename=original_filename)
try:
_queue.put_nowait((job_id, input_path, output_dir, trim_start_sec, trim_end_sec))
except asyncio.QueueFull:
_jobs.pop(job_id, None)
raise RuntimeError("Processing queue is full. Please try again later.")
async def _run_subprocess_exec(*args: str) -> tuple[int, bytes, bytes]:
proc = await asyncio.create_subprocess_exec(
*args,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
return proc.returncode, stdout, stderr
async def _trim_audio(
input_file: Path,
job_id: str,
trim_start_sec: float | None,
trim_end_sec: float | None,
) -> Path:
if trim_start_sec is None and trim_end_sec is None:
return input_file
trim_start = trim_start_sec or 0.0
trimmed_file = input_file.parent / f"{job_id}_trimmed.mp3"
ffmpeg_cmd = [
"ffmpeg",
"-y",
"-hide_banner",
"-loglevel",
"error",
"-ss",
str(trim_start),
"-i",
str(input_file),
]
if trim_end_sec is not None:
ffmpeg_cmd.extend(["-to", str(trim_end_sec)])
ffmpeg_cmd.extend(["-c", "copy", str(trimmed_file)])
code, _, stderr = await _run_subprocess_exec(*ffmpeg_cmd)
if code != 0:
fallback_cmd = [
"ffmpeg",
"-y",
"-hide_banner",
"-loglevel",
"error",
"-ss",
str(trim_start),
"-i",
str(input_file),
]
if trim_end_sec is not None:
fallback_cmd.extend(["-to", str(trim_end_sec)])
fallback_cmd.extend(["-codec:a", "libmp3lame", "-b:a", "320k", str(trimmed_file)])
fallback_code, _, fallback_stderr = await _run_subprocess_exec(*fallback_cmd)
if fallback_code != 0:
msg = fallback_stderr.decode(errors="replace")[:1200]
if not msg:
msg = stderr.decode(errors="replace")[:1200]
raise RuntimeError(f"Audio trim failed: {msg}")
return trimmed_file
async def _mix_instrumental(output_dir: Path) -> Path:
drums = output_dir / "drums.mp3"
bass = output_dir / "bass.mp3"
other = output_dir / "other.mp3"
instrumental = output_dir / "instrumental.mp3"
cmd = [
"ffmpeg",
"-y",
"-hide_banner",
"-loglevel",
"error",
"-i",
str(drums),
"-i",
str(bass),
"-i",
str(other),
"-filter_complex",
"amix=inputs=3:duration=longest:normalize=0",
"-codec:a",
"libmp3lame",
"-b:a",
"320k",
str(instrumental),
]
code, _, stderr = await _run_subprocess_exec(*cmd)
if code != 0:
msg = stderr.decode(errors="replace")[:1200]
raise RuntimeError(f"Failed to create instrumental stem: {msg}")
return instrumental
def _move_demucs_stems(demucs_work_dir: Path, output_dir: Path) -> dict[str, str]:
stems: dict[str, str] = {}
output_dir.mkdir(parents=True, exist_ok=True)
for stem_name in STEM_NAMES:
source_file = next(demucs_work_dir.rglob(f"{stem_name}.mp3"), None)
if source_file is None:
continue
target_file = output_dir / f"{stem_name}.mp3"
source_file.replace(target_file)
stems[stem_name] = target_file.name
return stems
async def _run_demucs(
job_id: str,
input_path: str,
output_dir: str,
trim_start_sec: float | None = None,
trim_end_sec: float | None = None,
) -> None:
_jobs[job_id].state = JobState.processing
_jobs[job_id].progress = 10
_jobs[job_id].stems = None
_jobs[job_id].bpm = None
_jobs[job_id].bpm_confidence = None
_jobs[job_id].musical_key = None
_jobs[job_id].key_scale = None
_jobs[job_id].key_confidence = None
input_file = Path(input_path)
output_directory = Path(output_dir)
demucs_work_dir = output_directory.parent / f"demucs_{job_id}"
trimmed_file: Path | None = None
try:
demucs_input = input_file
if trim_start_sec is not None or trim_end_sec is not None:
_jobs[job_id].progress = 20
demucs_input = await _trim_audio(
input_file=input_file,
job_id=job_id,
trim_start_sec=trim_start_sec,
trim_end_sec=trim_end_sec,
)
trimmed_file = demucs_input
proc = await asyncio.create_subprocess_exec(
sys.executable,
"-m",
"demucs",
"--device",
_get_device(),
"--mp3",
"--mp3-bitrate",
"320",
"-o",
str(demucs_work_dir),
str(demucs_input),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
_jobs[job_id].progress = 35
_, stderr = await proc.communicate()
_jobs[job_id].progress = 80
if proc.returncode != 0:
raise RuntimeError(f"Demucs failed: {stderr[:2000].decode(errors='replace')}")
stems = _move_demucs_stems(demucs_work_dir=demucs_work_dir, output_dir=output_directory)
if not stems:
raise RuntimeError("Demucs did not produce any stem MP3 outputs")
if all(k in stems for k in ("drums", "bass", "other")):
instrumental_path = await _mix_instrumental(output_directory)
stems["instrumental"] = instrumental_path.name
_jobs[job_id].progress = 92
original_for_analysis = output_directory / "original.mp3"
try:
if original_for_analysis.exists():
analysis = await asyncio.to_thread(analyze_audio_file, original_for_analysis)
_jobs[job_id].bpm = analysis.get("bpm")
_jobs[job_id].bpm_confidence = analysis.get("bpm_confidence")
_jobs[job_id].musical_key = analysis.get("musical_key")
_jobs[job_id].key_scale = analysis.get("key_scale")
_jobs[job_id].key_confidence = analysis.get("key_confidence")
_jobs[job_id].tala = analysis.get("tala")
_jobs[job_id].time_signature = analysis.get("time_signature")
except Exception:
# Analysis is additive metadata only; stem generation should still succeed.
pass
_jobs[job_id].stems = stems
_jobs[job_id].state = JobState.done
_jobs[job_id].progress = 100
except Exception as e:
_jobs[job_id].state = JobState.error
_jobs[job_id].error = str(e)[:500]
raise
finally:
shutil.rmtree(demucs_work_dir, ignore_errors=True)
if trimmed_file and trimmed_file.exists():
trimmed_file.unlink(missing_ok=True)
async def _worker() -> None:
if _queue is None:
raise RuntimeError("Queue not initialized before worker started")
while True:
job_id, input_path, output_dir, trim_start_sec, trim_end_sec = await _queue.get()
try:
await _run_demucs(
job_id,
input_path,
output_dir,
trim_start_sec=trim_start_sec,
trim_end_sec=trim_end_sec,
)
except Exception:
pass
finally:
_queue.task_done()
async def start_worker() -> None:
asyncio.create_task(_worker())
|