Spaces:
Running
Running
2B Studio v2: Toxic-Studio-Redesign, Raum-Leiste, Vorlagen-Bibliothek (30), Verlauf mit Wellenformen, Modell-Status-Chips; Fix MiniMax _studio_state, Paused-Status
d360571 verified Download studio/adapters/space_master.py from 2Btainment/2b-studio: direct link, hf CLI and curl.
- Browser
- Download file 5.9 kB
-
https://huggingface.co/spaces/2Btainment/2b-studio/resolve/main/studio/adapters/space_master.py
- Command line
-
hf download hf://spaces/2Btainment/2b-studio/studio/adapters/space_master.py
-
curl -L -o space_master.py https://huggingface.co/spaces/2Btainment/2b-studio/resolve/main/studio/adapters/space_master.py
5.9 kB
| """Space-Mastering-Adapter: ONUS-Doktrin direkt im HF-Space (in-process). | |
| Liefert dieselbe OnusResult-Struktur wie der lokale OnusAdapter, damit die | |
| Master-UI unverändert funktioniert. ONUS bleibt lokal für die Premium-Kette. | |
| """ | |
| from __future__ import annotations | |
| from pathlib import Path | |
| from typing import Any | |
| from uuid import uuid4 | |
| from studio.adapters.base import AdapterError, InvalidAudioError | |
| from studio.adapters.onus import OnusRequest, OnusResult | |
| from studio.audio_analysis import analyze_wav | |
| from studio.audio_io import ensure_browser_wav | |
| from studio.mastering import ( | |
| apply_master, | |
| apply_purity, | |
| build_report_markdown, | |
| estimate_lufs, | |
| read_wav, | |
| write_wav, | |
| ) | |
| from studio.state import Track | |
| class SpaceMasterAdapter: | |
| """In-process Mastering-Engine nach ONUS-Doktrin (soft_keep_air, Profile, QA).""" | |
| def __init__(self, runtime_dir: Path) -> None: | |
| self.runtime_dir = runtime_dir.resolve() | |
| self.runtime_dir.mkdir(parents=True, exist_ok=True) | |
| def health(self) -> bool: | |
| return True # immer verfügbar (numpy-basiert, keine externe CLI) | |
| def _track_from_path( | |
| self, | |
| path: Path, | |
| *, | |
| role: str, | |
| parent_id: str, | |
| metadata: dict[str, Any], | |
| ) -> Track: | |
| if not path.is_file(): | |
| raise InvalidAudioError(f"Mastering-Ausgabe fehlt: {path.name}") | |
| playback = self.runtime_dir / f"{role}-{uuid4().hex[:10]}.wav" | |
| ensure_browser_wav(path, playback) | |
| analysis = analyze_wav(playback) | |
| return Track( | |
| id=uuid4().hex, | |
| role=role, # type: ignore[arg-type] | |
| path=str(playback), | |
| engine="space-master", | |
| duration_s=analysis.duration_s, | |
| sample_rate=analysis.sample_rate, | |
| parent_id=parent_id, | |
| metrics={"analysis": analysis.as_dict()}, | |
| metadata=metadata, | |
| ) | |
| def process(self, source: Track, request: OnusRequest) -> OnusResult: | |
| if request.purity_mode == "strict" and not request.strict_approved: | |
| raise AdapterError("Strict Purity benötigt eine ausdrückliche menschliche Bestätigung.") | |
| source_path = Path(source.path).resolve() | |
| if not source_path.is_file(): | |
| raise InvalidAudioError("Der Master-Quelltrack fehlt.") | |
| try: | |
| data, sample_rate = read_wav(source_path) | |
| except Exception as error: | |
| raise InvalidAudioError(f"Mastering konnte die WAV nicht lesen: {error}") from error | |
| before_lufs = estimate_lufs(data, sample_rate) | |
| analysis: dict[str, Any] = { | |
| "loudness": {"integrated_lufs": round(before_lufs, 2)}, | |
| "sample_rate": sample_rate, | |
| } | |
| if request.analyze_only: | |
| return OnusResult( | |
| session_dir=str(self.runtime_dir), | |
| analysis=analysis, | |
| original_unchanged=True, | |
| ) | |
| session_dir = self.runtime_dir / f"space-master-{uuid4().hex[:12]}" | |
| session_dir.mkdir(parents=True, exist_ok=False) | |
| common_metadata = { | |
| "mastering_engine": "space-master", | |
| "session_dir": str(session_dir), | |
| "profile": request.profile, | |
| "purity_mode": request.purity_mode, | |
| "keep_air_db": request.keep_air_db, | |
| "margin": request.margin, | |
| "human_approval": "pending", | |
| } | |
| purity_metrics: dict[str, Any] | None = None | |
| tracks: list[Track] = [] | |
| master_input = data | |
| if request.purity_mode in {"soft_keep_air", "strict"}: | |
| strict = request.purity_mode == "strict" | |
| clean, residual_audio, purity_metrics = apply_purity( | |
| data, | |
| sample_rate, | |
| keep_air_db=request.keep_air_db, | |
| margin=request.margin, | |
| strict=strict, | |
| ) | |
| clean_path = session_dir / "clean_final.wav" | |
| residual_path = session_dir / "residual.wav" | |
| write_wav(clean_path, clean, sample_rate) | |
| write_wav(residual_path, residual_audio, sample_rate) | |
| purity_metrics["outputs"] = { | |
| "clean_final": str(clean_path), | |
| "residual": str(residual_path), | |
| } | |
| tracks.extend( | |
| [ | |
| self._track_from_path( | |
| clean_path, role="clean", parent_id=source.id, metadata=common_metadata | |
| ), | |
| self._track_from_path( | |
| residual_path, role="residual", parent_id=source.id, metadata=common_metadata | |
| ), | |
| ] | |
| ) | |
| master_input = clean | |
| mastered, master_metrics = apply_master(master_input, sample_rate, request.profile) | |
| master_path = session_dir / "master.wav" | |
| write_wav(master_path, mastered, sample_rate) | |
| master_metrics["output"] = str(master_path) | |
| master_parent = tracks[0].id if tracks else source.id | |
| tracks.append( | |
| self._track_from_path( | |
| master_path, role="master", parent_id=master_parent, metadata=common_metadata | |
| ) | |
| ) | |
| report_markdown = build_report_markdown( | |
| source_path=source_path, | |
| profile=request.profile, | |
| purity_mode=request.purity_mode, | |
| analysis=analysis, | |
| purity=purity_metrics, | |
| master=master_metrics, | |
| output_paths=[master_path] | |
| + ([clean_path, residual_path] if purity_metrics else []), | |
| ) | |
| return OnusResult( | |
| session_dir=str(session_dir), | |
| analysis=analysis, | |
| purity=purity_metrics, | |
| master=master_metrics, | |
| report_markdown=report_markdown, | |
| tracks=tracks, | |
| original_unchanged=True, | |
| ) | |