Sort initial_key harmonic (by circle of fiths)#6669
Draft
JOJ0 wants to merge 1 commit into
Draft
Conversation
|
Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #6669 +/- ##
==========================================
+ Coverage 72.50% 72.51% +0.01%
==========================================
Files 161 163 +2
Lines 20766 20815 +49
Branches 3288 3291 +3
==========================================
+ Hits 15056 15094 +38
- Misses 4984 4996 +12
+ Partials 726 725 -1
🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
grug see PR want make initial_key sort harmonic (Circle of Fifths) instead of plain abc. this fit beets library sort system, so user can do better DJ / playlist flow.
Changes:
- add
beets.util.musictheorywith key normalize + Circle-of-Fifths position function - add
HarmonicKeySortand wire it as default sort handler forItem.initial_key(config gatesort_initial_key_harmonic) - move
MusicalKeyparsing to reuse shared normalize helper; add default config option enabled
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| beets/util/musictheory.py | new shared key normalization + harmonic position mapping |
| beets/library/models.py | register initial_key to use HarmonicKeySort |
| beets/dbcore/types.py | reuse shared normalize helper for MusicalKey.parse() |
| beets/dbcore/sort.py | implement config-gated harmonic sorting (python slow path) |
| beets/dbcore/init.py | export HarmonicKeySort via __all__ |
| beets/config_default.yaml | add default sort_initial_key_harmonic: yes |
Comment on lines
+224
to
+243
| def is_slow(self) -> bool: | ||
| return self._harmonic_enabled() | ||
|
|
||
| def order_clause(self) -> str | None: | ||
| if self._harmonic_enabled(): | ||
| return None # Python-level sort; no SQL ORDER BY fragment | ||
| return FixedFieldSort( | ||
| self.field, self.ascending, self.case_insensitive | ||
| ).order_clause() | ||
|
|
||
| def sort(self, objs: list[AnyModel]) -> list[AnyModel]: | ||
| if not self._harmonic_enabled(): | ||
| return FieldSort.sort(self, objs) | ||
| from beets.util.musictheory import harmonic_sort_key | ||
|
|
||
| return sorted( | ||
| objs, | ||
| key=lambda obj: harmonic_sort_key(obj.get(self.field)), | ||
| reverse=not self.ascending, | ||
| ) |
Comment on lines
+208
to
+243
| class HarmonicKeySort(FieldSort): | ||
| """Sort by musical key in Circle of Fifths order when | ||
| ``sort_initial_key_harmonic`` is enabled; falls back to standard | ||
| case-insensitive alphabetical ordering otherwise. | ||
|
|
||
| Applies enharmonic normalization before lookup, so keys stored in either | ||
| flat or sharp notation sort correctly relative to each other. Keys not | ||
| present in the Circle of Fifths (or missing entirely) sort to the end. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def _harmonic_enabled() -> bool: | ||
| import beets | ||
|
|
||
| return beets.config["sort_initial_key_harmonic"].get(bool) | ||
|
|
||
| def is_slow(self) -> bool: | ||
| return self._harmonic_enabled() | ||
|
|
||
| def order_clause(self) -> str | None: | ||
| if self._harmonic_enabled(): | ||
| return None # Python-level sort; no SQL ORDER BY fragment | ||
| return FixedFieldSort( | ||
| self.field, self.ascending, self.case_insensitive | ||
| ).order_clause() | ||
|
|
||
| def sort(self, objs: list[AnyModel]) -> list[AnyModel]: | ||
| if not self._harmonic_enabled(): | ||
| return FieldSort.sort(self, objs) | ||
| from beets.util.musictheory import harmonic_sort_key | ||
|
|
||
| return sorted( | ||
| objs, | ||
| key=lambda obj: harmonic_sort_key(obj.get(self.field)), | ||
| reverse=not self.ascending, | ||
| ) |
| """Normalize a musical key string to canonical form. | ||
|
|
||
| Applies flat-to-sharp enharmonic conversion, handles ``minor``/``major`` | ||
| suffixes, and capitalizes the result. Mirrors the logic in |
Comment on lines
+33
to
+40
| from .sort import HarmonicKeySort | ||
| from .types import Type | ||
|
|
||
| __all__ = [ | ||
| "AndQuery", | ||
| "Database", | ||
| "FieldQuery", | ||
| "HarmonicKeySort", |
77243ea to
edcd32c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Enable sorting the
initial_keyfield by the circle of fiths. For now, first major then minor keys (TBD make configurable?)I even propose to set this as the default sorting behavior for this field. To disable it (revert back to alphabetical sorting the user can set
sort_initial_key_harmonic: noSolves the core part of #6241
To Do