Пример #1
0
        /// <summary>
        /// Checks whether a song is in the heirarchy and adds if necessary
        /// </summary>
        /// <param name="Mp3Node"></param>
        public void AddInformation(Mp3Node newNode)
        {
            string separator     = " - ";
            bool   addCurrentMp3 = true;
            var    nodeList      = new List <Mp3Node>();

            // TODO: add bitrate to the file name ?
            // Title: trackNum - artist - album - tracktitle.mp3
            newNode.NewFileName = newNode.TrackNumber + separator + newNode.ArtistName + separator + newNode.AlbumName + separator + newNode.Title + ".mp3";

            if (!_artistAlbumMp3Nodes.ContainsKey(newNode.ArtistName))
            {
                // artist not in catalog
                _artistAlbumMp3Nodes[newNode.ArtistName] = new Dictionary <string, List <Mp3Node> >();
                _artistAlbumMp3Nodes[newNode.ArtistName][newNode.AlbumName] = new List <Mp3Node>();
            }
            else if (!_artistAlbumMp3Nodes[newNode.ArtistName].ContainsKey(newNode.AlbumName))
            {
                // artist in catalog, but album is not
                _artistAlbumMp3Nodes[newNode.ArtistName][newNode.AlbumName] = new List <Mp3Node>();
            }

            nodeList = _artistAlbumMp3Nodes[newNode.ArtistName][newNode.AlbumName];

            // go through the nodeList stored for the album
            foreach (Mp3Node existingNode in nodeList)
            {
                // if we find a matching node based on the new filename
                // pick the one with the best bitrate
                if (existingNode.NewFileName == newNode.NewFileName)
                {
                    if (newNode.Bitrate <= existingNode.Bitrate)
                    {
                        addCurrentMp3 = false;
                    }
                    else
                    {
                        nodeList.Remove(existingNode);
                    }

                    break;
                }
            }

            if (addCurrentMp3)
            {
                nodeList.Add(newNode);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets tags from the raw MP3 files and generates a List of MP3Node data structures with
        /// necessary info to construct our MP3 folder structure
        ///
        /// Not sure whether I understand why we are moving this into a separate structure
        /// </summary>
        /// <param name="mp3FilePaths">List of string paths to the MP3 files</param>
        /// <returns>List of MP3 representations</returns>
        public IList <Mp3Node> RetrieveTagsFromMp3Files(IList <string> mp3FilePaths)
        {
            var mp3FileList = new List <Mp3Node>();
            var count       = 0;

            foreach (var currentMp3FilePath in mp3FilePaths)
            {
                // filtering out these two types.  Don't know why
                string extension = Path.GetExtension(currentMp3FilePath);

                if ((extension != ".cue") && (extension != ".db"))
                {
                    try
                    {
                        // Codesite is a logging tool. Where is this going???
                        CodeSite.Send("Processing file " + count++ + " from " + mp3FilePaths.Count);

                        TagLib.File tagLibFile = null;

                        try
                        {
                            // hydrate TagLib File data structure from raw mp3 file
                            tagLibFile = TagLib.File.Create(currentMp3FilePath);
                        }
                        catch (Exception exception)
                        {
                            _filesWithMissingTags.Add(currentMp3FilePath);
                            CodeSite.Send(currentMp3FilePath);
                            CodeSite.SendException(exception);
                        }

                        string artist      = "Unknown artist";
                        string album       = "Unknown album";
                        string title       = "Unknown title";
                        string trackNumber = "00";
                        int    bitrate     = 1;

                        // if we have a tag library, we'll go through it and create an MP3Node to represent it
                        // TODO: Not sure the justification of moving from this tagLib format to our own custom format
                        if (tagLibFile != null && tagLibFile.Tag != null)
                        {
                            // set artist
                            if (tagLibFile.Tag.AlbumArtists.Length > 0)
                            {
                                artist = tagLibFile.Tag.AlbumArtists[0];
                            }
                            // this property is obsoleted so only check as a fallback
                            else if (tagLibFile.Tag.Artists.Length > 0)
                            {
                                artist = tagLibFile.Tag.Artists[0];
                            }
                            else
                            {
                                _filesWithMissingTags.Add(currentMp3FilePath);
                            }

                            // set album
                            if (tagLibFile.Tag.Album.Length > 0)
                            {
                                album = tagLibFile.Tag.Album;
                            }
                            else if (tagLibFile.Tag.AlbumArtists.Length > 0)
                            {
                                album = tagLibFile.Tag.AlbumArtists[0];
                            }
                            else
                            {
                                _filesWithMissingTags.Add(currentMp3FilePath);
                            }

                            // set trackName
                            if (tagLibFile.Tag.Title.Length > 0)
                            {
                                title = tagLibFile.Tag.Title;
                            }
                            else
                            {
                                title = currentMp3FilePath;
                            }

                            // set track number
                            trackNumber = tagLibFile.Tag.Track.ToString();

                            if (string.IsNullOrEmpty(trackNumber))
                            {
                                trackNumber = "00";
                            }

                            bitrate = tagLibFile.Properties.AudioBitrate;
                        }
                        else
                        {
                            _filesWithMissingTags.Add(currentMp3FilePath);
                        }

                        // create new MP3 Node in the list
                        var Mp3Node1 = new Mp3Node()
                        {
                            AlbumName   = album,
                            ArtistName  = artist,
                            FileName    = currentMp3FilePath,
                            Title       = title,
                            Bitrate     = bitrate,
                            TrackNumber = trackNumber
                        };

                        mp3FileList.Add(Mp3Node1);
                    }
                    catch (Exception ex)
                    {
                        CodeSite.Send(currentMp3FilePath);
                        CodeSite.SendException(ex);
                        _filesWithMissingTags.Add(currentMp3FilePath);
                    }
                }
            }

            return(mp3FileList);
        }