retrograde.py
python
| 1 | """muse retrograde — reverse the note order of a MIDI track. |
| 2 | |
| 3 | Plays the melody backward: the last note becomes the first. A classical |
| 4 | transformation used in canon, fugue, and serial music. Agents composing |
| 5 | palindromic or mirror structures can apply this automatically. |
| 6 | |
| 7 | Usage:: |
| 8 | |
| 9 | muse retrograde tracks/melody.mid |
| 10 | muse retrograde tracks/melody.mid --dry-run |
| 11 | |
| 12 | Output:: |
| 13 | |
| 14 | ✅ Retrograded tracks/melody.mid |
| 15 | 23 notes reversed (C4 → was last, now first) |
| 16 | Duration preserved · original span: 8.0 beats |
| 17 | Run `muse status` to review, then `muse commit` |
| 18 | """ |
| 19 | |
| 20 | from __future__ import annotations |
| 21 | |
| 22 | import argparse |
| 23 | import logging |
| 24 | import pathlib |
| 25 | import sys |
| 26 | |
| 27 | from muse.core.errors import ExitCode |
| 28 | from muse.core.validation import contain_path |
| 29 | from muse.core.repo import require_repo |
| 30 | from muse.plugins.midi._query import NoteInfo, load_track_from_workdir, notes_to_midi_bytes |
| 31 | from muse.plugins.midi.midi_diff import _pitch_name |
| 32 | |
| 33 | logger = logging.getLogger(__name__) |
| 34 | |
| 35 | |
| 36 | def register(subparsers: "argparse._SubParsersAction[argparse.ArgumentParser]") -> None: |
| 37 | """Register the retrograde subcommand.""" |
| 38 | parser = subparsers.add_parser("retrograde", help="Reverse the pitch order of all notes (retrograde transformation).", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 39 | parser.add_argument("track", metavar="TRACK", help="Workspace-relative path to a .mid file.") |
| 40 | parser.add_argument("--dry-run", "-n", action="store_true", help="Preview without writing.") |
| 41 | parser.set_defaults(func=run) |
| 42 | |
| 43 | |
| 44 | def run(args: argparse.Namespace) -> None: |
| 45 | """Reverse the pitch order of all notes (retrograde transformation). |
| 46 | |
| 47 | ``muse retrograde`` maps note positions: the note that was at tick T from |
| 48 | the end is placed at tick T from the start, so the last note plays first. |
| 49 | Durations and velocities are preserved; only pitch and position are swapped. |
| 50 | |
| 51 | This is a foundational operation in serial/twelve-tone composition and is |
| 52 | impossible to describe meaningfully in Git's binary-blob model. |
| 53 | """ |
| 54 | track: str = args.track |
| 55 | dry_run: bool = args.dry_run |
| 56 | |
| 57 | root = require_repo() |
| 58 | result = load_track_from_workdir(root, track) |
| 59 | if result is None: |
| 60 | print(f"❌ Track '{track}' not found or not a valid MIDI file.", file=sys.stderr) |
| 61 | raise SystemExit(ExitCode.USER_ERROR) |
| 62 | |
| 63 | notes, tpb = result |
| 64 | if not notes: |
| 65 | print(f" (track '{track}' contains no notes — nothing to retrograde)") |
| 66 | return |
| 67 | |
| 68 | # Sort by start time to get the temporal order, then reverse pitches. |
| 69 | by_time = sorted(notes, key=lambda n: n.start_tick) |
| 70 | reversed_pitches = [n.pitch for n in reversed(by_time)] |
| 71 | |
| 72 | total_ticks = max(n.start_tick + n.duration_ticks for n in notes) |
| 73 | retro: list[NoteInfo] = [ |
| 74 | NoteInfo( |
| 75 | pitch=reversed_pitches[i], |
| 76 | velocity=by_time[i].velocity, |
| 77 | start_tick=by_time[i].start_tick, |
| 78 | duration_ticks=by_time[i].duration_ticks, |
| 79 | channel=by_time[i].channel, |
| 80 | ticks_per_beat=by_time[i].ticks_per_beat, |
| 81 | ) |
| 82 | for i in range(len(by_time)) |
| 83 | ] |
| 84 | |
| 85 | span_beats = total_ticks / max(tpb, 1) |
| 86 | first_orig = _pitch_name(by_time[0].pitch) |
| 87 | first_retro = _pitch_name(retro[0].pitch) |
| 88 | |
| 89 | if dry_run: |
| 90 | print(f"\n[dry-run] Would retrograde {track}") |
| 91 | print(f" Notes: {len(notes)}") |
| 92 | print(f" Was: first note = {first_orig}") |
| 93 | print(f" Would: first note = {first_retro}") |
| 94 | print(f" Span: {span_beats:.2f} beats (unchanged)") |
| 95 | print(" No changes written (--dry-run).") |
| 96 | return |
| 97 | |
| 98 | midi_bytes = notes_to_midi_bytes(retro, tpb) |
| 99 | workdir = root |
| 100 | try: |
| 101 | work_path = contain_path(workdir, track) |
| 102 | except ValueError as exc: |
| 103 | print(f"❌ Invalid track path: {exc}") |
| 104 | raise SystemExit(ExitCode.USER_ERROR) |
| 105 | work_path.parent.mkdir(parents=True, exist_ok=True) |
| 106 | work_path.write_bytes(midi_bytes) |
| 107 | |
| 108 | print(f"\n✅ Retrograded {track}") |
| 109 | print(f" {len(retro)} notes reversed ({first_orig} → was last, now first)") |
| 110 | print(f" Duration preserved · original span: {span_beats:.2f} beats") |
| 111 | print(" Run `muse status` to review, then `muse commit`") |