From 29370c659cd1957c94717532129b3c44ff33b433 Mon Sep 17 00:00:00 2001 From: Stephen Shaw Date: Tue, 17 Nov 2026 16:02:52 -0700 Subject: [PATCH 1/3] fix: Keep Matroska track and album titles apart TITLE at target 30 is the track. TITLE at target 50 is the album. Setting one no longer replaces the other, and an album-only ARTIST is not replaced when the track artist is set. --- src/TagLibSharp2/Matroska/MatroskaTag.cs | 57 ++++++++++-------- .../Matroska/MatroskaFileTests.cs | 59 +++++++++++++++---- .../Matroska/MatroskaSaveTests.cs | 31 ++++++---- 3 files changed, 102 insertions(+), 45 deletions(-) diff --git a/src/TagLibSharp2/Matroska/MatroskaTag.cs b/src/TagLibSharp2/Matroska/MatroskaTag.cs index d0f1474..3f7c694 100644 --- a/src/TagLibSharp2/Matroska/MatroskaTag.cs +++ b/src/TagLibSharp2/Matroska/MatroskaTag.cs @@ -32,20 +32,19 @@ internal MatroskaTag () /// public override string? Title { get { - // An empty TITLE is a real value and blocks the segment title. - // Any other tag does not. - var values = Values ("TITLE"); - if (values.Count > 0) - return First ("TITLE"); + // TITLE at 30 is the track. An empty one blocks the segment title. + // TITLE at 50 is the album and is not this property. + if (HasFileLevelName (TrackLevel, "TITLE")) + return FirstAt (TrackLevel, "TITLE"); return _segmentTitle; } - set => SetItem ("TITLE", value); + set => SetAt (TrackLevel, "TITLE", value); } /// public override string? Artist { get => First ("ARTIST"); - set => SetItem ("ARTIST", value); + set => SetAt (TrackLevel, "ARTIST", value); } /// @@ -54,12 +53,12 @@ public override string[] Performers { var artists = All ("ARTIST"); return artists.Length > 0 ? artists : All ("ACTOR"); } - set => SetItemValues ("ARTIST", value); + set => SetAtValues (TrackLevel, "ARTIST", value); } /// public override string? Album { - get => ItemLevel >= AlbumLevel ? null : FirstAt (AlbumLevel, "TITLE"); + get => FirstAt (AlbumLevel, "TITLE"); set => SetAt (AlbumLevel, "TITLE", value); } @@ -221,8 +220,8 @@ public override string? DateTagged { /// public override string? TitleSort { - get => NestedSort ("TITLE") ?? First ("TITLE_SORT"); - set => SetSort ("TITLE", "TITLE_SORT", value, null); + get => NestedSortAt (TrackLevel, "TITLE") ?? FirstAt (TrackLevel, "TITLE_SORT"); + set => SetSort ("TITLE", "TITLE_SORT", value, TrackLevel); } /// @@ -233,15 +232,8 @@ public override string? ArtistSort { /// public override string? AlbumSort { - get => (ItemLevel < AlbumLevel ? NestedSortAt (AlbumLevel, "TITLE") : null) ?? First ("ALBUM_SORT"); - set { - if (ItemLevel < AlbumLevel) - SetSort ("TITLE", "ALBUM_SORT", value, AlbumLevel); - else if (value is null) - RemoveFlatSort ("ALBUM_SORT"); - else - SetItem ("ALBUM_SORT", value); - } + get => NestedSortAt (AlbumLevel, "TITLE") ?? FirstAt (AlbumLevel, "ALBUM_SORT"); + set => SetSort ("TITLE", "ALBUM_SORT", value, AlbumLevel); } /// @@ -368,6 +360,16 @@ internal ulong ItemLevel { } } + bool HasFileLevelName (ulong level, string name) + { + foreach (var block in _blocks) { + if (block.IsFileLevel && block.Level == level && block.HasName (name)) + return true; + } + + return false; + } + string? First (string name) { var values = Values (name); @@ -475,10 +477,17 @@ void SetSort (string name, string flatName, string? value, ulong? level) var replaced = level is ulong fixedLevel ? ReplaceNestedSortAt (fixedLevel, name, value) : ReplaceNestedSort (name, value); - if (value is null) - RemoveFlatSort (flatName); - else if (!replaced) - SetItem (flatName, value); + if (value is null) { + if (level is ulong clearLevel) + RemoveAt (clearLevel, flatName); + else + RemoveFlatSort (flatName); + } else if (!replaced) { + if (level is ulong writeLevel) + SetAt (writeLevel, flatName, value); + else + SetItem (flatName, value); + } } /// diff --git a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs index 4a60622..a684a71 100644 --- a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs +++ b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs @@ -77,8 +77,8 @@ public void Read_ChapterTitle_IsNotTheFileTitle () var tag = MatroskaFile.Read (file).File!.Tag; - Assert.AreEqual ("Movie", tag.Title); - Assert.IsNull (tag.Album); + Assert.IsNull (tag.Title); + Assert.AreEqual ("Movie", tag.Album); } [TestMethod] @@ -400,16 +400,20 @@ public void SortSet_SharedAlbumTitle_KeepsBothSorts () Tag (30, Simple ("GENRE", "Jazz")))); var tag = MatroskaFile.Read (file).File!.Tag; + Assert.IsNull (tag.Title); + Assert.AreEqual ("The Album", tag.Album); + Assert.AreEqual ("album", tag.AlbumSort); + tag.TitleSort = "track sort"; Assert.AreEqual ("track sort", tag.TitleSort); - Assert.AreEqual ("track sort", tag.AlbumSort); - Assert.AreEqual ("The Album", tag.Title); + Assert.AreEqual ("album", tag.AlbumSort); + Assert.IsNull (tag.Title); Assert.AreEqual ("The Album", tag.Album); tag.AlbumSort = "other"; - Assert.AreEqual ("other", tag.TitleSort); + Assert.AreEqual ("track sort", tag.TitleSort); Assert.AreEqual ("other", tag.AlbumSort); } @@ -426,7 +430,7 @@ public void SortSet_Null_ClearsAFlatSortAboveTheItem () var title = MatroskaFile.Read (FileWith (Tags ( Tag (50, Simple ("TITLE", "The Album"), Simple ("TITLE_SORT", "flat")), Tag (30, Simple ("TITLE", "The Song"))))).File!.Tag; - Assert.AreEqual ("flat", title.TitleSort); + Assert.IsNull (title.TitleSort); title.TitleSort = null; Assert.IsNull (title.TitleSort); @@ -563,7 +567,9 @@ public void Read_WebM_ReadsTags () { var file = Matroska (Master (0x18538067, Tags (Tag (50, Simple ("TITLE", "Web")))), "webm"); - Assert.AreEqual ("Web", MatroskaFile.Read (file).File!.Tag.Title); + var tag = MatroskaFile.Read (file).File!.Tag; + Assert.IsNull (tag.Title); + Assert.AreEqual ("Web", tag.Album); } [TestMethod] @@ -848,7 +854,40 @@ public void AlbumSort_OnAnAlbumLevelFile_RoundTripsTheFlatName () } [TestMethod] - public void Title_SetEmpty_LetsTheAlbumTitleShowThrough () + public void Artist_Set_DoesNotReplaceTheAlbumArtist () + { + var tag = MatroskaFile.Read (FileWith (Tags ( + Tag (50, Simple ("ARTIST", "Album Person"))))).File!.Tag; + + Assert.AreEqual ("Album Person", tag.Artist); + tag.Artist = "Track Person"; + + Assert.AreEqual ("Track Person", tag.Artist); + Assert.AreEqual ("Album Person", tag.AlbumArtists[0]); + var again = Reread (tag); + Assert.AreEqual ("Track Person", again.Artist); + Assert.AreEqual ("Album Person", again.AlbumArtists[0]); + } + + [TestMethod] + public void TitleAndAlbum_OnAnAlbumTarget_StayIndependent () + { + var tag = MatroskaFile.Read (FileWith (Tags (Tag (50, Simple ("TITLE", "Album"))))).File!.Tag; + + Assert.IsNull (tag.Title); + Assert.AreEqual ("Album", tag.Album); + + tag.Title = "Song"; + + Assert.AreEqual ("Song", tag.Title); + Assert.AreEqual ("Album", tag.Album); + var again = Reread (tag); + Assert.AreEqual ("Song", again.Title); + Assert.AreEqual ("Album", again.Album); + } + + [TestMethod] + public void Title_SetEmpty_RemovesOnlyTheTrackTitle () { var tag = MatroskaFile.Read (FileWith (Tags ( Tag (50, Simple ("TITLE", "Album")), @@ -856,10 +895,10 @@ public void Title_SetEmpty_LetsTheAlbumTitleShowThrough () tag.Title = ""; - Assert.AreEqual ("Album", tag.Title); + Assert.IsNull (tag.Title); Assert.AreEqual ("Album", tag.Album); var again = Reread (tag); - Assert.AreEqual ("Album", again.Title); + Assert.IsNull (again.Title); Assert.AreEqual ("Album", again.Album); } diff --git a/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs b/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs index 899172e..a46763b 100644 --- a/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs +++ b/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs @@ -21,7 +21,9 @@ public void Read_SecondUnknownSegment_StaysOutOfTheFirst () var parsed = MatroskaFile.Read (Matroska (Concat (first, second))); Assert.IsTrue (parsed.IsSuccess, parsed.Error); - Assert.AreEqual ("Movie", parsed.File!.Tag.Title); + Assert.IsNull (parsed.File!.Tag.Title); + Assert.AreEqual ("Movie", parsed.File.Tag.Album); + Assert.AreNotEqual ("Later", parsed.File.Tag.Title); } [TestMethod] @@ -48,7 +50,8 @@ public void Save_UnknownSizeSegment_StopsBeforeTheNextSegment () var head = bytes.AsSpan (0, bytes.Length - second.Length).ToArray (); var again = MatroskaFile.Read (head); Assert.IsTrue (again.IsSuccess, again.Error); - Assert.AreEqual ("First", again.File!.Tag.Title); + Assert.IsNull (again.File!.Tag.Title); + Assert.AreEqual ("First", again.File.Tag.Album); Assert.AreEqual ("Added", again.File.Tag.Artist); Assert.AreEqual (1, CountOf (head, marker)); } @@ -62,7 +65,8 @@ public void Save_ChapterTitle_StaysOffTheFile () ChapterTag (30, Chapter (7), Simple ("TITLE", "Chapter"))), Master (0x1F43B675, new byte[] { 0x33 })); var parsed = MatroskaFile.Read (original); - Assert.AreEqual ("Movie", parsed.File!.Tag.Title); + Assert.IsNull (parsed.File!.Tag.Title); + Assert.AreEqual ("Movie", parsed.File.Tag.Album); parsed.File.Tag.Artist = "Someone"; var fs = new MockFileSystem (); @@ -72,9 +76,9 @@ public void Save_ChapterTitle_StaysOffTheFile () Assert.IsTrue (saved.IsSuccess, saved.Error); var bytes = fs.ReadAllBytes ("/chap.mkv"); var again = MatroskaFile.Read (bytes).File!; - Assert.AreEqual ("Movie", again.Tag.Title); + Assert.IsNull (again.Tag.Title); + Assert.AreEqual ("Movie", again.Tag.Album); Assert.AreEqual ("Someone", again.Tag.Artist); - Assert.IsNull (again.Tag.Album); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x63C4, new byte[] { 7 })) >= 0); } @@ -97,7 +101,9 @@ public void Save_FileLevelLanguage_IsWrittenBack () Assert.IsTrue (saved.IsSuccess, saved.Error); var bytes = fs.ReadAllBytes ("/lang.mkv"); - Assert.AreEqual ("Movie", MatroskaFile.Read (bytes).File!.Tag.Title); + var round = MatroskaFile.Read (bytes).File!.Tag; + Assert.IsNull (round.Title); + Assert.AreEqual ("Movie", round.Album); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x447A, "eng"u8)) >= 0); } @@ -203,7 +209,6 @@ public void Save_InsertsTagsWhenTheFileHasNone () var original = FileWith (info, Master (0x1F43B675, marker)); var parsed = MatroskaFile.Read (original).File!; - // With no existing tag block the item level is 50, so TITLE there is the title. parsed.Tag.Title = "Added"; parsed.Tag.Artist = "Someone"; @@ -589,7 +594,7 @@ public void Save_Level70Title_StaysOnThatTarget () { var original = FileWith (Tags (Tag (70, Simple ("TITLE", "Collection"))), Master (0x1F43B675, new byte[] { 3 })); var parsed = MatroskaFile.Read (original).File!; - Assert.AreEqual ("Collection", parsed.Tag.Title); + Assert.IsNull (parsed.Tag.Title); Assert.IsNull (parsed.Tag.Album); parsed.Tag.Title = "Renamed"; @@ -601,8 +606,9 @@ public void Save_Level70Title_StaysOnThatTarget () var again = MatroskaFile.Read (bytes).File!; Assert.AreEqual ("Renamed", again.Tag.Title); Assert.IsNull (again.Tag.Album); + Assert.IsTrue (bytes.AsSpan ().IndexOf ("Collection"u8) >= 0); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x68CA, new byte[] { 70 })) >= 0); - Assert.IsFalse (bytes.AsSpan ().IndexOf (Element (0x68CA, new byte[] { 30 })) >= 0); + Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x68CA, new byte[] { 30 })) >= 0); } [TestMethod] @@ -778,7 +784,8 @@ public void Save_FileLevelLanguageAndBinary_AreWrittenBack () Element (0x4487, "Movie"u8))); var original = FileWith (Tags (Master (0x7373, Concat (targets, title))), Master (0x1F43B675, new byte[] { 9 })); var parsed = MatroskaFile.Read (original).File!; - Assert.AreEqual ("Movie", parsed.Tag.Title); + Assert.IsNull (parsed.Tag.Title); + Assert.AreEqual ("Movie", parsed.Tag.Album); parsed.Tag.Artist = "Someone"; var fs = new MockFileSystem (); @@ -786,7 +793,9 @@ public void Save_FileLevelLanguageAndBinary_AreWrittenBack () var saved = parsed.SaveToFile ("/lang.mkv", original, fs); Assert.IsTrue (saved.IsSuccess, saved.Error); var bytes = fs.ReadAllBytes ("/lang.mkv"); - Assert.AreEqual ("Movie", MatroskaFile.Read (bytes).File!.Tag.Title); + var round = MatroskaFile.Read (bytes).File!.Tag; + Assert.IsNull (round.Title); + Assert.AreEqual ("Movie", round.Album); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x447B, "en-US"u8)) >= 0); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x4485, new byte[] { 1, 2, 3 })) >= 0); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x63CA, "MOVIE"u8)) >= 0); From 1d436fdeff08cde7d7ca4f13738d0f02fd88633c Mon Sep 17 00:00:00 2001 From: Stephen Shaw Date: Tue, 17 Nov 2026 16:14:54 -0700 Subject: [PATCH 2/3] fix: Show an album or movie title when the track has none A TITLE at target 50 still displays as Title until a track title or a segment title exists. Writing Title still goes to target 30, so it does not replace the album title. --- src/TagLibSharp2/Matroska/MatroskaTag.cs | 9 ++++++--- .../Matroska/MatroskaFileTests.cs | 14 +++++++------- .../Matroska/MatroskaSaveTests.cs | 14 +++++++------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/TagLibSharp2/Matroska/MatroskaTag.cs b/src/TagLibSharp2/Matroska/MatroskaTag.cs index 3f7c694..16afdbe 100644 --- a/src/TagLibSharp2/Matroska/MatroskaTag.cs +++ b/src/TagLibSharp2/Matroska/MatroskaTag.cs @@ -32,11 +32,14 @@ internal MatroskaTag () /// public override string? Title { get { - // TITLE at 30 is the track. An empty one blocks the segment title. - // TITLE at 50 is the album and is not this property. + // TITLE at 30 is the track. An empty one blocks every broader title. if (HasFileLevelName (TrackLevel, "TITLE")) return FirstAt (TrackLevel, "TITLE"); - return _segmentTitle; + // The segment title is the file title. A lone album or movie TITLE at 50 + // is what a caller still displays when the file has no track title. + if (!string.IsNullOrEmpty (_segmentTitle)) + return _segmentTitle; + return FirstAt (AlbumLevel, "TITLE"); } set => SetAt (TrackLevel, "TITLE", value); } diff --git a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs index a684a71..18ea3e0 100644 --- a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs +++ b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs @@ -77,7 +77,7 @@ public void Read_ChapterTitle_IsNotTheFileTitle () var tag = MatroskaFile.Read (file).File!.Tag; - Assert.IsNull (tag.Title); + Assert.AreEqual ("Movie", tag.Title); Assert.AreEqual ("Movie", tag.Album); } @@ -400,7 +400,7 @@ public void SortSet_SharedAlbumTitle_KeepsBothSorts () Tag (30, Simple ("GENRE", "Jazz")))); var tag = MatroskaFile.Read (file).File!.Tag; - Assert.IsNull (tag.Title); + Assert.AreEqual ("The Album", tag.Title); Assert.AreEqual ("The Album", tag.Album); Assert.AreEqual ("album", tag.AlbumSort); @@ -408,7 +408,7 @@ public void SortSet_SharedAlbumTitle_KeepsBothSorts () Assert.AreEqual ("track sort", tag.TitleSort); Assert.AreEqual ("album", tag.AlbumSort); - Assert.IsNull (tag.Title); + Assert.AreEqual ("The Album", tag.Title); Assert.AreEqual ("The Album", tag.Album); tag.AlbumSort = "other"; @@ -568,7 +568,7 @@ public void Read_WebM_ReadsTags () var file = Matroska (Master (0x18538067, Tags (Tag (50, Simple ("TITLE", "Web")))), "webm"); var tag = MatroskaFile.Read (file).File!.Tag; - Assert.IsNull (tag.Title); + Assert.AreEqual ("Web", tag.Title); Assert.AreEqual ("Web", tag.Album); } @@ -874,7 +874,7 @@ public void TitleAndAlbum_OnAnAlbumTarget_StayIndependent () { var tag = MatroskaFile.Read (FileWith (Tags (Tag (50, Simple ("TITLE", "Album"))))).File!.Tag; - Assert.IsNull (tag.Title); + Assert.AreEqual ("Album", tag.Title); Assert.AreEqual ("Album", tag.Album); tag.Title = "Song"; @@ -895,10 +895,10 @@ public void Title_SetEmpty_RemovesOnlyTheTrackTitle () tag.Title = ""; - Assert.IsNull (tag.Title); + Assert.AreEqual ("Album", tag.Title); Assert.AreEqual ("Album", tag.Album); var again = Reread (tag); - Assert.IsNull (again.Title); + Assert.AreEqual ("Album", again.Title); Assert.AreEqual ("Album", again.Album); } diff --git a/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs b/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs index a46763b..d2420c7 100644 --- a/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs +++ b/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs @@ -21,7 +21,7 @@ public void Read_SecondUnknownSegment_StaysOutOfTheFirst () var parsed = MatroskaFile.Read (Matroska (Concat (first, second))); Assert.IsTrue (parsed.IsSuccess, parsed.Error); - Assert.IsNull (parsed.File!.Tag.Title); + Assert.AreEqual ("Movie", parsed.File!.Tag.Title); Assert.AreEqual ("Movie", parsed.File.Tag.Album); Assert.AreNotEqual ("Later", parsed.File.Tag.Title); } @@ -50,7 +50,7 @@ public void Save_UnknownSizeSegment_StopsBeforeTheNextSegment () var head = bytes.AsSpan (0, bytes.Length - second.Length).ToArray (); var again = MatroskaFile.Read (head); Assert.IsTrue (again.IsSuccess, again.Error); - Assert.IsNull (again.File!.Tag.Title); + Assert.AreEqual ("First", again.File!.Tag.Title); Assert.AreEqual ("First", again.File.Tag.Album); Assert.AreEqual ("Added", again.File.Tag.Artist); Assert.AreEqual (1, CountOf (head, marker)); @@ -65,7 +65,7 @@ public void Save_ChapterTitle_StaysOffTheFile () ChapterTag (30, Chapter (7), Simple ("TITLE", "Chapter"))), Master (0x1F43B675, new byte[] { 0x33 })); var parsed = MatroskaFile.Read (original); - Assert.IsNull (parsed.File!.Tag.Title); + Assert.AreEqual ("Movie", parsed.File!.Tag.Title); Assert.AreEqual ("Movie", parsed.File.Tag.Album); parsed.File.Tag.Artist = "Someone"; @@ -76,7 +76,7 @@ public void Save_ChapterTitle_StaysOffTheFile () Assert.IsTrue (saved.IsSuccess, saved.Error); var bytes = fs.ReadAllBytes ("/chap.mkv"); var again = MatroskaFile.Read (bytes).File!; - Assert.IsNull (again.Tag.Title); + Assert.AreEqual ("Movie", again.Tag.Title); Assert.AreEqual ("Movie", again.Tag.Album); Assert.AreEqual ("Someone", again.Tag.Artist); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x63C4, new byte[] { 7 })) >= 0); @@ -102,7 +102,7 @@ public void Save_FileLevelLanguage_IsWrittenBack () Assert.IsTrue (saved.IsSuccess, saved.Error); var bytes = fs.ReadAllBytes ("/lang.mkv"); var round = MatroskaFile.Read (bytes).File!.Tag; - Assert.IsNull (round.Title); + Assert.AreEqual ("Movie", round.Title); Assert.AreEqual ("Movie", round.Album); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x447A, "eng"u8)) >= 0); } @@ -784,7 +784,7 @@ public void Save_FileLevelLanguageAndBinary_AreWrittenBack () Element (0x4487, "Movie"u8))); var original = FileWith (Tags (Master (0x7373, Concat (targets, title))), Master (0x1F43B675, new byte[] { 9 })); var parsed = MatroskaFile.Read (original).File!; - Assert.IsNull (parsed.Tag.Title); + Assert.AreEqual ("Movie", parsed.Tag.Title); Assert.AreEqual ("Movie", parsed.Tag.Album); parsed.Tag.Artist = "Someone"; @@ -794,7 +794,7 @@ public void Save_FileLevelLanguageAndBinary_AreWrittenBack () Assert.IsTrue (saved.IsSuccess, saved.Error); var bytes = fs.ReadAllBytes ("/lang.mkv"); var round = MatroskaFile.Read (bytes).File!.Tag; - Assert.IsNull (round.Title); + Assert.AreEqual ("Movie", round.Title); Assert.AreEqual ("Movie", round.Album); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x447B, "en-US"u8)) >= 0); Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x4485, new byte[] { 1, 2, 3 })) >= 0); From 6443478b281716b0c20c35528b22967a4d98a862 Mon Sep 17 00:00:00 2001 From: Stephen Shaw Date: Tue, 17 Nov 2026 23:13:35 -0700 Subject: [PATCH 3/3] fix: Show a part or edition title when the track has none A file-level TITLE below a collection still displays when the track has none. Album stays the title at level 50. --- src/TagLibSharp2/Matroska/MatroskaTag.cs | 22 ++++++++++++++++--- .../Matroska/MatroskaFileTests.cs | 22 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/TagLibSharp2/Matroska/MatroskaTag.cs b/src/TagLibSharp2/Matroska/MatroskaTag.cs index 16afdbe..47f47e9 100644 --- a/src/TagLibSharp2/Matroska/MatroskaTag.cs +++ b/src/TagLibSharp2/Matroska/MatroskaTag.cs @@ -16,6 +16,7 @@ namespace TagLibSharp2.Matroska; /// public sealed class MatroskaTag : Tag { + const ulong CollectionLevel = 70; const ulong AlbumLevel = 50; const ulong TrackLevel = 30; @@ -35,11 +36,13 @@ public override string? Title { // TITLE at 30 is the track. An empty one blocks every broader title. if (HasFileLevelName (TrackLevel, "TITLE")) return FirstAt (TrackLevel, "TITLE"); - // The segment title is the file title. A lone album or movie TITLE at 50 - // is what a caller still displays when the file has no track title. + // The segment title is the file title. Otherwise the most specific + // file-level TITLE below a collection still displays, so a part, + // edition, or lone album title is not a blank name. if (!string.IsNullOrEmpty (_segmentTitle)) return _segmentTitle; - return FirstAt (AlbumLevel, "TITLE"); + var level = MostSpecificFileTitleBelowCollection (); + return level is ulong found ? FirstAt (found, "TITLE") : null; } set => SetAt (TrackLevel, "TITLE", value); } @@ -363,6 +366,19 @@ internal ulong ItemLevel { } } + ulong? MostSpecificFileTitleBelowCollection () + { + ulong? best = null; + foreach (var block in _blocks) { + if (!block.IsFileLevel || block.Level >= CollectionLevel || !block.HasName ("TITLE")) + continue; + if (best is null || block.Level < best) + best = block.Level; + } + + return best; + } + bool HasFileLevelName (ulong level, string name) { foreach (var block in _blocks) { diff --git a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs index 18ea3e0..71c764d 100644 --- a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs +++ b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs @@ -869,6 +869,28 @@ public void Artist_Set_DoesNotReplaceTheAlbumArtist () Assert.AreEqual ("Album Person", again.AlbumArtists[0]); } + [TestMethod] + public void Title_BelowACollection_DisplaysWithoutBecomingTheAlbum () + { + foreach (var level in new ulong[] { 10, 20, 40, 60 }) { + var tag = MatroskaFile.Read (FileWith (Tags (Tag (level, Simple ("TITLE", "Part"))))).File!.Tag; + + Assert.AreEqual ("Part", tag.Title, $"level {level}"); + Assert.IsNull (tag.Album, $"level {level}"); + } + } + + [TestMethod] + public void Title_OnAPartTarget_StaysOffTheAlbumTitle () + { + var tag = MatroskaFile.Read (FileWith (Tags ( + Tag (40, Simple ("TITLE", "Part")), + Tag (50, Simple ("TITLE", "Album"))))).File!.Tag; + + Assert.AreEqual ("Part", tag.Title); + Assert.AreEqual ("Album", tag.Album); + } + [TestMethod] public void TitleAndAlbum_OnAnAlbumTarget_StayIndependent () {