transpose.py
python
| 1 | """muse transpose — transpose a MIDI track by N semitones. |
| 2 | |
| 3 | Reads the MIDI file from the working tree, shifts every note's pitch by |
| 4 | the specified number of semitones, and writes the result back in-place. |
| 5 | |
| 6 | This is a surgical agent command: the content hash changes (Muse treats the |
| 7 | transposed version as a distinct composition), but every note's timing and |
| 8 | velocity are preserved exactly. Run ``muse status`` and ``muse commit`` to |
| 9 | record the transposition in the structured delta. |
| 10 | |
| 11 | Usage:: |
| 12 | |
| 13 | muse transpose tracks/melody.mid --semitones 2 # up a major second |
| 14 | muse transpose tracks/bass.mid --semitones -7 # down a fifth |
| 15 | muse transpose tracks/piano.mid --semitones 12 # up an octave |
| 16 | muse transpose tracks/melody.mid --semitones 5 --dry-run |
| 17 | |
| 18 | Output:: |
| 19 | |
| 20 | ✅ Transposed tracks/melody.mid +2 semitones |
| 21 | 23 notes shifted (C4 → D4, G5 → A5, …) |
| 22 | Pitch range: C3–A5 (was A2–G5) |
| 23 | Run `muse status` to review, then `muse commit` |
| 24 | """ |
| 25 | from __future__ import annotations |
| 26 | |
| 27 | import json |
| 28 | import logging |
| 29 | import pathlib |
| 30 | |
| 31 | import typer |
| 32 | |
| 33 | from muse.core.errors import ExitCode |
| 34 | from muse.core.repo import require_repo |
| 35 | from muse.plugins.music._query import ( |
| 36 | NoteInfo, |
| 37 | load_track_from_workdir, |
| 38 | notes_to_midi_bytes, |
| 39 | ) |
| 40 | from muse.plugins.music.midi_diff import _pitch_name |
| 41 | |
| 42 | logger = logging.getLogger(__name__) |
| 43 | |
| 44 | app = typer.Typer() |
| 45 | |
| 46 | _MIDI_MIN = 0 |
| 47 | _MIDI_MAX = 127 |
| 48 | |
| 49 | |
| 50 | @app.callback(invoke_without_command=True) |
| 51 | def transpose( |
| 52 | ctx: typer.Context, |
| 53 | track: str = typer.Argument(..., metavar="TRACK", help="Workspace-relative path to a .mid file."), |
| 54 | semitones: int = typer.Option( |
| 55 | ..., "--semitones", "-s", metavar="N", |
| 56 | help="Number of semitones to shift (positive = up, negative = down).", |
| 57 | ), |
| 58 | dry_run: bool = typer.Option( |
| 59 | False, "--dry-run", "-n", |
| 60 | help="Preview what would change without writing to disk.", |
| 61 | ), |
| 62 | clamp: bool = typer.Option( |
| 63 | False, "--clamp", |
| 64 | help="Clamp pitches to 0–127 instead of failing on out-of-range notes.", |
| 65 | ), |
| 66 | ) -> None: |
| 67 | """Transpose all notes in a MIDI track by N semitones. |
| 68 | |
| 69 | ``muse transpose`` reads the MIDI file from the working tree, shifts |
| 70 | every note's pitch by *--semitones*, and writes the result back. |
| 71 | Timing and velocity are preserved exactly. |
| 72 | |
| 73 | After transposing, run ``muse status`` to see the structured delta |
| 74 | (note-level insertions and deletions), then ``muse commit`` to record |
| 75 | the transposition with full musical attribution. |
| 76 | |
| 77 | For AI agents: this is the equivalent of ``muse patch`` for music — |
| 78 | a single command that applies a well-defined musical transformation |
| 79 | without touching anything else. |
| 80 | |
| 81 | Use ``--dry-run`` to preview the operation without writing. |
| 82 | Use ``--clamp`` to clip pitches to the valid MIDI range (0–127) |
| 83 | instead of raising an error. |
| 84 | """ |
| 85 | root = require_repo() |
| 86 | |
| 87 | result = load_track_from_workdir(root, track) |
| 88 | if result is None: |
| 89 | typer.echo(f"❌ Track '{track}' not found or not a valid MIDI file.", err=True) |
| 90 | raise typer.Exit(code=ExitCode.USER_ERROR) |
| 91 | |
| 92 | original_notes, tpb = result |
| 93 | |
| 94 | if not original_notes: |
| 95 | typer.echo(f" (track '{track}' contains no notes — nothing to transpose)") |
| 96 | return |
| 97 | |
| 98 | # Validate pitch range. |
| 99 | new_pitches = [n.pitch + semitones for n in original_notes] |
| 100 | out_of_range = [p for p in new_pitches if p < _MIDI_MIN or p > _MIDI_MAX] |
| 101 | if out_of_range and not clamp: |
| 102 | lo = min(out_of_range) |
| 103 | hi = max(out_of_range) |
| 104 | typer.echo( |
| 105 | f"❌ Transposing by {semitones:+d} semitones would produce " |
| 106 | f"out-of-range MIDI pitches ({lo}–{hi}). " |
| 107 | f"Use --clamp to clip to 0–127.", |
| 108 | err=True, |
| 109 | ) |
| 110 | raise typer.Exit(code=ExitCode.USER_ERROR) |
| 111 | |
| 112 | # Build transposed notes. |
| 113 | transposed: list[NoteInfo] = [] |
| 114 | for note in original_notes: |
| 115 | new_pitch = max(_MIDI_MIN, min(_MIDI_MAX, note.pitch + semitones)) |
| 116 | transposed.append(NoteInfo( |
| 117 | pitch=new_pitch, |
| 118 | velocity=note.velocity, |
| 119 | start_tick=note.start_tick, |
| 120 | duration_ticks=note.duration_ticks, |
| 121 | channel=note.channel, |
| 122 | ticks_per_beat=note.ticks_per_beat, |
| 123 | )) |
| 124 | |
| 125 | old_lo = min(n.pitch for n in original_notes) |
| 126 | old_hi = max(n.pitch for n in original_notes) |
| 127 | new_lo = min(n.pitch for n in transposed) |
| 128 | new_hi = max(n.pitch for n in transposed) |
| 129 | |
| 130 | sign = "+" if semitones >= 0 else "" |
| 131 | sample_pairs = [ |
| 132 | f"{_pitch_name(original_notes[i].pitch)} → {_pitch_name(transposed[i].pitch)}" |
| 133 | for i in range(min(3, len(original_notes))) |
| 134 | ] |
| 135 | |
| 136 | if dry_run: |
| 137 | typer.echo(f"\n[dry-run] Would transpose {track} {sign}{semitones} semitones") |
| 138 | typer.echo(f" Notes: {len(original_notes)}") |
| 139 | typer.echo(f" Shifts: {', '.join(sample_pairs)}, …") |
| 140 | typer.echo(f" Pitch range: {_pitch_name(new_lo)}–{_pitch_name(new_hi)} " |
| 141 | f"(was {_pitch_name(old_lo)}–{_pitch_name(old_hi)})") |
| 142 | typer.echo(" No changes written (--dry-run).") |
| 143 | return |
| 144 | |
| 145 | midi_bytes = notes_to_midi_bytes(transposed, tpb) |
| 146 | |
| 147 | # Write back to the working tree. |
| 148 | work_path = root / "muse-work" / track |
| 149 | if not work_path.parent.exists(): |
| 150 | work_path = root / track |
| 151 | work_path.write_bytes(midi_bytes) |
| 152 | |
| 153 | typer.echo(f"\n✅ Transposed {track} {sign}{semitones} semitones") |
| 154 | typer.echo(f" {len(transposed)} notes shifted ({', '.join(sample_pairs)}, …)") |
| 155 | typer.echo(f" Pitch range: {_pitch_name(new_lo)}–{_pitch_name(new_hi)}" |
| 156 | f" (was {_pitch_name(old_lo)}–{_pitch_name(old_hi)})") |
| 157 | typer.echo(" Run `muse status` to review, then `muse commit`") |