diff --git a/pyproject.toml b/pyproject.toml index f0c8e99..f6f7862 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ dependencies = [ "pyphen>=0.14.0", "torchcrepe>=0.0.23", "faster-whisper>=1.0.0", + "av==18.1.0", "scipy>=1.12.0", "jiwer>=3.0.0", "beat-this>=1.1.0", diff --git a/src/unmix/config.py b/src/unmix/config.py index e78be16..9c3f084 100644 --- a/src/unmix/config.py +++ b/src/unmix/config.py @@ -6,6 +6,20 @@ from pathlib import Path from pydantic import BaseModel, Field +def _detect_default_device() -> str: + env_dev = os.getenv("UNMIX_DEFAULT_DEVICE") + if env_dev: + return env_dev + try: + import torch + + if torch.cuda.is_available(): + return "cuda:0" + except Exception: + pass + return "cpu" + + class Config(BaseModel): """Runtime configuration for pipeline stages, devices, and directories.""" @@ -15,23 +29,32 @@ class Config(BaseModel): ).expanduser() ) - default_device: str = Field(default_factory=lambda: os.getenv("UNMIX_DEFAULT_DEVICE", "cuda:0")) + default_device: str = Field(default_factory=_detect_default_device) - stage_devices: dict[str, str] = Field( - default_factory=lambda: { - "s1_extract": os.getenv("UNMIX_DEVICE_S1", "cpu"), - "s2_stems": os.getenv("UNMIX_DEVICE_S2", "cuda:0"), - "s3_lyrics": os.getenv("UNMIX_DEVICE_S3", "cuda:0"), - "s4_pitch": os.getenv("UNMIX_DEVICE_S4", "cuda:0"), - "s5_tempo": os.getenv("UNMIX_DEVICE_S5", "cpu"), - "s6_chords": os.getenv("UNMIX_DEVICE_S6", "cpu"), - "s7_strumming": os.getenv("UNMIX_DEVICE_S7", "cpu"), - } - ) + stage_devices: dict[str, str] = Field(default_factory=dict) def get_device(self, stage: str) -> str: - """Get target compute device for a specific stage.""" - return self.stage_devices.get(stage, self.default_device) + """Get target compute device for a specific stage, with automatic CPU fallback.""" + stage_env_key = f"UNMIX_DEVICE_{stage.upper()}" + # Also check common shorthand like UNMIX_DEVICE_S2 + stage_prefix = stage.split("_")[0].upper() + shorthand_key = f"UNMIX_DEVICE_{stage_prefix}" + + dev = ( + os.getenv(stage_env_key) or os.getenv(shorthand_key) or self.stage_devices.get(stage) or self.default_device + ) + + # Fallback if CUDA requested but not available + if dev.startswith("cuda"): + try: + import torch + + if not torch.cuda.is_available(): + return "cpu" + except Exception: + return "cpu" + + return dev def get_models_dir(self) -> Path: """Ensure and return the model cache directory.""" diff --git a/src/unmix/pipeline/runner.py b/src/unmix/pipeline/runner.py index d74a90a..f1be1d5 100644 --- a/src/unmix/pipeline/runner.py +++ b/src/unmix/pipeline/runner.py @@ -59,13 +59,17 @@ class PipelineContext: if self.meta_file.exists(): with open(self.meta_file, encoding="utf-8") as f: data = json.load(f) - return MetaDocument.model_validate(data) + meta = MetaDocument.model_validate(data) + if self.lang and not meta.language: + meta.language = self.lang + return meta source_hash = compute_file_sha256(self.source_file) if self.source_file.exists() else "" size_bytes = self.source_file.stat().st_size if self.source_file.exists() else 0 return MetaDocument( duration_sec=0.0, + language=self.lang, source=SourceFileInfo( filename=self.source_file.name, path=str(self.source_file), @@ -259,6 +263,9 @@ class PipelineRunner: meta = ctx.load_or_init_meta() stages_to_run = self.get_stages_to_run(ctx) + # Clear previous failure entries for stages that are being executed in this run + for s in stages_to_run: + meta.stages_failed.pop(s.name, None) failed_stages: set[str] = set(meta.stages_failed.keys()) for stage in stages_to_run: diff --git a/src/unmix/pipeline/s1_extract.py b/src/unmix/pipeline/s1_extract.py index ef2dd99..dd9ec05 100644 --- a/src/unmix/pipeline/s1_extract.py +++ b/src/unmix/pipeline/s1_extract.py @@ -9,19 +9,18 @@ from typing import Any from unmix.pipeline.runner import PipelineContext, Stage -def get_audio_duration(file_path: Path) -> float: - """Get audio duration in seconds using ffprobe.""" +def get_audio_metadata(file_path: Path) -> dict[str, Any]: + """Get audio duration and metadata tags using ffprobe.""" ffprobe_path = shutil.which("ffprobe") if not ffprobe_path: - # Fallback to ffmpeg - return 0.0 + return {} cmd = [ ffprobe_path, "-v", "error", "-show_entries", - "format=duration", + "format=duration:format_tags=title,artist,album", "-of", "json", str(file_path), @@ -29,9 +28,23 @@ def get_audio_duration(file_path: Path) -> float: try: res = subprocess.run(cmd, capture_output=True, text=True, check=True) data = json.loads(res.stdout) - return float(data.get("format", {}).get("duration", 0.0)) + fmt = data.get("format", {}) + tags = fmt.get("tags", {}) + tags_lower = {k.lower(): v for k, v in tags.items()} + return { + "duration": float(fmt.get("duration", 0.0)), + "title": tags_lower.get("title"), + "artist": tags_lower.get("artist"), + "album": tags_lower.get("album"), + } except Exception: - return 0.0 + return {} + + +def get_audio_duration(file_path: Path) -> float: + """Get audio duration in seconds using ffprobe.""" + meta = get_audio_metadata(file_path) + return meta.get("duration", 0.0) class ExtractStage(Stage): @@ -90,9 +103,15 @@ class ExtractStage(Stage): if res.returncode != 0: raise RuntimeError(f"ffmpeg extraction failed: {res.stderr}") - # Update duration in meta document if available - duration = get_audio_duration(output_wav) - if duration > 0.0: - meta = ctx.load_or_init_meta() - meta.duration_sec = duration - ctx.save_meta(meta) + # Update metadata (duration and ID3/container tags) if available + meta_info = get_audio_metadata(ctx.source_file) or get_audio_metadata(output_wav) + meta = ctx.load_or_init_meta() + if meta_info.get("duration", 0.0) > 0.0: + meta.duration_sec = meta_info["duration"] + if not meta.title and meta_info.get("title"): + meta.title = meta_info["title"] + if not meta.artist and meta_info.get("artist"): + meta.artist = meta_info["artist"] + if not meta.album and meta_info.get("album"): + meta.album = meta_info["album"] + ctx.save_meta(meta) diff --git a/src/unmix/schema/meta.py b/src/unmix/schema/meta.py index 2950bf4..6f2b972 100644 --- a/src/unmix/schema/meta.py +++ b/src/unmix/schema/meta.py @@ -1,6 +1,6 @@ """Schema for meta.json.""" -from typing import Literal +from typing import Any, Literal from pydantic import BaseModel, ConfigDict, Field @@ -70,7 +70,7 @@ class ModelInfo(BaseModel): class MetaDocument(SongPackageBase): """Songpack metadata document (meta.json).""" - model_config = ConfigDict(extra="ignore") + model_config = ConfigDict(extra="ignore", populate_by_name=True) title: str | None = None artist: str | None = None @@ -78,7 +78,7 @@ class MetaDocument(SongPackageBase): year: int | None = None language: str | None = None duration_sec: float = Field(default=0.0, ge=0.0) - source: SourceFileInfo = Field(default_factory=SourceFileInfo) + source: SourceFileInfo = Field(default_factory=SourceFileInfo, alias="source_file") audio_ref: AudioRefInfo | None = None audio_offset_sec: float = 0.0 key: KeyInfo | str | None = None @@ -89,6 +89,7 @@ class MetaDocument(SongPackageBase): created_at: str | None = None stages_done: list[str] = Field(default_factory=list) stages_failed: dict[str, str] = Field(default_factory=dict) + reference_eval: dict[str, Any] | None = None # Backward compatibility helper @property diff --git a/uv.lock b/uv.lock index 4cc6c4f..2bb10e2 100644 --- a/uv.lock +++ b/uv.lock @@ -54,11 +54,6 @@ wheels = [ name = "av" version = "18.1.0" source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version < '3.12' and platform_machine == 'x86_64' and sys_platform == 'darwin'", - "python_full_version < '3.12' and sys_platform != 'darwin'", - "python_full_version < '3.12' and platform_machine != 'x86_64' and sys_platform == 'darwin'", -] sdist = { url = "https://files.pythonhosted.org/packages/8d/f4/f22114d30d3435e38c6af2b4870f37b864403dca6ae7af747a289ce0a18e/av-18.1.0.tar.gz", hash = "sha256:47bfc286e1bc9de7ab4681fc2b575cd2460a66919d31ffe1bd5aa54fae531a28", size = 4451061, upload-time = "2026-08-12T22:28:18.761Z" } wheels = [ { url = "https://files.pythonhosted.org/packages/05/d4/d7cdc8bff143c17a6d35924375ae28dd692cacde38700a7d419fde54f44a/av-18.1.0-cp311-abi3-macosx_11_0_x86_64.whl", hash = "sha256:ae75d8bb6467895ed1f8572ededf7ffa49eac07f6e483222f5d7d62a41d12f04", size = 22546147, upload-time = "2026-08-12T22:27:11.851Z" }, @@ -72,28 +67,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/4f/ad/a3135884c5753b09773176b97201ae602f67ad14206c395ff838d66bf9b0/av-18.1.0-cp311-abi3-win_arm64.whl", hash = "sha256:5509ec12aaa19fd6601de13cfa6f4cdad450da07982118510592875d970454d6", size = 20257584, upload-time = "2026-08-12T22:27:38.472Z" }, ] -[[package]] -name = "av" -version = "19.0.2" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version >= '3.12' and platform_machine == 'x86_64' and sys_platform == 'darwin'", - "python_full_version >= '3.12' and sys_platform != 'darwin'", - "python_full_version >= '3.12' and platform_machine != 'x86_64' and sys_platform == 'darwin'", -] -sdist = { url = "https://files.pythonhosted.org/packages/23/01/76b6bf563fe907597cf58f10d10168e93fe588b6db9f2658db87e87efa7e/av-19.0.2.tar.gz", hash = "sha256:bebbc52598886480f4d861c575dc2b020785d6c04e1308680ddcc545e1b94c66", size = 4277157, upload-time = "2026-10-21T03:45:30.799Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/4a/2f/f51f37820741134516e841053d5ecebf6350fbd451adcbff9ead96aa1244/av-19.0.2-cp312-abi3-macosx_11_0_x86_64.whl", hash = "sha256:f5f7bcf987567cd3f9b01f1c375c59bf59d8a5b98c9c5c154aeda7a11e0c5dce", size = 22627374, upload-time = "2026-10-21T03:44:28.296Z" }, - { url = "https://files.pythonhosted.org/packages/7c/42/067d0bcd2c07103f18cc23b510a8289dd80330e984c49d51ce0bafcee017/av-19.0.2-cp312-abi3-macosx_14_0_arm64.whl", hash = "sha256:b0dff6dfcc6ff9944fbbff15bc4ee2d90a2c1695d493bdea96e86f48823e9feb", size = 18556422, upload-time = "2026-10-21T03:44:31.267Z" }, - { url = "https://files.pythonhosted.org/packages/de/0c/448f89f76cf5e4da35dd6294a1e8155b75daa8839f2f2fa353775855d152/av-19.0.2-cp312-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:04c8e412642c652cb2b1997a9cb2114546ee7d894600c631f4be8ceb26a0d0d2", size = 32683287, upload-time = "2026-10-21T03:44:34.588Z" }, - { url = "https://files.pythonhosted.org/packages/e1/7f/9f1237feb894afa472ef834aa1fbfe4d6398f28dc7e86a1b418650204c36/av-19.0.2-cp312-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:9c1da7aad0fa6c13b4a5c1e7985be0d13f8526a96284573acaf3d55ead6d3fe8", size = 34990159, upload-time = "2026-10-21T03:44:37.637Z" }, - { url = "https://files.pythonhosted.org/packages/f1/b5/1bde0e54e525d88ad8f5becae657f8142543fd3be9d10b72141f041ca868/av-19.0.2-cp312-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:9d4f9f78c0ce1f20deb72626fdb3c723757f61a8cd6fbb7dd44d9401884a91fd", size = 44422922, upload-time = "2026-10-21T03:44:41.847Z" }, - { url = "https://files.pythonhosted.org/packages/08/21/65997929fa4dd7c20f64e742aa87205a8d65485718a2ab51633756078e83/av-19.0.2-cp312-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:dfe7af5aa8243afd5d4778f85ad1a7c948e37fad272869da1f8f93f8d57fa7ad", size = 33754699, upload-time = "2026-10-21T03:44:45.772Z" }, - { url = "https://files.pythonhosted.org/packages/30/44/4de8c0ff5a5e25ceb0841efc0a4a7530a22d61800bc0cc19a1d03aa04358/av-19.0.2-cp312-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:44d3faae747cf04a33f39db8ae983ff3d93adc864ee663bc186b25b686803c3e", size = 36014845, upload-time = "2026-10-21T03:44:49.64Z" }, - { url = "https://files.pythonhosted.org/packages/32/5a/743a83666ded197df8b9957e2800bef9a39d52b23be2ca211bbd14b5fb22/av-19.0.2-cp312-abi3-win_amd64.whl", hash = "sha256:3ce5a1d7b916d13c16ac58f1756f0fee4af2d53f02bc1403e18550b50d375a22", size = 28150949, upload-time = "2026-10-21T03:44:53.434Z" }, - { url = "https://files.pythonhosted.org/packages/42/fe/e79d786628d9c0428f72214dbe4b90ee0c625e04d552a604d5235ff35515/av-19.0.2-cp312-abi3-win_arm64.whl", hash = "sha256:79a277b077e3768a0ea9f881ca0b3bc96d4135a8d3a7a579938adb58718d0dfd", size = 20708023, upload-time = "2026-10-21T03:44:56.435Z" }, -] - [[package]] name = "beat-this" version = "1.1.0" @@ -402,8 +375,7 @@ name = "faster-whisper" version = "1.2.1" source = { registry = "https://pypi.org/simple" } dependencies = [ - { name = "av", version = "18.1.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" }, - { name = "av", version = "19.0.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" }, + { name = "av" }, { name = "ctranslate2" }, { name = "huggingface-hub" }, { name = "onnxruntime" }, @@ -1665,6 +1637,7 @@ name = "unmix" version = "0.1.0" source = { editable = "." } dependencies = [ + { name = "av" }, { name = "beat-this" }, { name = "chord-extractor" }, { name = "demucs", version = "4.0.1", source = { registry = "https://pypi.org/simple" }, marker = "platform_machine == 'x86_64' and sys_platform == 'darwin'" }, @@ -1694,6 +1667,7 @@ dev = [ [package.metadata] requires-dist = [ + { name = "av", specifier = "==18.1.0" }, { name = "beat-this", specifier = ">=1.1.0" }, { name = "chord-extractor", specifier = ">=0.1.3" }, { name = "demucs", specifier = ">=4.0.0" },