Files
stemengine_old/backend/config.py
T
2026-03-16 16:35:08 +09:00

372 lines
16 KiB
Python
Executable File

import os
from pathlib import Path
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()]
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")))
# 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"))
# 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_deux_becruily", "label": "MelBand RoFormer Deux Becruily", "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"},
]
# 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_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_vocals", "label": "Apollo Vocals (Enhancement)", "group": "Apollo"},
{"id": "apollo_edm", "label": "Apollo EDM", "group": "Apollo"},
{"id": "apollo_edm_big", "label": "Apollo EDM Big", "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",
"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",
},
# ── 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,
},
}
# Hardware
CPU_SEPARATION = os.environ.get("CPU_SEPARATION", "true").lower() != "false"
# Limits
MAX_UPLOAD_BYTES = int(os.environ.get("MAX_UPLOAD_MB", "500")) * 1024 * 1024
POLL_INTERVAL_SECONDS = 5.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",
"backing": "Backing Vocals",
"male": "Male Vocals",
"female": "Female Vocals",
"instrumental": "Instrumental",
"dry_vocals": "Dry Vocals",
"reverb": "Reverb",
# 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 = MEDIA_DIR / "subsonic.json"
SETTINGS_PATH = MEDIA_DIR / "settings.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", "large-v2")
# 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",
)
# 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)