public static int CompareSongsByDiscAndTrack(Song x, Song y) { if (x.DiscNumber == y.DiscNumber && x.TrackNumber == y.TrackNumber) { return 0; } // if the disc numbers are equal, we have to compare by track else if (x.DiscNumber == y.DiscNumber) { return x.TrackNumber > y.TrackNumber ? 1 : -1; } // if the disc numbers are not equal, the one with the higher disc number is greater. else { return x.DiscNumber > y.DiscNumber ? 1 : -1; } }
public Song CreateSong(string filePath, int? folderId, TagLib.File file) { int? itemId = Injection.Kernel.Get<IItemRepository>().GenerateItemId(ItemType.Song); if (itemId == null) { return new Song(); } Song song = new Song(); song.ItemId = itemId; song.FolderId = folderId; // Parse taglib tags TagLib.Tag tag = file.Tag; try { string firstPerformer = tag.FirstPerformer; if (firstPerformer != null) { Artist artist = Injection.Kernel.Get<IArtistRepository>().ArtistForNameOrCreate(firstPerformer.Trim()); song.ArtistId = artist.ArtistId; song.ArtistName = artist.ArtistName; } } catch (Exception e) { if (logger.IsErrorEnabled) logger.Error("Error creating artist info for song: ", e); song.ArtistId = null; song.ArtistName = null; } try { string firstAlbumArtist = tag.FirstAlbumArtist; if (firstAlbumArtist != null) { AlbumArtist albumArtist = Injection.Kernel.Get<IAlbumArtistRepository>().AlbumArtistForNameOrCreate(firstAlbumArtist.Trim()); song.AlbumArtistId = albumArtist.AlbumArtistId; song.AlbumArtistName = albumArtist.AlbumArtistName; } } catch (Exception e) { if (logger.IsErrorEnabled) logger.Error("Error creating album artist info for song: ", e); song.AlbumArtistId = null; song.AlbumArtistName = null; } // If we have an artist, but not an albumArtist, then use the artist info for albumArtist if (song.AlbumArtistId == null && song.ArtistName != null) { AlbumArtist albumArtist = Injection.Kernel.Get<IAlbumArtistRepository>().AlbumArtistForNameOrCreate(song.ArtistName); song.AlbumArtistId = albumArtist.AlbumArtistId; song.AlbumArtistName = albumArtist.AlbumArtistName; } try { string albumName = tag.Album; if (albumName != null) { Album album = Injection.Kernel.Get<IAlbumRepository>().AlbumForName(albumName.Trim(), song.AlbumArtistId, Convert.ToInt32(tag.Year)); song.AlbumId = album.AlbumId; song.AlbumName = album.AlbumName; song.ReleaseYear = album.ReleaseYear; } } catch (Exception e) { if (logger.IsErrorEnabled) logger.Error("Error creating album info for song: ", e); song.AlbumId = null; song.AlbumName = null; song.ReleaseYear = null; } song.FileType = song.FileType.FileTypeForTagLibMimeType(file.MimeType); if (song.FileType == FileType.Unknown) { logger.IfInfo("\"" + filePath + "\" Unknown file type: " + file.Properties.Description); } try { string title = tag.Title; if (title != null) { song.SongName = title.Trim(); } } catch { song.SongName = null; } try { song.TrackNumber = Convert.ToInt32(tag.Track); } catch { song.TrackNumber = null; } try { song.DiscNumber = Convert.ToInt32(tag.Disc); } catch { song.DiscNumber = null; } try { string firstGenre = tag.FirstGenre; if (firstGenre != null) { song.GenreName = firstGenre.Trim(); } } catch { song.GenreName = null; } try { song.BeatsPerMinute = tag.BeatsPerMinute; } catch { song.BeatsPerMinute = null; } try { song.Lyrics = tag.Lyrics; } catch { song.Lyrics = null; } try { song.Comment = tag.Comment; } catch { song.Comment = null; } // Dispose tag tag = null; if ((object)song.GenreName != null) { // Retreive the genre id song.GenreId = Injection.Kernel.Get<IGenreRepository>().GenreForName(song.GenreName).GenreId; } song.Duration = Convert.ToInt32(file.Properties.Duration.TotalSeconds); song.Bitrate = file.Properties.AudioBitrate; // Get necessary filesystem information about file FileInfo fsFile = new FileInfo(filePath); song.FileSize = fsFile.Length; song.LastModified = fsFile.LastWriteTime.ToUnixTime(); song.FileName = fsFile.Name; // If there is no song name, use the file name if (song.SongName == null || song.SongName == "") { song.SongName = song.FileName; } // Generate an art id from the embedded art, if it exists int? artId = CreateArt(file).ArtId; // Dispose file handles fsFile = null; file.Dispose(); // If there was no embedded art, use the folder's art artId = (object)artId == null ? Injection.Kernel.Get<IArtRepository>().ArtIdForItemId(song.FolderId) : artId; // Create the art/item relationship Injection.Kernel.Get<IArtRepository>().UpdateArtItemRelationship(artId, song.ItemId, true); return song; }