scale_detect.py
python
sha256:37af4ab271a64f11d7bf14b4871ebaaf5dd68fb0247521007c6b034d35aa01be
Merge branch 'fix/hub-user-read-update-path' into dev
Human
9 days ago
| 1 | """muse scale — detect the scale or mode of a MIDI track. |
| 2 | |
| 3 | Goes far beyond key signature: detects pentatonic, blues, modal, whole-tone, |
| 4 | diminished, and chromatic scales by pitch-class frequency analysis. Essential |
| 5 | for agent pipelines that need to harmonically reason about or transform a track. |
| 6 | |
| 7 | Usage:: |
| 8 | |
| 9 | muse scale tracks/melody.mid |
| 10 | muse scale tracks/lead.mid --commit HEAD~2 --top 3 |
| 11 | muse scale tracks/bass.mid --json |
| 12 | |
| 13 | Output:: |
| 14 | |
| 15 | Scale analysis: tracks/melody.mid — working tree |
| 16 | |
| 17 | Rank Root Scale Confidence Out-of-scale |
| 18 | ─────────────────────────────────────────────────────── |
| 19 | 1 D dorian 0.964 0 |
| 20 | 2 A natural minor 0.946 1 |
| 21 | 3 G major 0.929 2 |
| 22 | """ |
| 23 | |
| 24 | import argparse |
| 25 | import json |
| 26 | import logging |
| 27 | import pathlib |
| 28 | import sys |
| 29 | |
| 30 | from muse.core.types import short_id |
| 31 | from muse.core.errors import ExitCode |
| 32 | from muse.core.repo import require_repo |
| 33 | from muse.core.refs import read_current_branch |
| 34 | from muse.core.commits import resolve_commit_ref |
| 35 | from muse.plugins.midi._analysis import detect_scale |
| 36 | from muse.plugins.midi._query import load_track, load_track_from_workdir |
| 37 | from muse.core.validation import clamp_int |
| 38 | |
| 39 | logger = logging.getLogger(__name__) |
| 40 | |
| 41 | def _read_branch(root: pathlib.Path) -> str: |
| 42 | return read_current_branch(root) |
| 43 | |
| 44 | def register(subparsers: "argparse._SubParsersAction[argparse.ArgumentParser]") -> None: |
| 45 | """Register the scale subcommand.""" |
| 46 | parser = subparsers.add_parser("scale", help="Detect the scale or mode of a MIDI track by pitch-class analysis.", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 47 | parser.add_argument("track", metavar="TRACK", help="Workspace-relative path to a .mid file.") |
| 48 | parser.add_argument("--commit", "-c", metavar="REF", default=None, dest="ref", help="Analyse a historical snapshot instead of the working tree.") |
| 49 | parser.add_argument("--top", "-n", metavar="N", type=int, default=3, help="Number of top scale matches to show.") |
| 50 | parser.add_argument("--json", action="store_true", dest="as_json", help="Emit results as JSON.") |
| 51 | parser.set_defaults(func=run) |
| 52 | |
| 53 | def run(args: argparse.Namespace) -> None: |
| 54 | """Detect the scale or mode of a MIDI track by pitch-class analysis. |
| 55 | |
| 56 | ``muse scale`` tests every root × scale combination and ranks them by the |
| 57 | fraction of note weight covered by that scale's pitch classes. Supports |
| 58 | major, minor, all church modes, pentatonic, blues, whole-tone, diminished, |
| 59 | and chromatic scales. |
| 60 | |
| 61 | For agents: combine with ``muse harmony`` to get both the implied chord |
| 62 | progression and the underlying scale in one pipeline. |
| 63 | """ |
| 64 | track: str = args.track |
| 65 | ref: str | None = args.ref |
| 66 | top: int = clamp_int(args.top, 1, 10000, 'top') |
| 67 | as_json: bool = args.as_json |
| 68 | |
| 69 | root = require_repo() |
| 70 | commit_label = "working tree" |
| 71 | |
| 72 | if ref is not None: |
| 73 | branch = _read_branch(root) |
| 74 | commit = resolve_commit_ref(root, branch, ref) |
| 75 | if commit is None: |
| 76 | print(f"❌ Commit '{ref}' not found.", file=sys.stderr) |
| 77 | raise SystemExit(ExitCode.USER_ERROR) |
| 78 | result = load_track(root, commit.commit_id, track) |
| 79 | commit_label = commit.commit_id |
| 80 | else: |
| 81 | result = load_track_from_workdir(root, track) |
| 82 | |
| 83 | if result is None: |
| 84 | print(f"❌ Track '{track}' not found or not a valid MIDI file.", file=sys.stderr) |
| 85 | raise SystemExit(ExitCode.USER_ERROR) |
| 86 | |
| 87 | notes, _tpb = result |
| 88 | if not notes: |
| 89 | print(f" (no notes found in '{track}')") |
| 90 | return |
| 91 | |
| 92 | matches = detect_scale(notes)[: max(1, top)] |
| 93 | |
| 94 | if as_json: |
| 95 | print(json.dumps( |
| 96 | {"track": track, "commit": commit_label, "matches": list(matches)}, |
| 97 | )) |
| 98 | return |
| 99 | |
| 100 | print(f"\nScale analysis: {track} — {commit_label}\n") |
| 101 | print(f" {'Rank':>4} {'Root':<5} {'Scale':<20} {'Confidence':>10} {'Out-of-scale':>12}") |
| 102 | print(f" {'─' * 57}") |
| 103 | for i, m in enumerate(matches, 1): |
| 104 | print( |
| 105 | f" {i:>4} {m['root']:<5} {m['name']:<20} {m['confidence']:>10.3f} {m['out_of_scale_notes']:>12}" |
| 106 | ) |
File History
11 commits
sha256:37af4ab271a64f11d7bf14b4871ebaaf5dd68fb0247521007c6b034d35aa01be
Merge branch 'fix/hub-user-read-update-path' into dev
Human
9 days ago
sha256:b7be56ec091919a612cffe7f3c8b600209d5155517443fdac0e16954c8c7d81f
Merge 'task/git-export-ignored-file-deletion' into 'dev' — …
Human
12 days ago
sha256:8de4334a98c945aace420969d389ad678aa926d4ab4e886b2ac4c4241cb3bf2b
revert: keep pyproject.toml in canonical PEP 440 form
Sonnet 4.6
patch
15 days ago
sha256:50bb615c573aa4b928fca75b60f82ba2a659910066507cec6a95c412ae22ccb9
docs: add issue docs for push have-negotiation bug (#55) an…
Sonnet 4.6
18 days ago
sha256:d90d175cded68aae1d4ffcf4858917854195d0cd8ce1fe73cee4dbc02541cb74
chore: trigger push to surface null-OID paths
Sonnet 4.6
24 days ago
sha256:e452ad9a6ace6ccc6d875a35e06caf9da5576a970c1c36133b69a891ce5fefa8
chore: prebuild timing test
Sonnet 4.6
34 days ago
sha256:0008ab6695e3e064b3e236b24fd19e538fef6a588eb0d211622f4466d919c0b1
merge: pull staging/dev — advance to 0.2.0rc12
Sonnet 4.6
patch
36 days ago
sha256:9c33d61749fff814c5226d5386aa2af7064c2c02788594a25fdd709358132eea
fix: _PROPOSAL_PREFIX_RESOLVE_LIMIT 200 → 100 to match hub …
Sonnet 4.6
47 days ago
sha256:36c3cb3e76619d4c30a6d9bf81b5ec4ff148e30dcfed913e3114ca7b43b81c7e
fix: rename objects→blobs in push client and all stale test…
Sonnet 4.6
patch
50 days ago
sha256:c06a9b9b9fee26c68ea725b44d54b2c0a171301ce9de746d5b656617b4463a9a
fix: repair four test failures from post-migration audit
Sonnet 4.6
patch
56 days ago
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf
fix: unified object store migration — idempotent writes, JS…
Sonnet 4.6
minor
⚠
57 days ago