private static void GetGenre(SongTagFile song, Id3Tag tag) { if (tag.Genre.IsAssigned) { song.Genre = tag.Genre.Value.CleanString(); } }
/// <summary> /// Gets the rating from an Unknown frame. POPM /// </summary> /// <remarks> /// http://id3.org/id3v2.4.0-frames POPM /// </remarks> /// <param name="tag">The tag.</param> /// <returns></returns> private static void GetRating(SongTagFile song, Id3.Id3Tag tag) { var unknownFrames = (tag.Frames.Where(x => x.GetType() == typeof(UnknownFrame))); foreach (var item in unknownFrames) { var s = item as UnknownFrame; if (s.Id == "POPM") { //Byte Length 35 - if it includes a playcount //Byte Length 31 - No Playcount - Rating should be at the end of the array var f = s.Encode(); //Get rating byte depending on length byte[] ratings = new byte[1]; if (f.Length == 31) { ratings[0] = f[30]; } else if (f.Length == 35) { ratings[0] = f[f.Length - 5]; } song.Rating = ConvertRating(ratings[0]); return; } } }
private static void GetArtist(SongTagFile song, Id3Tag tag) { if (tag.Artists.IsAssigned) { song.Artist = tag.Artists.Value.CleanString(); } }
private static void GetLabelPublisher(SongTagFile song, Id3Tag tag) { if (tag.Publisher.IsAssigned) { song.Label = tag.Publisher.Value.CleanString(); } }
private SongTagFile BuildSongFromTag(TagLib.Tag tag, TagLib.File track) { var song = new SongTagFile(); song.Album = tag.Album; song.Artist = tag.FirstPerformer; song.Bpm = (int?)tag.BeatsPerMinute; song.Label = tag.Publisher; song.Genre = tag.FirstGenre; song.Title = tag.Title; song.Year = (int)tag.Year; song.ImageData = tag.Pictures[0].Data.Data; song.Comment = tag.Comment; song.Country = tag.Country; song.MusicalKey = tag.TKey; song.TrackNumber = (int?)tag.Track; //Set discogs int discogsId = 0; try { discogsId = Convert.ToInt32(tag.DiscogsId); song.DiscogReleaseId = discogsId; } catch { } song.Duration = track.Properties.Duration.ToString("hh':'mm':'ss"); song.BitRate = track.Properties.AudioBitrate; return(song); }
private static void GetYear(SongTagFile song, Id3Tag tag) { //Get year from DateTime if (tag.Year.AsDateTime.HasValue) { song.Year = tag.Year.AsDateTime.HasValue ? tag.Year.AsDateTime.Value.Year : 0; } }
private static void GetAlbum(SongTagFile song, Id3Tag tag) { //set standard issue props if (tag.Album.IsAssigned) { song.Album = tag.Album.Value.CleanString(); } }
private static void GetComments(SongTagFile song, Id3Tag tag) { //Get comment from list if (tag.Comments.Count > 0) { song.Comment = tag.Comments[0].Comment.CleanString(); } }
private static void GetPictureBytes(SongTagFile song, Id3Tag tag) { //Extract the picture to bytes. if (tag.Pictures?.Count > 0) { song.ImageData = tag.Pictures[0].PictureData; } }
private void GetDiscogsId(SongTagFile song, Id3Tag tag) { long discogId = GetDiscogId(tag).ReleaseId; if (discogId > 0) { song.DiscogReleaseId = (int)discogId; } }
/// <summary> /// Updates the extra tables using the methods in this class, eg <see cref="AddOrGetAlbumId(SongTagFile, Song)"/> /// </summary> /// <param name="songTag">The song tag.</param> /// <param name="song">The song.</param> private void UpdateExtraTables(SongTagFile songTag, Song song) { //Add artist or assign Id AddOrGetArtistId(songTag, song); AddOrGetAlbumId(songTag, song); AddOrGetLabelId(songTag, song); AddOrGetDiscogsId(songTag, song); AddOrGetGenreId(songTag, song); AddOrGetMusicKeyId(songTag, song); }
public override SongTagFile PopulateSongTag(string fileName, TagOption tagOption = TagOption.All) { using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) using (Mp3Stream mp3Stream = new Mp3Stream(fileStream, Mp3Permissions.ReadWrite)) { var id3Tag = GetTagFromStream(mp3Stream); if (id3Tag != null) { var song = new SongTagFile(); song.Duration = mp3Stream.Audio.Duration.ToString(); song.BitRate = mp3Stream.Audio.Bitrate; return(CreateSongTag(song, id3Tag, tagOption)); } } return(null); }
/// <summary> /// Parses Id3 to create a song model /// </summary> /// <param name="songFile">song file path</param> /// <returns>new song</returns> private SongTagFile CreateSongTag(SongTagFile song, Id3Tag tag, TagOption options = TagOption.All) { //If that tag returned null return if (tag == null) { return(song); } GetTrackNumber(song, tag); GetArtist(song, tag); GetTitle(song, tag); GetAlbum(song, tag); GetYear(song, tag); GetRating(song, tag); GetBpm(song, tag); GetGenre(song, tag); GetComments(song, tag); GetMusicalKey(song, tag); //ARTWORK if (options.HasFlag(TagOption.All) || options.HasFlag(TagOption.Artwork)) { GetPictureBytes(song, tag); } //Country if (options.HasFlag(TagOption.All) || options.HasFlag(TagOption.Country)) { song.Country = GetCountry(tag); } //DISCOGS if (options.HasFlag(TagOption.All) || options.HasFlag(TagOption.Discog)) { GetDiscogsId(song, tag); } //LABEL if (options.HasFlag(TagOption.All) || options.HasFlag(TagOption.Label)) { GetLabelPublisher(song, tag); } return(song); }
private void AddOrGetMusicKeyId(SongTagFile songTag, Song song) { if (!string.IsNullOrEmpty(songTag.MusicalKey)) { var musicalKey = MusicalKeyRepository.Get(x => x.MusicKey == songTag.MusicalKey).FirstOrDefault(); if (musicalKey == null) { song.MusicalKey = new MusicalKey() { MusicKey = songTag.MusicalKey }; MusicalKeyRepository.Insert(song.MusicalKey); this.Save(); } else { song.MusicalKeyId = musicalKey.Id; } } }
private void AddOrGetAlbumId(SongTagFile songTag, Song song) { if (songTag.Album != null) { var album = AlbumRepository.Get(x => x.Title == songTag.Album).FirstOrDefault(); if (album == null) { song.Album = new Album() { Title = songTag.Album }; AlbumRepository.Insert(song.Album); this.Save(); } else { song.AlbumId = album.Id; } } }
private void AddOrGetArtistId(SongTagFile songTag, Song song) { if (songTag.Artist != null) { var artist = ArtistRepository.Get(x => x.Name == songTag.Artist).FirstOrDefault(); if (artist == null) { song.Artist = new Artist() { Name = songTag.Artist }; ArtistRepository.Insert(song.Artist); this.Save(); } else { song.ArtistId = artist.Id; } } }
private void AddOrGetDiscogsId(SongTagFile songTag, Song song) { if (songTag.DiscogReleaseId != null && songTag.DiscogReleaseId > 0) { var discog = DiscogRepository.Get(x => x.ReleaseId == songTag.DiscogReleaseId).FirstOrDefault(); if (discog == null) { song.Discog = new Discog() { ReleaseId = (int)songTag.DiscogReleaseId }; DiscogRepository.Insert(song.Discog); this.Save(); } else { song.DiscogId = discog.Id; } } }
private void AddOrGetLabelId(SongTagFile songTag, Song song) { if (songTag.Label != null) { var label = LabelRepository.Get(x => x.Name == songTag.Label).FirstOrDefault(); if (label == null) { song.Label = new Label() { Name = songTag.Label }; LabelRepository.Insert(song.Label); this.Save(); } else { song.LabelId = label.Id; } } }
private void AddOrGetGenreId(SongTagFile songTag, Song song) { if (songTag.Genre != null) { var genre = GenreRepository.Get(x => x.Name == songTag.Genre).FirstOrDefault(); if (genre == null) { song.Genre = new Genre() { Name = songTag.Genre }; GenreRepository.Insert(song.Genre); this.Save(); } else { song.GenreId = genre.Id; } } }
public void UpdateDbSongTag(SongTagFile songTag, int fileId) { Song song = new Song { FileId = fileId, //Update standrad fields AddedDate = ConvertUnixTime(DateTime.Now).ToString(), BitRate = songTag.BitRate, Bpm = songTag.Bpm, Comment = songTag.Comment, Country = songTag.Country, ImageLocation = songTag.ImageLocation, Rating = (int)songTag.Rating, Title = songTag.Title, Time = songTag.Duration, Track = songTag.TrackNumber, Year = songTag.Year }; UpdateExtraTables(songTag, song); SongRepository.Insert(song); }
/// <summary> /// Save an image for the given song if it doesn't exist. /// Checks discogs releaseId, album name & artist - title for images and saves in this order if available to Art/discog, Art/Album, Art/song /// </summary> /// <param name="song">A song with image data</param> private bool SongImageToImage(SongTagFile song, string artworkFolder) { if (song.ImageData?.Length > 0) { string discogImagePath = Path.Combine(artworkFolder, $"discog\\{song.DiscogReleaseId}.jpg"); var art = song.Artist?.Replace("\"", ""); var title = song.Title?.Replace("\"", ""); var album = song.Album?.Replace("\"", ""); string albumImageFile = Path.Combine(artworkFolder, $@"album\\{art} - {album}.jpg"); string songImageFile = Path.Combine(artworkFolder, $@"song\\{art} - {title} - {album}.jpg"); if (System.IO.File.Exists(discogImagePath)) { song.ImageLocation = discogImagePath; return(false); } if (System.IO.File.Exists(albumImageFile)) { song.ImageLocation = albumImageFile; return(false); } if (System.IO.File.Exists(songImageFile)) { song.ImageLocation = songImageFile; return(false); } using (MemoryStream ms = new MemoryStream(song.ImageData)) { //Check if we have a discogs ID and save under that ID.jpg if (song.DiscogReleaseId != null && song.DiscogReleaseId != 0) { song.ImageLocation = discogImagePath; //Only save the image if we don't have it already. if (!System.IO.File.Exists(discogImagePath)) { SaveImage(ms, discogImagePath); return(true); } } else if (song.Album != string.Empty) { song.ImageLocation = albumImageFile; if (!System.IO.File.Exists(albumImageFile)) { SaveImage(ms, albumImageFile); return(true); } } else if (song.Title != string.Empty) { song.ImageLocation = songImageFile; if (!System.IO.File.Exists(songImageFile)) { SaveImage(ms, songImageFile); return(true); } } return(false); } } else { return(false); } }
/// <summary> /// Updates the database song tag. /// </summary> /// <param name="taggedSong">The tagged song.</param> /// <param name="id">The identifier.</param> public void UpdateDbSongTag(SongTagFile taggedSong, int id) { _horsifyDataRepo.UpdateDbSongTag(taggedSong, id); }
/// <summary> /// Saves the image from a tag. <see cref="SongFileTag"/> /// </summary> /// <param name="taggedSong">The tagged song.</param> private void SaveImageFromTag(SongTagFile taggedSong) { var coverArtDir = Path.Combine(_horsifySettings.HorsifyPath, "CoverArt"); SongImageToImage(taggedSong, coverArtDir); }
private static void GetBpm(SongTagFile song, Id3Tag tag) { song.Bpm = tag.BeatsPerMinute.AsInt; }
private void GetMusicalKey(SongTagFile song, Id3Tag tag) { song.MusicalKey = GetMusicKey(tag).Replace("þ", ""); }
private static void GetTitle(SongTagFile song, Id3Tag tag) { song.Title = tag.Title.IsAssigned ? tag.Title.Value.CleanString() : string.Empty; }
private static void GetTrackNumber(SongTagFile song, Id3Tag tag) { song.TrackNumber = tag.Track.AsInt ?? 1; }