Skip to content

Commit

Permalink
Add support for setting the genre of the song.
Browse files Browse the repository at this point in the history
This also contains a workaround for JeevanJames/Id3#2
  • Loading branch information
mibe committed Aug 30, 2022
1 parent 087a827 commit 27ab06f
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 18 deletions.
194 changes: 194 additions & 0 deletions Id3Genre.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* Part of the ID3-AutoGen project
* Copyright (c) 2012-2022 Michael Bemmerl
*
* SPDX-License-Identifier: MIT
*/

// ReSharper disable UnusedMember.Global
// ReSharper disable InconsistentNaming
namespace ID3_AutoGen
{
/// <summary>
/// Represents the available genres.
/// </summary>
public enum Id3Genre : byte
{
Blues = 0,
ClassicRock = 1,
Country = 2,
Dance = 3,
Disco = 4,
Funk = 5,
Grunge = 6,
HipHop = 7,
Jazz = 8,
Metal = 9,
NewAge = 10,
Oldies = 11,
Other = 12,
Pop = 13,
RnB = 14,
Rap = 15,
Reggae = 16,
Rock = 17,
Techno = 18,
Industrial = 19,
Alternative = 20,
Ska = 21,
DeathMetal = 22,
Pranks = 23,
Soundtrack = 24,
EuroTechno = 25,
Ambient = 26,
TripHop = 27,
Vocal = 28,
JazzFunk = 29,
Fusion = 30,
Trance = 31,
Classical = 32,
Instrumental = 33,
Acid = 34,
House = 35,
Game = 36,
SoundClip = 37,
Gospel = 38,
Noise = 39,
AlternativeRock = 40,
Bass = 41,
Soul = 42,
Punk = 43,
Space = 44,
Meditative = 45,
InstrumentalPop = 46,
InstrumentalRock = 47,
Ethnic = 48,
Gothic = 49,
Darkwave = 50,
TechnoIndustrial = 51,
Electronic = 52,
PopFolk = 53,
Eurodance = 54,
Dream = 55,
SouthernRock = 56,
Comedy = 57,
Cult = 58,
Gangsta = 59,
Top40 = 60,
ChristianRap = 61,
PopFunk = 62,
Jungle = 63,
NativeAmerican = 64,
Cabaret = 65,
NewWave = 66,
Psychadelic = 67,
Rave = 68,
Showtunes = 69,
Trailer = 70,
LoFi = 71,
Tribal = 72,
AcidPunk = 73,
AcidJazz = 74,
Polka = 75,
Retro = 76,
Musical = 77,
RocknRoll = 78,
HardRock = 79,
Folk = 80,
FolkRock = 81,
NationalFolk = 82,
Swing = 83,
FastFusion = 84,
Bebob = 85,
Latin = 86,
Revival = 87,
Celtic = 88,
Bluegrass = 89,
Avantgarde = 90,
GothicRock = 91,
ProgressiveRock = 92,
PsychedelicRock = 93,
SymphonicRock = 94,
SlowRock = 95,
BigBand = 96,
Chorus = 97,
EasyListening = 98,
Acoustic = 99,
Humour = 100,
Speech = 101,
Chanson = 102,
Opera = 103,
ChamberMusic = 104,
Sonata = 105,
Symphony = 106,
BootyBrass = 107,
Primus = 108,
PornGroove = 109,
Satire = 110,
SlowJam = 111,
Club = 112,
Tango = 113,
Samba = 114,
Folklore = 115,
Ballad = 116,
PoweerBallad = 117,
RhytmicSoul = 118,
Freestyle = 119,
Duet = 120,
PunkRock = 121,
DrumSolo = 122,
ACapela = 123,
EuroHouse = 124,
DanceHall = 125,
Merengue = 142,
Salsa = 143,
ThrashMetal = 144,
Anime = 145,
Jpop = 146,
Synthpop = 147,
Abstract = 148,
ArtRock = 149,
Baroque = 150,
Bhangra = 151,
Bigbeat = 152,
Breakbeat = 153,
Chillout = 154,
Downtempo = 155,
Dub = 156,
EBM = 157,
Eclectic = 158,
Electro = 159,
Electroclash = 160,
Emo = 161,
Experimental = 162,
Garage = 163,
Global = 164,
IDM = 165,
Illbient = 166,
IndustroGoth = 167,
JamBand = 168,
Krautrock = 169,
Leftfield = 170,
Lounge = 171,
MathRock = 172,
NewRomantic = 173,
NuBreakz = 174,
PostPunk = 175,
PostRock = 176,
Psytrance = 177,
Shoegaze = 178,
SpaceRock = 179,
TropRock = 180,
WorldMusic = 181,
Neoclassical = 182,
Audiobook = 183,
AudioTheatre = 184,
NeueDeutscheWelle = 185,
Podcast = 186,
IndieRock = 187,
GFunk = 188,
Dubstep = 189,
GarageRock = 190,
Psybient = 191,
}
}
7 changes: 7 additions & 0 deletions Id3Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public string Artist
public string Title
{ get; set; }

