Spaces:
Running
Running
Download studio/library.py from 2Btainment/2b-studio: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/spaces/2Btainment/2b-studio/resolve/main/studio/library.py
- Command line
-
hf download hf://spaces/2Btainment/2b-studio/studio/library.py
-
curl -L -o library.py https://huggingface.co/spaces/2Btainment/2b-studio/resolve/main/studio/library.py
3.45 kB
| from __future__ import annotations | |
| import re | |
| from pathlib import Path | |
| from uuid import uuid4 | |
| from studio.audio_analysis import analyze_wav | |
| from studio.audio_io import ensure_browser_wav, resolve_audio_path | |
| from studio.state import StudioSession, Track | |
| ROLE_LABELS = { | |
| "draft": "Draft", | |
| "upload": "Upload", | |
| "clean": "Clean", | |
| "residual": "Residual", | |
| "master": "Master", | |
| "studio-edit": "Studio", | |
| "mix": "Mix", | |
| "export": "Export", | |
| } | |
| def add_track(session: StudioSession, track: Track) -> StudioSession: | |
| session.tracks.append(track) | |
| session.master.source_track_id = track.id | |
| return session | |
| def library_choices(session: StudioSession) -> list[tuple[str, str]]: | |
| counters: dict[str, int] = {} | |
| choices: list[tuple[str, str]] = [] | |
| for track in session.tracks: | |
| counters[track.role] = counters.get(track.role, 0) + 1 | |
| role = ROLE_LABELS[track.role] | |
| number = counters[track.role] | |
| filename = Path(track.path).name | |
| choices.append((f"▶ {role} {number:02d} · {filename}", track.id)) | |
| return choices | |
| def select_track(session: StudioSession, track_id: str) -> Track: | |
| track = session.track_by_id(track_id) | |
| if track is None: | |
| raise KeyError("Der gewählte Track ist nicht mehr in dieser Session verfügbar.") | |
| session.master.source_track_id = track.id | |
| return track | |
| def delete_track(session: StudioSession, track_id: str) -> Track: | |
| track = session.track_by_id(track_id) | |
| if track is None: | |
| raise KeyError("Der gewählte Track ist nicht mehr in dieser Session verfügbar.") | |
| session.tracks = [item for item in session.tracks if item.id != track_id] | |
| session.master.source_track_id = session.tracks[-1].id if session.tracks else None | |
| track_path = Path(track.path).resolve() | |
| runtime_dir = Path(session.runtime_dir).resolve() | |
| path_is_still_used = any(Path(item.path).resolve() == track_path for item in session.tracks) | |
| if track_path.is_relative_to(runtime_dir) and not path_is_still_used: | |
| track_path.unlink(missing_ok=True) | |
| return track | |
| def import_wav(session: StudioSession, source_path: str | Path) -> Track: | |
| source = resolve_audio_path(source_path) | |
| if not source.is_file(): | |
| raise ValueError("Die ausgewählte Audiodatei wurde nicht gefunden.") | |
| if source.suffix.casefold() != ".wav": | |
| raise ValueError("Bitte eine WAV-Datei importieren.") | |
| if source.stat().st_size > 250 * 1024 * 1024: | |
| raise ValueError("Die WAV-Datei darf maximal 250 MB groß sein.") | |
| safe_stem = re.sub(r"[^a-zA-Z0-9_-]+", "-", source.stem).strip("-") or "audio" | |
| destination = Path(session.runtime_dir) / f"upload-{uuid4().hex[:8]}-{safe_stem}.wav" | |
| ensure_browser_wav(source, destination) | |
| try: | |
| analysis = analyze_wav(destination) | |
| except Exception: | |
| destination.unlink(missing_ok=True) | |
| raise | |
| track = Track( | |
| id=uuid4().hex, | |
| role="upload", | |
| path=str(destination), | |
| engine="local-upload", | |
| duration_s=analysis.duration_s, | |
| sample_rate=analysis.sample_rate, | |
| metrics={"analysis": analysis.as_dict()}, | |
| metadata={ | |
| "original_name": source.name, | |
| "genre_style_instruments": "", | |
| "lyrics": "[Instrumental]", | |
| "negative": "", | |
| "model_id": "minimax-music3", | |
| }, | |
| ) | |
| add_track(session, track) | |
| return track | |