GenerateSearchAndDoubleMetaphone() public method

public GenerateSearchAndDoubleMetaphone ( ) : void
return void
Beispiel #1
0
        /// <summary>
        /// Update tags for single song
        /// </summary>
        public static void UpdateSingleTag(Song song)
        {
            try
            {
                using (TagLib.File taglibFile = TagLib.File.Create(song.FileName))
                {
                    if (taglibFile.Tag != null)
                    {
                        if (!string.IsNullOrEmpty(taglibFile.Tag.Title))
                            song.Name = taglibFile.Tag.Title.Trim();

                        if (!string.IsNullOrEmpty(taglibFile.Tag.JoinedPerformers))
                            song.Artist = taglibFile.Tag.JoinedPerformers.Trim();

                        if (!string.IsNullOrEmpty(taglibFile.Tag.JoinedGenres))
                            song.Genre = taglibFile.Tag.JoinedGenres.Trim();

                        if (!string.IsNullOrEmpty(taglibFile.Tag.Album))
                            song.Album = taglibFile.Tag.Album.Trim();

                        int rating = int.MinValue;
                        if (taglibFile.Tag.TagTypes.HasFlag(TagLib.TagTypes.Id3v2))
                        {
                            TagLib.Id3v2.Tag id3v2Tag = (TagLib.Id3v2.Tag)taglibFile.GetTag(TagLib.TagTypes.Id3v2);

                            List<TagLib.Id3v2.PopularimeterFrame> popularimeterFrames = id3v2Tag
                                .Where(x => x is TagLib.Id3v2.PopularimeterFrame)
                                .Cast<TagLib.Id3v2.PopularimeterFrame>()
                                .OrderBy(y => y.User)
                                .ToList();

                            if (popularimeterFrames != null && popularimeterFrames.Count > 0)
                            {
                                foreach (TagLib.Id3v2.PopularimeterFrame popularimeterFrame in popularimeterFrames)
                                {
                                    if (popularimeterFrame.Rating > 0)
                                    {
                                        rating = popularimeterFrame.Rating;
                                        break;
                                    }
                                }
                            }
                        }

                        if (rating == 0)
                            song.Rating = 1;
                        else if (rating == 1)
                            song.Rating = 2;
                        else if (rating > 1 && rating < 64)
                            song.Rating = 3;
                        else if (rating == 64)
                            song.Rating = 4;
                        else if (rating > 64 && rating < 128)
                            song.Rating = 5;
                        else if (rating == 128)
                            song.Rating = 6;
                        else if (rating > 128 && rating < 196)
                            song.Rating = 7;
                        else if (rating == 196)
                            song.Rating = 8;
                        else if (rating > 196 && rating < 255)
                            song.Rating = 9;
                        else if (rating == 255)
                            song.Rating = 10;
                        else
                            song.Rating = -1;

                        song.Duration = (int)taglibFile.Properties.Duration.TotalSeconds;

                        uint year = taglibFile.Tag.Year;
                        song.Year = year > 0 ? year.ToString() : string.Empty;

                        FileInfo fileInfo = new FileInfo(song.FileName);
                        song.DateCreated = fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm");

                        song.GenerateSearchAndDoubleMetaphone();
                    }
                }
                song.TagRead = true;
                song.ErrorReadingTag = false;
            }
            catch (System.IO.IOException)
            {
                song.TagRead = false;
                song.ErrorReadingTag = true;
            }
            catch (Exception)
            {
                song.TagRead = true;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Update tags for single song
        /// </summary>
        public static void UpdateSingleTag(Song song)
        {
            try
            {
                using (TagLib.File taglibFile = TagLib.File.Create(song.FileName))
                {
                    if (taglibFile.Tag != null)
                    {
                        if (!string.IsNullOrEmpty(taglibFile.Tag.Title))
                        {
                            song.Name = taglibFile.Tag.Title.Trim();
                        }

                        if (!string.IsNullOrEmpty(taglibFile.Tag.JoinedPerformers))
                        {
                            song.Artist = taglibFile.Tag.JoinedPerformers.Trim();
                        }

                        if (!string.IsNullOrEmpty(taglibFile.Tag.JoinedGenres))
                        {
                            song.Genre = taglibFile.Tag.JoinedGenres.Trim();
                        }

                        if (!string.IsNullOrEmpty(taglibFile.Tag.Album))
                        {
                            song.Album = taglibFile.Tag.Album.Trim();
                        }

                        int rating = int.MinValue;
                        if (taglibFile.Tag.TagTypes.HasFlag(TagLib.TagTypes.Id3v2))
                        {
                            TagLib.Id3v2.Tag id3v2Tag = (TagLib.Id3v2.Tag)taglibFile.GetTag(TagLib.TagTypes.Id3v2);

                            List <TagLib.Id3v2.PopularimeterFrame> popularimeterFrames = id3v2Tag
                                                                                         .Where(x => x is TagLib.Id3v2.PopularimeterFrame)
                                                                                         .Cast <TagLib.Id3v2.PopularimeterFrame>()
                                                                                         .OrderBy(y => y.User)
                                                                                         .ToList();

                            if (popularimeterFrames != null && popularimeterFrames.Count > 0)
                            {
                                foreach (TagLib.Id3v2.PopularimeterFrame popularimeterFrame in popularimeterFrames)
                                {
                                    if (popularimeterFrame.Rating > 0)
                                    {
                                        rating = popularimeterFrame.Rating;
                                        break;
                                    }
                                }
                            }
                        }

                        if (rating == 0)
                        {
                            song.Rating = 1;
                        }
                        else if (rating == 1)
                        {
                            song.Rating = 2;
                        }
                        else if (rating > 1 && rating < 64)
                        {
                            song.Rating = 3;
                        }
                        else if (rating == 64)
                        {
                            song.Rating = 4;
                        }
                        else if (rating > 64 && rating < 128)
                        {
                            song.Rating = 5;
                        }
                        else if (rating == 128)
                        {
                            song.Rating = 6;
                        }
                        else if (rating > 128 && rating < 196)
                        {
                            song.Rating = 7;
                        }
                        else if (rating == 196)
                        {
                            song.Rating = 8;
                        }
                        else if (rating > 196 && rating < 255)
                        {
                            song.Rating = 9;
                        }
                        else if (rating == 255)
                        {
                            song.Rating = 10;
                        }
                        else
                        {
                            song.Rating = -1;
                        }

                        song.Duration = (int)taglibFile.Properties.Duration.TotalSeconds;

                        uint year = taglibFile.Tag.Year;
                        song.Year = year > 0 ? year.ToString() : string.Empty;

                        FileInfo fileInfo = new FileInfo(song.FileName);
                        song.DateCreated = fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm");

                        song.GenerateSearchAndDoubleMetaphone();
                    }
                }
                song.TagRead         = true;
                song.ErrorReadingTag = false;
            }
            catch (System.IO.IOException)
            {
                song.TagRead         = false;
                song.ErrorReadingTag = true;
            }
            catch (Exception)
            {
                song.TagRead = true;
            }
        }
Beispiel #3
0
        private void ScanSongsThread()
        {
            Config.ConfigFile config = SongPlayerFactory.GetConfigFile();
            string[] directories = config.GetValue("library.path").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            string[] extensionsArray = config.GetValue("library.extensions").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.StartsWith(".", StringComparison.OrdinalIgnoreCase) ? x : "." + x)
                .ToArray();
            HashSet<string> extensions = new HashSet<string>(extensionsArray, StringComparer.OrdinalIgnoreCase);

            if (extensions.Count == 0)
                extensions.Add(".mp3");

            HashSet<string> files = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            //assuming we could have several dirs here, lets speed up the process
            Parallel.ForEach(directories, directory =>
            {
                if (Directory.Exists(directory))
                {
                    HashSet<string> filesFound = GetFilesRecursive(directory, extensions);

                    // lock files object
                    lock (lockObject)
                    {
                        files.UnionWith(filesFound);
                    }
                }
            });

            //Find removed songs
            lock (lockObject)
            {
                string[] keysToRemove = _songs.Keys.Where(x => !files.Contains(x, StringComparer.OrdinalIgnoreCase)).ToArray();

                foreach (string key in keysToRemove)
                {
                    if (_songs.Remove(key))
                    {
                        _scanFoundChange = true;
                    }
                }
            }

            //Find added songs. Here we can have thousands of files
            Parallel.ForEach(files, fileName =>
            {
                bool checkNext = false;

                lock (lockObject)
                {
                    if (_songs.ContainsKey(fileName))
                        checkNext = true;
                }

                if (!checkNext)
                {
                    FileInfo fileInfo = new FileInfo(fileName);
                    Song song = new Song();
                    song.FileName = fileName;
                    song.Extension = fileInfo.Extension;
                    song.Name = Regex.Replace(fileInfo.Name, @"\" + fileInfo.Extension + "$", string.Empty, RegexOptions.IgnoreCase);
                    song.DateCreated = fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm");
                    song.GenerateSearchAndDoubleMetaphone();

                    lock (lockObject)
                    {
                        _songs.Add(fileName, song);
                    }

                    _scanFoundChange = true;
                }
            });

            _scanRunning = false;
        }
Beispiel #4
0
        private void ScanSongsThread()
        {
            Config.ConfigFile config          = SongPlayerFactory.GetConfigFile();
            string[]          directories     = config.GetValue("library.path").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            string[]          extensionsArray = config.GetValue("library.extensions").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                                                .Select(x => x.StartsWith(".", StringComparison.OrdinalIgnoreCase) ? x : "." + x)
                                                .ToArray();
            HashSet <string> extensions = new HashSet <string>(extensionsArray, StringComparer.OrdinalIgnoreCase);

            if (extensions.Count == 0)
            {
                extensions.Add(".mp3");
            }

            HashSet <string> files = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            //assuming we could have several dirs here, lets speed up the process
            Parallel.ForEach(directories, directory =>
            {
                if (Directory.Exists(directory))
                {
                    HashSet <string> filesFound = GetFilesRecursive(directory, extensions);

                    // lock files object
                    lock (lockObject)
                    {
                        files.UnionWith(filesFound);
                    }
                }
            });

            //Find removed songs
            lock (lockObject)
            {
                string[] keysToRemove = _songs.Keys.Where(x => !files.Contains(x, StringComparer.OrdinalIgnoreCase)).ToArray();

                foreach (string key in keysToRemove)
                {
                    if (_songs.Remove(key))
                    {
                        _scanFoundChange = true;
                    }
                }
            }

            //Find added songs. Here we can have thousands of files
            Parallel.ForEach(files, fileName =>
            {
                bool checkNext = false;

                lock (lockObject)
                {
                    if (_songs.ContainsKey(fileName))
                    {
                        checkNext = true;
                    }
                }

                if (!checkNext)
                {
                    FileInfo fileInfo = new FileInfo(fileName);
                    Song song         = new Song();
                    song.FileName     = fileName;
                    song.Extension    = fileInfo.Extension;
                    song.Name         = Regex.Replace(fileInfo.Name, @"\" + fileInfo.Extension + "$", string.Empty, RegexOptions.IgnoreCase);
                    song.DateCreated  = fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm");
                    song.GenerateSearchAndDoubleMetaphone();

                    lock (lockObject)
                    {
                        _songs.Add(fileName, song);
                    }

                    _scanFoundChange = true;
                }
            });

            _scanRunning = false;
        }