213 lines
7.7 KiB
Python
213 lines
7.7 KiB
Python
"""Reusable polyphonic attack plans, exact timing, strict bounds and compiler budgets."""
|
|
|
|
from fractions import Fraction
|
|
from typing import Literal
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from riff_groove_builder.articulations import ArticulationCompileError, compile_score
|
|
from riff_groove_builder.compose import CompositionError, compose_score
|
|
from riff_groove_builder.errors import SerializationError, UnsupportedArticulationError
|
|
from riff_groove_builder.profiles import load_profile_registry
|
|
from riff_groove_builder.schemas import (
|
|
ExplicitAttack,
|
|
ExplicitAttackPlan,
|
|
MusicalIntent,
|
|
PitchedNote,
|
|
PositiveBeat,
|
|
ProfileSelection,
|
|
RationalBeat,
|
|
TimeSignature,
|
|
)
|
|
|
|
|
|
def attack(
|
|
pitches: list[int],
|
|
start: Fraction = Fraction(0),
|
|
duration: Fraction = Fraction(1),
|
|
technique: Literal["normal", "palm_mute", "pinch_harmonic"] = "normal",
|
|
intensity: float | None = None,
|
|
) -> ExplicitAttack:
|
|
return ExplicitAttack(
|
|
pitches=pitches,
|
|
start=RationalBeat(numerator=start.numerator, denominator=start.denominator),
|
|
duration=PositiveBeat(numerator=duration.numerator, denominator=duration.denominator),
|
|
technique=technique,
|
|
intensity=intensity,
|
|
)
|
|
|
|
|
|
def intent_for(attacks: list[ExplicitAttack]) -> MusicalIntent:
|
|
return MusicalIntent(
|
|
description="Original explicit polyphonic plan; no public text parsing.",
|
|
parts=["lead"],
|
|
tempo_bpm=120,
|
|
bars=1,
|
|
time_signature=TimeSignature(numerator=4, denominator=4),
|
|
key="D minor",
|
|
seed=0,
|
|
explicit_plan=ExplicitAttackPlan(
|
|
part="lead", duration=PositiveBeat(numerator=4), attacks=attacks
|
|
),
|
|
)
|
|
|
|
|
|
def test_polyphonic_exact_plan_sustains_and_silence() -> None:
|
|
intent = intent_for(
|
|
[
|
|
attack([60, 64, 67], duration=Fraction(4, 3), intensity=0.25),
|
|
attack([71], start=Fraction(1, 3), duration=Fraction(2, 3)),
|
|
attack([47, 64, 67, 71], start=Fraction(2), technique="palm_mute"),
|
|
attack([66], start=Fraction(3), technique="pinch_harmonic"),
|
|
]
|
|
)
|
|
assert MusicalIntent.model_validate_json(intent.model_dump_json()) == intent
|
|
profiles = load_profile_registry().select(["lead"], ProfileSelection())
|
|
score = compose_score(intent, profiles)
|
|
assert score == compose_score(intent, profiles)
|
|
notes = score.tracks[0].notes
|
|
assert len(notes) == 9
|
|
assert [n.pitch for n in notes if isinstance(n, PitchedNote)] == [
|
|
60,
|
|
64,
|
|
67,
|
|
71,
|
|
47,
|
|
64,
|
|
67,
|
|
71,
|
|
66,
|
|
]
|
|
assert [n.intensity for n in notes[:3]] == [0.25] * 3
|
|
assert len({n.intensity for n in notes[4:8]}) == 1
|
|
assert notes[3].start.value == Fraction(1, 3)
|
|
assert notes[0].duration.value == Fraction(4, 3)
|
|
assert score.end.value == 4
|
|
assert [n.articulation for n in notes] == ["normal"] * 4 + ["palm_mute"] * 4 + [
|
|
"pinch_harmonic"
|
|
]
|
|
assert compile_score(score, profiles).musical_end_tick == 3840
|
|
assert score != compose_score(intent.model_copy(update={"seed": 8}), profiles)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"data",
|
|
[
|
|
{"pitches": []},
|
|
{"pitches": [60, 60]},
|
|
{"pitches": [60, True]},
|
|
{"pitches": [60.0]},
|
|
{"pitches": ["60"]},
|
|
{"pitches": [-1]},
|
|
{"pitches": [128]},
|
|
{"duration": {"numerator": 0}},
|
|
{"start": {"numerator": -1}},
|
|
{"technique": "slide"},
|
|
{"intensity": float("nan")},
|
|
{"intensity": 1.1},
|
|
{"continuation": True},
|
|
{"rest": True},
|
|
],
|
|
)
|
|
def test_strict_attack(data: dict[str, object]) -> None:
|
|
values = attack([60]).model_dump()
|
|
values.update(data)
|
|
with pytest.raises(ValidationError):
|
|
ExplicitAttack.model_validate(values)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"attacks",
|
|
[
|
|
[],
|
|
[attack([60]), attack([60], Fraction(1, 2))],
|
|
[attack([60, 64]), attack([64, 67], Fraction(1, 2))],
|
|
[attack([60], Fraction(1)), attack([62])],
|
|
[attack([60], Fraction(4))],
|
|
[attack([60], duration=Fraction(5))],
|
|
[attack([60]), attack([62], Fraction(1, 2), technique="palm_mute")],
|
|
],
|
|
)
|
|
def test_invalid_timelines(attacks: list[ExplicitAttack]) -> None:
|
|
with pytest.raises(ValidationError):
|
|
intent_for(attacks)
|
|
|
|
|
|
def test_half_open_adjacent_repeated_pitches_and_disjoint_polyphony() -> None:
|
|
intent_for([attack([60]), attack([60], Fraction(1))])
|
|
intent_for([attack([60]), attack([62], Fraction(1, 2))])
|
|
intent_for([attack([60]), attack([62])])
|
|
with pytest.raises(ValidationError):
|
|
ExplicitAttackPlan(
|
|
part="lead", duration=PositiveBeat(numerator=16385), attacks=[attack([60])]
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"update",
|
|
[
|
|
{"parts": ["bass"]},
|
|
{"parts": ["lead", "bass"]},
|
|
{"rhythm_terms": ["quarter"]},
|
|
{"techniques": ["normal"]},
|
|
{"bars": 1, "time_signature": {"numerator": 1, "denominator": 4}},
|
|
],
|
|
)
|
|
def test_intent_coverage_and_globals(update: dict[str, object]) -> None:
|
|
data = intent_for([attack([60])]).model_dump()
|
|
data.update(update)
|
|
with pytest.raises(ValidationError):
|
|
MusicalIntent.model_validate(data)
|
|
|
|
|
|
def test_legacy_and_explicit_plans_cannot_compete() -> None:
|
|
data = intent_for([attack([60])]).model_dump()
|
|
data["phrase_plan"] = {
|
|
"part": "lead",
|
|
"duration": {"numerator": 4},
|
|
"notes": [{"start": {"numerator": 0}, "duration": {"numerator": 1}, "technique": "normal"}],
|
|
}
|
|
with pytest.raises(ValidationError):
|
|
MusicalIntent.model_validate(data)
|
|
|
|
|
|
@pytest.mark.parametrize("pitches", [[35], [60, 64, 85]])
|
|
def test_no_hidden_transpose_or_dropped_out_of_map_tone(pitches: list[int]) -> None:
|
|
intent = intent_for([attack(pitches)])
|
|
profiles = load_profile_registry().select(["lead"], ProfileSelection())
|
|
with pytest.raises(CompositionError, match="composed safely"):
|
|
compose_score(intent, profiles)
|
|
|
|
|
|
def test_exact_grid_and_profile_technique_are_required() -> None:
|
|
profiles = load_profile_registry().select(["lead"], ProfileSelection())
|
|
with pytest.raises(SerializationError):
|
|
compose_score(intent_for([attack([60], duration=Fraction(1, 7))]), profiles)
|
|
standard = load_profile_registry().select(["lead"], ProfileSelection(lead="standard.lead.v1"))
|
|
with pytest.raises(UnsupportedArticulationError):
|
|
compose_score(intent_for([attack([60], technique="palm_mute")]), standard)
|
|
|
|
|
|
def test_expanded_tones_and_controls_share_budget(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr("riff_groove_builder.schemas.MAX_SCHEDULED_EVENTS", 12)
|
|
monkeypatch.setattr("riff_groove_builder.articulations.MAX_SCHEDULED_EVENTS", 12)
|
|
intent = intent_for([attack([60, 64, 67]), attack([62, 66, 69], Fraction(1))])
|
|
standard = load_profile_registry().select(["lead"], ProfileSelection(lead="standard.lead.v1"))
|
|
assert len(compose_score(intent, standard).tracks[0].notes) == 6
|
|
native = load_profile_registry().select(["lead"], ProfileSelection())
|
|
with pytest.raises(ArticulationCompileError) as caught:
|
|
compose_score(intent, native)
|
|
assert caught.value.details["reason"] == "event_budget_exceeded"
|
|
with pytest.raises(ValidationError, match="Expanded chord"):
|
|
intent_for([attack([60, 64, 67]), attack([61, 65, 68, 72], Fraction(1))])
|
|
|
|
|
|
def test_composer_revalidates_mutated_attack_models() -> None:
|
|
intent = intent_for([attack([60, 64, 67])])
|
|
assert intent.explicit_plan is not None
|
|
intent.explicit_plan.attacks[0].pitches.append(60)
|
|
profiles = load_profile_registry().select(["lead"], ProfileSelection())
|
|
with pytest.raises(CompositionError):
|
|
compose_score(intent, profiles)
|