diff --git a/src/TagLibSharp2/Matroska/MatroskaTag.cs b/src/TagLibSharp2/Matroska/MatroskaTag.cs
index d0f1474..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;
@@ -32,20 +33,24 @@ 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");
- return _segmentTitle;
+ // 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. 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;
+ var level = MostSpecificFileTitleBelowCollection ();
+ return level is ulong found ? FirstAt (found, "TITLE") : null;
}
- 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 +59,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 +226,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 +238,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 +366,29 @@ 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) {
+ if (block.IsFileLevel && block.Level == level && block.HasName (name))
+ return true;
+ }
+
+ return false;
+ }
+
string? First (string name)
{
var values = Values (name);
@@ -475,10 +496,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..71c764d 100644
--- a/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs
+++ b/tests/TagLibSharp2.Tests/Matroska/MatroskaFileTests.cs
@@ -78,7 +78,7 @@ public void Read_ChapterTitle_IsNotTheFileTitle ()
var tag = MatroskaFile.Read (file).File!.Tag;
Assert.AreEqual ("Movie", tag.Title);
- Assert.IsNull (tag.Album);
+ 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.AreEqual ("The Album", 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 ("album", tag.AlbumSort);
Assert.AreEqual ("The Album", 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.AreEqual ("Web", tag.Title);
+ Assert.AreEqual ("Web", tag.Album);
}
[TestMethod]
@@ -848,7 +854,62 @@ 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 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 ()
+ {
+ var tag = MatroskaFile.Read (FileWith (Tags (Tag (50, Simple ("TITLE", "Album"))))).File!.Tag;
+
+ Assert.AreEqual ("Album", 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")),
diff --git a/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs b/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs
index 899172e..d2420c7 100644
--- a/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs
+++ b/tests/TagLibSharp2.Tests/Matroska/MatroskaSaveTests.cs
@@ -22,6 +22,8 @@ public void Read_SecondUnknownSegment_StaysOutOfTheFirst ()
Assert.IsTrue (parsed.IsSuccess, parsed.Error);
Assert.AreEqual ("Movie", parsed.File!.Tag.Title);
+ Assert.AreEqual ("Movie", parsed.File.Tag.Album);
+ Assert.AreNotEqual ("Later", parsed.File.Tag.Title);
}
[TestMethod]
@@ -49,6 +51,7 @@ public void Save_UnknownSizeSegment_StopsBeforeTheNextSegment ()
var again = MatroskaFile.Read (head);
Assert.IsTrue (again.IsSuccess, again.Error);
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));
}
@@ -63,6 +66,7 @@ public void Save_ChapterTitle_StaysOffTheFile ()
Master (0x1F43B675, new byte[] { 0x33 }));
var parsed = MatroskaFile.Read (original);
Assert.AreEqual ("Movie", parsed.File!.Tag.Title);
+ Assert.AreEqual ("Movie", parsed.File.Tag.Album);
parsed.File.Tag.Artist = "Someone";
var fs = new MockFileSystem ();
@@ -73,8 +77,8 @@ public void Save_ChapterTitle_StaysOffTheFile ()
var bytes = fs.ReadAllBytes ("/chap.mkv");
var again = MatroskaFile.Read (bytes).File!;
Assert.AreEqual ("Movie", 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.AreEqual ("Movie", 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]
@@ -779,6 +785,7 @@ public void Save_FileLevelLanguageAndBinary_AreWrittenBack ()
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.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.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);
Assert.IsTrue (bytes.AsSpan ().IndexOf (Element (0x63CA, "MOVIE"u8)) >= 0);