""" Mix service: create static or dynamic mixes from separated stems. CPU-bound (pydub) — uses the general executor, not the GPU queue. """ from pathlib import Path from typing import Dict, List, Optional from .. import job_manager as jm from .preset_service import make_static, make_dynamic def run_mix( job_id: str, stem_files: Dict[str, str], # stem_name (lowercase) → abs file path preset_type: str, # 'static' | 'dynamic' output_path: str, output_format: str, static_option: Optional[str] = None, dynamic_levels: Optional[Dict[str, float]] = None, ): """Background task: create a mix from separated stems.""" jm.update_job(job_id, status=jm.JobStatus.RUNNING, progress="Creating mix...") try: out = Path(output_path) out.parent.mkdir(parents=True, exist_ok=True) if preset_type == "static": muted = _get_muted_stems(static_option) result_path = make_static(stem_files, muted, out, output_format) elif preset_type == "dynamic": volumes = dynamic_levels or {} result_path = make_dynamic(stem_files, volumes, out, output_format) else: raise ValueError(f"Unknown preset type: {preset_type}") jm.update_job( job_id, status=jm.JobStatus.DONE, progress="Mix complete.", files=[{"name": out.name, "path": result_path, "type": preset_type}], ) except Exception as exc: jm.update_job(job_id, status=jm.JobStatus.ERROR, error=str(exc), progress="Error.") def _get_muted_stems(static_option: Optional[str]) -> List[str]: 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, [])