Static ID3V1.1 Genre Class

dwarvenassassin 0 Tallied Votes 218 Views Share

For anyone working with ID3 version 1 or 1.1 tags in audio files, this static class may come in handy.
It uses indexers to convert between the textual to the numerical representation or from numerical to textual and also accounts for unset and invalid values.

Being static, it is always around when you need it instead of having to instantiate it each time.


Enjoy!

using System;

namespace DGRP
{
    /// <summary>
    /// Usage:  Genre.Type[<string>]    - Returns byte index to given genre.
    ///         Genre.Type[<byte>]      - Returns genre associated with given index.
    /// </summary>
    public sealed class Genre
    {
        /// <summary>
        /// Reference to single instance of this class.
        /// </summary>
        private static readonly Genre instance = new Genre();

        /// <summary>
        /// Private Constructor to prevent Instantiation.
        /// </summary>
        private Genre() { }

        /// <summary>
        /// Publicly available Accessor.
        /// </summary>
        /// <returns>Reference to singleton instance to provide an anchor for the indexers.</returns>
        public static Genre Type
        {
            get
            {
                return instance;
            }
        }

        #region List of currently accepted genre names.
        /// <summary>
        /// Fixed List of all currently accepted Genres.
        /// </summary>
        private static readonly string[] name = {
            "Blues",
            "Classic Rock",
            "Country",
            "Dance",
            "Disco",
            "Funk",
            "Grunge",
            "Hip Hop",
            "Jazz",
            "Metal",
            "New Age",
            "Oldies",
            "Other",
            "Pop",
            "R&B",
            "Rap",
            "Reggae",
            "Rock",
            "Techno",
            "Industrial",
            "Alternative",
            "Ska",
            "Death Metal",
            "Pranks",
            "Soundtrack",
            "Euro-Techno",
            "Ambient",
            "Trip Hop",
            "Vocal",
            "Jazz-Funk",
            "Fusion",
            "Trance",
            "Classical",
            "Instrumental",
            "Acid",
            "House",
            "Game",
            "Sound Clip",
            "Gospel",
            "Noise",
            "Alternative Rock",
            "Bass",
            "Soul",
            "Punk",
            "Space",
            "Meditative",
            "Instrumental Pop",
            "Instrumental Rock",
            "Ethnic",
            "Gothic",
            "Darkwave",
            "Techno-Industrial",
            "Electronic",
            "Pop / Folk",
            "Eurodance",
            "Dream",
            "Southern Rock",
            "Comedy",
            "Cult",
            "Gangsta Rap",
            "Top 40",
            "Christian Rap",
            "Pop / Funk",
            "Jungle",
            "Native American",
            "Cabaret",
            "New Wave",
            "Psychedelic",
            "Rave",
            "Showtunes",
            "Trailer",
            "Lo-fi",
            "Tribal",
            "Acid Punk",
            "Acid Jazz",
            "Polka",
            "Retro",
            "Musical",
            "Rock 'n'Roll",
            "Hard Rock",
            "Folk",
            "Folk / Rock",
            "National Folk",
            "Swing",
            "Fast Fusion",
            "Bebob",
            "Latin",
            "Revival",
            "Celtic",
            "Blue Grass",
            "Avant Garde",
            "Gothic Rock",
            "Progressive Rock",
            "Psychedelic Rock",
            "Symphonic Rock",
            "Slow Rock",
            "Big Band",
            "Chorus",
            "Easy Listening",
            "Acoustic",
            "Humour",
            "Speech",
            "Chanson",
            "Opera",
            "Chamber Music",
            "Sonata",
            "Symphony",
            "Booty Bass",
            "Primus",
            "Pr0n Groove",
            "Satire",
            "Slow Jam",
            "Club",
            "Tango",
            "Samba",
            "Folklore",
            "Ballad",
            "Power Ballad",
            "Rhythmic Soul",
            "Freestyle",
            "Duet",
            "Punk Rock",
            "Drum Solo",
            "Acapella",
            "Euro-House",
            "Dance Hall",
            "Goa",
            "Drum & Bass",
            "Club-House",
            "Hardcore",
            "Terror",
            "Indie",
            "Brit Pop",
            "Negerpunk",
            "Polsk Punk",
            "Beat",
            "Christian Gangsta Rap",
            "Heavy Metal",
            "Black Metal",
            "Crossover",
            "Contemporary Christian",
            "Christian Rock",
            "Merengue",
            "Salsa",
            "Thrash Metal",
            "Anime",
            "JPop",
            "Synth Pop"
        };
        #endregion

        #region Indexers
        /// <summary>
        /// Indexer.
        /// </summary>
        /// <param name="index">A one-byte value representing the genre.</param>
        /// <returns>A textual representation of the genre.</returns>
        public string this[byte index]
        {
            get
            {
                if (index > 148)
                {
                    if (index == 255) return "<not set>";
                    return "<invalid>";
                }
                return name[index];
            }
        }

        /// <summary>
        /// Indexer.
        /// </summary>
        /// <param name="genName">Textual genre representation.</param>
        /// <returns>Numeric value in the range of 0 to 148, or 255.</returns>
        public byte this[string genName]
        {
            get
            {
                if (genName == "<invalid>") return 148;
                if (genName == "<not set>") return 255;
                return (byte)Array.IndexOf(name, genName);
            }
        }
        #endregion
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.