Files
stemengine_old/backend/services_root_backup/pipeline_service.py
T
2026-03-03 14:52:19 +09:00

311 lines
10 KiB
Python
Executable File

"""
Full pipeline: separate → transcribe → abc-convert.
Supports both Demucs (CLI) and BS-RoFormer (Python API) for the separation stage.
Uses project-based folder structure for organized output.
"""
import sys
import subprocess
import time
import shutil
from pathlib import Path
from .. import job_manager as jm
from ..config import PROJECTS_DIR, JOBS_DIR, CPU_SEPARATION, TRANSCRIBE_PRESETS
from .metadata_util import copy_metadata, read_metadata
from .project_namer import project_name, make_project_dirs
from .separator_service import BSR_PREFIX, BSR_STEM_MODE
from . import abc_generator
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,
):
"""
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.
"""
# 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 = []
# Normalise mp3_320 → mp3 with explicit bitrate
mp3_bitrate = '320k' if sep_format == 'mp3_320' else '256k'
normalised_format = 'mp3' if sep_format == 'mp3_320' else sep_format
# ------------------------------------------------------------------
# Stage 1: Separation
# ------------------------------------------------------------------
jm.update_job(job_id, progress="[1/3] Separating stems...")
try:
if sep_model.startswith(BSR_PREFIX):
stem_files = _separate_bsroformer(
job_id, input_path, sep_model, normalised_format, stems_dir, mp3_bitrate
)
else:
stem_files = _separate_demucs(
job_id, input_path, sep_model, sep_format, stems_dir
)
except Exception as exc:
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=str(exc), progress="Separation failed.")
return
if stem_files is None:
return # cancelled inside sub-function
# Record stems with proper metadata
for stem_name, path in sorted(stem_files.items()):
copy_metadata(input_path, path, stem_label=stem_name.capitalize())
all_files.append({
"name": Path(path).name,
"path": path,
"stage": "separation",
"stem": stem_name
})
# ------------------------------------------------------------------
# Stage 2: Transcription (skip drum stems)
# ------------------------------------------------------------------
from basic_pitch.inference import predict
from basic_pitch import ICASSP_2022_MODEL_PATH
jm.update_job(job_id, progress="[2/3] Transcribing stems to MIDI...")
midi_files = []
transcribe_paths = [
(name, path) for name, path in stem_files.items()
if 'drums' not in name.lower()
]
params = TRANSCRIBE_PRESETS.get(transcribe_mode, TRANSCRIBE_PRESETS["polyphonic"])
for stem_name, stem_path in transcribe_paths:
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
jm.update_job(job_id, progress=f"[2/3] Transcribing: {stem_name}...")
try:
_, midi_data, _ = predict(stem_path, ICASSP_2022_MODEL_PATH, **params)
output_name = f"{proj_name} - {stem_name.capitalize()} ({transcribe_mode}).mid"
dest = transcriptions_dir / output_name
midi_data.write(str(dest))
all_files.append({
"name": dest.name,
"path": str(dest),
"stage": "transcription"
})
midi_files.append(str(dest))
except Exception as exc:
jm.update_job(job_id, progress=f"[2/3] Error on {stem_name}: {exc}")
# ------------------------------------------------------------------
# Stage 3: ABC conversion
# ------------------------------------------------------------------
if midi_files:
jm.update_job(job_id, progress="[3/3] Converting MIDI to ABC notation...")
try:
abc_results = abc_generator.convert_midi_to_abc(midi_files, song_title or proj_name)
if abc_results:
for part_name, abc_text in abc_results.items():
safe = part_name.replace("/", "_").replace("\\", "_")
dest = transcriptions_dir / f"{safe}.abc"
dest.write_text(abc_text, encoding="utf-8")
all_files.append({
"name": dest.name,
"path": str(dest),
"stage": "abc"
})
except Exception as exc:
jm.update_job(job_id, progress=f"[3/3] ABC conversion error: {exc}")
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),
}
)
jm.find_or_create_project(proj_name, job_id)
# ---------------------------------------------------------------------------
# Separation helpers
# ---------------------------------------------------------------------------
def _separate_demucs(
job_id: str, input_path: str, model: str, output_format: str, stems_dir: Path
) -> dict | None:
"""
Run Demucs CLI; returns dict mapping stem names to file paths, or None if cancelled.
"""
ext = output_format.lower()
fmt_flag = []
if ext == "mp3_320":
fmt_flag = ["--mp3", "--mp3-bitrate", "320"]
ext = "mp3"
elif ext == "mp3":
fmt_flag = ["--mp3", "--mp3-bitrate", "256"]
elif ext == "flac":
fmt_flag = ["--flac"]
# Use temp directory for demucs output
temp_out = JOBS_DIR / job_id / "demucs_out"
temp_out.mkdir(parents=True, exist_ok=True)
cmd = [
sys.executable, "-m", "demucs",
"--name", model,
"--out", str(temp_out),
*fmt_flag,
input_path,
]
try:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
)
while True:
if jm.should_cancel(job_id):
process.terminate()
process.wait()
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return None
line = process.stdout.readline()
if line:
jm.update_job(job_id, progress=f"[1/3] {line.rstrip()}")
elif process.poll() is not None:
break
else:
time.sleep(0.1)
if process.returncode != 0:
raise RuntimeError(f"Demucs failed with code {process.returncode}")
input_stem = Path(input_path).stem
stem_src_dir = temp_out / model / input_stem
stem_files = {}
if stem_src_dir.exists():
for f in sorted(stem_src_dir.iterdir()):
if f.is_file():
stem_name = f.stem.lower()
for prefix in [input_stem.lower(), 'htdemucs', 'mdx']:
stem_name = stem_name.replace(prefix, '').strip('_- ')
if not stem_name:
stem_name = f.stem
dest = stems_dir / f"{input_stem} - {stem_name.capitalize()}{f.suffix}"
shutil.move(str(f), str(dest))
stem_files[stem_name] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
def _separate_bsroformer(
job_id: str, input_path: str, model: str, output_format: str, stems_dir: Path,
mp3_bitrate: str = '256k'
) -> dict | None:
"""
Run BS-RoFormer in-process; returns dict mapping stem names to file paths, or None if cancelled.
"""
from .bs_roformer_separator import BSRoformerSeparator
stem_mode = BSR_STEM_MODE.get(model, "4stem")
jm.update_job(job_id, progress=f"[1/3] Loading BS-RoFormer ({stem_mode})...")
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return None
sep = BSRoformerSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
stem_mode=stem_mode,
)
jm.update_job(job_id, progress="[1/3] Separating with BS-RoFormer...")
temp_out = JOBS_DIR / job_id / "bsr_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return None
input_stem = Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / f"{input_stem} - {stem_name.capitalize()}{src.suffix}"
shutil.move(str(src), str(dest))
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise