128 lines
4.0 KiB
Python
Executable File
128 lines
4.0 KiB
Python
Executable File
"""Pipeline orchestrator composed from explicit separation/transcription/ABC stages."""
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
from .. import job_manager as jm
|
|
from ..config import PROJECTS_DIR
|
|
from .metadata_util import read_metadata
|
|
from .pipeline_stages import run_abc_stage, run_separation_stage, run_transcription_stage
|
|
from .project_namer import make_project_dirs, project_name
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def run_pipeline(
|
|
job_id: str,
|
|
input_path: str,
|
|
sep_model: str,
|
|
sep_format: str,
|
|
transcribe_mode: str,
|
|
song_title: str,
|
|
project_name_override: str = None,
|
|
instrument_models: dict | None = None,
|
|
lyric_engine: str | None = None,
|
|
lyric_model: str | None = None,
|
|
):
|
|
"""
|
|
Full pipeline:
|
|
1. Stem separation (Demucs CLI or BS-RoFormer Python API)
|
|
2. MIDI transcription (basic-pitch) for each non-drum stem
|
|
3. ABC notation conversion
|
|
|
|
Creates organized project folder structure under MEDIA_DIR.
|
|
"""
|
|
log.debug("[%s] run_pipeline start — model=%s format=%s transcribe_mode=%s input=%s",
|
|
job_id, sep_model, sep_format, transcribe_mode, input_path)
|
|
|
|
if lyric_engine or lyric_model:
|
|
jm.update_job(
|
|
job_id,
|
|
progress=(
|
|
"Lyric engine/model options received "
|
|
f"(engine={lyric_engine or 'default'}, model={lyric_model or 'default'})."
|
|
),
|
|
)
|
|
|
|
# Read metadata for project naming
|
|
jm.update_job(job_id, status=jm.JobStatus.RUNNING, progress="Reading metadata...")
|
|
metadata = read_metadata(input_path)
|
|
|
|
# Determine project name
|
|
if project_name_override:
|
|
proj_name = project_name_override
|
|
else:
|
|
filename_stem = Path(input_path).stem
|
|
proj_name = project_name(metadata.get('artist', ''), metadata.get('title', ''), filename_stem)
|
|
|
|
# Create project directories
|
|
jm.update_job(job_id, progress=f"Creating project: {proj_name}...")
|
|
dirs = make_project_dirs(
|
|
proj_name, PROJECTS_DIR,
|
|
{'instrumental': False, 'static': False, 'dynamic': False}
|
|
)
|
|
|
|
project_dir = dirs['project']
|
|
stems_dir = dirs['stems']
|
|
transcriptions_dir = project_dir / "Transcriptions"
|
|
transcriptions_dir.mkdir(exist_ok=True)
|
|
|
|
all_files = []
|
|
|
|
try:
|
|
separation_result = run_separation_stage(
|
|
job_id,
|
|
input_path,
|
|
sep_model,
|
|
sep_format,
|
|
stems_dir,
|
|
proj_name,
|
|
instrument_models=instrument_models,
|
|
)
|
|
except Exception as exc:
|
|
log.debug("[%s] pipeline separation failed — %s: %s", job_id, type(exc).__name__, exc)
|
|
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=str(exc), progress="Separation failed.")
|
|
return
|
|
|
|
if separation_result is None:
|
|
return # cancelled inside sub-function
|
|
|
|
stem_files, stem_cleanup = separation_result
|
|
|
|
midi_files, stage_files = run_transcription_stage(
|
|
job_id,
|
|
proj_name,
|
|
input_path,
|
|
stem_files,
|
|
transcribe_mode,
|
|
transcriptions_dir,
|
|
)
|
|
all_files.extend(stage_files)
|
|
if not midi_files and jm.should_cancel(job_id):
|
|
return
|
|
|
|
run_abc_stage(job_id, midi_files, song_title or proj_name, all_files)
|
|
|
|
log.debug("[%s] pipeline complete — project=%s stems=%d files=%d",
|
|
job_id, proj_name, len(stem_files), len(all_files))
|
|
jm.update_job(
|
|
job_id, status=jm.JobStatus.DONE,
|
|
progress="Pipeline complete.",
|
|
files=all_files,
|
|
result={
|
|
"project_name": proj_name,
|
|
"project_path": str(project_dir),
|
|
"stems_dir": str(stems_dir),
|
|
"transcriptions_dir": str(transcriptions_dir),
|
|
"instrument_models": {
|
|
"requested": instrument_models or {},
|
|
},
|
|
"stem_cleanup": stem_cleanup,
|
|
"lyric": {
|
|
"engine": lyric_engine,
|
|
"model": lyric_model,
|
|
},
|
|
}
|
|
)
|
|
jm.find_or_create_project(proj_name, job_id)
|