71 lines
2.3 KiB
Python
Executable File
71 lines
2.3 KiB
Python
Executable File
"""
|
|
ABC notation conversion via abc_generator (backend/services/abc_generator.py).
|
|
"""
|
|
from .. import job_manager as jm
|
|
from ..config import JOBS_DIR
|
|
from . import abc_generator
|
|
|
|
|
|
def run_abc_conversion(job_id: str, midi_paths: list[str], song_title: str):
|
|
"""
|
|
Background task: convert MIDI file(s) to ABC notation.
|
|
midi_paths: list of MIDI file paths
|
|
song_title: title to use in ABC headers
|
|
"""
|
|
output_dir = JOBS_DIR / job_id
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
jm.update_job(job_id, status=jm.JobStatus.RUNNING, progress="Converting MIDI to ABC...")
|
|
|
|
try:
|
|
abc_results = abc_generator.convert_midi_to_abc(midi_paths, song_title)
|
|
|
|
if not abc_results:
|
|
raise RuntimeError("No ABC output produced — MIDI may be empty or unreadable.")
|
|
|
|
files = []
|
|
for part_name, abc_text in abc_results.items():
|
|
safe_name = part_name.replace("/", "_").replace("\\", "_")
|
|
filename = f"{safe_name}.abc"
|
|
dest = output_dir / filename
|
|
dest.write_text(abc_text, encoding="utf-8")
|
|
files.append({"name": filename, "path": str(dest)})
|
|
|
|
jm.update_job(
|
|
job_id,
|
|
status=jm.JobStatus.DONE,
|
|
progress="ABC conversion complete.",
|
|
files=files,
|
|
)
|
|
|
|
except Exception as exc:
|
|
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=str(exc), progress="Error.")
|
|
|
|
|
|
def run_mix_abc_conversion(job_id: str, mix_midi_path: str, song_title: str):
|
|
"""Convert a single mix MIDI to ABC notation."""
|
|
output_dir = JOBS_DIR / job_id
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
jm.update_job(job_id, status=jm.JobStatus.RUNNING, progress="Converting mix MIDI to ABC...")
|
|
|
|
try:
|
|
abc_text = abc_generator.generate_mix_abc(mix_midi_path, song_title)
|
|
|
|
if not abc_text:
|
|
raise RuntimeError("No ABC output produced.")
|
|
|
|
dest = output_dir / "mix.abc"
|
|
dest.write_text(abc_text, encoding="utf-8")
|
|
|
|
files = [{"name": "mix.abc", "path": str(dest)}]
|
|
jm.update_job(
|
|
job_id,
|
|
status=jm.JobStatus.DONE,
|
|
progress="ABC conversion complete.",
|
|
files=files,
|
|
)
|
|
|
|
except Exception as exc:
|
|
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=str(exc), progress="Error.")
|