/// <summary> /// Called when a group of songs from the same folder have been scanned /// Group the songs into albums by album name and then process each of these albums /// </summary> /// <param name="songs"></param> public async Task SongsScanned(List <ScannedSong> songs) { Dictionary <string, ScannedAlbum> albumGroups = new(); // Group the list of songs according to the associated album, and determine if all the songs are by the same artist foreach (ScannedSong song in songs) { // Replace empty tags etc. song.NormaliseTags(); // Is there a group for this album if (albumGroups.TryGetValue(song.Tags.Album, out ScannedAlbum album) == false) { album = new ScannedAlbum() { Name = song.Tags.Album }; albumGroups[album.Name] = album; } if (await DoesSongRequireAdding(song) == true) { // Add the song to the album album.Songs.Add(song); } // If this is not the first song in the group then check if the artist is the same if (album.Songs.Count > 1) { if ((album.SingleArtist == true) && (album.Songs[0].ArtistName.ToUpper() != song.ArtistName.ToUpper())) { album.SingleArtist = false; } } } // Store the scanned song data foreach (ScannedAlbum album in albumGroups.Values) { if (album.Songs.Count > 0) { await StoreAlbumAsync(album); } } }
/// <summary> /// Either find an existing album or create a new album to hold the songs. /// If all songs are from the same artist then check is there is an existing album of the same name associated with that artist. /// If the songs are from different artists then check in the "Various Artists" artist. /// These artists may not exist at this stage /// </summary> /// <returns></returns> private async Task <Album> GetAlbumToHoldSongsAsync(ScannedAlbum album) { Album songAlbum = null; string artistName = (album.SingleArtist == true) ? album.Songs[0].ArtistName : VariousArtistsString; // Check if the artist already exists in the library. Artist songArtist = artistsInLibrary.GetValueOrDefault(artistName.ToUpper()); // If the artist exists then check for existing album. The artist will hold ArtistAlbum entries rather than Album entries, but the ArtistAlbum entries // have the same name as the albums. Cannot just use the album name as that may not be unique. if (songArtist != null) { ArtistAlbum songArtistAlbum = songArtist.ArtistAlbums.SingleOrDefault(p => (p.Name.ToUpper() == album.Songs[0].Tags.Album.ToUpper())); if (songArtistAlbum != null) { songAlbum = songArtistAlbum.Album; // The rest of the code expects the Album to have its songs populated, so check here songAlbum.GetSongs(); } } Logger.Log(string.Format("Album: {0} {1} for artist {2}", album.Name, (songAlbum != null) ? "found" : "not found", artistName)); // If no existing album create a new one if (songAlbum == null) { songAlbum = new Album() { Name = album.Name, Songs = new List <Song>(), LibraryId = scanLibrary }; await Albums.AddAlbumAsync(songAlbum); } return(songAlbum); }
/// <summary> /// Called to process a group of songs belonging to the same album name ( but not necessarily the same artist ) /// </summary> /// <param name="album"></param> private async Task StoreAlbumAsync(ScannedAlbum album) { // Set the modified flag LibraryModified = true; Logger.Log(string.Format("Album: {0} Single artist: {1}", album.Name, album.SingleArtist)); // Get an existing or new Album entry for the songs Album songAlbum = await GetAlbumToHoldSongsAsync(album); // Keep track of Artist and ArtistAlbum entries in case they can be reused for different artists ArtistAlbum songArtistAlbum = null; Artist songArtist = null; // Add all the songs in the album foreach (ScannedSong songScanned in album.Songs) { // If there is no existing Artist entry or the current one if for the wrong Artist name then get or create a new one if ((songArtist == null) || (songArtist.Name != songScanned.ArtistName)) { // As this is a new Artist the ArtistAlbum needs to be re-initialised. if (songArtistAlbum != null) { songArtistAlbum.Songs.Sort((a, b) => a.Track.CompareTo(b.Track)); } songArtistAlbum = null; // Find the Artist for this song songArtist = await GetArtistToHoldSongsAsync(songScanned.ArtistName); } // If there is an existing ArtistAlbum then use it as it will be for the correct Artist, i.e. not cleared above if (songArtistAlbum == null) { // Find an existing or create a new ArtistAlbum entry songArtistAlbum = await GetArtistAlbumToHoldSongsAsync(songArtist, songAlbum); } // Add the song to the database, the album and the album artist Song songToAdd = new() { Title = songScanned.Tags.Title, Track = songScanned.Track, Path = songScanned.SourcePath, ModifiedTime = songScanned.Modified, Length = songScanned.Length, AlbumId = songAlbum.Id, ArtistAlbumId = songArtistAlbum.Id, SourceId = sourceBeingScanned.Id }; // No need to wait for this Songs.AddSongAsync(songToAdd); Logger.Log(string.Format("Artist: {0} Title: {1} Track: {2} Modified: {3} Length {4} Year {5}", songScanned.Tags.Artist, songScanned.Tags.Title, songScanned.Tags.Track, songScanned.Modified, songScanned.Length, songScanned.Year)); // Add to the Album songAlbum.Songs.Add(songToAdd); // Keep track whether or not to update the album bool updateAlbum = false; // Store the artist name with the album if ((songAlbum.ArtistName == null) || (songAlbum.ArtistName.Length == 0)) { songAlbum.ArtistName = songArtist.Name; updateAlbum = true; } else { // The artist has already been stored - check if it is the same artist if (songAlbum.ArtistName != songArtist.Name) { songAlbum.ArtistName = VariousArtistsString; updateAlbum = true; } } // Update the album year if not already set and this song has a year set if ((songAlbum.Year != songScanned.Year) && (songAlbum.Year == 0)) { songAlbum.Year = songScanned.Year; updateAlbum = true; } // Update the album genre. // Only try updating if a genre is defined for the song // If the album does not have a genre then get one for the song and store it in the album if ((songScanned.Tags.Genre.Length > 0) && (songAlbum.Genre.Length == 0)) { songAlbum.Genre = songScanned.Tags.Genre; updateAlbum = true; } if (updateAlbum == true) { await ConnectionDetailsModel.AsynchConnection.UpdateAsync(songAlbum); } // Add to the source sourceBeingScanned.Songs.Add(songToAdd); // Add to the ArtistAlbum songArtistAlbum.Songs.Add(songToAdd); } if (songArtistAlbum != null) { songArtistAlbum.Songs.Sort((a, b) => a.Track.CompareTo(b.Track)); } }