/// <summary>
/// Genre tag
/// </summary>
public Id3Genre? Genre
{ get; set; }

/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
Expand All @@ -69,6 +75,7 @@ public object Clone()
newTag.Year = this.Year;
newTag.Artist = this.Artist;
newTag.Title = this.Title;
newTag.Genre = this.Genre;

return newTag;
}
Expand Down
8 changes: 7 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void Main(string[] args)
if (result is NotParsed<Options>)
{
// Special case for showing license info
if (args.Contains("--license"))
if (args.Select(y => y.ToLower()).Contains("--license"))
{
Console.Write(Properties.Resources.licenses);
return;
Expand All @@ -45,6 +45,7 @@ static void Main(string[] args)
tag.Artist = opt.Artist;
tag.Comment = opt.Comment;
tag.Year = opt.Year;
tag.Genre = opt.Genre;

Tagger tagger = new Tagger(tag);
tagger.DryRun = opt.DryRun;
Expand Down Expand Up @@ -92,6 +93,7 @@ static void processFile(Tagger tagger, FileInfo file, bool verbose)
Console.WriteLine("\tAlbum: {0}", writtenTag.Album);
Console.WriteLine("\tYear: {0}", writtenTag.Year);
Console.WriteLine("\tComment: {0}", writtenTag.Comment);
Console.WriteLine("\tGenre: {0}", writtenTag.Genre.HasValue ? writtenTag.Genre.Value.ToString() : "None");
}
}

Expand Down Expand Up @@ -127,6 +129,10 @@ public string Album
public ushort? Year
{ get; set; }

[Option('g', "genre", HelpText = "Genre of the song.")]
public Id3Genre? Genre
{ get; set; }

[Option('p', "pattern", HelpText = "Process only files matching this pattern.")]
public string Pattern
{ get; set; }
Expand Down
60 changes: 43 additions & 17 deletions Tagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace ID3_AutoGen
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using CommandLine;

/// <summary>
/// Provides methods for tagging MP3 files while automatically detecting ID3 data from the filename.
Expand Down Expand Up @@ -121,35 +122,60 @@ private bool writeTag(FileInfo file, ref Id3Tag tag)
{
Guard.IsNotNull(file);
Guard.IsNotNull(tag);
bool success = false;

tag.Artist = tag.Artist.Trim();
tag.Title = tag.Title.Trim();

using Mp3 mp3 = new Mp3(file, Mp3Permissions.ReadWrite);
using (Mp3 mp3 = new Mp3(file, Mp3Permissions.ReadWrite))
{
Id3.Id3Tag fileTag = new Id3.Id3Tag();

Id3.Id3Tag fileTag = new Id3.Id3Tag();
fileTag.Artists = new ArtistsFrame();
fileTag.Artists.Value.Add(tag.Artist);
fileTag.Title = new TitleFrame(tag.Title);

fileTag.Artists = new ArtistsFrame();
fileTag.Artists.Value.Add(tag.Artist);
fileTag.Title = new TitleFrame(tag.Title);
if (tag.Year.HasValue)
fileTag.Year = new YearFrame(tag.Year.Value);

if (tag.Year.HasValue)
fileTag.Year = new YearFrame(tag.Year.Value);
if (tag.Comment != null)
{
tag.Comment = tag.Comment.Trim();
fileTag.Comments.Add(tag.Comment);
}

if (tag.Comment != null)
{
tag.Comment = tag.Comment.Trim();
fileTag.Comments.Add(tag.Comment);
if (tag.Album != null)
{
tag.Album = tag.Album.Trim();
fileTag.Album = new AlbumFrame(tag.Album);
}

// Don't write changes to the file if DryRun is active.
success = DryRun || mp3.WriteTag(fileTag, Id3Version.V1X);
}

if (tag.Album != null)
// The library used for writing the ID3 tags to the MP3 file lacks support for writing a genre tag.
// (See https://github.com/JeevanJames/Id3/issues/2)
// We're lucky here: The genre tag is in ID3v1 actually the very last byte of the segment and thus the file.
// So open the file for writing, jump to the last byte and set it accordingly.
// But only if DryRun is not active and writing of the other tags was successful.
if (success && !DryRun)
{
tag.Album = tag.Album.Trim();
fileTag.Album = new AlbumFrame(tag.Album);
using FileStream stream = file.OpenWrite();
Guard.CanWrite(stream);

stream.Position = stream.Length - 1;

// The specification has no information what value means "genre not set", but Winamp uses 0xFF, so we do so too.
byte genre = 0xFF;

if (tag.Genre.HasValue)
genre = (byte)tag.Genre.Value;

stream.WriteByte(genre);
}

// Don't write changes to the file if DryRun is active.
return DryRun || mp3.WriteTag(fileTag, Id3Version.V1X);
return success;
}
}
}
}

0 comments on commit 27ab06f

Please sign in to comment.