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

226 lines
6.7 KiB
Python

"""Notation atoms and register policy, with independent numeric pitch expectations."""
from typing import Literal
import pytest
from riff_groove_builder.pitch import (
ChordSymbol,
ExplicitVoicing,
PitchToken,
parse_chord,
parse_pitch,
resolve_voicing,
tokenize_notation,
)
from riff_groove_builder.profiles import InstrumentProfile, load_profile_registry
from riff_groove_builder.schemas import ProfileSelection
@pytest.fixture
def lead() -> InstrumentProfile:
return load_profile_registry().select(["lead"], ProfileSelection())["lead"]
@pytest.mark.parametrize(
("text", "midi"),
[
("C4", 60),
("E4", 64),
("G4", 67),
("F#3", 54),
("Gb3", 54),
("Bb2", 46),
("Cb4", 59),
("B#3", 60),
("E#4", 65),
("Fb4", 64),
("C-1", 0),
("C+4", 60),
("G9", 127),
],
)
def test_explicit_pitch_arithmetic(text: str, midi: int) -> None:
assert parse_pitch(text).midi == midi
assert parse_pitch(text).pitch_class == midi % 12
@pytest.mark.parametrize(
"text",
[
"",
"c4",
"H4",
"C##4",
"Dbb3",
"Fx4",
"C♯4",
"C 4",
" C4",
"C4 ",
"C4.0",
"C--1",
"C+-1",
"C10",
"C-2",
"G#9",
"Cb-1",
"A4junk",
"C4/E4",
],
)
def test_invalid_pitch_symbols(text: str) -> None:
with pytest.raises(ValueError):
parse_pitch(text)
@pytest.mark.parametrize("text", ["F#", "D", "Bb", "Cb", "B#"])
def test_octave_free_notes_are_unresolved(text: str) -> None:
note = parse_pitch(text)
assert note.octave is None
with pytest.raises(ValueError, match="register"):
_ = note.midi
@pytest.mark.parametrize(
("text", "expected"),
[
("F#", (42, 46, 49)),
("D", (38, 42, 45)),
("Bb", (46, 50, 53)),
("Em4", (64, 67, 71)),
("Emin4", (64, 67, 71)),
("F#3", (54, 58, 61)),
("Bb2", (46, 50, 53)),
("Em4OverB", (59, 64, 67, 71)),
("Em4/B", (59, 64, 67, 71)),
("Em4OverB2", (47, 64, 67, 71)),
("Em4/B2", (47, 64, 67, 71)),
("Em(add4)", (40, 43, 45, 47)),
("Em4(add4)", (64, 67, 69, 71)),
("E4(add4)", (64, 68, 69, 71)),
("Em4(add4)/B2", (47, 64, 67, 69, 71)),
("[C3 E3 G3]", (48, 52, 55)),
("[G3 C3 E3]", (48, 52, 55)),
("[Cb4 F#4]", (59, 66)),
],
)
def test_chord_voicings(lead: InstrumentProfile, text: str, expected: tuple[int, ...]) -> None:
resolved = resolve_voicing(parse_chord(text), lead)
assert resolved.pitches == expected
assert len(set(resolved.pitches)) == len(resolved.pitches)
def test_octave_and_bass_defaults_are_reported(lead: InstrumentProfile) -> None:
assert resolve_voicing(parse_pitch("E"), lead).pitches == (40,)
assert "nearest MIDI40" in resolve_voicing(parse_chord("F#"), lead).assumptions[0]
assert "MIDI42" in resolve_voicing(parse_chord("F#"), lead).assumptions[0]
default_bass = resolve_voicing(parse_chord("Em4OverB"), lead)
assert len(default_bass.assumptions) == 1
assert "MIDI59" in default_bass.assumptions[0]
assert "strictly below" in default_bass.assumptions[0]
assert resolve_voicing(parse_chord("Em4OverB2"), lead).assumptions == ()
assert resolve_voicing(parse_chord("[C4 E4 G4]"), lead).assumptions == ()
@pytest.mark.parametrize(
"text",
[
"",
"Emaj4",
"Emajor",
"Eminor4",
"Em7",
"E7",
"Esus4",
"Eadd4",
"Emadd4",
"Em4add4",
"Em(add2)",
"Em4(add4)(add4)",
"Eaug",
"Edim",
"Em4/B2junk",
"Em4/BB2",
"Em4Over",
"Em4/",
"[C E G]",
"[C4 C4]",
"[C4 B#3]",
"[]",
"[C4,E4,G4]",
"[C4 E4 G4",
"C4 E4",
"Em4//B2",
"E##4",
"C10",
"Cm-2",
],
)
def test_invalid_or_unsupported_chords(lead: InstrumentProfile, text: str) -> None:
with pytest.raises(ValueError):
resolve_voicing(parse_chord(text), lead)
@pytest.mark.parametrize("text", ["Em4/B4", "Em4/E4", "Em4OverC5"])
def test_contradictory_bass_is_not_revoiced(lead: InstrumentProfile, text: str) -> None:
with pytest.raises(ValueError, match="strictly below"):
resolve_voicing(parse_chord(text), lead)
@pytest.mark.parametrize("text", ["C7", "Em6", "Em4/B1", "[C2 E4 G7]", "G9"])
def test_whole_voicing_must_fit_map_without_dropping(lead: InstrumentProfile, text: str) -> None:
with pytest.raises(ValueError):
resolve_voicing(parse_chord(text), lead)
@pytest.mark.parametrize("text", ["F#F#", "DD", "BbBb", "CEG"])
@pytest.mark.parametrize("kind", ["note", "chord"])
def test_deterministic_compact_runs(text: str, kind: Literal["note", "chord"]) -> None:
tokens = tokenize_notation(text, kind=kind)
expected = ["C", "E", "G"] if text == "CEG" else [text[: len(text) // 2]] * 2
assert [token.text for token in tokens] == expected
assert "".join(token.text for token in tokens) == text
assert all(token.leading_space == "" for token in tokens)
def test_tokens_preserve_boundaries_and_spacing_without_guessing_time() -> None:
text = " F# F#\tDD BbBb Em4/B2[C3 E3 G3]"
tokens = tokenize_notation(text, kind="chord")
assert [t.text for t in tokens] == ["F#", "F#", "D", "D", "Bb", "Bb", "Em4/B2", "[C3 E3 G3]"]
assert [t.leading_space for t in tokens] == [" ", " ", "\t", "", " ", "", " ", ""]
assert all(text[t.start : t.end] == t.text for t in tokens)
assert isinstance(tokens[-1].value, ExplicitVoicing)
assert isinstance(tokens[-2].value, ChordSymbol)
assert [t.text for t in tokenize_notation("C4E4G4", kind="note")] == ["C4", "E4", "G4"]
assert [t.text for t in tokenize_notation("Emin4Em4(add4)", kind="chord")] == [
"Emin4",
"Em4(add4)",
]
@pytest.mark.parametrize(
"text",
["F##F#", "D7x", "BbBbb", "Em4/B2/", "Em4(add)", "CEGX", "Em4?", "D-d", "C4~", "C4->E4"],
)
def test_compact_leftovers_are_errors(text: str) -> None:
with pytest.raises(ValueError):
tokenize_notation(text, kind="chord")
@pytest.mark.parametrize("text", ["", " \t ", "C" * 4001])
def test_tokenizer_is_bounded(text: str) -> None:
with pytest.raises(ValueError):
tokenize_notation(text, kind="note")
def test_typed_tokens_reject_invalid_construction() -> None:
with pytest.raises(ValueError):
PitchToken("C", octave=True)
with pytest.raises(ValueError):
PitchToken("H")
with pytest.raises(ValueError):
ExplicitVoicing((parse_pitch("C4"), parse_pitch("B#3")))
with pytest.raises(ValueError):
ExplicitVoicing((parse_pitch("C"),))