175 lines
6.3 KiB
Python
175 lines
6.3 KiB
Python
"""
|
|
Post-processing helpers for separated stems.
|
|
|
|
Handles the three preset types that can be applied after separation:
|
|
- Instrumental (vocals removed)
|
|
- Static (specific stems removed or muted)
|
|
- Dynamic (per-stem volume adjustment)
|
|
|
|
These were previously inlined inside ``run_separation()`` in
|
|
``separator_service.py``. New callers should use ``apply_presets()`` or the
|
|
individual helpers directly.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Any, Callable, Dict, List, Optional
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def apply_presets(
|
|
*,
|
|
preset_type: str,
|
|
static_option: Optional[str],
|
|
dynamic_levels: Optional[Dict[str, float]],
|
|
stem_files: Dict[str, str],
|
|
dirs: Dict[str, Path],
|
|
proj_name: str,
|
|
input_path: str,
|
|
normalised_format: str,
|
|
job_id: str,
|
|
update_job_fn: Callable[..., None],
|
|
) -> List[Dict[str, Any]]:
|
|
"""Apply the requested preset to already-separated stems.
|
|
|
|
Returns a list of file-entry dicts (same shape as the ``files`` list in job
|
|
records) for any preset outputs that were produced. Returns an empty list
|
|
when ``preset_type`` is ``"stems_only"`` or the relevant directory is absent.
|
|
|
|
Args:
|
|
preset_type: ``"instrumental"``, ``"static"``, ``"dynamic"``, or
|
|
``"stems_only"``.
|
|
static_option: Static preset variant (e.g. ``"no_vocals"``).
|
|
dynamic_levels: Per-stem volume multipliers for the dynamic preset.
|
|
stem_files: ``{stem_name: abs_path}`` dict from the separator.
|
|
dirs: Project directory map returned by ``make_project_dirs``.
|
|
proj_name: Human-readable project name used for output filenames.
|
|
input_path: Original source file — used as metadata donor.
|
|
normalised_format: Output audio format (``"mp3"``, ``"flac"``, etc.).
|
|
job_id: Used only for progress updates.
|
|
update_job_fn: ``jm.update_job`` (or compatible callable).
|
|
|
|
Returns:
|
|
List of ``{"name": ..., "path": ..., "type": "instrumental"|"static"|"dynamic"}``
|
|
dicts for each file produced, or ``[]``.
|
|
"""
|
|
if preset_type == "instrumental" and "instrumental" in dirs:
|
|
return _apply_instrumental(
|
|
stem_files=stem_files,
|
|
out_dir=dirs["instrumental"],
|
|
proj_name=proj_name,
|
|
input_path=input_path,
|
|
normalised_format=normalised_format,
|
|
job_id=job_id,
|
|
update_job_fn=update_job_fn,
|
|
)
|
|
|
|
if preset_type == "static" and "static" in dirs:
|
|
return _apply_static(
|
|
stem_files=stem_files,
|
|
out_dir=dirs["static"],
|
|
proj_name=proj_name,
|
|
input_path=input_path,
|
|
normalised_format=normalised_format,
|
|
static_option=static_option,
|
|
job_id=job_id,
|
|
update_job_fn=update_job_fn,
|
|
)
|
|
|
|
if preset_type == "dynamic" and "dynamic" in dirs:
|
|
return _apply_dynamic(
|
|
stem_files=stem_files,
|
|
out_dir=dirs["dynamic"],
|
|
proj_name=proj_name,
|
|
input_path=input_path,
|
|
normalised_format=normalised_format,
|
|
dynamic_levels=dynamic_levels or {},
|
|
job_id=job_id,
|
|
update_job_fn=update_job_fn,
|
|
)
|
|
|
|
return []
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Individual preset helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _apply_instrumental(
|
|
stem_files: Dict[str, str],
|
|
out_dir: Path,
|
|
proj_name: str,
|
|
input_path: str,
|
|
normalised_format: str,
|
|
job_id: str,
|
|
update_job_fn: Callable[..., None],
|
|
) -> List[Dict[str, Any]]:
|
|
from ..preset_service import make_instrumental
|
|
from ..metadata_util import copy_metadata
|
|
|
|
update_job_fn(job_id, progress="Creating instrumental mix...")
|
|
inst_path = out_dir / f"{proj_name} - Instrumental.{normalised_format}"
|
|
try:
|
|
result = make_instrumental(stem_files, inst_path, normalised_format)
|
|
copy_metadata(input_path, result, stem_label="Instrumental", copy_lyrics=True)
|
|
return [{"name": inst_path.name, "path": result, "type": "instrumental"}]
|
|
except Exception as exc:
|
|
update_job_fn(job_id, progress=f"Instrumental preset failed: {exc}")
|
|
log.warning("[%s] instrumental preset failed: %s", job_id, exc)
|
|
return []
|
|
|
|
|
|
def _apply_static(
|
|
stem_files: Dict[str, str],
|
|
out_dir: Path,
|
|
proj_name: str,
|
|
input_path: str,
|
|
normalised_format: str,
|
|
static_option: Optional[str],
|
|
job_id: str,
|
|
update_job_fn: Callable[..., None],
|
|
) -> List[Dict[str, Any]]:
|
|
from ..preset_service import make_static
|
|
from ..metadata_util import copy_metadata
|
|
from .output_layout import get_muted_stems, static_name as _static_name
|
|
|
|
update_job_fn(job_id, progress="Creating static mix...")
|
|
muted = get_muted_stems(static_option)
|
|
label = _static_name(static_option)
|
|
static_path = out_dir / f"{proj_name} - {label}.{normalised_format}"
|
|
try:
|
|
result = make_static(stem_files, muted, static_path, normalised_format)
|
|
copy_metadata(input_path, result, stem_label=label, copy_lyrics=True)
|
|
return [{"name": static_path.name, "path": result, "type": "static"}]
|
|
except Exception as exc:
|
|
update_job_fn(job_id, progress=f"Static preset failed: {exc}")
|
|
log.warning("[%s] static preset failed: %s", job_id, exc)
|
|
return []
|
|
|
|
|
|
def _apply_dynamic(
|
|
stem_files: Dict[str, str],
|
|
out_dir: Path,
|
|
proj_name: str,
|
|
input_path: str,
|
|
normalised_format: str,
|
|
dynamic_levels: Dict[str, float],
|
|
job_id: str,
|
|
update_job_fn: Callable[..., None],
|
|
) -> List[Dict[str, Any]]:
|
|
from ..preset_service import make_dynamic
|
|
from ..metadata_util import copy_metadata
|
|
|
|
update_job_fn(job_id, progress="Creating dynamic mix...")
|
|
dynamic_path = out_dir / f"{proj_name} - Dynamic Mix.{normalised_format}"
|
|
try:
|
|
result = make_dynamic(stem_files, dynamic_levels, dynamic_path, normalised_format)
|
|
copy_metadata(input_path, result, stem_label="Dynamic Mix", copy_lyrics=True)
|
|
return [{"name": dynamic_path.name, "path": result, "type": "dynamic"}]
|
|
except Exception as exc:
|
|
update_job_fn(job_id, progress=f"Dynamic preset failed: {exc}")
|
|
log.warning("[%s] dynamic preset failed: %s", job_id, exc)
|
|
return []
|