Files
stemengine_old/backend/services/project_reprocess_service.py
T

297 lines
10 KiB
Python

from __future__ import annotations
from pathlib import Path
from typing import Optional
from .. import job_manager as jm
from ..config import ALL_MODEL_IDS, STATIC_OPTIONS, TRANSCRIBE_MODES
from ..repositories import jobs as jobs_repo
from ..repositories import projects as projects_repo
from .project_service_common import (
ProjectOperationError,
_find_stem_path,
_infer_output_format_from_paths,
_static_label,
_stem_files_from_sep_job,
project_output_dir,
)
def enqueue_stem_reprocess(project: dict, stem_name: str, model: str, output_format: str = "mp3") -> dict:
"""Queue re-separation of a single project stem with a chosen model."""
from .separator_service import run_separation
if model not in ALL_MODEL_IDS:
raise ProjectOperationError(400, f"Unknown model: {model}")
sep_job = jobs_repo.get(project["sep_job_id"])
if sep_job is None:
raise ProjectOperationError(404, "Separation job not found")
stem_path = _find_stem_path(_stem_files_from_sep_job(sep_job), stem_name)
if stem_path is None:
raise ProjectOperationError(404, f"Stem not found: {stem_name}")
stems_dir = Path(stem_path).parent
job_id = jm.create_job("separate")
jm.submit_gpu(
run_separation,
job_id,
stem_path,
model,
output_format,
preset_type="stems_only",
output_dir_override=stems_dir,
parent_job_id=project["sep_job_id"],
)
return {"job_id": job_id}
def enqueue_project_transcription(project: dict, stem_name: str, mode: str) -> dict:
"""Queue stem transcription for a project and persist the stem→job link."""
valid_modes = {mode_entry["id"] for mode_entry in TRANSCRIBE_MODES}
if mode not in valid_modes:
raise ProjectOperationError(400, f"Unknown transcribe mode: {mode}")
sep_job = jobs_repo.get(project["sep_job_id"])
if sep_job is None:
raise ProjectOperationError(404, "Separation job not found")
stem_path = _find_stem_path(_stem_files_from_sep_job(sep_job), stem_name)
if stem_path is None:
raise ProjectOperationError(404, f"Stem not found: {stem_name}")
from .transcriber_service import run_transcription
job_id = jm.create_job("transcribe")
projects_repo.set_transcription_job(project["id"], stem_name, job_id)
jm.submit_gpu(run_transcription, job_id, stem_path, mode)
return {"job_id": job_id}
def enqueue_project_abc_conversion(project: dict, stem_name: str, song_title: Optional[str]) -> dict:
"""Queue ABC conversion from a completed stem transcription job."""
trans_job_id = (project.get("trans_jobs") or {}).get(stem_name)
if not trans_job_id:
raise ProjectOperationError(400, f"No transcription job for stem '{stem_name}'")
trans_job = jobs_repo.get(trans_job_id)
if trans_job is None:
raise ProjectOperationError(404, "Transcription job not found")
if trans_job.get("status") != "done":
raise ProjectOperationError(400, "Transcription job has not completed successfully")
midi_paths = [
file_entry["path"]
for file_entry in trans_job.get("files", [])
if Path(file_entry.get("path", "")).suffix.lower() == ".mid"
and Path(file_entry.get("path", "")).exists()
]
if not midi_paths:
raise ProjectOperationError(404, "No MIDI files found in transcription job")
from .abc_service import run_abc_conversion
title = (song_title or "").strip() or project["name"]
job_id = jm.create_job("abc-convert")
projects_repo.set_abc_job(project["id"], stem_name, job_id)
jm.submit(run_abc_conversion, job_id, midi_paths, title)
return {"job_id": job_id}
def enqueue_project_mix(
project: dict,
preset_type: str,
static_option: Optional[str],
dynamic_levels: Optional[dict],
output_format: Optional[str],
) -> dict:
"""Queue static or dynamic mix generation for a project."""
if preset_type not in ("static", "dynamic"):
raise ProjectOperationError(400, "preset_type must be 'static' or 'dynamic'")
valid_static_options = {option["id"] for option in STATIC_OPTIONS}
if preset_type == "static" and static_option and static_option not in valid_static_options:
raise ProjectOperationError(400, f"Unknown static_option: {static_option}")
sep_job = jobs_repo.get(project["sep_job_id"])
if sep_job is None:
raise ProjectOperationError(404, "Separation job not found")
stem_files = _stem_files_from_sep_job(sep_job)
if not stem_files:
raise ProjectOperationError(404, "No stem files found in separation job")
resolved_output_format = output_format or _infer_output_format_from_paths(stem_files)
ext = "mp3" if resolved_output_format.startswith("mp3") else resolved_output_format
project_dir = project_output_dir(project)
if preset_type == "static":
subfolder = "Static"
suffix = _static_label(static_option)
else:
subfolder = "Dynamic"
suffix = "Dynamic Mix"
mix_dir = project_dir / subfolder
mix_dir.mkdir(parents=True, exist_ok=True)
output_path = str(mix_dir / f"{project['name']} - {suffix}.{ext}")
from .mix_service import run_mix
job_id = jm.create_job("mix")
projects_repo.update(project["id"], mix_job_id=job_id)
jm.submit(
run_mix,
job_id,
stem_files,
preset_type,
output_path,
resolved_output_format,
static_option,
{str(key): float(value) for key, value in (dynamic_levels or {}).items()},
)
return {"job_id": job_id}
def enqueue_project_lrc_alignment(
project: dict,
stem_name: str,
lyrics_source: str,
lyrics_text: Optional[str],
output_filename: Optional[str],
style_options: Optional[dict],
timing_precision: str,
lrc_power_preset: str,
) -> dict:
"""Queue lyrics alignment for a project stem and persist lrc stem linkage."""
if lyrics_source not in ("auto", "tag", "text"):
raise ProjectOperationError(400, f"Invalid lyrics_source: {lyrics_source}")
if lyrics_source == "text" and not (lyrics_text or "").strip():
raise ProjectOperationError(400, "lyrics_text required when lyrics_source='text'")
sep_job = jobs_repo.get(project["sep_job_id"])
if sep_job is None:
raise ProjectOperationError(404, "Separation job not found")
source_file = next(
(file_entry for file_entry in sep_job.get("files", []) if file_entry.get("type") == "source"),
None,
)
if source_file is None:
raise ProjectOperationError(404, "Source audio file not found in project")
base_name = Path(source_file["name"]).stem
resolved_output_filename = (output_filename or "").strip() or f"{base_name}.elrc"
stem_path = _find_stem_path(_stem_files_from_sep_job(sep_job), stem_name)
if stem_path and Path(stem_path).exists():
audio_path = stem_path
is_vocals = True
else:
audio_path = source_file["path"]
is_vocals = False
from .lrc_service import run_lrc_alignment
project_dir = project_output_dir(project)
job_id = jm.create_job("lrc_align")
projects_repo.set_lrc_job(project["id"], stem_name, job_id)
jm.submit_transcription(
job_id,
run_lrc_alignment,
job_id,
audio_path,
lyrics_source=lyrics_source,
lyrics_content=lyrics_text,
output_filename=resolved_output_filename,
style_options=style_options,
is_vocals=is_vocals,
output_dir=str(project_dir),
timing_precision=timing_precision,
lrc_power_preset=lrc_power_preset,
)
return {"job_id": job_id}
def enqueue_project_lyric_transcription(
project: dict,
stem_name: str,
model: str,
language: Optional[str],
task: str,
output_formats: list,
initial_prompt: Optional[str],
temperature: float,
condition_on_previous_text: bool,
engine: str,
diarize: bool,
lrc_power_preset: str,
phoneme_align: bool,
lsync_align: bool,
two_pass: bool,
vocal_style: str,
explicit: bool,
lyrics_source: str,
existing_lyrics: Optional[str],
) -> dict:
"""Queue AI lyric transcription and persist project linkage for the output job."""
from .lyric_transcriber_service import WHISPER_MODELS, run_lyric_transcription
if model not in WHISPER_MODELS:
raise ProjectOperationError(400, f"Unknown model: {model}")
if task not in ("transcribe", "translate"):
raise ProjectOperationError(400, f"Invalid task: {task}")
sep_job = jobs_repo.get(project["sep_job_id"])
if sep_job is None:
raise ProjectOperationError(404, "Separation job not found")
stem_files = _stem_files_from_sep_job(sep_job)
if stem_name not in stem_files:
raise ProjectOperationError(404, f"Stem not found: {stem_name}")
stem_path = stem_files[stem_name]
output_dir = project_output_dir(project) / "Transcriptions"
output_dir.mkdir(parents=True, exist_ok=True)
resolved_lyrics = existing_lyrics
if resolved_lyrics is None and lyrics_source != "transcribe":
from .metadata_util import extract_lyrics_from_file
lyrics_data = extract_lyrics_from_file(stem_path)
if lyrics_data.get("plain"):
resolved_lyrics = lyrics_data.get("raw") or lyrics_data["plain"]
else:
raise ProjectOperationError(
422,
f"lyrics_source='{lyrics_source}' requested but no lyrics found in file tags or companion LRC.",
)
job_id = jm.create_job("lyric-transcribe")
projects_repo.set_lrc_job(project["id"], f"{stem_name}_whisper", job_id)
jm.submit_transcription(
job_id,
run_lyric_transcription,
job_id,
stem_path,
model=model,
language=language,
task=task,
output_formats=output_formats,
initial_prompt=initial_prompt,
temperature=temperature,
condition_on_previous_text=condition_on_previous_text,
output_dir=str(output_dir),
engine=engine,
diarize=diarize,
power_preset=lrc_power_preset,
phoneme_align=phoneme_align,
lsync_align=lsync_align,
two_pass=two_pass,
existing_lyrics=resolved_lyrics,
vocal_style=vocal_style,
explicit=explicit,
)
return {"job_id": job_id, "output_dir": str(output_dir)}