Static ID3V1.1 Genre Class

dwarvenassassin 0 Tallied Votes 202 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.


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
                return instance;

        #region List of currently accepted genre names.
        /// <summary>
        /// Fixed List of all currently accepted Genres.
        /// </summary>
        private static readonly string[] name = {
            "Classic Rock",
            "Hip Hop",
            "New Age",
            "Death Metal",
            "Trip Hop",
            "Sound Clip",
            "Alternative Rock",
            "Instrumental Pop",
            "Instrumental Rock",
            "Pop / Folk",
            "Southern Rock",
            "Gangsta Rap",
            "Top 40",
            "Christian Rap",
            "Pop / Funk",
            "Native American",
            "New Wave",
            "Acid Punk",
            "Acid Jazz",
            "Rock 'n'Roll",
            "Hard Rock",
            "Folk / Rock",
            "National Folk",
            "Fast Fusion",
            "Blue Grass",
            "Avant Garde",
            "Gothic Rock",
            "Progressive Rock",
            "Psychedelic Rock",
            "Symphonic Rock",
            "Slow Rock",
            "Big Band",
            "Easy Listening",
            "Chamber Music",
            "Booty Bass",
            "Pr0n Groove",
            "Slow Jam",
            "Power Ballad",
            "Rhythmic Soul",
            "Punk Rock",
            "Drum Solo",
            "Dance Hall",
            "Drum & Bass",
            "Brit Pop",
            "Polsk Punk",
            "Christian Gangsta Rap",
            "Heavy Metal",
            "Black Metal",
            "Contemporary Christian",
            "Christian Rock",
            "Thrash Metal",
            "Synth Pop"

        #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]
                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]
                if (genName == "<invalid>") return 148;
                if (genName == "<not set>") return 255;
                return (byte)Array.IndexOf(name, genName);