Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio Id3v2 split CreateID3Tags into CreateID3V1Tags and CreateID3V2Tags properties #324

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/TaglibSharp.Tests/FileFormats/Id3BothFormatTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,49 @@ public void TestCreateId3Tags ()
file = File.Create (tempFile);
Assert.AreEqual (TagTypes.Id3v1 | TagTypes.Id3v2, file.TagTypes);
}

[Test]
public void TestCreateId3SeparateTags ()
{
string tempFile = TestPath.Samples + "tmpwrite_sample_createid3tags.mp3";

System.IO.File.Copy (sample_file, tempFile, true);

// Remove All Tags first
var file = File.Create (tempFile);
file.RemoveTags (TagTypes.AllTags);
file.Save ();

// No TagTypes should exist
TagLib.Mpeg.AudioFile.CreateID3V1Tags = false;
TagLib.Mpeg.AudioFile.CreateID3V2Tags = false;
file = File.Create (tempFile);
Assert.AreEqual (TagTypes.None, file.TagTypes);
file.RemoveTags (TagTypes.AllTags);
file.Save ();

// Only V1 TagTypes should exist
TagLib.Mpeg.AudioFile.CreateID3V1Tags = true;
TagLib.Mpeg.AudioFile.CreateID3V2Tags = false;
file = File.Create (tempFile);
Assert.AreEqual (TagTypes.Id3v1, file.TagTypes);
file.RemoveTags (TagTypes.AllTags);
file.Save ();

// Only V2 TagTypes should exist
TagLib.Mpeg.AudioFile.CreateID3V1Tags = false;
TagLib.Mpeg.AudioFile.CreateID3V2Tags = true;
file = File.Create (tempFile);
Assert.AreEqual (TagTypes.Id3v2, file.TagTypes);
file.RemoveTags (TagTypes.AllTags);
file.Save ();

// Both V1 and V2 TagTypes should exist
TagLib.Mpeg.AudioFile.CreateID3V1Tags = true;
TagLib.Mpeg.AudioFile.CreateID3V2Tags = true;
file = File.Create (tempFile);
Assert.AreEqual (TagTypes.Id3v1 | TagTypes.Id3v2, file.TagTypes);
file.Save ();
}
}
}
73 changes: 68 additions & 5 deletions src/TaglibSharp/Mpeg/AudioFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ public class AudioFile : TagLib.NonContainer.File
#region Private Static Fields

/// <summary>
/// Specifies whether or not to create ID3v1 and
/// ID3v2 tags when they don't exist..
/// Specifies whether or not to create ID3v1
/// tag when it doesn't exist.
/// </summary>
private static bool create_id3_tags = true;
private static bool create_id3_v1_tags = true;

/// <summary>
/// Specifies whether or not to create ID3v2
/// tag when it doesn't exist.
/// </summary>
private static bool create_id3_v2_tags = true;

#endregion

Expand Down Expand Up @@ -187,6 +193,63 @@ public AudioFile (IFileAbstraction abstraction)
public static bool CreateID3Tags {
get { return create_id3_tags; }
set { create_id3_tags = value; }
get {
if (create_id3_v1_tags && create_id3_v2_tags) {
return true;
} else if (!create_id3_v1_tags && !create_id3_v2_tags) {
return false;
}

throw new InvalidOperationException("Inconsistent V1 and V2 state.");
}
set {
create_id3_v1_tags = value;
create_id3_v2_tags = value;
}
}

/// <summary>
/// Gets and sets whether or not to create an ID3v1 tag
/// automatically when it is not existing.
/// </summary>
/// <value>
/// <see langword="true" /> if the tag is to be created automatically.
/// Otherwise, <see langword="false" />.
/// </value>
/// <remarks>
/// <para>Sometimes an MP3 file should not contain an ID3v1 Tag.
/// By setting this property to <see langword="false" />,
/// no ID3v1 Tag will be created when creating the file,
/// if it doesn't exist.
/// It needs to be created explicitly if needed.</para>
/// <para>The default is <see langword="true" /> which means that
/// an ID3v1 tag is created when it doesn't exist.</para>
/// </remarks>
public static bool CreateID3V1Tags {
get { return create_id3_v1_tags; }
set { create_id3_v1_tags = value; }
}

/// <summary>
/// Gets and sets whether or not to create an ID3v2 tag
/// automatically when it is not existing.
/// </summary>
/// <value>
/// <see langword="true" /> if the tag is to be created automatically.
/// Otherwise, <see langword="false" />.
/// </value>
/// <remarks>
/// <para>Sometimes an MP3 file should not contain an ID3v2 Tag.
/// By setting this property to <see langword="false" />,
/// no ID3v1 Tag will be created when creating the file,
/// if it doesn't exist.
/// It needs to be created explicitly if needed.</para>
/// <para>The default is <see langword="true" /> which means that
/// an ID3v2 tag is created when it doesn't exist.</para>
/// </remarks>
public static bool CreateID3V2Tags {
get { return create_id3_v2_tags; }
set { create_id3_v2_tags = value; }
}

#endregion
Expand Down Expand Up @@ -290,8 +353,8 @@ protected override void ReadStart (long start, ReadStyle propertiesStyle)
protected override void ReadEnd (long end, ReadStyle propertiesStyle)
{
// Creation of ID3v1 and ID3v2 tags based on CreateID3Tags property
GetTag (TagTypes.Id3v1, create_id3_tags);
GetTag (TagTypes.Id3v2, create_id3_tags);
GetTag (TagTypes.Id3v1, create_id3_v1_tags);
GetTag (TagTypes.Id3v2, create_id3_v2_tags);
}

/// <summary>
Expand Down