Files
stemengine_old/backend/services_root_backup/separator_service.py
T
2026-03-03 14:52:19 +09:00

1809 lines
61 KiB
Python
Executable File

"""
Stem separation service with project-based folder structure and preset mixing.
- Demucs models → CLI subprocess (python -m demucs)
- BS-RoFormer → Python API (in-process, no subprocess needed)
- Presets → Instrumental, Static, Dynamic mixing via preset_service
"""
import os
import subprocess
import sys
import time
import shutil
from pathlib import Path
from typing import Dict, List, Optional
from .. import job_manager as jm
from ..config import (
PROJECTS_DIR, JOBS_DIR, CPU_SEPARATION, PROJECT_SUBFOLDERS, ALL_MODELS, OUTPUT_FORMATS,
MEL_BAND_MODELS, BS_ROFORMER_SPECIALIZED_MODELS, MDXNET_MODELS,
SCNET_MODELS, MEDLEYVOX_MODELS, ENSEMBLE_MODELS, APOLLO_MODELS, ASPIRATION_MODELS, BANDIT_MODELS,
BS_ROFORMER_SW_MODELS
)
from .metadata_util import copy_metadata, read_metadata
from .project_namer import project_name, make_project_dirs
from .preset_service import make_instrumental, make_static, make_dynamic
from .project_metadata import write_project_metadata
# ---------------------------------------------------------------------------
# Stem file naming helper
# ---------------------------------------------------------------------------
def _stem_filename(prefix: str, stem_name: str, suffix: str) -> str:
"""
Return the destination filename for a separated stem.
Format: "Artist - Title (vocals).mp3"
"""
label = stem_name.lower().replace('_', ' ')
return f"{prefix} ({label}){suffix}"
# Lookup maps for human-readable labels
_MODEL_LABELS = {m['id']: m['label'] for m in ALL_MODELS}
_FORMAT_LABELS = {f['id']: f['label'] for f in OUTPUT_FORMATS}
# Model ID prefix that identifies BS-RoFormer models
BSR_PREFIX = 'bs_roformer_'
# Map from our model ID suffix → BSRoformerSeparator stem_mode string
BSR_STEM_MODE = {
'bs_roformer_4stem': '4stem',
'bs_roformer_5stem_guitar': '5stem_guitar',
'bs_roformer_5stem_piano': '5stem_piano',
'bs_roformer_6stem': '6stem',
}
# Model ID prefix that identifies MelBand RoFormer models
MBR_PREFIX = 'mel_band_roformer_'
MBR_VOCALS_PREFIX = 'mel_band_vocals'
MBR_DEREVERB_PREFIX = 'mel_band_dereverb'
MBR_KARAOKE_PREFIX = 'mel_band_karaoke'
# MelBand RoFormer model IDs
MBR_MODELS = {m['id'] for m in MEL_BAND_MODELS}
# MelBand specialized model groups
MBR_VOCALS_MODELS = {m['id'] for m in MEL_BAND_MODELS if 'vocals' in m['id']}
MBR_DEREVERB_MODELS = {m['id'] for m in MEL_BAND_MODELS if 'dereverb' in m['id']}
MBR_KARAOKE_MODELS = {m['id'] for m in MEL_BAND_MODELS if 'karaoke' in m['id']}
MBR_DEUX_BECRUILY_MODELS = {m['id'] for m in MEL_BAND_MODELS if 'deux_becruily' in m['id']}
# BS-RoFormer specialized model IDs
BSR_CHORUS_MODELS = {m['id'] for m in BS_ROFORMER_SPECIALIZED_MODELS if 'chorus' in m['id']}
BSR_ANVUEW_MODELS = {m['id'] for m in BS_ROFORMER_SPECIALIZED_MODELS if 'anvuew' in m['id'] and 'karaoke' not in m['id']}
BSR_KARAOKE_ANVUEW_MODELS = {m['id'] for m in BS_ROFORMER_SPECIALIZED_MODELS if 'karaoke' in m['id'] and 'anvuew' in m['id']}
# MDX-Net model IDs
MDXNET_MODEL_IDS = {m['id'] for m in MDXNET_MODELS}
# SCNet XL model IDs
SCNET_MODEL_IDS = {m['id'] for m in SCNET_MODELS}
# BS-RoFormer SW model IDs
BSR_SW_MODEL_IDS = {m['id'] for m in BS_ROFORMER_SW_MODELS}
# MedleyVox model IDs
MEDLEYVOX_MODEL_IDS = {m['id'] for m in MEDLEYVOX_MODELS}
# Apollo model IDs
APOLLO_MODEL_IDS = {m['id'] for m in APOLLO_MODELS}
# Aspiration model IDs
ASPIRATION_MODEL_IDS = {m['id'] for m in ASPIRATION_MODELS}
# Bandit model IDs
BANDIT_MODEL_IDS = {m['id'] for m in BANDIT_MODELS}
# Ensemble model IDs
ENSEMBLE_MODEL_IDS = {m['id'] for m in ENSEMBLE_MODELS}
# ---------------------------------------------------------------------------
# Separation power presets
# ---------------------------------------------------------------------------
# Each preset maps to:
# roformer: batch_size and num_overlap overrides (None = use model config default)
# demucs: --segment and --overlap CLI flags (None = omit flag, use demucs default)
_SEP_POWER_PRESETS: Dict[str, Dict] = {
'eco': {
'roformer': {'batch_size': 1, 'num_overlap': 2},
'demucs': {'segment': 5, 'overlap': 0.1},
},
'standard': {
'roformer': {'batch_size': None, 'num_overlap': None},
'demucs': {'segment': None, 'overlap': None},
},
'performance': {
'roformer': {'batch_size': 8, 'num_overlap': 4},
'demucs': {'segment': 20, 'overlap': 0.5},
},
}
def _get_power_opts(preset: str) -> Dict:
"""Resolve a power preset name to its options dict."""
return _SEP_POWER_PRESETS.get(preset, _SEP_POWER_PRESETS['standard'])
def _ensure_model_available(job_id: str, model_id: str) -> bool:
"""
Download model weights if they are not already present on disk.
Demucs models are skipped — they self-download through the demucs library.
Returns True if the model is ready, False if download failed (job set to ERROR).
"""
# Demucs downloads itself via the CLI; no action needed
demucs_ids = {'htdemucs', 'htdemucs_ft', 'mdx_extra_q', 'mdx'}
if model_id in demucs_ids:
return True
from ..routers.models import MODEL_DOWNLOAD_INFO, get_model_directory, is_model_downloaded
if model_id not in MODEL_DOWNLOAD_INFO:
return True # Not in registry — assume bundled / self-managed
if is_model_downloaded(model_id):
return True
info = MODEL_DOWNLOAD_INFO[model_id]
hf_repo = info.get("hf_repo")
if not hf_repo:
# No public download URL — the individual separator service will attempt its
# own download (snapshot_download) when it runs. Don't fail the job here.
return True
jm.update_job(job_id, progress=f"Downloading model weights for '{model_id}'...")
try:
import shutil as _shutil
import tempfile
from huggingface_hub import hf_hub_download
model_dir = get_model_directory(model_id)
model_dir.mkdir(parents=True, exist_ok=True)
for file_path in info["files"]:
dest = model_dir / Path(file_path).name
if dest.exists():
continue
jm.update_job(job_id, progress=f"Downloading {Path(file_path).name}...")
with tempfile.TemporaryDirectory() as tmp:
tmp_path = hf_hub_download(
repo_id=hf_repo,
filename=file_path,
local_dir=tmp,
local_dir_use_symlinks=False,
)
_shutil.copy2(tmp_path, str(dest))
return True
except Exception as exc:
jm.update_job(
job_id,
status=jm.JobStatus.ERROR,
error=f"Failed to download model '{model_id}': {exc}",
progress="Error: model download failed.",
)
return False
def run_separation(
job_id: str,
input_path: str,
model: str,
output_format: str,
# Preset options
preset_type: str = "stems_only", # stems_only, static, instrumental, dynamic
static_option: Optional[str] = None, # for static preset: no_vocals, no_drums, etc.
dynamic_levels: Optional[Dict[str, float]] = None, # for dynamic preset
# Project naming
project_name_override: Optional[str] = None,
# Power preset
sep_power_preset: str = 'standard',
):
"""
Background task: separate audio into stems and apply presets.
Creates human-readable project folder structure:
/Artist - Title/
Stems/ (always created)
Instrumental/ (if preset includes instrumental)
Static/ (if preset is static)
Dynamic/ (if preset is dynamic)
"""
# Read metadata for project naming
jm.update_job(job_id, status=jm.JobStatus.RUNNING, progress="Reading metadata...")
metadata = read_metadata(input_path)
# Ensure model weights are present — download automatically if missing
if not _ensure_model_available(job_id, model):
return
# Resolve human-readable labels for tab naming
model_label = _MODEL_LABELS.get(model, model)
format_label = _FORMAT_LABELS.get(output_format, output_format)
tab_label = f"{model_label}_{format_label}"
# Determine project name
if project_name_override:
proj_name = project_name_override
else:
filename_stem = Path(input_path).stem
proj_name = project_name(metadata.get('artist', ''), metadata.get('title', ''), filename_stem)
# Determine which folders to create based on preset
preset_flags = {
'instrumental': preset_type in ('instrumental',),
'static': preset_type == 'static',
'dynamic': preset_type == 'dynamic',
}
# Create project directories
jm.update_job(job_id, progress=f"Creating project: {proj_name}...")
dirs = make_project_dirs(proj_name, PROJECTS_DIR, preset_flags)
stems_dir = dirs['stems']
project_dir = dirs['project']
# Copy original source file to project root with full metadata
jm.update_job(job_id, progress="Copying source file to project...")
input_ext = Path(input_path).suffix.lower()
source_dest = project_dir / f"{proj_name}{input_ext}"
try:
shutil.copy2(input_path, source_dest)
# Ensure full metadata including lyrics is copied
copy_metadata(input_path, str(source_dest), stem_label=None, copy_lyrics=True)
except Exception as e:
jm.update_job(job_id, progress=f"Warning: Could not copy source file: {e}")
# Normalise mp3_320 → mp3 with explicit bitrate
mp3_bitrate = '320k' if output_format == 'mp3_320' else '256k'
normalised_format = 'mp3' if output_format == 'mp3_320' else output_format
# Resolve power preset options
_power = _get_power_opts(sep_power_preset)
_roformer_opts = _power['roformer']
_demucs_opts = _power['demucs']
# Free any cached GPU memory from previous jobs before loading a new model.
# This reduces OOM risk when Whisper or a prior separation left allocations behind.
try:
import torch
if torch.cuda.is_available() and not CPU_SEPARATION:
torch.cuda.empty_cache()
except Exception:
pass
# Run separation
try:
if model.startswith(BSR_PREFIX):
stem_files = _run_bsroformer(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model in BSR_CHORUS_MODELS:
stem_files = _run_bsroformer_chorus(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model in BSR_ANVUEW_MODELS:
# Route to MelBand Vocals Anvuew since it's the same architecture
stem_files = _run_mel_band_vocals(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model in BSR_KARAOKE_ANVUEW_MODELS:
# Route to Karaoke Gabox as closest match
stem_files = _run_mel_band_karaoke(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model.startswith(MBR_PREFIX):
stem_files = _run_mel_band_roformer(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model in MBR_VOCALS_MODELS:
stem_files = _run_mel_band_vocals(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model in MBR_DEREVERB_MODELS:
stem_files = _run_mel_band_dereverb(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model in MBR_KARAOKE_MODELS:
stem_files = _run_mel_band_karaoke(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model in MBR_DEUX_BECRUILY_MODELS:
stem_files = _run_mel_band_deux_becruily(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
elif model in MDXNET_MODEL_IDS:
# Route MelBand-based MDX models appropriately
if model == 'mdxnet_dereverb':
stem_files = _run_mel_band_dereverb(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
batch_size_override=_roformer_opts.get('batch_size'),
num_overlap_override=_roformer_opts.get('num_overlap'),
)
else:
stem_files = _run_mdxnet(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata
)
elif model in SCNET_MODEL_IDS:
stem_files = _run_scnet(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
)
elif model in BSR_SW_MODEL_IDS:
stem_files = _run_bsroformer_sw(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
)
elif model in MEDLEYVOX_MODEL_IDS:
stem_files = _run_medleyvox(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
)
elif model in ENSEMBLE_MODEL_IDS:
stem_files = _run_ensemble(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
)
elif model in APOLLO_MODEL_IDS:
stem_files = _run_apollo(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
)
elif model in ASPIRATION_MODEL_IDS:
stem_files = _run_aspiration(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
)
elif model in BANDIT_MODEL_IDS:
stem_files = _run_bandit(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=proj_name,
)
else:
stem_files = _run_demucs(
job_id, input_path, model, output_format, stems_dir,
metadata=metadata,
stem_prefix=proj_name,
segment_override=_demucs_opts.get('segment'),
overlap_override=_demucs_opts.get('overlap'),
)
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
# Apply presets if requested
preset_files = []
if preset_type == 'instrumental' and 'instrumental' in dirs:
jm.update_job(job_id, progress="Creating instrumental mix...")
inst_path = dirs['instrumental'] / f"{proj_name} - Instrumental.{normalised_format}"
try:
result = make_instrumental(stem_files, inst_path, output_format)
copy_metadata(input_path, result, stem_label="Instrumental", copy_lyrics=True)
preset_files.append({
"name": inst_path.name,
"path": result,
"type": "instrumental"
})
except Exception as e:
jm.update_job(job_id, progress=f"Instrumental preset failed: {e}")
elif preset_type == 'static' and 'static' in dirs:
jm.update_job(job_id, progress="Creating static mix...")
muted = _get_muted_stems(static_option)
static_name = _static_name(static_option)
static_path = dirs['static'] / f"{proj_name} - {static_name}.{normalised_format}"
try:
result = make_static(stem_files, muted, static_path, output_format)
copy_metadata(input_path, result, stem_label=static_name, copy_lyrics=True)
preset_files.append({
"name": static_path.name,
"path": result,
"type": "static"
})
except Exception as e:
jm.update_job(job_id, progress=f"Static preset failed: {e}")
elif preset_type == 'dynamic' and 'dynamic' in dirs:
jm.update_job(job_id, progress="Creating dynamic mix...")
volumes = dynamic_levels or {}
dynamic_path = dirs['dynamic'] / f"{proj_name} - Dynamic Mix.{normalised_format}"
try:
result = make_dynamic(stem_files, volumes, dynamic_path, output_format)
copy_metadata(input_path, result, stem_label="Dynamic Mix", copy_lyrics=True)
preset_files.append({
"name": dynamic_path.name,
"path": result,
"type": "dynamic"
})
except Exception as e:
jm.update_job(job_id, progress=f"Dynamic preset failed: {e}")
# Build result file list
all_files = []
# Add source file first if it exists
if source_dest.exists():
all_files.append({
"name": source_dest.name,
"path": str(source_dest),
"type": "source"
})
for stem_name, path in sorted(stem_files.items()):
all_files.append({
"name": Path(path).name,
"path": path,
"type": "stem",
"stem": stem_name
})
all_files.extend(preset_files)
project = jm.find_or_create_project(proj_name, job_id, tab_label=tab_label)
# Write project.json snapshot to the project folder
try:
write_project_metadata(
project_dir,
project_id=project['id'],
name=proj_name,
artist=metadata.get('artist', ''),
title=metadata.get('title', ''),
model=model,
model_label=model_label,
format=normalised_format,
format_label=format_label,
source_file=str(source_dest) if source_dest.exists() else '',
stems=stem_files,
sep_job_id=job_id,
trans_jobs={},
abc_jobs={},
lrc_jobs={},
mix_job_id=None,
)
except Exception as meta_err:
jm.update_job(job_id, progress=f"Warning: could not write project.json: {meta_err}")
jm.update_job(
job_id,
status=jm.JobStatus.DONE,
progress="Separation complete.",
files=all_files,
result={
"project_name": proj_name,
"project_path": str(dirs['project']),
"stems_dir": str(stems_dir),
"model_label": model_label,
"format_label": format_label,
"tab_label": tab_label,
"project_id": project['id'],
}
)
except Exception as exc:
err_str = str(exc)
if "CUDA" in err_str or "cuda" in err_str:
# CUDA context is corrupted (typically after a prior OOM).
# Auto-reset clears allocations and re-enables GPU for the next attempt.
try:
from .lyric_transcriber_service import reset_cuda
reset_cuda()
except Exception:
try:
import torch
torch.cuda.empty_cache()
except Exception:
pass
user_msg = (
f"GPU (CUDA) error: {err_str[:300]}\n\n"
"The GPU was automatically reset — please retry the separation. "
"If the error keeps occurring, restart the container."
)
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=user_msg, progress="GPU error — retry to use reset GPU.")
else:
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=err_str, progress="Error.")
def _get_muted_stems(static_option: Optional[str]) -> List[str]:
"""Convert static option ID to list of stem names to mute."""
if not static_option:
return []
option_map = {
'no_vocals': ['vocals'],
'no_drums': ['drums'],
'no_bass': ['bass'],
'no_vocals_no_bass': ['vocals', 'bass'],
'no_drums_no_bass': ['drums', 'bass'],
}
return option_map.get(static_option, [])
def _static_name(static_option: Optional[str]) -> str:
"""Convert static option ID to human-readable name."""
name_map = {
'no_vocals': 'No Vocals',
'no_drums': 'No Drums',
'no_bass': 'No Bass',
'no_vocals_no_bass': 'No Vocals No Bass',
'no_drums_no_bass': 'No Drums No Bass',
}
return name_map.get(static_option, 'Static Mix')
# ---------------------------------------------------------------------------
# Demucs (CLI)
# ---------------------------------------------------------------------------
def _run_demucs(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
segment_override: Optional[int] = None,
overlap_override: Optional[float] = None,
) -> Dict[str, str]:
"""
Run Demucs separation. Returns dict mapping stem names to file paths.
"""
jm.update_job(job_id, progress="Starting Demucs separation...")
ext = output_format.lower()
fmt_flag = []
if ext == 'mp3_320':
fmt_flag = ['--mp3', '--mp3-bitrate', '320']
ext = 'mp3'
elif ext == 'mp3':
fmt_flag = ['--mp3', '--mp3-bitrate', '256']
elif ext == 'flac':
fmt_flag = ['--flac']
power_flags = []
if segment_override is not None:
power_flags.extend(['--segment', str(segment_override)])
if overlap_override is not None:
power_flags.extend(['--overlap', str(overlap_override)])
# Use a temp directory for demucs output
temp_out = JOBS_DIR / job_id / "demucs_out"
temp_out.mkdir(parents=True, exist_ok=True)
cmd = [
sys.executable, '-m', 'demucs',
'--name', model,
'--out', str(temp_out),
*fmt_flag,
*power_flags,
input_path,
]
try:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
)
while True:
if jm.should_cancel(job_id):
process.terminate()
process.wait()
raise RuntimeError("Cancelled by user")
line = process.stdout.readline()
if line:
jm.update_job(job_id, progress=line.rstrip())
elif process.poll() is not None:
break
else:
time.sleep(0.1)
if process.returncode != 0:
raise RuntimeError(f"Demucs exited with code {process.returncode}")
# Move stems to project folder
input_stem = Path(input_path).stem
_prefix = stem_prefix or input_stem
stem_dir = temp_out / model / input_stem
stem_files = {}
if stem_dir.exists():
for f in sorted(stem_dir.iterdir()):
if f.is_file():
# Determine stem name from filename
stem_name = f.stem.lower()
# Clean up common demucs naming patterns
for prefix in [input_stem.lower(), 'htdemucs', 'mdx']:
stem_name = stem_name.replace(prefix, '').strip('_- ')
if not stem_name:
stem_name = f.stem
dest = stems_dir / _stem_filename(_prefix, stem_name, f.suffix)
shutil.move(str(f), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name] = str(dest)
# Cleanup temp directory
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
# Cleanup on error
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# BS-RoFormer Chorus (Male/Female) (Python API)
# ---------------------------------------------------------------------------
def _run_bsroformer_chorus(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""
Run BS-RoFormer Chorus separation (male/female/instrumental).
Returns dict mapping stem names to file paths.
"""
from .bs_roformer_chorus_separator import BSRoformerChorusSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading BS-RoFormer Chorus model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = BSRoformerChorusSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating male/female chorus... (this may take a while)")
temp_out = JOBS_DIR / job_id / "bsr_chorus_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MelBand Vocals (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_vocals(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run MelBand Vocals separation (vocals/instrumental)."""
# Route to appropriate separator based on model variant
if model == 'mel_band_vocals_anvuew':
from .mel_band_specialized_separator import MelBandVocalsAnvuewSeparator
sep_class = MelBandVocalsAnvuewSeparator
model_name = "MelBand Vocals Anvuew"
else:
from .mel_band_specialized_separator import MelBandVocalsSeparator
sep_class = MelBandVocalsSeparator
model_name = "MelBand Vocals"
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading {model_name} model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = sep_class(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating vocals with MelBand...")
temp_out = JOBS_DIR / job_id / "mel_band_vocals_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MelBand Dereverb (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_dereverb(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run MelBand Dereverb separation (dry vocals/reverb)."""
# Route to appropriate separator based on model variant
if model == 'mel_band_dereverb_room':
from .mel_band_specialized_separator import MelBandDereverbRoomSeparator
sep_class = MelBandDereverbRoomSeparator
model_name = "MelBand Room Dereverb"
elif model == 'mel_band_dereverb_anvuew':
from .mel_band_specialized_separator import MelBandDereverbAnvuewSeparator
sep_class = MelBandDereverbAnvuewSeparator
model_name = "MelBand Dereverb Anvuew"
else:
from .mel_band_specialized_separator import MelBandDereverbSeparator
sep_class = MelBandDereverbSeparator
model_name = "MelBand Dereverb"
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading {model_name} model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = sep_class(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Removing reverb with MelBand...")
temp_out = JOBS_DIR / job_id / "mel_band_dereverb_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
# Map internal names to display names
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MelBand Karaoke (Lead/Back) (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_karaoke(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run MelBand Karaoke separation (lead vocals/backing+instrumental)."""
# Route to appropriate separator based on model variant
if model == 'mel_band_karaoke_gabox':
from .mel_band_specialized_separator import MelBandKaraokeGaboxSeparator
sep_class = MelBandKaraokeGaboxSeparator
model_name = "MelBand Karaoke Gabox"
else:
from .mel_band_specialized_separator import MelBandKaraokeSeparator
sep_class = MelBandKaraokeSeparator
model_name = "MelBand Karaoke"
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading {model_name} model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = sep_class(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating lead/backing vocals...")
temp_out = JOBS_DIR / job_id / "mel_band_karaoke_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
# Map internal names to display names
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MDX-Net (ONNX) (Python API)
# ---------------------------------------------------------------------------
def _run_mdxnet(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""
Run MDX-Net separation using ONNX models.
Returns dict mapping stem names to file paths.
"""
from .mdxnet_separator import MDXNetSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading MDX-Net model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = MDXNetSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with MDX-Net...")
temp_out = JOBS_DIR / job_id / "mdxnet_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MelBand RoFormer (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_roformer(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""
Run MelBand RoFormer separation. Returns dict mapping stem names to file paths.
"""
from .mel_band_roformer_separator import MelBandRoformerSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading MelBand RoFormer model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = MelBandRoformerSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with MelBand RoFormer... (this may take a while)")
# Use a temp directory then move to project
temp_out = JOBS_DIR / job_id / "mel_band_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
# Move to project stems folder with proper naming
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
# Cleanup temp directory
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
# Cleanup on error
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# BS-RoFormer (Python API)
# ---------------------------------------------------------------------------
def _run_bsroformer(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""
Run BS-RoFormer separation. Returns dict mapping stem names to file paths.
"""
from .bs_roformer_separator import BSRoformerSeparator
stem_mode = BSR_STEM_MODE.get(model, '4stem')
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading BS-RoFormer ({stem_mode}) model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = BSRoformerSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
stem_mode=stem_mode,
)
jm.update_job(job_id, progress="Separating with BS-RoFormer... (this may take a while)")
# Use a temp directory then move to project
temp_out = JOBS_DIR / job_id / "bsr_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
# Move to project stems folder with proper naming
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
# Cleanup temp directory
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
# Cleanup on error
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MelBand RoFormer Deux Becruily (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_deux_becruily(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run MelBand RoFormer Deux Becruily separation (2-stem vocals)."""
from .mel_band_specialized_separator import MelBandDeuxBecruilySeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading MelBand Deux Becruily model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = MelBandDeuxBecruilySeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with Deux Becruily...")
temp_out = JOBS_DIR / job_id / "mel_band_deux_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# SCNet XL (Python API)
# ---------------------------------------------------------------------------
def _run_scnet(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run SCNet XL separation (state-of-the-art 2024-2025)."""
from .scnet_separator import SCNetSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading SCNet model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = SCNetSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with SCNet... (this may take a while)")
temp_out = JOBS_DIR / job_id / "scnet_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# BS-RoFormer SW (Swin Transformer variant) (Python API)
# ---------------------------------------------------------------------------
def _run_bsroformer_sw(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run BS-RoFormer SW (Swin Transformer) separation (6-stem)."""
from .bs_roformer_sw_separator import BSRoformerSWSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading BS-RoFormer SW model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = BSRoformerSWSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with BS-RoFormer SW... (this may take a while)")
temp_out = JOBS_DIR / job_id / "bsr_sw_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MedleyVox (HuggingFace) (Python API)
# ---------------------------------------------------------------------------
def _run_medleyvox(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run MedleyVox separation (multi-singer vocal separation)."""
from .medleyvox_separator import MedleyVoxSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading MedleyVox model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = MedleyVoxSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with MedleyVox...")
temp_out = JOBS_DIR / job_id / "medleyvox_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# Apollo (Vocal Enhancement/Reverb Removal) (Python API)
# ---------------------------------------------------------------------------
def _run_apollo(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run Apollo vocal enhancement/reverb removal."""
from .apollo_separator import ApolloSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading Apollo model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = ApolloSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Processing with Apollo...")
temp_out = JOBS_DIR / job_id / "apollo_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# Aspiration (Breath/Aspiration Removal) (Python API)
# ---------------------------------------------------------------------------
def _run_aspiration(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run Aspiration removal (breath/aspiration separation)."""
from .aspiration_separator import AspirationSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading Aspiration model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = AspirationSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Removing aspiration with MelBand...")
temp_out = JOBS_DIR / job_id / "aspiration_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# Bandit-v2 (Multi-Language/Multi-Stem) (Python API)
# ---------------------------------------------------------------------------
def _run_bandit(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run Bandit-v2 separation (multi-language/multi-stem)."""
from .bandit_separator import BanditSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading Bandit model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = BanditSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with Bandit-v2...")
temp_out = JOBS_DIR / job_id / "bandit_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# Ensemble (Combines Multiple Models) (Python API)
# ---------------------------------------------------------------------------
def _run_ensemble(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""
Run Ensemble separation (combines multiple models for best quality).
Models combined:
- ensemble_vocal_best: BS Roformer x2 + MelBand Roformer ft + SCNet XL IHF
- ensemble_4stem: Multiple 4-stem models averaged
- ensemble_6stem: Multiple 6-stem models averaged
"""
from .ensemble_separator import EnsembleSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading Ensemble models ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = EnsembleSeparator(
ensemble_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Running Ensemble separation... (this will take significantly longer)")
temp_out = JOBS_DIR / job_id / "ensemble_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise