Skip to content

Commit 413e429

Browse files
authored
Merge pull request #253 from hwahrmann/Multiple_MusicBrainz_Entries
Multiple music brainz entries
2 parents 9979d59 + 713494e commit 413e429

File tree

3 files changed

+169
-14
lines changed

3 files changed

+169
-14
lines changed

src/TaglibSharp/Mpeg4/AppleTag.cs

Lines changed: 124 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Collections;
2626
using System.Collections.Generic;
2727
using System.Globalization;
28+
using System.Linq;
2829

2930
namespace TagLib.Mpeg4
3031
{
@@ -402,9 +403,28 @@ public void DetachIlst ()
402403
/// <returns>Text string from data box</returns>
403404
public string GetDashBox (string meanstring, string namestring)
404405
{
405-
AppleDataBox data_box = GetDashAtoms (meanstring, namestring);
406-
if (data_box != null) {
407-
return data_box.Text;
406+
List<AppleDataBox> data_boxes = GetDashAtoms (meanstring, namestring);
407+
if (data_boxes != null) {
408+
return data_boxes[0].Text;
409+
} else {
410+
return null;
411+
}
412+
}
413+
414+
/// <summary>
415+
/// Gets the text strings from a specific data boxes in Dash (----) atoms
416+
/// </summary>
417+
/// <param name="meanstring">String specifying text from mean box</param>
418+
/// <param name="namestring">String specifying text from name box</param>
419+
/// <returns>Text string from data box</returns>
420+
public string[] GetDashBoxes (string meanstring, string namestring)
421+
{
422+
List<AppleDataBox> data_boxes = GetDashAtoms (meanstring, namestring);
423+
if (data_boxes != null) {
424+
string[] box_text = new string[data_boxes.Count];
425+
for (int i = 0; i < data_boxes.Count; i++)
426+
box_text[i] = data_boxes[i].Text;
427+
return box_text;
408428
} else {
409429
return null;
410430
}
@@ -420,7 +440,7 @@ public string GetDashBox (string meanstring, string namestring)
420440
/// <param name="datastring">String specifying text for data box</param>
421441
public void SetDashBox (string meanstring, string namestring, string datastring)
422442
{
423-
AppleDataBox data_box = GetDashAtoms (meanstring, namestring);
443+
AppleDataBox data_box = GetDashAtom (meanstring, namestring);
424444

425445
// If we did find a data_box and we have an empty datastring we should
426446
// remove the entire dash box.
@@ -449,13 +469,95 @@ public void SetDashBox (string meanstring, string namestring, string datastring)
449469
}
450470
}
451471

472+
/// <summary>
473+
/// Sets specific strings in Dash (----) atom. This method updates
474+
/// existing atoms, or creates new one. If an empty datastring is
475+
/// specified, the Dash boxes and its children are removed.
476+
/// </summary>
477+
/// <param name="meanstring">String specifying text for mean box</param>
478+
/// <param name="namestring">String specifying text for name box</param>
479+
/// <param name="datastring">String values specifying text for data boxes</param>
480+
public void SetDashBoxes (string meanstring, string namestring, string[] datastring)
481+
{
482+
List<AppleDataBox> data_boxes = GetDashAtoms (meanstring, namestring);
483+
484+
// If we did find a data_box and we have an empty datastring we should
485+
// remove the entire dash box.
486+
if (data_boxes != null && string.IsNullOrEmpty (datastring[0])) {
487+
AppleAnnotationBox dash_box = GetParentDashBox (meanstring, namestring);
488+
dash_box.ClearChildren ();
489+
ilst_box.RemoveChild (dash_box);
490+
return;
491+
}
492+
493+
if (data_boxes != null && data_boxes.Count == datastring.Length) {
494+
for (int i = 0; i < data_boxes.Count; i++)
495+
data_boxes[i].Text = datastring[i];
496+
} else {
497+
// Remove all Boxes
498+
AppleAnnotationBox dash_box = GetParentDashBox (meanstring, namestring);
499+
if (dash_box != null) {
500+
dash_box.ClearChildren ();
501+
ilst_box.RemoveChild (dash_box);
502+
}
503+
504+
var whole_box = new AppleAnnotationBox (BoxType.DASH);
505+
foreach (var text in datastring)
506+
{
507+
//Create the new boxes, should use 1 for text as a flag
508+
var amean_box = new AppleAdditionalInfoBox (BoxType.Mean, 0, 1);
509+
var aname_box = new AppleAdditionalInfoBox (BoxType.Name, 0, 1);
510+
var adata_box = new AppleDataBox (BoxType.Data, 1);
511+
amean_box.Text = meanstring;
512+
aname_box.Text = namestring;
513+
adata_box.Text = text;
514+
whole_box.AddChild (amean_box);
515+
whole_box.AddChild (aname_box);
516+
whole_box.AddChild (adata_box);
517+
ilst_box.AddChild (whole_box);
518+
}
519+
}
520+
}
521+
452522
/// <summary>
453523
/// Gets the AppleDataBox that corresponds to the specified mean and name values.
454524
/// </summary>
455525
/// <param name="meanstring">String specifying text for mean box</param>
456526
/// <param name="namestring">String specifying text for name box</param>
457527
/// <returns>Existing AppleDataBox or null if one does not exist</returns>
458-
AppleDataBox GetDashAtoms (string meanstring, string namestring)
528+
AppleDataBox GetDashAtom (string meanstring, string namestring)
529+
{
530+
foreach (Box box in ilst_box.Children) {
531+
if (box.BoxType != BoxType.DASH)
532+
continue;
533+
534+
// Get the mean and name boxes, make sure
535+
// they're legit, check the Text fields for
536+
// a match. If we have a match return
537+
// the AppleDatabox containing the data
538+
539+
var mean_box = (AppleAdditionalInfoBox)box.GetChild (BoxType.Mean);
540+
var name_box = (AppleAdditionalInfoBox)box.GetChild (BoxType.Name);
541+
542+
if (mean_box == null || name_box == null ||
543+
mean_box.Text != meanstring ||
544+
!name_box.Text.Equals (namestring, StringComparison.OrdinalIgnoreCase)) {
545+
continue;
546+
} else {
547+
return (AppleDataBox)box.GetChild (BoxType.Data);
548+
}
549+
}
550+
// If we haven't returned the found box yet, there isn't one, return null
551+
return null;
552+
}
553+
554+
/// <summary>
555+
/// Gets the AppleDataBox that corresponds to the specified mean and name values.
556+
/// </summary>
557+
/// <param name="meanstring">String specifying text for mean box</param>
558+
/// <param name="namestring">String specifying text for name box</param>
559+
/// <returns>Existing AppleDataBox or null if one does not exist</returns>
560+
List<AppleDataBox> GetDashAtoms (string meanstring, string namestring)
459561
{
460562
foreach (Box box in ilst_box.Children) {
461563
if (box.BoxType != BoxType.DASH)
@@ -474,7 +576,7 @@ AppleDataBox GetDashAtoms (string meanstring, string namestring)
474576
!name_box.Text.Equals (namestring, StringComparison.OrdinalIgnoreCase)) {
475577
continue;
476578
} else {
477-
return (AppleDataBox)box.GetChild (BoxType.Data);
579+
return box.GetChildren (BoxType.Data).Cast<AppleDataBox>().ToList ();
478580
}
479581
}
480582
// If we haven't returned the found box yet, there isn't one, return null
@@ -1292,8 +1394,14 @@ public override string TitleSort {
12921394
/// http://musicbrainz.org/doc/PicardTagMapping
12931395
/// </remarks>
12941396
public override string MusicBrainzArtistId {
1295-
get { return GetDashBox ("com.apple.iTunes", "MusicBrainz Artist Id"); }
1296-
set { SetDashBox ("com.apple.iTunes", "MusicBrainz Artist Id", value); }
1397+
get {
1398+
string[] artistIds = GetDashBoxes ("com.apple.iTunes", "MusicBrainz Artist Id");
1399+
return artistIds == null ? null : string.Join ("/", artistIds);
1400+
}
1401+
set {
1402+
string[] artistIds = value.Split ('/');
1403+
SetDashBoxes ("com.apple.iTunes", "MusicBrainz Artist Id", artistIds);
1404+
}
12971405
}
12981406

12991407
/// <summary>
@@ -1343,8 +1451,14 @@ public override string MusicBrainzReleaseId {
13431451
/// http://musicbrainz.org/doc/PicardTagMapping
13441452
/// </remarks>
13451453
public override string MusicBrainzReleaseArtistId {
1346-
get { return GetDashBox ("com.apple.iTunes", "MusicBrainz Album Artist Id"); }
1347-
set { SetDashBox ("com.apple.iTunes", "MusicBrainz Album Artist Id", value); }
1454+
get {
1455+
string[] releaseArtistIds = GetDashBoxes ("com.apple.iTunes", "MusicBrainz Album Artist Id");
1456+
return releaseArtistIds == null ? null : string.Join ("/", releaseArtistIds);
1457+
}
1458+
set {
1459+
string[] releaseArtistIds = value.Split ('/');
1460+
SetDashBoxes ("com.apple.iTunes", "MusicBrainz Album Artist Id", releaseArtistIds);
1461+
}
13481462
}
13491463

13501464
/// <summary>

src/TaglibSharp/Mpeg4/Box.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,34 @@ public Box GetChild (ByteVector type)
202202
return null;
203203
}
204204

205+
/// <summary>
206+
/// Gets all child boxes from the current instance by finding
207+
/// a matching box type.
208+
/// </summary>
209+
/// <param name="type">
210+
/// A <see cref="ByteVector" /> object containing the box
211+
/// type to match.
212+
/// </param>
213+
/// <returns>
214+
/// A List of <see cref="Box" /> objects containing the matched box,
215+
/// or <see langword="null" /> if no matching boxes was found.
216+
/// </returns>
217+
public List<Box> GetChildren (ByteVector type)
218+
{
219+
if (Children == null)
220+
return null;
221+
222+
List<Box> boxes = new List<Box> ();
223+
foreach (Box box in Children)
224+
if (box.BoxType == type)
225+
boxes.Add (box);
226+
227+
if (boxes.Count > 0)
228+
return boxes;
229+
230+
return null;
231+
}
232+
205233
/*
206234
/// <summary>
207235
/// Gets a child box from the current instance by finding

src/TaglibSharp/Ogg/XiphComment.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using System.Collections;
3030
using System.Collections.Generic;
3131
using System.Globalization;
32+
using System.Linq;
3233

3334
namespace TagLib.Ogg
3435
{
@@ -1213,8 +1214,14 @@ public override DateTime? DateTagged {
12131214
/// This property is implemented using the "MUSICBRAINZ_ARTISTID" field.
12141215
/// </remarks>
12151216
public override string MusicBrainzArtistId {
1216-
get { return GetFirstField ("MUSICBRAINZ_ARTISTID"); }
1217-
set { SetField ("MUSICBRAINZ_ARTISTID", value); }
1217+
get {
1218+
string[] artistIds = GetField ("MUSICBRAINZ_ARTISTID");
1219+
return artistIds.Length == 0 ? null : string.Join ("/", artistIds);
1220+
}
1221+
set {
1222+
string[] artistIds = value.Split ('/');
1223+
SetField ("MUSICBRAINZ_ARTISTID", artistIds);
1224+
}
12181225
}
12191226

12201227
/// <summary>
@@ -1264,8 +1271,14 @@ public override string MusicBrainzReleaseId {
12641271
/// This property is implemented using the "MUSICBRAINZ_ALBUMARTISTID" field.
12651272
/// </remarks>
12661273
public override string MusicBrainzReleaseArtistId {
1267-
get { return GetFirstField ("MUSICBRAINZ_ALBUMARTISTID"); }
1268-
set { SetField ("MUSICBRAINZ_ALBUMARTISTID", value); }
1274+
get {
1275+
string[] releaseArtistIds = GetField ("MUSICBRAINZ_ALBUMARTISTID");
1276+
return releaseArtistIds.Length == 0 ? null : string.Join ("/", releaseArtistIds);
1277+
}
1278+
set {
1279+
string[] releaseArtistIds = value.Split ('/');
1280+
SetField ("MUSICBRAINZ_ALBUMARTISTID", releaseArtistIds);
1281+
}
12691282
}
12701283

12711284
/// <summary>

0 commit comments

Comments
 (0)