96 lines
4.1 KiB
Python
96 lines
4.1 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import pathlib
|
|
import typing
|
|
|
|
import generator.model
|
|
import generator.parts
|
|
import generator.score
|
|
|
|
def _arranged_staff(document: dict[str, typing.Any]) -> dict[str, typing.Any]:
|
|
staves = document.get("staves")
|
|
if not isinstance(staves, list) or not staves or not isinstance(staves[0], dict):
|
|
raise ValueError("generated score document must contain an arranged staff")
|
|
return staves[0]
|
|
|
|
def _measure_summary(staff: dict[str, typing.Any], measure: dict[str, typing.Any], measure_number: int) -> dict[str, typing.Any]:
|
|
notes = generator.score.measure_notes(measure)
|
|
summary: dict[str, typing.Any] = {
|
|
"measure": measure_number,
|
|
"section_label": str(measure.get("section_label", "")),
|
|
"harmonies": [{"onset": str(generator.model.fraction(h["onset"])), "symbol": h["symbol"]} for h in measure["harmonies"]],
|
|
"duration": str(generator.model.fraction(measure["duration"])),
|
|
"slash_count": sum(1 for note in notes if note.get("kind", "note") == "note" and note.get("notehead") == "slash"),
|
|
}
|
|
if staff.get("arrangement_part_type") == "vocal":
|
|
summary["vocal_skeleton"] = [
|
|
f'{generator.model.fraction(note["onset"])}+{generator.model.fraction(note["duration"])} '
|
|
+ note['written']['type'] + '.' * note['written'].get('dots', 0)
|
|
+ ' ' + note['kind'] + ' ' + ','.join(generator.model.pitch_name(p) + str(p['octave']) for p in note['pitches'])
|
|
+ ' ' + note.get('lyric', '')
|
|
for note in notes
|
|
]
|
|
return summary
|
|
|
|
def performance_document_summary(document: dict[str, typing.Any]) -> dict[str, typing.Any]:
|
|
metadata = document.get("metadata")
|
|
if not isinstance(metadata, dict):
|
|
metadata = {}
|
|
staff = _arranged_staff(document)
|
|
measures = staff.get("measures")
|
|
if not isinstance(measures, list):
|
|
measures = []
|
|
measure_summaries = [
|
|
_measure_summary(staff, measure, index + 1)
|
|
for index, measure in enumerate(measures)
|
|
if isinstance(measure, dict)
|
|
]
|
|
return {
|
|
"title": str(metadata.get("title", "")),
|
|
"composer": str(metadata.get("composer", "")),
|
|
"key_signature": str(metadata.get("key_signature", "")),
|
|
"part_type": str(staff.get("arrangement_part_type", "")),
|
|
"measure_count": len(measure_summaries),
|
|
"section_labels": [
|
|
{"measure": item["measure"], "label": item["section_label"]}
|
|
for item in measure_summaries
|
|
if item["section_label"]
|
|
],
|
|
"measures": measure_summaries,
|
|
}
|
|
|
|
def performance_tree_summary(root: pathlib.Path) -> dict[str, typing.Any]:
|
|
return {
|
|
path.name: performance_document_summary(generator.score.load_json(path))
|
|
for path in sorted(root.glob("*.json"))
|
|
}
|
|
|
|
def validate_expected_performance_summary(expected_path: pathlib.Path, generated_songs_dir: pathlib.Path) -> None:
|
|
if not expected_path.exists():
|
|
raise ValueError(f"missing generator compatibility summary: {expected_path}")
|
|
with expected_path.open("r", encoding="utf-8") as stream:
|
|
expected = json.load(stream)
|
|
actual = performance_tree_summary(generated_songs_dir)
|
|
if actual == expected:
|
|
return
|
|
expected_names = set(expected.keys()) if isinstance(expected, dict) else set()
|
|
actual_names = set(actual.keys())
|
|
missing = sorted(expected_names - actual_names)
|
|
extra = sorted(actual_names - expected_names)
|
|
if missing or extra:
|
|
raise ValueError(f"performance generator drift: missing={missing}, extra={extra}")
|
|
for name in sorted(actual_names):
|
|
if actual[name] != expected[name]:
|
|
raise ValueError(f"performance generator drift in {name}; update generator logic or refresh the expected summary intentionally")
|
|
raise ValueError("performance generator drift")
|
|
|
|
def validate_practice_parts(generated_practice_dir: pathlib.Path) -> None:
|
|
if not generated_practice_dir.exists():
|
|
return
|
|
expected_names = {part + '.json' for part in generator.parts.PART_TYPES}
|
|
for exercise_dir in sorted(path for path in generated_practice_dir.iterdir() if path.is_dir()):
|
|
json_names = {path.name for path in exercise_dir.glob("*.json")}
|
|
if json_names != expected_names:
|
|
raise ValueError(f"practice generator drift in {exercise_dir}: expected {sorted(expected_names)}, found {sorted(json_names)}")
|