/// <summary> /// Enqueue song /// </summary> public void Enqueue(Song song, string requesterName) { int maximalsonginqueue; if (!int.TryParse(SongPlayerFactory.GetConfigFile().GetValue("player.maximalsonginqueue"), out maximalsonginqueue)) maximalsonginqueue = int.MaxValue; if (_queue.Count >= maximalsonginqueue) return; SongLibrary.UpdateSingleTag(song); _queue.Add(new RequestedSong { Song = song, RequesterName = requesterName, RequestedDate = DateTime.Now }); }
/// <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; } }
/// <summary> /// Dequeue song /// </summary> public void Dequeue(Song song, string requester) { Dequeue(song.TempId, requester); }
private bool SearchFunction(Song song, string searchValue, string searchValueDoubleMetaphone, bool includeFileNameInSearch) { // with double metaphone if (!string.IsNullOrEmpty(song.NameDoubleMetaphone) && song.NameDoubleMetaphone.Equals(searchValueDoubleMetaphone, StringComparison.Ordinal)) return true; if (!string.IsNullOrEmpty(song.ArtistDoubleMetaphone) && song.ArtistDoubleMetaphone.Equals(searchValueDoubleMetaphone, StringComparison.Ordinal)) return true; if (!string.IsNullOrEmpty(song.AlbumDoubleMetaphone) && song.AlbumDoubleMetaphone.Equals(searchValueDoubleMetaphone, StringComparison.Ordinal)) return true; // contains if (!string.IsNullOrEmpty(song.NameSearchValue) && song.NameSearchValue.ContainsIgnoreCaseNonSpace(searchValue)) return true; if (!string.IsNullOrEmpty(song.ArtistSearchValue) && song.ArtistSearchValue.ContainsIgnoreCaseNonSpace(searchValue)) return true; if (!string.IsNullOrEmpty(song.AlbumSearchValue) && song.AlbumSearchValue.ContainsIgnoreCaseNonSpace(searchValue)) return true; // file name? if (includeFileNameInSearch && !string.IsNullOrEmpty(song.FileNameSearchValue) && song.FileNameSearchValue.ContainsIgnoreCaseNonSpace(searchValue)) return true; return false; }
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; }
/// <summary> /// Get correct sort string /// </summary> private string GetSortString(Song song, SortBy sortBy, int level) { if (sortBy == SortBy.Artist) { switch (level) { case 0: return song.Artist; case 1: return song.Name; case 2: return song.Year; } } else if (sortBy == SortBy.Name) { switch (level) { case 0: return song.Name; case 1: return song.Artist; case 2: return song.TempId; } } else if (sortBy == SortBy.Date) { switch (level) { case 0: return song.DateCreated; case 1: return song.Artist; case 2: return song.Name; } } else if (sortBy == SortBy.PlayDate) { switch (level) { case 0: if (song.LastPlayDateTime.HasValue) return song.LastPlayDateTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff"); return string.Empty; case 1: return song.Artist; case 2: return song.Name; } } else if (sortBy == SortBy.Genre) { switch (level) { case 0: return song.Genre; case 1: return song.Artist; case 2: return song.Name; } } else if (sortBy == SortBy.Year) { switch (level) { case 0: return song.Year; case 1: return song.Artist; case 2: return song.Name; } } else if (sortBy == SortBy.Rating) { switch (level) { case 0: return song.Rating.ToString("00"); case 1: return song.Name; case 2: return song.Artist; } } else if (sortBy == SortBy.Album) { switch (level) { case 0: return song.Album; case 1: return song.Name; case 2: return song.Artist; } } return string.Empty; }
private static bool CanReadTagsForSong(Song song) { if (_tagLibExtensions.Contains(song.Extension)) return true; return false; }