Repository navigation
feat: Map Matroska AlbumArtistSort and ComposerSort - #138
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #138 +/- ##
==========================================
+ Coverage 93.29% 93.40% +0.10%
==========================================
Files 409 415 +6
Lines 86202 88291 +2089
Branches 9886 10288 +402
==========================================
+ Hits 80421 82464 +2043
- Misses 3412 3429 +17
- Partials 2369 2398 +29 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
decriptor
force-pushed
the
feat/matroska-sort-names
branch
8 times, most recently
from
November 22, 2026 22:42
a57ba1e to
723be21
Compare
MatroskaTag now reads and writes AlbumArtistSort and ComposerSort. Both were no-ops from the Tag base. Each reads the SORT_WITH nested in the value it sorts: the album-level ARTIST, or a flat ALBUM_ARTIST when the file has one, for AlbumArtistSort, and COMPOSER for ComposerSort. A flat ALBUM_ARTIST_SORT or COMPOSER_SORT is the fallback. Flat sorts follow one ownership model, written up in the PR: - A flat sort belongs to the value in its own Tag. - A repeated equal value is one run. - A value-less sort at 50, 60 or 70 passes down to the nearest value. Get, set, clear and CopyTo follow from that model. Setting replaces the sort in the value's own Tag, and a clear takes only that value's sorts. Before a write would change a sort shared through inheritance, the setter copies ARTIST to the track level, so ArtistSort and AlbumArtistSort stay independent. Item fields (Year, Comment, Genre, Composers, ...) are written where their getter reads them, and a clear removes the name at every level the getter sees. Writing back the value read changes nothing. Each setter checks that first, against what its own getter returns, following Tag.IsWriteBack; CopyTo replaces the target's list. The five sort names reached through the custom-field API behave exactly like their properties. A model test checks reads, write-back no-ops, single copies, sort ownership and clear completeness over 333,643 shape and operation cases. TagCopyReplacesListTests covers Matroska. Closes #116
decriptor
force-pushed
the
feat/matroska-sort-names
branch
from
November 22, 2026 23:14
4859a54 to
267ea3f
Compare
Owner
Author
|
Review: 0 findings at 267ea3f. The PR is now squashed to one commit, and its tree is identical to the reviewed fix head 4859a54. What was checked:
|
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
MatroskaTagnow overridesAlbumArtistSortandComposerSort. Before this change both were no-ops from theTagbase.Album artist. This is the album-level
ARTIST(TargetTypeValue50), per the spec.ALBUM_ARTIST(the name ffmpeg writes), that tag is the album artist.AlbumArtistsreads and writes it, so it pairs with the file'sALBUM_ARTIST_SORT, the way ffmpeg and exiftool pair them.AlbumArtistson such a file removes bothALBUM_ARTISTandALBUM_ARTIST_SORT.AlbumArtistSortreadsSORT_WITHnested in the album-artist SimpleTag described above. The fallback is the flatALBUM_ARTIST_SORTthat belongs to it (see the flat-sort rule below). On a file with a track level that is the album level's run: a value-lessALBUM_ARTIST_SORTat 60 or 70 is read, cleared and replaced like any other sort, and the track and album title sorts follow the same rule.ComposerSortreadsSORT_WITHnested in theCOMPOSERthatComposersreads. The fallback is the flatCOMPOSER_SORTthat belongs to it.Flat sorts follow the sort ownership model below. It covers which value a sort belongs to, repeated values, value-less sorts, and get, set, clear and CopyTo.
Precedence and set/clear follow
ArtistSortandAlbumSort:ARTISTto the track level, as described below.""removes the nested sort and the flat sort at every level where it belongs to that value. All five Matroska sort setters now treat""like null, the way the parent setters already do.The two artist sorts stay independent. When the track has no
ARTISTof its own, it inherits the album's, andArtistSortandAlbumArtistSortboth read oneSORT_WITH. Files written by TagLib#, and any single-level file with a nested sort, are like this.ARTISTat that level to the track level (30). If theARTISTcame from above the album, it is also copied to the album level.ARTIST_SORTandALBUM_ARTIST_SORTstill reach the original's, and no second copy is left. After the copy, each property keeps its own value, andArtist,PerformersandAlbumArtistsread the same names as before.ARTISThas noSORT_WITH, or when the file names its album artist withALBUM_ARTIST.Item fields are written where their getter reads them. This covers
Year,Comment,Genre/Genres,Composersand the other fields that apply to the whole file.Genre = nullno longer leaves an inherited genre showing. That affected any file with a track-level Tag, and the copy above creates one on single-level files.ARTIST(and its sort). It never goes above 50, the album.Writing back the value read is a no-op in every setter this PR reaches (the sorts, Title, Artist, Performers, Album, AlbumArtists, Composers and the whole-file fields), so the bytes stay as they were. It follows main's
Tag.IsWriteBack(fix: Drop a terminating NUL from ID3 text #136):""is a write-back only when no value holds text, andCopyToreplaces the target's list, so a copy of ISRCAinto a file holdingAandBleaves onlyA, the way ID3v2, APE and Vorbis do (model rules 8 and 9).Publisher = nullalso removesLABEL.ReplayGainTrackGain/ReplayGainTrackPeakreplace the olderREPLAYGAIN_GAIN/REPLAYGAIN_PEAKcopy, as their getters read those names.Custom fields named after a flat sort (
TITLE_SORT,ARTIST_SORT,ALBUM_SORT,ALBUM_ARTIST_SORT,COMPOSER_SORT, feat: Read and write an arbitrary field on any tag #139) are read, set and removed through the matching sort property, so they pair with their value exactly as the property does; a sort takes one value. Other names stay raw, as feat: Read and write an arbitrary field on any tag #139 defines them.Pairing a flat sort is linear in the number of Tags. A step counter pins this.
Sort ownership model (Matroska)
Levels are
TargetTypeValues. A tag applies at its level and at every narrower level, until a narrower level sets the same name; an empty string counts as setting it (tagging.html, "Target types"; RFC 9559 §24.2). The spec defines only the nestedSORT_WITH, "a child SimpleTag ... to indicate what alternative value the parent SimpleTag element can have to be sorted" ("Nested Information"). The flatARTIST_SORT,ALBUM_ARTIST_SORTandCOMPOSER_SORTare the ffmpeg convention:-metadatakeys written upper-case into one Tag, beside the value. Only file-level Tags take part: a Tag whose Targets name a chapter, attachment, track or edition UID belongs to that item, and no rule below reads or writes it.SORT_WITHsorts its own parent SimpleTag and nothing else.X_SORTsorts theXin its own Tag.ARTISTat 50) once the file has a level below the album, asAlbumArtistsreads it there. With no value at that level, the run is that level and the value-less levels above it. Every rule below applies to these runs too.X_SORTin a Tag with noX(at 50, 60 or 70) passes down to the value below it, so it belongs to the run of the nearest value below it.SORT_WITHon the SimpleTag the getter reads. If there is none, it is the first flatX_SORTin the item's run.SORT_WITH, that child is replaced.SORT_WITHon the SimpleTag read and every flatX_SORTin the run, including value-less ones above it. A null is always a clear, so nothing a broader level holds is left behind for another reader.Artist,Performers,AlbumArtists,Composers,Title,Album):Titlealready did on main (Save_SettingTitle_KeepsSortAndOtherLanguages) and as a separate sort field does in every other format.AlbumArtists = ["Coll"]under a collectionARTIST=Collwith aSORT_WITH) repeats that SimpleTag, itsSORT_WITHincluded, so a narrower level that read the broader value reads the same sort.ARTIST=Ada(withARTIST_SORT=Ada sort) to the track'sSolomakesAda sortthe track's sort too, which is what main, ffprobe and exiftool already read from those bytes; clearing aBandthat sat between twoAdas joins them.Tag.IsWriteBack, fix: Drop a terminating NUL from ID3 text #136). Every setter checks first, before it changes anything, whether it was handed what its getter returns; the check reads the same list the getter returns the first of, so the two cannot disagree. Outside a copy, writing back the value read is a no-op and the bytes stay as they were."", or a list with no text, is the value read only when the getter reads none and no value of the name holds text where the setter clears (fix: Drop a terminating NUL from ID3 text #136), so the clear would remove nothing. A null clears every level the getter climbs, including a value a broader level holds behind an empty one; only a setter that writes one level (Title,Artist,Album,Performers, the spec-levelAlbumArtists) treats a null over a field that reads null as a no-op, as its clear could only uncover a broader value.SORT_WITHis unchanged in a copy, as the flat sorts beside it are separate fields (main's rule for a Vorbis fallback field); a flat sort read beside other texts is rewritten as the one copy. A source with no sort leaves the target's sort as it was, because CopyTo skips nulls.ARTISTat 30 keepsArtistSortapart fromAlbumArtistSort. It is a separate SimpleTag, so its nestedSORT_WITHis its own (rule 1).ARTIST_SORTandALBUM_ARTIST_SORT, and no second copy is left.ALBUM_ARTIST_SORT, notARTIST_SORT.What other tools do with each case (black box, on files built from ffmpeg's
base.mka;r5/blackbox-model.txtand the review'sblackbox.txt):ARTIST=Ada,ARTIST_SORT=Ada sortArtist Ada,ArtistSort Ada sortAda sortARTIST=Ada,ARTIST_SORT=Ada sortArtist Ada×2,ArtistSort Ada sortAda sort(same value, one run)ARTIST/SORT_WITH=ada nested, mediainfoPerformer_SORT_WITHSortWith ada nestedalbumArtistsSort ada nestedArtist Band,Artist Solo, oneArtistSort xNo reader pairs a flat sort with a value; ffprobe and mediainfo merge by name. Where they differ from us, the spec's own design, a sort as a child of its value, supports the pairing.
Motivation and Context
Spec: https://www.matroska.org/technical/tagging.html.
SORT_WITHis a child SimpleTag giving the parent's sort value.ARTISTat 50 is the album artist.Flat names: ffmpeg writes
-metadatakeys as upper-case SimpleTags, for exampleALBUM_ARTISTandALBUM_ARTIST_SORT. exiftool reads those asAlbumArtistandAlbumArtistSort, andCOMPOSER_SORTasComposerSort.Closes #116
Type of Change
Checklist
dotnet test)Testing
Gates
dotnet build -c Release: 0 warnings, 0 errors, all four target frameworks.dotnet test -c Release(on main e1dc0ab): net8.0 7153 passed, 57 skipped, 0 failed (7210). net10.0 the same. The skips are the env-gated integration tests.dotnet format --verify-no-changes: clean.Unit tests are in
MatroskaSortNameTests.cs,MatroskaSortIndependenceTests.cs,MatroskaSortOwnershipTests.cs,MatroskaSortModelTests.cs,MatroskaWriteBackTests.csandMatroskaCopyReplacesTests.cs, and main'sTagCopyReplacesListTestsnow has Matroska rows. They include:""on each of the two artist sorts). Each operation must change only its own property. Every other getter (title, album, artist, performers, album artists, composers, and the other sorts) must stay the same in memory and after a render.ALBUM_ARTIST: read, set, clear, and the nested sort winning over the flat name.""clearing all five sort setters.CopyTointo a single-level nested target.ARTIST(with aGENREthat must be replaced, not duplicated), and a flatALBUM_ARTIST.Genre,Comment,Year,Genres) × {clear, set}. Each field must read null or the new value from exactly one SimpleTag, in memory and after a render, and the sort must hold.SORT_WITH, both, flat sort only; × track filler) × {sort set, null,"", value set,CopyTo}. It requires that no value or sort belonging to another level changes, and that a copied sort sits with its copied value. It fails at 41fd351.WritingBackTheValueRead_ChangesNoBytes: about 3,800 shapes × 19 setters, each byte-identical. Also tests for null clears behind a blocker and for fallback names.SortPairing_IsLinearInTheTags: 3,000 Tags at distinct TargetTypeValues, at most 64 Tag visits per Tag.SortAndValueWrites_OnHostileAndEmptyFields_StayConsistent: empty and missing TagString, TagName and TargetTypeValue; SORT_WITH nested 200 deep; Tags that were read empty.ARTISTcopy for a bareARTISTor besideALBUM_ARTIST;ALBUM_ARTISTstays at its own level;AlbumArtists = [""]clears; new fields stay at 50 after a copy; the Tag order follows fix: Update Matroska cluster positions and disc numbers #132; an emptied Tag is dropped and memory matches the saved file.Targeted reverts on the new code. Each one, applied alone, makes the Matroska tests fail:
ALBUM_ARTISTnot used as the album artist""not treated as nullARTISTto the track before a shared writeARTIST46 further guard reverts each fail a named test. They are listed in the round-3 fix report: flat-sort pairing and its linear bound, write level, clear range, the album cap, copies carrying their sorts, artist-only and dropped Tags, value clears taking their sorts, the write-back no-ops, null clears, and fallback names.
Integration tests:
MatroskaIntegrationTests(sync and async), gated onTAGLIBSHARP2_TEST_MATROSKA. The same helper runs in CI on built files. Locally they passed on all 20 files below, beside the #140 track tests in the same class. The helper now also sets and clearsGenreafter the sort writes, including the TagLib#-writtentls.mkaandnest1-plain.mka.Real files: TaglibSharpSamples
matroska/test1-8.mkv, taglib-sharpTurning Lime.mkv, ffmpeg-madegen.mka/gen.mkv/gen.webm/plain.mka, and an ffmpeg file withALBUM_ARTISTandALBUM_ARTIST_SORT. Also a TagLib#-writtentls.mka, where every tag is at level 50 with a nestedARTISTsort, and fixtures with a nested sort injected. Each file went through set, clear, an injected nestedSORT_WITH, then set and clear again.ffmpeg -c copy -f framemd5were identical every time. ffmpeg cannot opentest4at all, original included.test7prints the same "Invalid track number 87" as its original.ffmpeg -f nulldecodes cleanly.tls.mka.tls.mka,ArtistSortstaysVariousafterAlbumArtistSortis set and after it is cleared.Before/after, round 1 (against 880056d), using the review's 1536-row grid and the corpus of 583 files:
ArtistSortis now kept on set and on clear. This is the shared-ARTISTcase.= "".AlbumArtistschanged only on the ffmpeg files that carryALBUM_ARTIST. Nothing else changed.Before/after, round 2 (against 228f12a rebased on 304bc06, and against main 304bc06). Grid2 from review: 720 shapes × 9 operations, then
Genre = null.The follow-on genre clear failed in 624 to 720 shapes per operation at main and at the old head. Now it fails in 0.
The only cells worse than the old head are 4
AlbumArtists = []cells. There,ALBUM_ARTISTwas the only track-level tag, so the cleared Tag is dropped and the collectionARTISTbecomes the album artist. At the old head, memory said[]but the saved file read[A70]. Now both say[A70].Round-1 grid and the 569-file corpus are unchanged against the old head.
TagLib#-written
tls2.mka:ArtistSort = x; Genre = null; Comment = nullleaves no genre or comment in our reader, ffprobe or TagLib#.Genre = Jazzreads Jazz in all three. A newConductorafter the copy is read by TagLib#.SortModel_HoldsForEveryShapeAndOperationencodes the model above as an oracle that reads the rendered bytes; no part of it calls or copies the library's pairing code. It runs 333,643 cases: 4 value/sort pairs × (432 shapes with distinct texts, 432 with one text repeated, 3 × 432 mixed AAB/ABA/ABB shapes, 8 × 216 shapes with empty texts, 216 shapes beside Tags of a chapter, attachment, track and edition that hold the pair's names, two values at a level, value-less album-artist sorts at 60/70, the review's shapes R1–R5 and the ffmpeg untargeted Tag) × up to 31 operations. It skips no case. For every case it checks that:It fails at 34f8331 (1,868 of 333,643 cases) and passes now.
Before/after, round 6 (against 34f8331 with the review's harnesses; main de3ac07 for the corpus):
Title = Aover the segment title A and30{TITLE=A,TITLE=Zq2}leaves30{TITLE=A};Composers = Composersover an emptyCOMPOSERkeepsCOMPOSER_SORT=xbyte for byte;AlbumArtists = []on70{ARTIST=Ada,ARTIST_SORT=x} 50{ARTIST=Ada}keeps the collection'sx, so the track still readsAda/x(also forALBUM_ARTIST_SORT).TITLE=A,COMPOSER_SORT=xandARTIST_SORT=x;ffmpeg -f nulldecodes cleanly. Fuzz: 33,936 truncation and bit-flip cases, 0 throws. Env-gatedMatroskaIntegrationTestspass on 41 files.Before/after, round 5 (against b378bea with the review's harnesses; main f1ecb88 for the corpus):
get: a value-lessALBUM_ARTIST_SORTabove an album with a track level is now read (F3).[A],[C1],Performers [Ada]) while a plainIsrc = "A"stays byte-identical; trigger 4 is a no-op; the F3 triggers read, clear (ffprobe and exiftool no longer showx) and replace in place;AlbumArtists = [Coll]under a nested collection artist keepscoll sort.base.mka: T3, T4, T7, T8, T9, T10): ffprobe, exiftool and mediainfo read the new values, andffmpeg -f nulldecodes cleanly. Fuzz: 22,586 truncation and bit-flip cases, 0 throws.MatroskaIntegrationTestspass on 39 files.Before/after, round 4 (against 5dd208e and ac510c3, using the review's harnesses):
idem/idem2probes now readAda sort,Bach, Jandx sortfor the repeated R5 layouts, and the ffmpeg clear afterArtist = ArtistremovesARTIST_SORTin ffprobe, exiftool and mediainfo.interpkeepsxfor the track: it moves to 30 whenAlbumArtists = ["Band"]is placed between.perfclears the track'sARTIST_SORTwithPerformers = [].Before/after, round 3 (against 41fd351, using the review's harnesses):
COMPOSER_SORTwith noCOMPOSERanywhere, which goes to 50 instead of an artist-only 30 Tag; the review called this neutral.ffs.mka, afterA=Solo AS="Solo, X"and afterCopyTo, exiftool showsAda / Ada sortandSolo / Solo, X, the same as main.A=NEWA,AA=-) change only because a sort now follows its value; for example, clearing the only album artist no longer leaves its sort readable.C=-on the ffmpeg shape) now clear the removed composer'sCOMPOSER_SORT.Additional Notes
main, not changed here. Ontest1.mkvandtest6.mkv, any save movesTagsto the end of the segment and drops its SeekHead entry, so ffprobe and mediainfo then see no tags. TagLibSharp2 still re-reads them.AlbumArtistsSortandComposersSort.PERFORMERas the track artist.AlbumArtist(singular) on Matroska.