226 lines
7.9 KiB
Python
Executable File
226 lines
7.9 KiB
Python
Executable File
"""
|
|
MIDI transcription dispatcher.
|
|
|
|
Routes to the appropriate engine based on the mode prefix:
|
|
'polyphonic' | 'melody' | 'monophonic' | 'guitar' | 'bass' → basic-pitch (in-process, fast)
|
|
'omnizart-*' → Omnizart CLI or sidecar HTTP (requires OMNIZART_ENABLED=true)
|
|
"""
|
|
import logging
|
|
from statistics import mean
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from .. import job_manager as jm
|
|
from ..config import JOBS_DIR, TRANSCRIBE_PRESETS, OMNIZART_ENABLED
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
_BEND_POLICY_VALUES = {"auto", "force_on", "force_off"}
|
|
|
|
|
|
def run_transcription(job_id: str, input_path: str, mode: str, bend_policy: str = "auto") -> None:
|
|
"""
|
|
Background task: transcribe audio to MIDI.
|
|
|
|
mode: 'polyphonic' | 'melody' | 'monophonic' | 'guitar' | 'bass' → basic-pitch
|
|
'omnizart-music' → Omnizart music mode
|
|
'omnizart-vocal' → Omnizart vocal mode
|
|
'omnizart-vocal-contour' → Omnizart vocal-contour mode
|
|
"""
|
|
resolved_bend_policy = _normalize_bend_policy(bend_policy)
|
|
|
|
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, resolved_bend_policy)
|
|
else:
|
|
_run_basic_pitch(job_id, input_path, mode, resolved_bend_policy)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# basic-pitch (original engine — unchanged)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _run_basic_pitch(job_id: str, input_path: str, mode: str, bend_policy: 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 = dict(TRANSCRIBE_PRESETS.get(mode, TRANSCRIBE_PRESETS["polyphonic"]))
|
|
# Always infer with bends enabled for adaptive modes; policy is applied
|
|
# after prediction by either keeping or stripping bend events.
|
|
if mode in {"polyphonic", "melody"}:
|
|
params["multiple_pitch_bends"] = True
|
|
jm.update_job(job_id, progress=f"Transcribing with basic-pitch ({mode})...")
|
|
|
|
_, midi_data, note_events = predict(
|
|
input_path,
|
|
ICASSP_2022_MODEL_PATH,
|
|
**params,
|
|
)
|
|
|
|
confidence_summary = _summarize_confidence(note_events)
|
|
stability = _estimate_note_stability(midi_data)
|
|
allow_bends, decision = _resolve_basic_pitch_bend_policy(
|
|
mode=mode,
|
|
bend_policy=bend_policy,
|
|
confidence_summary=confidence_summary,
|
|
note_stability=stability,
|
|
)
|
|
if not allow_bends:
|
|
_strip_pitch_bends(midi_data)
|
|
|
|
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))
|
|
|
|
bend_event_count = _count_pitch_bends(midi_data)
|
|
bend_stats = {
|
|
"engine": "basic-pitch",
|
|
"mode": mode,
|
|
"bend_policy": bend_policy,
|
|
"bend_policy_decision": decision,
|
|
"bend_event_count": bend_event_count,
|
|
"bend_confidence_summary": confidence_summary,
|
|
"segment_stability": stability,
|
|
"bends_enabled": bool(allow_bends),
|
|
}
|
|
|
|
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)}],
|
|
result={"bend_stats": bend_stats},
|
|
)
|
|
|
|
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.")
|
|
|
|
|
|
def _normalize_bend_policy(value: str | None) -> str:
|
|
cleaned = str(value or "auto").strip().lower()
|
|
return cleaned if cleaned in _BEND_POLICY_VALUES else "auto"
|
|
|
|
|
|
def _summarize_confidence(note_events: Any) -> dict[str, Any]:
|
|
values: list[float] = []
|
|
if isinstance(note_events, list):
|
|
for item in note_events:
|
|
if isinstance(item, dict):
|
|
for key in ("confidence", "amplitude", "velocity", "score"):
|
|
raw = item.get(key)
|
|
if isinstance(raw, (int, float)):
|
|
values.append(float(raw))
|
|
break
|
|
elif isinstance(item, (tuple, list)) and item:
|
|
tail = item[-1]
|
|
if isinstance(tail, (int, float)):
|
|
values.append(float(tail))
|
|
|
|
if not values:
|
|
return {
|
|
"count": 0,
|
|
"avg": None,
|
|
"min": None,
|
|
"max": None,
|
|
}
|
|
|
|
return {
|
|
"count": len(values),
|
|
"avg": float(mean(values)),
|
|
"min": float(min(values)),
|
|
"max": float(max(values)),
|
|
}
|
|
|
|
|
|
def _estimate_note_stability(midi_data: Any) -> float:
|
|
deltas: list[float] = []
|
|
for instrument in getattr(midi_data, "instruments", []):
|
|
notes = sorted(getattr(instrument, "notes", []) or [], key=lambda n: float(getattr(n, "start", 0.0)))
|
|
for idx in range(1, len(notes)):
|
|
prev = notes[idx - 1]
|
|
cur = notes[idx]
|
|
prev_pitch = float(getattr(prev, "pitch", 0.0))
|
|
cur_pitch = float(getattr(cur, "pitch", 0.0))
|
|
deltas.append(abs(cur_pitch - prev_pitch))
|
|
|
|
if not deltas:
|
|
return 0.0
|
|
|
|
# 1.0 = highly stable, 0.0 = highly unstable.
|
|
avg_delta = float(mean(deltas))
|
|
return max(0.0, min(1.0, 1.0 - (avg_delta / 12.0)))
|
|
|
|
|
|
def _resolve_basic_pitch_bend_policy(
|
|
*,
|
|
mode: str,
|
|
bend_policy: str,
|
|
confidence_summary: dict[str, Any],
|
|
note_stability: float,
|
|
) -> tuple[bool, str]:
|
|
if bend_policy == "force_on":
|
|
return True, "forced_on"
|
|
if bend_policy == "force_off":
|
|
return False, "forced_off"
|
|
|
|
if mode in {"guitar", "bass"}:
|
|
return True, "auto_guitar_bass"
|
|
if mode == "monophonic":
|
|
return False, "auto_monophonic"
|
|
|
|
confidence_avg = confidence_summary.get("avg")
|
|
enough_confidence = isinstance(confidence_avg, (int, float)) and float(confidence_avg) >= 0.45
|
|
stable_segments = note_stability >= 0.55
|
|
if enough_confidence and stable_segments:
|
|
return True, "auto_adaptive_on"
|
|
return False, "auto_adaptive_off"
|
|
|
|
|
|
def _strip_pitch_bends(midi_data: Any) -> None:
|
|
for instrument in getattr(midi_data, "instruments", []):
|
|
if hasattr(instrument, "pitch_bends"):
|
|
instrument.pitch_bends = []
|
|
|
|
|
|
def _count_pitch_bends(midi_data: Any) -> int:
|
|
count = 0
|
|
for instrument in getattr(midi_data, "instruments", []):
|
|
bends = getattr(instrument, "pitch_bends", []) or []
|
|
count += len(bends)
|
|
return count
|