Files
stemengine_old/backend/services/transcriber_service.py
T
2026-03-17 11:39:17 +09:00

97 lines
3.5 KiB
Python
Executable File

"""
MIDI transcription dispatcher.
Routes to the appropriate engine based on the mode prefix:
'polyphonic' | 'melody' → basic-pitch (in-process, fast)
'omnizart-*' → Omnizart CLI or sidecar HTTP (requires OMNIZART_ENABLED=true)
"""
import logging
from pathlib import Path
from .. import job_manager as jm
from ..config import JOBS_DIR, TRANSCRIBE_PRESETS, OMNIZART_ENABLED
log = logging.getLogger(__name__)
def run_transcription(job_id: str, input_path: str, mode: str) -> None:
"""
Background task: transcribe audio to MIDI.
mode: 'polyphonic' | 'melody' → basic-pitch
'omnizart-music' → Omnizart music mode
'omnizart-vocal' → Omnizart vocal mode
'omnizart-vocal-contour' → Omnizart vocal-contour mode
"""
if mode.startswith("omnizart-"):
if not OMNIZART_ENABLED:
jm.update_job(
job_id,
status=jm.JobStatus.ERROR,
error="Omnizart is not enabled on this server (OMNIZART_ENABLED=false).",
progress="Error.",
)
return
from .omnizart_service import run_omnizart_transcription
run_omnizart_transcription(job_id, input_path, mode)
else:
_run_basic_pitch(job_id, input_path, mode)
# ---------------------------------------------------------------------------
# basic-pitch (original engine — unchanged)
# ---------------------------------------------------------------------------
def _run_basic_pitch(job_id: str, input_path: str, mode: str) -> None:
# Reuse global GPU safety limits (VRAM reserve + TF32 settings) before
# loading any transcription backend for basic-pitch.
try:
from .lyric_transcriber_service import _apply_cuda_limits
_apply_cuda_limits()
except Exception:
# Keep transcription functional even if limiter setup fails.
pass
from basic_pitch.inference import predict
from basic_pitch import ICASSP_2022_MODEL_PATH
log.debug("[%s] basic-pitch start — mode=%s input=%s", job_id, mode, input_path)
output_dir = JOBS_DIR / job_id
output_dir.mkdir(parents=True, exist_ok=True)
jm.update_job(job_id, status=jm.JobStatus.RUNNING, progress="Loading basic-pitch model...")
try:
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
params = TRANSCRIBE_PRESETS.get(mode, TRANSCRIBE_PRESETS["polyphonic"])
jm.update_job(job_id, progress=f"Transcribing with basic-pitch ({mode})...")
_, midi_data, _ = predict(
input_path,
ICASSP_2022_MODEL_PATH,
**params,
)
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
output_name = f"{Path(input_path).stem}-{mode}.mid"
dest = output_dir / output_name
midi_data.write(str(dest))
log.debug("[%s] basic-pitch complete — output=%s", job_id, dest.name)
jm.update_job(
job_id,
status=jm.JobStatus.DONE,
progress="Transcription complete.",
files=[{"name": dest.name, "path": str(dest)}],
)
except Exception as exc:
log.debug("[%s] basic-pitch failed — %s: %s", job_id, type(exc).__name__, exc)
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=str(exc), progress="Error.")