"""Real disk and independent MIDIUtil evidence with exclusively synthetic maps.""" import shutil import uuid from collections.abc import Iterator from fractions import Fraction from pathlib import Path import mido # type: ignore[import-untyped] import pytest from midiutil import MIDIFile # type: ignore[import-untyped] from riff_groove_builder.errors import SerializationError from riff_groove_builder.midi import validate_midi_file, write_midi from riff_groove_builder.schemas import ( ControlChangeEvent, DrumNote, EventPlan, EventTrack, MusicalIntent, NoteOffEvent, NoteOnEvent, Part, PitchedNote, PositiveBeat, RationalBeat, ScheduledEvent, Score, ScoreTrack, TimeSignature, ) pytestmark = pytest.mark.integration Event = tuple[int, str, int, int, int] PARTS: list[Part] = ["bass", "drums", "lead"] @pytest.fixture def workspace() -> Iterator[Path]: root = ( Path(__file__).resolve().parents[2] / ".test-artifacts" / f"midi-files-{uuid.uuid4().hex}" ) root.mkdir(parents=True) try: yield root finally: shutil.rmtree(root) def synthetic_plan(numerator: int = 4, denominator: int = 4, bpm: float = 137.5) -> EventPlan: """All pitches/controllers/channels below are arbitrary test-local numbers.""" signature = TimeSignature.model_validate({"numerator": numerator, "denominator": denominator}) duration = Fraction(numerator * 4, denominator) musical_end = 240 + int(duration * 960) score_end = Fraction(2) + duration score = Score( intent=MusicalIntent( description="Synthetic three-part MIDI interoperability fixture", parts=PARTS.copy(), tempo_bpm=bpm, bars=1, time_signature=signature, key="A minor", seed=0, ), start=RationalBeat(numerator=2), end=RationalBeat(numerator=score_end.numerator, denominator=score_end.denominator), tracks=[ ScoreTrack( part="bass", notes=[ PitchedNote( part="bass", pitch=45, start=RationalBeat(numerator=2), duration=PositiveBeat( numerator=duration.numerator, denominator=duration.denominator ), intensity=0.5, articulation="synthetic_bass", ) ], ), ScoreTrack( part="drums", notes=[ DrumNote( voice="synthetic_hit", start=RationalBeat(numerator=start, denominator=denom), duration=PositiveBeat(numerator=1, denominator=8), intensity=0.5, articulation="synthetic_drum", ) for start, denom in ((2, 1), (5, 2)) ], ), ScoreTrack( part="lead", notes=[ PitchedNote( part="lead", pitch=69, start=RationalBeat(numerator=start, denominator=3), duration=PositiveBeat(numerator=length, denominator=3), intensity=0.5, articulation="synthetic_lead", ) for start, length in ((6, 1), (7, 2)) ], ), ], ) # tick, kind, channel, data1, data2, role. Kept deliberately out of tick order. raw: dict[Part, list[tuple[int, str, int, int, int, str]]] = { "lead": [ (musical_end + 60, "control_change", 2, 21, 0, "articulation_control"), (240, "note_on", 2, 69, 97, "musical_note"), (0, "note_on", 2, 5, 33, "keyswitch"), (560, "note_on", 2, 69, 88, "musical_note"), (80, "control_change", 2, 21, 100, "articulation_control"), (280, "note_off", 2, 5, 33, "keyswitch"), (1200, "note_off", 2, 69, 88, "musical_note"), (560, "note_off", 2, 69, 97, "musical_note"), ], "bass": [ (musical_end, "note_off", 5, 45, 83, "musical_note"), (0, "note_on", 5, 6, 34, "keyswitch"), (120, "control_change", 5, 22, 88, "articulation_control"), (240, "note_on", 5, 45, 83, "musical_note"), (100, "note_off", 5, 6, 34, "keyswitch"), (musical_end + 80, "control_change", 5, 22, 0, "articulation_control"), ], "drums": [ (musical_end + 120, "control_change", 12, 23, 0, "articulation_control"), (240, "note_on", 12, 73, 110, "musical_note"), (20, "note_on", 12, 7, 35, "keyswitch"), (360, "note_off", 12, 73, 110, "musical_note"), (120, "control_change", 12, 23, 66, "articulation_control"), (720, "note_on", 12, 73, 104, "musical_note"), (100, "note_off", 12, 7, 35, "keyswitch"), (840, "note_off", 12, 73, 104, "musical_note"), ], } tracks = [] for part in PARTS: events: list[ScheduledEvent] = [] for sequence, (tick, kind, channel, data1, data2, role) in enumerate(raw[part]): common = { "tick": tick, "part": part, "channel": channel, "sequence": sequence, "role": role, } if kind == "control_change": events.append( ControlChangeEvent.model_validate( {**common, "priority": 10, "controller": data1, "value": data2} ) ) else: cls = NoteOnEvent if kind == "note_on" else NoteOffEvent events.append( cls.model_validate( { **common, "priority": 20 if kind == "note_on" else 0, "note": data1, "velocity": data2, } ) ) tracks.append(EventTrack(part=part, events=events)) return EventPlan( score=score, musical_start_tick=240, musical_end_tick=musical_end, end_tick=musical_end + 120, tracks=tracks, ) def normalized(path: Path) -> list[list[Event]]: result = [] for track in mido.MidiFile(path).tracks: events: list[Event] = [] tick = 0 for message in track: assert type(message.time) is int and message.time >= 0 tick += message.time if message.is_meta: continue assert message.type in ("note_on", "note_off", "control_change") data1 = message.control if message.type == "control_change" else message.note data2 = message.value if message.type == "control_change" else message.velocity events.append((tick, message.type, message.channel, data1, data2)) result.append(events) return result def reference(path: Path, numerator: int, denominator: int, bpm: float) -> None: """Construct independently with MIDIUtil's note/duration and CC APIs.""" end = 240 + numerator * 4 * 960 // denominator midi = MIDIFile( 3, file_format=1, ticks_per_quarternote=960, eventtime_is_ticks=True, adjust_origin=False, removeDuplicates=False, deinterleave=False, ) midi.addTempo(0, 0, bpm) midi.addTimeSignature(0, 0, numerator, {4: 2, 8: 3}[denominator], 24) for index, name in enumerate(("Bass", "Drums", "Lead")): midi.addTrackName(index, 0, name) midi.addNote(0, 5, 6, 0, 100, 34) midi.addNote(0, 5, 45, 240, end - 240, 83) midi.addControllerEvent(0, 5, 120, 22, 88) midi.addControllerEvent(0, 5, end + 80, 22, 0) midi.addNote(1, 12, 7, 20, 80, 35) midi.addNote(1, 12, 73, 240, 120, 110) midi.addNote(1, 12, 73, 720, 120, 104) midi.addControllerEvent(1, 12, 120, 23, 66) midi.addControllerEvent(1, 12, end + 120, 23, 0) midi.addNote(2, 2, 5, 0, 280, 33) midi.addNote(2, 2, 69, 240, 320, 97) midi.addNote(2, 2, 69, 560, 640, 88) midi.addControllerEvent(2, 2, 80, 21, 100) midi.addControllerEvent(2, 2, end + 60, 21, 0) with path.open("xb") as stream: midi.writeFile(stream) @pytest.mark.parametrize( ("numerator", "denominator", "bpm", "tempo"), [(4, 4, 120.0, 500000), (3, 4, 137.5, 436364), (6, 8, 120.125, 499480), (7, 8, 137.5, 436364)], ) def test_real_disk_and_independent_reference( workspace: Path, numerator: int, denominator: int, bpm: float, tempo: int ) -> None: plan = synthetic_plan(numerator, denominator, bpm) production = write_midi(workspace / "production.mid", plan) comparison = workspace / "midiutil.mid" reference(comparison, numerator, denominator, bpm) validate_midi_file(production, plan) observed = normalized(production) assert observed == normalized(comparison) end = 240 + numerator * 4 * 960 // denominator assert observed == [ [], [ (0, "note_on", 5, 6, 34), (100, "note_off", 5, 6, 34), (120, "control_change", 5, 22, 88), (240, "note_on", 5, 45, 83), (end, "note_off", 5, 45, 83), (end + 80, "control_change", 5, 22, 0), ], [ (20, "note_on", 12, 7, 35), (100, "note_off", 12, 7, 35), (120, "control_change", 12, 23, 66), (240, "note_on", 12, 73, 110), (360, "note_off", 12, 73, 110), (720, "note_on", 12, 73, 104), (840, "note_off", 12, 73, 104), (end + 120, "control_change", 12, 23, 0), ], [ (0, "note_on", 2, 5, 33), (80, "control_change", 2, 21, 100), (240, "note_on", 2, 69, 97), (280, "note_off", 2, 5, 33), (560, "note_off", 2, 69, 97), (560, "note_on", 2, 69, 88), (1200, "note_off", 2, 69, 88), (end + 60, "control_change", 2, 21, 0), ], ] loaded = mido.MidiFile(production) ref = mido.MidiFile(comparison) assert production.read_bytes()[:14] == (b"MThd\x00\x00\x00\x06\x00\x01\x00\x04\x03\xc0") assert loaded.type == ref.type == 1 and loaded.ticks_per_beat == ref.ticks_per_beat == 960 assert [track.name for track in loaded.tracks] == ["Conductor", "Bass", "Drums", "Lead"] assert len(ref.tracks) == 4 # MIDIUtil inserts its own conductor ahead of three user tracks. assert [track.name for track in ref.tracks[1:]] == ["Bass", "Drums", "Lead"] assert [(message.type, message.time) for message in loaded.tracks[0]] == [ ("track_name", 0), ("set_tempo", 0), ("time_signature", 0), ("end_of_track", end + 120), ] assert loaded.tracks[0][1].tempo == tempo assert tempo == round(Fraction(60_000_000) / Fraction(str(bpm))) # MIDIUtil 1.2.1 truncates, unlike the production nearest-integer policy. assert [message.tempo for message in ref.tracks[0] if message.type == "set_tempo"] == [ int(60_000_000 / bpm) ] for file in (loaded, ref): signatures = [message for message in file.tracks[0] if message.type == "time_signature"] assert len(signatures) == 1 assert (signatures[0].numerator, signatures[0].denominator) == (numerator, denominator) for track in loaded.tracks: assert sum(message.time for message in track) == end + 120 assert sum(message.type == "end_of_track" for message in track) == 1 assert track[-1].type == "end_of_track" # Reference EOTs are local last-event positions, not the production shared tail. assert [sum(message.time for message in track) for track in ref.tracks] == [ 0, end + 80, end + 120, end + 60, ] def test_repeated_input_is_byte_deterministic(workspace: Path) -> None: plan = synthetic_plan() first = write_midi(workspace / "first.mid", plan) second = write_midi(workspace / "second.mid", plan) for track in plan.tracks: track.events.reverse() third = write_midi(workspace / "reordered-input.mid", plan) assert first.read_bytes() == second.read_bytes() == third.read_bytes() validate_midi_file(third, plan) @pytest.mark.parametrize("damage", ["empty", "header", "truncated", "trailing", "track_size"]) def test_corrupt_files_fail_without_deletion(workspace: Path, damage: str) -> None: plan = synthetic_plan() path = write_midi(workspace / "damaged.mid", plan) data = path.read_bytes() if damage == "empty": broken = b"" elif damage == "header": broken = b"XXXX" + data[4:] elif damage == "truncated": broken = data[:-3] elif damage == "trailing": broken = data + b"unexplained trailing data" else: broken = data[:18] + b"\x00\x00\x00\x01" + data[22:] path.write_bytes(broken) with pytest.raises(SerializationError): validate_midi_file(path, plan) assert path.read_bytes() == broken @pytest.mark.parametrize( "change", [ "type", "ppq", "order", "name", "pitch", "velocity", "channel", "cc", "tempo", "meter", "timing", "end", "missing_release", "extra_program", ], ) def test_readable_but_wrong_content_is_not_success(workspace: Path, change: str) -> None: plan = synthetic_plan() path = write_midi(workspace / "wrong.mid", plan) loaded = mido.MidiFile(path) if change == "type": loaded.type = 2 elif change == "ppq": loaded.ticks_per_beat = 480 elif change == "order": loaded.tracks[1], loaded.tracks[2] = loaded.tracks[2], loaded.tracks[1] elif change == "name": loaded.tracks[1][0].name = "Wrong" elif change in ("pitch", "velocity", "channel"): attribute = "note" if change == "pitch" else change setattr(loaded.tracks[1][1], attribute, 11) elif change == "cc": next(message for message in loaded.tracks[1] if message.type == "control_change").value = 1 elif change == "tempo": loaded.tracks[0][1].tempo = 500000 elif change == "meter": loaded.tracks[0][2].numerator = 3 elif change == "timing": loaded.tracks[1][1].time += 1 elif change == "end": loaded.tracks[1][-1].time += 1 elif change == "missing_release": del loaded.tracks[1][2] else: loaded.tracks[1].insert(1, mido.Message("program_change", channel=5, program=1)) loaded.save(path) assert len(mido.MidiFile(path).tracks) == 4 with pytest.raises(SerializationError) as failure: validate_midi_file(path, plan) assert failure.value.details["reason"] in ("smf_header_mismatch", "smf_content_mismatch") assert path.exists() def test_writer_checks_actual_saved_content_and_cleans_mismatch( workspace: Path, monkeypatch: pytest.MonkeyPatch ) -> None: path = workspace / "changed-on-disk.mid" sentinel = workspace / "keep" sentinel.write_bytes(b"unrelated") read_bytes = Path.read_bytes observed_disk_read = [] def tamper_then_read(self: Path) -> bytes: assert self == path and path.exists() loaded = mido.MidiFile(path) loaded.tracks[1][1].note = 11 loaded.save(path) observed_disk_read.append(True) return read_bytes(path) monkeypatch.setattr(Path, "read_bytes", tamper_then_read) with pytest.raises(SerializationError) as failure: write_midi(path, synthetic_plan()) assert failure.value.details == {"reason": "smf_content_mismatch", "track": 1} assert observed_disk_read == [True] assert not path.exists() assert read_bytes(sentinel) == b"unrelated"