/// <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)); } }