655 lines
28 KiB
Python
Executable File
655 lines
28 KiB
Python
Executable File
import os
|
||
from pathlib import Path
|
||
from typing import Optional
|
||
|
||
|
||
def _csv_env(name: str) -> list[str]:
|
||
value = os.environ.get(name, "")
|
||
if not value:
|
||
return []
|
||
return [item.strip() for item in value.split(",") if item.strip()]
|
||
|
||
|
||
def _env_bool(name: str, default: bool) -> bool:
|
||
raw = os.environ.get(name)
|
||
if raw is None:
|
||
return default
|
||
return raw.strip().lower() in {"1", "true", "yes", "on"}
|
||
|
||
|
||
def _env_int(name: str, default: int, minimum: Optional[int] = None) -> int:
|
||
raw = os.environ.get(name)
|
||
try:
|
||
value = int(raw) if raw is not None else int(default)
|
||
except (TypeError, ValueError):
|
||
value = int(default)
|
||
if minimum is not None:
|
||
value = max(minimum, value)
|
||
return value
|
||
|
||
|
||
def _load_runtime_settings_dict() -> dict:
|
||
settings_path = Path(os.environ.get("SETTINGS_PATH", str(MEDIA_DIR / "settings.json")))
|
||
if not settings_path.exists():
|
||
return {}
|
||
try:
|
||
import json
|
||
|
||
raw = json.loads(settings_path.read_text(encoding="utf-8"))
|
||
return raw if isinstance(raw, dict) else {}
|
||
except Exception:
|
||
return {}
|
||
|
||
|
||
def _setting_raw(env_name: str, settings_key: str, default):
|
||
env_val = os.environ.get(env_name)
|
||
if env_val is not None:
|
||
return env_val
|
||
settings = _load_runtime_settings_dict()
|
||
val = settings.get(settings_key)
|
||
return default if val is None else val
|
||
|
||
|
||
def _setting_int(env_name: str, settings_key: str, default: int, minimum: Optional[int] = None) -> int:
|
||
raw = _setting_raw(env_name, settings_key, default)
|
||
try:
|
||
value = int(raw)
|
||
except (TypeError, ValueError):
|
||
value = int(default)
|
||
if minimum is not None:
|
||
value = max(minimum, value)
|
||
return value
|
||
|
||
|
||
def _setting_bool(env_name: str, settings_key: str, default: bool) -> bool:
|
||
raw = _setting_raw(env_name, settings_key, default)
|
||
if isinstance(raw, bool):
|
||
return raw
|
||
return str(raw).strip().lower() in {"1", "true", "yes", "on"}
|
||
|
||
|
||
def _setting_choice(env_name: str, settings_key: str, default: str, allowed: set[str]) -> str:
|
||
raw = _setting_raw(env_name, settings_key, default)
|
||
cleaned = str(raw).strip().lower()
|
||
if cleaned in allowed:
|
||
return cleaned
|
||
return default
|
||
|
||
|
||
def _setting_optional_int(env_name: str, settings_key: str, default: Optional[int] = None) -> Optional[int]:
|
||
raw = _setting_raw(env_name, settings_key, default)
|
||
if raw in {None, "", 0, "0"}:
|
||
return None
|
||
try:
|
||
return int(raw)
|
||
except (TypeError, ValueError):
|
||
return default
|
||
|
||
|
||
APP_ENV = os.environ.get("APP_ENV", "development").strip().lower()
|
||
ADMIN_TOKEN = os.environ.get("ADMIN_TOKEN", "").strip()
|
||
CORS_ORIGINS = _csv_env("CORS_ORIGINS")
|
||
CORS_ORIGIN_REGEX = os.environ.get("CORS_ORIGIN_REGEX", "").strip()
|
||
|
||
# Base paths — each can be overridden independently via its own env var,
|
||
# or all inherit from MEDIA_DIR when left unset.
|
||
MEDIA_DIR = Path(os.environ.get("MEDIA_DIR", "/app/media"))
|
||
UPLOAD_DIR = Path(os.environ.get("UPLOAD_DIR", str(MEDIA_DIR / "uploads")))
|
||
JOBS_DIR = Path(os.environ.get("JOBS_DIR", str(MEDIA_DIR / "jobs")))
|
||
PROJECTS_DIR = Path(os.environ.get("PROJECTS_DIR", str(MEDIA_DIR / "projects")))
|
||
DB_PATH = Path(os.environ.get("DB_PATH", str(MEDIA_DIR / "db" / "stemengine.db")))
|
||
LRC_SYNC_DIR = Path(os.environ.get("LRC_SYNC_DIR", str(MEDIA_DIR / "lrc_sync")))
|
||
# Optional global output directory for .sek packages.
|
||
# Defaults to <MEDIA_DIR>/sek_exports (the sekfold volume mount inside the container).
|
||
# Override with an absolute path in .env; relative paths are resolved against MEDIA_DIR.
|
||
STEMENGINE_KARAOKE_EXPORT_DIR = os.environ.get("STEMENGINE_KARAOKE_EXPORT_DIR", "").strip() or str(MEDIA_DIR / "sekfold")
|
||
|
||
# BS-RoFormer model directory
|
||
BS_ROFORMER_MODEL_DIR = Path(os.environ.get("BS_ROFORMER_MODEL_DIR", "/opt/bs_roformer_models"))
|
||
|
||
# MelBand RoFormer model directory
|
||
MEL_BAND_MODEL_DIR = Path(os.environ.get("MEL_BAND_MODEL_DIR", "/opt/mel_band_models"))
|
||
|
||
# MDX-Net model directory
|
||
MDXNET_MODEL_DIR = Path(os.environ.get("MDXNET_MODEL_DIR", "/opt/mdxnet_models"))
|
||
|
||
# SCNet XL model directory
|
||
SCNET_MODEL_DIR = Path(os.environ.get("SCNET_MODEL_DIR", "/opt/scnet_models"))
|
||
|
||
# MedleyVox model directory
|
||
MEDLEYVOX_MODEL_DIR = Path(os.environ.get("MEDLEYVOX_MODEL_DIR", "/opt/medleyvox_models"))
|
||
|
||
# Apollo model directory
|
||
APOLLO_MODEL_DIR = Path(os.environ.get("APOLLO_MODEL_DIR", "/opt/apollo_models"))
|
||
|
||
# Bandit model directory
|
||
BANDIT_MODEL_DIR = Path(os.environ.get("BANDIT_MODEL_DIR", "/opt/bandit_models"))
|
||
|
||
# Aspiration model directory
|
||
ASPIRATION_MODEL_DIR = Path(os.environ.get("ASPIRATION_MODEL_DIR", "/opt/aspiration_models"))
|
||
|
||
# SepACap model directory (a cappella multi-singer separation)
|
||
SEPACAP_MODEL_DIR = Path(os.environ.get("SEPACAP_MODEL_DIR", "/opt/sepacap_models"))
|
||
|
||
# SepReformer model directory (2-speaker speech separation)
|
||
SEPREFORMER_MODEL_DIR = Path(os.environ.get("SEPREFORMER_MODEL_DIR", "/opt/sepreformer_models"))
|
||
|
||
# Ensemble mode cache directory
|
||
ENSEMBLE_CACHE_DIR = Path(os.environ.get("ENSEMBLE_CACHE_DIR", "/app/media/ensemble_cache"))
|
||
|
||
# Gabox model rollout controls
|
||
GABOX_ENABLE_SMALL_MODELS: bool = os.environ.get("GABOX_ENABLE_SMALL_MODELS", "false").lower() == "true"
|
||
GABOX_ENABLE_EXPERIMENTAL_MODELS: bool = os.environ.get("GABOX_ENABLE_EXPERIMENTAL_MODELS", "false").lower() == "true"
|
||
GABOX_SMALL_MODEL_MAX_DURATION_SECS: int = int(os.environ.get("GABOX_SMALL_MODEL_MAX_DURATION_SECS", "360"))
|
||
GABOX_SMALL_MODEL_PREFERRED_FOR_LOW_VRAM: bool = (
|
||
os.environ.get("GABOX_SMALL_MODEL_PREFERRED_FOR_LOW_VRAM", "false").lower() == "true"
|
||
)
|
||
|
||
# Separation models — Demucs + BS-RoFormer
|
||
DEMUCS_MODELS = [
|
||
{"id": "htdemucs", "label": "Demucs v4", "group": "Demucs"},
|
||
{"id": "htdemucs_ft", "label": "Demucs v4 Fine-tuned", "group": "Demucs"},
|
||
{"id": "mdx_extra_q", "label": "Demucs v3 Extra Quantized","group": "Demucs"},
|
||
{"id": "mdx", "label": "Demucs v3", "group": "Demucs"},
|
||
]
|
||
|
||
BS_ROFORMER_MODELS = [
|
||
{"id": "bs_roformer_4stem", "label": "BS-RoFormer 4-stem", "group": "BS-RoFormer"},
|
||
{"id": "bs_roformer_5stem_guitar", "label": "BS-RoFormer 5-stem (+ Guitar)", "group": "BS-RoFormer"},
|
||
{"id": "bs_roformer_5stem_piano", "label": "BS-RoFormer 5-stem (+ Piano)", "group": "BS-RoFormer"},
|
||
{"id": "bs_roformer_6stem", "label": "BS-RoFormer 6-stem (full)", "group": "BS-RoFormer"},
|
||
]
|
||
|
||
# MelBand RoFormer models
|
||
MEL_BAND_MODELS = [
|
||
{"id": "mel_band_roformer_4stem", "label": "MelBand RoFormer 4-stem", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_vocals", "label": "MelBand Vocals (2-stem)", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_vocals_anvuew", "label": "MelBand Vocals Anvuew", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_dereverb", "label": "MelBand Dereverb", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_dereverb_anvuew", "label": "MelBand Dereverb Anvuew", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_dereverb_room", "label": "MelBand Dereverb Room", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_karaoke", "label": "MelBand Karaoke (Lead/Back)", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_karaoke_gabox", "label": "MelBand Karaoke Gabox", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_karaoke_gabox_v1", "label": "MelBand Karaoke Gabox V1", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_vocals_gabox", "label": "MelBand Vocals Gabox", "group": "MelBand RoFormer"},
|
||
{"id": "mel_band_deux_becruily", "label": "MelBand RoFormer Deux Becruily", "group": "MelBand RoFormer"},
|
||
]
|
||
|
||
if GABOX_ENABLE_SMALL_MODELS:
|
||
MEL_BAND_MODELS.extend([
|
||
{
|
||
"id": "mel_band_karaoke_gabox_small",
|
||
"label": "MelBand Karaoke Gabox (Small)",
|
||
"group": "MelBand RoFormer",
|
||
},
|
||
])
|
||
|
||
if GABOX_ENABLE_EXPERIMENTAL_MODELS:
|
||
MEL_BAND_MODELS.extend([
|
||
{
|
||
"id": "mel_band_karaoke_gabox_v2_exp",
|
||
"label": "MelBand Karaoke Gabox V2 (Experimental)",
|
||
"group": "MelBand RoFormer",
|
||
},
|
||
])
|
||
|
||
# BS-RoFormer specialized models
|
||
BS_ROFORMER_SPECIALIZED_MODELS = [
|
||
{"id": "bs_roformer_chorus", "label": "BS-RoFormer Male/Female Chorus", "group": "BS-RoFormer"},
|
||
{"id": "bs_roformer_anvuew", "label": "BS-RoFormer Anvuew (Vocals)", "group": "BS-RoFormer"},
|
||
{"id": "bs_roformer_karaoke_anvuew", "label": "BS-RoFormer Karaoke Anvuew", "group": "BS-RoFormer"},
|
||
{"id": "bs_roformer_karaoke_frazer_becruily", "label": "BS-RoFormer Karaoke Frazer+Becruily", "group": "BS-RoFormer"},
|
||
{"id": "bs_mamba2_v1", "label": "BS Mamba2 Vocals v1 (Adapter)", "group": "BS-RoFormer"},
|
||
{"id": "bs_polarformer_v1", "label": "BS Polarformer v1 (Adapter)", "group": "BS-RoFormer"},
|
||
]
|
||
|
||
# MDX-Net models (from Ultimate Vocal Remover)
|
||
MDXNET_MODELS = [
|
||
{"id": "mdxnet_voc_ft", "label": "MDX-Net Vocals FT", "group": "MDX-Net"},
|
||
{"id": "mdxnet_inst_hq_1", "label": "MDX-Net Inst HQ 1", "group": "MDX-Net"},
|
||
{"id": "mdxnet_inst_hq_2", "label": "MDX-Net Inst HQ 2", "group": "MDX-Net"},
|
||
{"id": "mdxnet_karaoke", "label": "MDX-Net Karaoke", "group": "MDX-Net"},
|
||
{"id": "mdxnet_dereverb", "label": "MDX-Net Dereverb", "group": "MDX-Net"},
|
||
{"id": "mdxnet_drumsep", "label": "MDX-Net Drum Separation", "group": "MDX-Net"},
|
||
]
|
||
|
||
# SCNet XL models (state-of-the-art 2024-2025)
|
||
SCNET_MODELS = [
|
||
{"id": "scnet_xl", "label": "SCNet XL (2-stem)", "group": "SCNet"},
|
||
{"id": "scnet_xl_ihf", "label": "SCNet XL IHF (2-stem)", "group": "SCNet"},
|
||
{"id": "scnet_masked_xl_ihf", "label": "SCNet Masked XL IHF (4-stem)", "group": "SCNet"},
|
||
{"id": "scnet_masked_small", "label": "SCNet Masked Small (4-stem)", "group": "SCNet"},
|
||
{"id": "scnet_tran_small", "label": "SCNet Tran Small (4-stem)", "group": "SCNet"},
|
||
{"id": "scnet_large", "label": "SCNet Large (4-stem)", "group": "SCNet"},
|
||
{"id": "scnet_large_starrytong", "label": "SCNet Large Starrytong", "group": "SCNet"},
|
||
]
|
||
|
||
# BS-RoFormer SW (Swin Transformer variant)
|
||
BS_ROFORMER_SW_MODELS = [
|
||
{"id": "bs_roformer_sw_6stem", "label": "BS-RoFormer SW 6-stem", "group": "BS-RoFormer SW"},
|
||
]
|
||
|
||
# MedleyVox models (specialized vocal separation)
|
||
MEDLEYVOX_MODELS = [
|
||
{"id": "medleyvox", "label": "MedleyVox (Vocals)", "group": "MedleyVox"},
|
||
{"id": "medleyvox_55ep", "label": "MedleyVox 55 Epochs", "group": "MedleyVox"},
|
||
{"id": "medleyvox_66ep", "label": "MedleyVox 66 Epochs", "group": "MedleyVox"},
|
||
{"id": "medleyvox_88ep", "label": "MedleyVox 88 Epochs", "group": "MedleyVox"},
|
||
{"id": "medleyvox_100ep", "label": "MedleyVox 100 Epochs", "group": "MedleyVox"},
|
||
]
|
||
|
||
# Apollo models (vocal enhancement/reverb removal)
|
||
APOLLO_MODELS = [
|
||
{"id": "apollo_lq", "label": "Apollo LQ MP3 Restoration", "group": "Apollo"},
|
||
{"id": "apollo_vocal_enhancer", "label": "Apollo Vocal Enhancer", "group": "Apollo"},
|
||
{"id": "apollo_vocal_enhancer_v2", "label": "Apollo Vocal Enhancer V2 (WIP)", "group": "Apollo"},
|
||
]
|
||
|
||
# Aspiration models (breath/aspiration removal)
|
||
ASPIRATION_MODELS = [
|
||
{"id": "aspiration_full", "label": "Aspiration Removal (Full)", "group": "Aspiration"},
|
||
{"id": "aspiration_light", "label": "Aspiration Removal (Light)", "group": "Aspiration"},
|
||
]
|
||
|
||
# Bandit-v2 models (multi-language/multi-stem)
|
||
BANDIT_MODELS = [
|
||
{"id": "bandit_v2_multi", "label": "Bandit v2 Multi-Language", "group": "Bandit"},
|
||
{"id": "bandit_v2_eng", "label": "Bandit v2 English", "group": "Bandit"},
|
||
]
|
||
|
||
# SepACap models (a cappella multi-singer vocal separation -- 7 stems)
|
||
SEPACAP_MODELS = [
|
||
{"id": "sepacap", "label": "SepACap (7-stem A Cappella)", "group": "SepACap"},
|
||
]
|
||
|
||
# SepReformer models (2-speaker speech separation -- NeurIPS 2024)
|
||
SEPREFORMER_MODELS = [
|
||
{"id": "sepreformer_base", "label": "SepReformer Base (Speech, WSJ0)", "group": "SepReformer"},
|
||
{"id": "sepreformer_large_wsj0", "label": "SepReformer Large (Speech, WSJ0)", "group": "SepReformer"},
|
||
{"id": "sepreformer_large_wham", "label": "SepReformer Large (Noisy Speech, WHAM)", "group": "SepReformer"},
|
||
{"id": "sepreformer_large_whamr", "label": "SepReformer Large (Noisy+Reverb, WHAMR)", "group": "SepReformer"},
|
||
]
|
||
|
||
# Ensemble modes (combines multiple models for best quality)
|
||
ENSEMBLE_MODELS = [
|
||
{"id": "ensemble_vocal_best", "label": "Ensemble - Vocal Best Quality", "group": "Ensemble"},
|
||
{"id": "ensemble_4stem", "label": "Ensemble - 4-Stem", "group": "Ensemble"},
|
||
{"id": "ensemble_6stem", "label": "Ensemble - 6-Stem Full", "group": "Ensemble"},
|
||
]
|
||
|
||
ALL_MODELS = (
|
||
DEMUCS_MODELS +
|
||
BS_ROFORMER_MODELS +
|
||
BS_ROFORMER_SW_MODELS +
|
||
BS_ROFORMER_SPECIALIZED_MODELS +
|
||
MEL_BAND_MODELS +
|
||
MDXNET_MODELS +
|
||
SCNET_MODELS +
|
||
MEDLEYVOX_MODELS +
|
||
APOLLO_MODELS +
|
||
ASPIRATION_MODELS +
|
||
BANDIT_MODELS +
|
||
SEPACAP_MODELS +
|
||
SEPREFORMER_MODELS +
|
||
ENSEMBLE_MODELS
|
||
)
|
||
DEFAULT_DEMUCS_MODEL = "htdemucs"
|
||
DEFAULT_MODEL = DEFAULT_DEMUCS_MODEL
|
||
|
||
ALL_MODEL_IDS = {m["id"] for m in ALL_MODELS}
|
||
|
||
# Canonical stem keys for per-instrument model refinement.
|
||
INSTRUMENT_MODEL_KEYS = (
|
||
"vocals",
|
||
"instrumental",
|
||
"backing",
|
||
"drums",
|
||
"bass",
|
||
"other",
|
||
"guitar",
|
||
"piano",
|
||
)
|
||
|
||
# Output formats
|
||
OUTPUT_FORMATS = [
|
||
{"id": "mp3", "label": "MP3 (256 kbps)"},
|
||
{"id": "mp3_320", "label": "MP3 (320 kbps)"},
|
||
{"id": "flac", "label": "FLAC (lossless)"},
|
||
{"id": "wav", "label": "WAV (lossless)"},
|
||
]
|
||
DEFAULT_OUTPUT_FORMAT = "mp3"
|
||
|
||
# Transcription modes — basic-pitch (in-process) and Omnizart (CLI/sidecar)
|
||
# Each entry carries an "engine" tag so the frontend can group/disable entries
|
||
# and a human-readable "description" surfaced as tooltip text.
|
||
TRANSCRIBE_MODES = [
|
||
{
|
||
"id": "polyphonic",
|
||
"label": "Polyphonic",
|
||
"engine": "basic-pitch",
|
||
"description": "General multi-instrument MIDI — basic-pitch model (fast, in-process)",
|
||
},
|
||
{
|
||
"id": "melody",
|
||
"label": "Melody / Vocal",
|
||
"engine": "basic-pitch",
|
||
"description": "Single-voice / vocal melody extraction — basic-pitch with melodia trick",
|
||
},
|
||
{
|
||
"id": "monophonic",
|
||
"label": "Monophonic",
|
||
"engine": "basic-pitch",
|
||
"description": "Strict single-voice output — melodia trick + higher thresholds + no pitch bends",
|
||
},
|
||
{
|
||
"id": "guitar",
|
||
"label": "Guitar",
|
||
"engine": "basic-pitch",
|
||
"description": "Guitar-optimised — pitch bends enabled, frequency range E2–D6 (82–1300 Hz)",
|
||
},
|
||
{
|
||
"id": "bass",
|
||
"label": "Bass",
|
||
"engine": "basic-pitch",
|
||
"description": "Bass-optimised — pitch bends enabled, frequency range E1–G4 (41–300 Hz)",
|
||
},
|
||
# ── Omnizart modes ──────────────────────────────────────────────────────
|
||
# Require OMNIZART_ENABLED=true; served by the omnizart-sidecar container
|
||
# (or a local Omnizart installation when OMNIZART_SIDECAR_URL is unset).
|
||
{
|
||
"id": "omnizart-music",
|
||
"label": "Omnizart — Multi-instrument",
|
||
"engine": "omnizart",
|
||
"description": "Full-arrangement, instrument-aware MIDI — Omnizart 'music' mode (slow, GPU)",
|
||
},
|
||
{
|
||
"id": "omnizart-vocal",
|
||
"label": "Omnizart — Vocal Melody",
|
||
"engine": "omnizart",
|
||
"description": "Vocal melody extraction — Omnizart 'vocal' mode",
|
||
},
|
||
{
|
||
"id": "omnizart-vocal-contour",
|
||
"label": "Omnizart — Vocal Contour",
|
||
"engine": "omnizart",
|
||
"description": "Vocal pitch contour (F0-based) — Omnizart 'vocal-contour' mode",
|
||
},
|
||
]
|
||
DEFAULT_TRANSCRIBE_MODE = "polyphonic"
|
||
|
||
# Omnizart availability — set OMNIZART_ENABLED=true to unlock the modes above.
|
||
# When False the router returns 503 for any omnizart-* mode request.
|
||
OMNIZART_ENABLED: bool = os.environ.get("OMNIZART_ENABLED", "false").lower() == "true"
|
||
|
||
# When set, omnizart_service will POST audio to this URL and retrieve the MIDI
|
||
# (sidecar HTTP mode). When empty the service falls back to a local subprocess.
|
||
OMNIZART_SIDECAR_URL: str = os.environ.get("OMNIZART_SIDECAR_URL", "").strip()
|
||
|
||
# basic-pitch inference parameter presets per mode
|
||
TRANSCRIBE_PRESETS = {
|
||
"polyphonic": {
|
||
"onset_threshold": 0.5,
|
||
"frame_threshold": 0.3,
|
||
"minimum_note_length": 58.0,
|
||
"melodia_trick": False,
|
||
},
|
||
"melody": {
|
||
"onset_threshold": 0.5,
|
||
"frame_threshold": 0.3,
|
||
"minimum_note_length": 58.0,
|
||
"minimum_frequency": 65.0, # Hz — below typical singing range
|
||
"melodia_trick": True,
|
||
},
|
||
"monophonic": {
|
||
"onset_threshold": 0.6,
|
||
"frame_threshold": 0.5,
|
||
"minimum_note_length": 100.0,
|
||
"melodia_trick": True,
|
||
"multiple_pitch_bends": False,
|
||
},
|
||
"guitar": {
|
||
"onset_threshold": 0.5,
|
||
"frame_threshold": 0.3,
|
||
"minimum_note_length": 58.0,
|
||
"minimum_frequency": 82.0,
|
||
"maximum_frequency": 1300.0,
|
||
"melodia_trick": False,
|
||
"multiple_pitch_bends": True,
|
||
},
|
||
"bass": {
|
||
"onset_threshold": 0.5,
|
||
"frame_threshold": 0.3,
|
||
"minimum_note_length": 58.0,
|
||
"minimum_frequency": 41.0,
|
||
"maximum_frequency": 300.0,
|
||
"melodia_trick": False,
|
||
"multiple_pitch_bends": True,
|
||
},
|
||
}
|
||
|
||
# Hardware
|
||
CPU_SEPARATION = _setting_bool("CPU_SEPARATION", "cpu_separation", False)
|
||
|
||
|
||
def get_gpu_vram_reserve_mb() -> int:
|
||
"""Runtime VRAM reserve setting (env → settings.json → default)."""
|
||
return _setting_int("STEMENGINE_GPU_VRAM_RESERVE_MB", "gpu_vram_reserve_mb", 2048, minimum=0)
|
||
|
||
|
||
def get_sep_power_preset() -> str:
|
||
"""Runtime sep_power setting (env → settings.json → default)."""
|
||
return _setting_choice("SEP_POWER", "sep_power", "eco", {"eco", "standard", "performance"})
|
||
|
||
|
||
def get_fallback_require_lead_back() -> bool:
|
||
"""Whether karaoke OOM may auto-fallback across models.
|
||
|
||
Runtime order: env → settings.json → default.
|
||
"""
|
||
return _setting_bool("FALLBACK_REQUIRE_LEAD_BACK", "fallback_require_lead_back", False)
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# RTX 3060 / constrained-VRAM stability
|
||
# ---------------------------------------------------------------------------
|
||
|
||
# When true: on CUDA OOM after all parameter reductions, fall back to a model
|
||
# that ALSO produces lead/backing vocal separation (karaoke-capable).
|
||
# When false (default): skip cross-model fallback and raise immediately.
|
||
|
||
# Tracks longer than this threshold (seconds) get MelBand eco params forced
|
||
# regardless of the active power preset. Prevents OOM on GPUs with ≤8 GB VRAM
|
||
# when running MelBand Karaoke or 4-stem on full-length tracks (≥8 min).
|
||
# Also used as the auto-chunking activation threshold when
|
||
# LONG_TRACK_CHUNKING_MODE=auto.
|
||
LONG_TRACK_DURATION_SECS: int = int(
|
||
_setting_int("LONG_TRACK_DURATION_SECS", "long_track_threshold_secs", 480, minimum=60)
|
||
)
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Long-track chunked separation
|
||
# ---------------------------------------------------------------------------
|
||
|
||
# Activation policy:
|
||
# auto — activate when track duration >= LONG_TRACK_DURATION_SECS
|
||
# always — always chunk regardless of duration
|
||
# off — never chunk (legacy flat-buffer behaviour)
|
||
LONG_TRACK_CHUNKING_MODE: str = (
|
||
os.environ.get("LONG_TRACK_CHUNKING_MODE", "auto").strip().lower() or "auto"
|
||
)
|
||
|
||
# Duration of each chunk in seconds. Smaller values reduce peak VRAM at the
|
||
# cost of more crossfade seams; 180 s (3 min) is a good default.
|
||
LONG_TRACK_CHUNK_DURATION_SECS: int = int(
|
||
_setting_int("LONG_TRACK_CHUNK_DURATION_SECS", "long_track_chunk_secs", 180, minimum=60)
|
||
)
|
||
|
||
# Width of the crossfade overlap between consecutive chunks in seconds.
|
||
# Adjacent trapezoidal fade-out / fade-in windows sum to 1.0 so there is no
|
||
# amplitude discontinuity at the seam.
|
||
LONG_TRACK_CHUNK_OVERLAP_SECS: int = int(
|
||
os.environ.get("LONG_TRACK_CHUNK_OVERLAP_SECS", "10")
|
||
)
|
||
|
||
# When true, MelBand separators emit per-stage memory diagnostics into the job
|
||
# progress stream and backend logs. Useful for understanding why resource usage
|
||
# spikes before the first separation segment begins.
|
||
MELBAND_STAGE_DIAGNOSTICS: bool = (
|
||
os.environ.get("MELBAND_STAGE_DIAGNOSTICS", "false").lower() == "true"
|
||
)
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Demucs long-track tuning
|
||
# ---------------------------------------------------------------------------
|
||
|
||
# Demucs segment size (seconds) for long tracks. Smaller values reduce peak VRAM
|
||
# but degrade quality slightly at seam boundaries. Auto mode scales segment based
|
||
# on track duration:
|
||
# < 120 s: 12 s (default Demucs)
|
||
# < 480 s: 8 s (quality-biased)
|
||
# >= 480 s: 4 s (VRAM-biased)
|
||
DEMUCS_SEGMENT_OVERRIDE: Optional[int] = (
|
||
int(os.environ.get("DEMUCS_SEGMENT_OVERRIDE", "0")) or None
|
||
)
|
||
|
||
# Demucs overlap factor for long tracks. Default 0.25; can increase to 0.5
|
||
# for better quality at seam boundaries (at cost of 2x compute).
|
||
DEMUCS_OVERLAP_OVERRIDE: Optional[float] = (
|
||
float(os.environ.get("DEMUCS_OVERLAP_OVERRIDE", "0")) or None
|
||
)
|
||
|
||
# MelBand RoFormer dim_t (segment time dimension) override. The standard MelBand
|
||
# models use dim_t=801 (slower) or dim_t=256 (faster, Becruily variant).
|
||
# This override forces a specific dim_t value across all MelBand models.
|
||
# Smaller values (e.g., 256) = faster but potentially lower quality; larger
|
||
# values (e.g., 1101) = slower but potentially better quality.
|
||
# WARNING: Setting a dim_t value incompatible with the model architecture
|
||
# will cause inference failures. Only override if you understand the model.
|
||
MELBAND_DIM_T_OVERRIDE: Optional[int] = (
|
||
_setting_optional_int("MELBAND_DIM_T_OVERRIDE", "melband_dim_t", None)
|
||
)
|
||
|
||
# Limits
|
||
|
||
MAX_UPLOAD_BYTES = int(os.environ.get("MAX_UPLOAD_MB", "500")) * 1024 * 1024
|
||
POLL_INTERVAL_SECONDS = 5.0
|
||
|
||
# Job lifecycle hardening
|
||
# Retains linked project jobs, while pruning terminal non-project jobs older than
|
||
# the configured age. Pruning is opportunistic and runs at a bounded interval.
|
||
STEMENGINE_JOB_RETENTION_ENABLED: bool = _env_bool("STEMENGINE_JOB_RETENTION_ENABLED", True)
|
||
STEMENGINE_JOB_RETENTION_DAYS: int = _env_int("STEMENGINE_JOB_RETENTION_DAYS", 30, minimum=1)
|
||
STEMENGINE_JOB_RETENTION_PRUNE_BATCH: int = _env_int("STEMENGINE_JOB_RETENTION_PRUNE_BATCH", 200, minimum=1)
|
||
STEMENGINE_JOB_RETENTION_INTERVAL_SECS: int = _env_int("STEMENGINE_JOB_RETENTION_INTERVAL_SECS", 900, minimum=0)
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Presets for stem mixing
|
||
# ---------------------------------------------------------------------------
|
||
|
||
# Available preset types
|
||
PRESET_TYPES = [
|
||
{"id": "stems_only", "label": "Stems Only", "description": "Generate separated stems only"},
|
||
{"id": "static", "label": "Static Mix", "description": "Remove specific elements completely"},
|
||
{"id": "instrumental", "label": "Instrumental", "description": "Remove vocals only"},
|
||
{"id": "dynamic", "label": "Dynamic Mix", "description": "Adjust volume levels of each element"},
|
||
]
|
||
|
||
# Static preset options (what to remove)
|
||
STATIC_OPTIONS = [
|
||
{"id": "no_vocals", "label": "No Vocals", "description": "Remove vocals (keep drums, bass, other)"},
|
||
{"id": "no_drums", "label": "No Drums", "description": "Remove drums (keep vocals, bass, other)"},
|
||
{"id": "no_bass", "label": "No Bass", "description": "Remove bass (keep vocals, drums, other)"},
|
||
{"id": "no_vocals_no_bass", "label": "No Vocals + No Bass", "description": "Remove vocals and bass"},
|
||
{"id": "no_drums_no_bass", "label": "No Drums + No Bass", "description": "Remove drums and bass (vocals + other)"},
|
||
]
|
||
|
||
# Default volume levels for dynamic preset (0.0 to 2.0, where 1.0 = original)
|
||
DEFAULT_DYNAMIC_LEVELS = {
|
||
"vocals": 1.0,
|
||
"drums": 1.0,
|
||
"bass": 1.0,
|
||
"other": 1.0,
|
||
}
|
||
|
||
# Available stems and their labels (varies by model)
|
||
STEM_LABELS = {
|
||
"vocals": "Vocals",
|
||
"drums": "Drums",
|
||
"bass": "Bass",
|
||
"other": "Other",
|
||
"guitar": "Guitar",
|
||
"piano": "Piano",
|
||
"lead": "Lead Vocals",
|
||
"leading": "Leading Vocals",
|
||
"backing": "Backing Vocals",
|
||
"male": "Male Vocals",
|
||
"female": "Female Vocals",
|
||
"instrumental": "Instrumental",
|
||
"dry_vocals": "Dry Vocals",
|
||
"reverb": "Reverb",
|
||
"residual": "Residual",
|
||
"restored": "Restored",
|
||
"enhanced": "Enhanced",
|
||
# SepACap a cappella stems
|
||
"alto": "Alto",
|
||
"soprano": "Soprano",
|
||
"tenor": "Tenor",
|
||
"finger_snap": "Finger Snap",
|
||
"vocal_percussion": "Vocal Percussion",
|
||
# SepReformer speech stems
|
||
"speaker_1": "Speaker 1",
|
||
"speaker_2": "Speaker 2",
|
||
}
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Project folder structure
|
||
# ---------------------------------------------------------------------------
|
||
|
||
PROJECT_SUBFOLDERS = {
|
||
"stems": "Stems", # Always generated - contains separated stems
|
||
"instrumental": "Instrumental", # Contains instrumental mixes (no vocals)
|
||
"static": "Static", # Contains static mixes (specific elements removed)
|
||
"dynamic": "Dynamic", # Contains dynamic mixes (volume adjusted)
|
||
"transcriptions": "Transcriptions", # Contains transcription outputs
|
||
"lrc": "LRC", # Contains synced lyrics files
|
||
}
|
||
|
||
# Subsonic integration
|
||
IMPORTS_DIR = MEDIA_DIR / "imports"
|
||
SUBSONIC_CONFIG_PATH = Path(os.environ.get("SUBSONIC_CONFIG_PATH", str(MEDIA_DIR / "subsonic.json")))
|
||
SETTINGS_PATH = Path(os.environ.get("SETTINGS_PATH", str(MEDIA_DIR / "settings.json")))
|
||
PIXABAY_CACHE_PATH = Path(os.environ.get("PIXABAY_CACHE_PATH", str(MEDIA_DIR / "pixabay_video_cache.json")))
|
||
|
||
# Lyric transcription (Whisper models)
|
||
WHISPER_MODEL_DIR = Path(os.environ.get("WHISPER_MODEL_DIR", "/opt/whisper_models"))
|
||
DEFAULT_WHISPER_MODEL = os.environ.get("DEFAULT_WHISPER_MODEL", "medium")
|
||
|
||
# Lyric transcription (Parakeet self-hosted runtime)
|
||
PARAKEET_ENABLED: bool = os.environ.get("PARAKEET_ENABLED", "false").lower() == "true"
|
||
PARAKEET_SIDECAR_URL: str = os.environ.get("PARAKEET_SIDECAR_URL", "").strip()
|
||
PARAKEET_HEALTH_PATH: str = os.environ.get("PARAKEET_HEALTH_PATH", "/health").strip() or "/health"
|
||
PARAKEET_TRANSCRIBE_PATH: str = os.environ.get("PARAKEET_TRANSCRIBE_PATH", "/transcribe").strip() or "/transcribe"
|
||
PARAKEET_TIMEOUT_SEC: float = float(os.environ.get("PARAKEET_TIMEOUT_SEC", "180"))
|
||
DEFAULT_PARAKEET_MODEL = os.environ.get(
|
||
"DEFAULT_PARAKEET_MODEL",
|
||
"parakeet-1.1b-rnnt-multilingual-asr",
|
||
)
|
||
|
||
# Lyric line segmentation strategy
|
||
# - legacy: current punctuation/uppercase/max-word heuristics
|
||
# - karaoke_processor: always use karaoke-lyrics-processor splitter
|
||
# - auto: karaoke-lyrics-processor with safe fallback to legacy heuristics
|
||
LYRIC_SEGMENTER: str = os.environ.get("STEMENGINE_LYRIC_SEGMENTER", "auto").strip().lower() or "auto"
|
||
|
||
try:
|
||
LYRIC_MAX_LINE_LENGTH: int = max(8, int(os.environ.get("STEMENGINE_LYRIC_MAX_LINE_LENGTH", "36")))
|
||
except ValueError:
|
||
LYRIC_MAX_LINE_LENGTH = 36
|
||
|
||
# Create directories on import
|
||
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
||
JOBS_DIR.mkdir(parents=True, exist_ok=True)
|
||
PROJECTS_DIR.mkdir(parents=True, exist_ok=True)
|
||
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||
LRC_SYNC_DIR.mkdir(parents=True, exist_ok=True)
|
||
IMPORTS_DIR.mkdir(parents=True, exist_ok=True)
|