Cubase-MCP-GAJ3T.MusicGen.R.../tests/unit/test_standard_profiles.py

181 lines
7.2 KiB
Python

"""Standard MIDI facts and generator policy, not installed vendor compatibility."""
import pytest
from pydantic import ValidationError
from riff_groove_builder.profiles import (
InstrumentProfile,
ProfileRegistry,
load_profile_registry,
parse_profile,
)
from riff_groove_builder.schemas import Part, ProfileSelection
def standard(part: Part = "lead") -> InstrumentProfile:
identifier = "standard.gm-drums.v1" if part == "drums" else f"standard.{part}.v1"
return load_profile_registry().select([part], ProfileSelection(**{part: identifier}))[part]
@pytest.mark.parametrize("part", ["lead", "bass", "drums"])
def test_standard_identity_policy_and_roundtrip(part: Part) -> None:
profile = standard(part)
assert profile.provenance.status == profile.summary.mapping_kind == "standard_midi"
assert profile.summary.mapping_version == "standard-fallback-1"
assert profile.summary.limitations == profile.provenance.limitations
assert "NOT verified Eclips/Ray/SD3" in profile.summary.limitations[0]
assert not profile.id.startswith("test.")
assert not profile.summary.product_name.startswith("SYNTHETIC")
assert profile.summary.supported_articulations == ["normal"]
assert profile.normal_articulation == "normal"
assert profile.resolve_articulation("sustain") == profile.resolve_articulation("normal")
assert profile.resolve_articulation("NORMAL").actions == []
assert profile.resolve_articulation("normal").mode == "latched"
assert parse_profile(profile.model_dump_json()) == profile
assert ProfileRegistry([profile]).summaries() == [profile.summary]
assert {s.kind for s in profile.provenance.sources} == {"standard_public", "generator_policy"}
assert all(
s.reference.startswith("https://")
if s.kind == "standard_public"
else s.reference == "docs/articulation-profiles.md#standard-fallback-policy-v1"
for s in profile.provenance.sources
)
if part != "drums":
assert profile.playable_range is not None
assert profile.playable_range.model_dump() == {"low": 0, "high": 127}
assert profile.drum_voices == []
def test_complete_gm_level_1_percussion_numbers() -> None:
# Independently transcribed numeric/name facts from the public teaching table:
# https://computermusicresource.com/GM.Percussion.KeyMap.html
names = (
"acoustic_bass_drum bass_drum_1 side_stick acoustic_snare hand_clap electric_snare "
"low_floor_tom closed_hi_hat high_floor_tom pedal_hi_hat low_tom open_hi_hat "
"low_mid_tom hi_mid_tom crash_cymbal_1 high_tom ride_cymbal_1 chinese_cymbal "
"ride_bell tambourine splash_cymbal cowbell crash_cymbal_2 vibraslap ride_cymbal_2 "
"hi_bongo low_bongo mute_hi_conga open_hi_conga low_conga high_timbale low_timbale "
"high_agogo low_agogo cabasa maracas short_whistle long_whistle short_guiro "
"long_guiro claves hi_wood_block low_wood_block mute_cuica open_cuica "
"mute_triangle open_triangle"
).split()
profile = standard("drums")
assert len(names) == len(profile.drum_voices) == 47
assert [(v.note, v.name) for v in profile.drum_voices] == list(
zip(range(35, 82), names, strict=True)
)
assert profile.playable_range is None
assert profile.channel == 9
assert {
name: profile.resolve_voice(name)
for name in ("kick", "snare", "closed hat", "open-hat", "pedal_hat", "crash", "ride")
} == {
"kick": 36,
"snare": 38,
"closed hat": 42,
"open-hat": 46,
"pedal_hat": 44,
"crash": 49,
"ride": 51,
}
assert {s.reference for s in profile.provenance.sources if s.kind == "standard_public"} == {
"https://midi.org/general-midi-level-1",
"https://computermusicresource.com/GM.Percussion.KeyMap.html",
}
@pytest.mark.parametrize("part", ["lead", "bass", "drums"])
@pytest.mark.parametrize(
("intensity", "velocity"), [(0.0, 1), (0.25, 32), (0.5, 64), (0.75, 96), (1.0, 127)]
)
def test_single_linear_velocity_policy(part: Part, intensity: float, velocity: int) -> None:
art = standard(part).resolve_articulation("normal")
assert len(art.layers) == 1
assert art.layers[0].model_dump() == {
"low": 0.0,
"high": 1.0,
"velocity_low": 1,
"velocity_high": 127,
}
assert art.velocity(intensity) == velocity
@pytest.mark.parametrize(
("section", "field", "value"),
[
("summary", "mapping_kind", "instrument"),
("summary", "product_name", "Verified Eclips map"),
("summary", "supported_articulations", ["normal", "slide"]),
("provenance", "status", "verified"),
("provenance", "status", "test_only"),
("provenance", "sources", []),
],
)
def test_no_standard_to_vendor_or_test_promotion(section: str, field: str, value: object) -> None:
data = standard().model_dump()
data[section][field] = value
with pytest.raises(ValidationError):
InstrumentProfile.model_validate(data)
@pytest.mark.parametrize("mutation", ["actions", "aliases", "mode", "name", "normal"])
def test_standard_cannot_invent_techniques_or_controls(mutation: str) -> None:
data = standard().model_dump()
art = data["articulations"][0]
if mutation == "actions":
art["actions"] = [
{
"kind": "cc",
"controller": 20,
"value": 0,
"reset_value": None,
"lead": {"numerator": 0},
"release_offset": {"numerator": 0},
}
]
elif mutation == "aliases":
art["aliases"].append("slide")
elif mutation == "mode":
art["mode"] = "momentary"
elif mutation == "name":
art["name"] = "sustain"
else:
data["normal_articulation"] = "sustain"
with pytest.raises(ValidationError):
InstrumentProfile.model_validate(data)
@pytest.mark.parametrize(
"mutation",
["no_facts", "no_policy", "missing_range", "missing_velocity", "http", "vendor", "bad_policy"],
)
def test_standard_provenance_requires_facts_and_separate_policy(mutation: str) -> None:
data = standard().model_dump()
sources = data["provenance"]["sources"]
if mutation == "no_facts":
sources.pop(0)
elif mutation == "no_policy":
sources.pop(1)
elif mutation == "missing_range":
sources[0]["aspects"].remove("range_or_voices")
elif mutation == "missing_velocity":
sources[1]["aspects"].remove("velocity")
elif mutation == "http":
sources[0]["reference"] = "http://example.invalid"
elif mutation == "vendor":
sources[0]["kind"] = "official_public"
else:
sources[1]["reference"] = "not a policy reference"
with pytest.raises(ValidationError):
InstrumentProfile.model_validate(data)
def test_standard_selection_returns_defensive_copies() -> None:
registry = load_profile_registry()
selected = registry.select(["lead"], ProfileSelection(lead="standard.lead.v1"))
selected["lead"].summary.mapping_kind = "instrument"
selected["lead"].articulations[0].aliases.append("slide")
fresh = registry.select(["lead"], ProfileSelection(lead="standard.lead.v1"))["lead"]
assert fresh.summary.mapping_kind == "standard_midi"
assert fresh.articulations[0].aliases == ["sustain"]