Spaces:
Sleeping
Sleeping
File size: 2,142 Bytes
1ebc6d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | """Optional adapter from theme-realization-v1 into the sibling melody parser."""
from pathlib import Path
import os
import sys
from .common import PITCH_CLASS
from .io import ensure_muses_import_path
def analyze_realization(payload, source: Path | None = None):
if payload.get("format") != "theme-realization-v1":
raise ValueError("Expected theme-realization-v1 payload")
source = source or Path(os.environ.get("INTENTION_COMPOSITION_SRC", Path(__file__).resolve().parents[2] / "intention_based_composition" / "src"))
if not source.exists():
raise RuntimeError("Sibling intention parser source is unavailable")
ensure_muses_import_path()
if str(source) not in sys.path:
sys.path.insert(0, str(source))
from muses.base.temporals import TemporalCollection
from intention_composition.melody.corpus import MelodyNote, ThemeMelody, metric_strength
from intention_composition.melody.parser import parse_theme
collection = TemporalCollection(name="intention realization")
events = [event for event in payload["events"] if event["pitch"] is not None]
for event in events:
collection.insert_note(event["pitch"], event["start_beat"], event["duration_beats"])
notes = tuple(MelodyNote(note, events[i]["rpc"], events[i]["duration"], metric_strength(note.start_beat, 4), i)
for i, note in enumerate(collection.iter_notes()))
theme = ThemeMelody("generated", "", "Intention realization", payload["key"], PITCH_CLASS[payload["key"]], "4/4", 4,
collection, notes)
analysis = parse_theme(theme).to_dict(include_notes=True)
agreement = [{"name": span["name"], "start": span["start"], "end": span["end"],
"exact_parser_match": any(match["name"] == span["name"] and match["start"] == span["start"] and match["end"] == span["end"]
for match in analysis["matches"])} for span in payload["spans"]]
return {"interpretation": "parser roundtrip diagnostic, not independent musical validation", "analysis": analysis,
"planned_span_agreement": agreement}
|