void CMusicInfoScanner::FileItemsToAlbums(CFileItemList& items, VECALBUMS& albums, MAPSONGS* songsMap /* = NULL */) { /* * Step 1: Convert the FileItems into Songs. * If they're MB tagged, create albums directly from the FileItems. * If they're non-MB tagged, index them by album name ready for step 2. */ map<string, VECSONGS> songsByAlbumNames; for (int i = 0; i < items.Size(); ++i) { CMusicInfoTag& tag = *items[i]->GetMusicInfoTag(); CSong song(*items[i]); // keep the db-only fields intact on rescan... if (songsMap != NULL) { MAPSONGS::iterator it = songsMap->find(items[i]->GetPath()); if (it != songsMap->end()) { song.iTimesPlayed = it->second.iTimesPlayed; song.lastPlayed = it->second.lastPlayed; song.iKaraokeNumber = it->second.iKaraokeNumber; if (song.rating == '0') song.rating = it->second.rating; if (song.strThumb.empty()) song.strThumb = it->second.strThumb; } } if (!tag.GetMusicBrainzAlbumID().empty()) { VECALBUMS::iterator it; for (it = albums.begin(); it != albums.end(); ++it) if (it->strMusicBrainzAlbumID == tag.GetMusicBrainzAlbumID()) break; if (it == albums.end()) { CAlbum album(*items[i]); album.songs.push_back(song); albums.push_back(album); } else it->songs.push_back(song); } else songsByAlbumNames[tag.GetAlbum()].push_back(song); } /* Step 2: Split into unique albums based on album name and album artist In the case where the album artist is unknown, we use the primary artist (i.e. first artist from each song). */ for (map<string, VECSONGS>::iterator songsByAlbumName = songsByAlbumNames.begin(); songsByAlbumName != songsByAlbumNames.end(); ++songsByAlbumName) { VECSONGS &songs = songsByAlbumName->second; // sort the songs by tracknumber to identify duplicate track numbers sort(songs.begin(), songs.end(), SortSongsByTrack); // map the songs to their primary artists bool tracksOverlap = false; bool hasAlbumArtist = false; bool isCompilation = true; map<string, vector<CSong *> > artists; for (VECSONGS::iterator song = songs.begin(); song != songs.end(); ++song) { // test for song overlap if (song != songs.begin() && song->iTrack == (song - 1)->iTrack) tracksOverlap = true; if (!song->bCompilation) isCompilation = false; // get primary artist string primary; if (!song->albumArtist.empty()) { primary = song->albumArtist[0]; hasAlbumArtist = true; } else if (!song->artist.empty()) primary = song->artist[0]; // add to the artist map artists[primary].push_back(&(*song)); } /* We have a compilation if 1. album name is non-empty AND 2a. no tracks overlap OR 2b. all tracks are marked as part of compilation AND 3a. a unique primary artist is specified as "various" or "various artists" OR 3b. we have at least two primary artists and no album artist specified. */ bool compilation = !songsByAlbumName->first.empty() && (isCompilation || !tracksOverlap); // 1+2b+2a if (artists.size() == 1) { string artist = artists.begin()->first; StringUtils::ToLower(artist); if (!StringUtils::EqualsNoCase(artist, "various") && !StringUtils::EqualsNoCase(artist, "various artists")) // 3a compilation = false; } else if (hasAlbumArtist) // 3b compilation = false; if (compilation) { CLog::Log(LOGDEBUG, "Album '%s' is a compilation as there's no overlapping tracks and %s", songsByAlbumName->first.c_str(), hasAlbumArtist ? "the album artist is 'Various'" : "there is more than one unique artist"); artists.clear(); std::string various = g_localizeStrings.Get(340); // Various Artists vector<string> va; va.push_back(various); for (VECSONGS::iterator song = songs.begin(); song != songs.end(); ++song) { song->albumArtist = va; artists[various].push_back(&(*song)); } } /* Step 3: Find the common albumartist for each song and assign albumartist to those tracks that don't have it set. */ for (map<string, vector<CSong *> >::iterator j = artists.begin(); j != artists.end(); ++j) { // find the common artist for these songs vector<CSong *> &artistSongs = j->second; vector<string> common = artistSongs.front()->albumArtist.empty() ? artistSongs.front()->artist : artistSongs.front()->albumArtist; for (vector<CSong *>::iterator k = artistSongs.begin() + 1; k != artistSongs.end(); ++k) { unsigned int match = 0; vector<string> &compare = (*k)->albumArtist.empty() ? (*k)->artist : (*k)->albumArtist; for (; match < common.size() && match < compare.size(); match++) { if (compare[match] != common[match]) break; } common.erase(common.begin() + match, common.end()); } /* Step 4: Assign the album artist for each song that doesn't have it set and add to the album vector */ CAlbum album; album.strAlbum = songsByAlbumName->first; album.artist = common; for (vector<string>::iterator it = common.begin(); it != common.end(); ++it) { std::string strJoinPhrase = (it == --common.end() ? "" : g_advancedSettings.m_musicItemSeparator); CArtistCredit artistCredit(*it, strJoinPhrase); album.artistCredits.push_back(artistCredit); } album.bCompilation = compilation; for (vector<CSong *>::iterator k = artistSongs.begin(); k != artistSongs.end(); ++k) { if ((*k)->albumArtist.empty()) (*k)->albumArtist = common; // TODO: in future we may wish to union up the genres, for now we assume they're the same album.genre = (*k)->genre; // in addition, we may want to use year as discriminating for albums album.iYear = (*k)->iYear; album.songs.push_back(**k); } albums.push_back(album); } } }
void CMusicInfoScanner::CategoriseAlbums(VECSONGS &songsToCheck, VECALBUMS &albums) { /* Step 1: categorise on the album name */ map<string, vector<CSong *> > albumNames; for (VECSONGS::iterator i = songsToCheck.begin(); i != songsToCheck.end(); ++i) albumNames[i->strAlbum].push_back(&(*i)); /* Step 2: Split into unique albums based on album name and album artist In the case where the album artist is unknown, we use the primary artist (i.e. first artist from each song). */ albums.clear(); for (map<string, vector<CSong *> >::iterator i = albumNames.begin(); i != albumNames.end(); ++i) { // sort the songs by tracknumber to identify duplicate track numbers vector<CSong *> &songs = i->second; sort(songs.begin(), songs.end(), SortSongsByTrack); // map the songs to their primary artists bool tracksOverlap = false; bool hasAlbumArtist = false; map<string, vector<CSong *> > artists; for (vector<CSong *>::iterator j = songs.begin(); j != songs.end(); ++j) { CSong *song = *j; // test for song overlap if (j != songs.begin() && song->iTrack == (*(j-1))->iTrack) tracksOverlap = true; // get primary artist string primary; if (!song->albumArtist.empty()) { primary = song->albumArtist[0]; hasAlbumArtist = true; } else if (!song->artist.empty()) primary = song->artist[0]; // add to the artist map artists[primary].push_back(song); } /* We have a compilation if 1. album name is non-empty AND 2. no tracks overlap AND 3a. a unique primary artist is specified as "various" or "various artists" OR 3b. we have at least two primary artists and no album artist specified. */ bool compilation = !i->first.empty() && !tracksOverlap; // 1+2 if (artists.size() == 1) { string artist = artists.begin()->first; StringUtils::ToLower(artist); if (!StringUtils::EqualsNoCase(artist, "various") && !StringUtils::EqualsNoCase(artist, "various artists")) // 3a compilation = false; } else if (hasAlbumArtist) // 3b compilation = false; if (compilation) { CLog::Log(LOGDEBUG, "Album '%s' is a compilation as there's no overlapping tracks and %s", i->first.c_str(), hasAlbumArtist ? "the album artist is 'Various'" : "there is more than one unique artist"); artists.clear(); std::string various = g_localizeStrings.Get(340); // Various Artists vector<string> va; va.push_back(various); for (vector<CSong *>::iterator j = songs.begin(); j != songs.end(); ++j) (*j)->albumArtist = va; artists.insert(make_pair(various, songs)); } /* Step 3: Find the common albumartist for each song and assign albumartist to those tracks that don't have it set. */ for (map<string, vector<CSong *> >::iterator j = artists.begin(); j != artists.end(); ++j) { // find the common artist for these songs vector<CSong *> &artistSongs = j->second; vector<string> common = artistSongs.front()->albumArtist.empty() ? artistSongs.front()->artist : artistSongs.front()->albumArtist; for (vector<CSong *>::iterator k = artistSongs.begin() + 1; k != artistSongs.end(); ++k) { unsigned int match = 0; vector<string> &compare = (*k)->albumArtist.empty() ? (*k)->artist : (*k)->albumArtist; for (; match < common.size() && match < compare.size(); match++) { if (compare[match] != common[match]) break; } common.erase(common.begin() + match, common.end()); } /* Step 4: Assign the album artist for each song that doesn't have it set and add to the album vector */ CAlbum album; album.strAlbum = i->first; album.artist = common; album.bCompilation = compilation; for (vector<CSong *>::iterator k = artistSongs.begin(); k != artistSongs.end(); ++k) { if ((*k)->albumArtist.empty()) (*k)->albumArtist = common; album.songs.push_back(*(*k)); // TODO: in future we may wish to union up the genres, for now we assume they're the same if (album.genre.empty()) album.genre = (*k)->genre; // in addition, we may want to use year as discriminating for albums if (album.iYear == 0) album.iYear = (*k)->iYear; } albums.push_back(album); } } }
void CMusicInfoScanner::FileItemsToAlbums(CFileItemList& items, VECALBUMS& albums, MAPSONGS* songsMap /* = NULL */) { /* * Step 1: Convert the FileItems into Songs. * If they're MB tagged, create albums directly from the FileItems. * If they're non-MB tagged, index them by album name ready for step 2. */ std::map<std::string, VECSONGS> songsByAlbumNames; for (int i = 0; i < items.Size(); ++i) { CMusicInfoTag& tag = *items[i]->GetMusicInfoTag(); CSong song(*items[i]); // keep the db-only fields intact on rescan... if (songsMap != NULL) { MAPSONGS::iterator it = songsMap->find(items[i]->GetPath()); if (it != songsMap->end()) { song.iTimesPlayed = it->second.iTimesPlayed; song.lastPlayed = it->second.lastPlayed; if (song.rating == 0) song.rating = it->second.rating; if (song.userrating == 0) song.userrating = it->second.userrating; if (song.strThumb.empty()) song.strThumb = it->second.strThumb; } } if (!tag.GetMusicBrainzAlbumID().empty()) { VECALBUMS::iterator it; for (it = albums.begin(); it != albums.end(); ++it) if (it->strMusicBrainzAlbumID == tag.GetMusicBrainzAlbumID()) break; if (it == albums.end()) { CAlbum album(*items[i]); album.songs.push_back(song); albums.push_back(album); } else it->songs.push_back(song); } else songsByAlbumNames[tag.GetAlbum()].push_back(song); } /* Step 2: Split into unique albums based on album name and album artist In the case where the album artist is unknown, we use the primary artist (i.e. first artist from each song). */ for (std::map<std::string, VECSONGS>::iterator songsByAlbumName = songsByAlbumNames.begin(); songsByAlbumName != songsByAlbumNames.end(); ++songsByAlbumName) { VECSONGS &songs = songsByAlbumName->second; // sort the songs by tracknumber to identify duplicate track numbers sort(songs.begin(), songs.end(), SortSongsByTrack); // map the songs to their primary artists bool tracksOverlap = false; bool hasAlbumArtist = false; bool isCompilation = true; std::map<std::string, std::vector<CSong *> > artists; for (VECSONGS::iterator song = songs.begin(); song != songs.end(); ++song) { // test for song overlap if (song != songs.begin() && song->iTrack == (song - 1)->iTrack) tracksOverlap = true; if (!song->bCompilation) isCompilation = false; // get primary artist std::string primary; if (!song->GetAlbumArtist().empty()) { primary = song->GetAlbumArtist()[0]; hasAlbumArtist = true; } else if (!song->artistCredits.empty()) primary = song->artistCredits.begin()->GetArtist(); // add to the artist map artists[primary].push_back(&(*song)); } /* We have a Various Artists compilation if 1. album name is non-empty AND 2a. no tracks overlap OR 2b. all tracks are marked as part of compilation AND 3a. a unique primary artist is specified as "various", "various artists" or the localized value OR 3b. we have at least two primary artists and no album artist specified. */ std::string various = g_localizeStrings.Get(340); // Various Artists bool compilation = !songsByAlbumName->first.empty() && (isCompilation || !tracksOverlap); // 1+2b+2a if (artists.size() == 1) { std::string artist = artists.begin()->first; StringUtils::ToLower(artist); if (!StringUtils::EqualsNoCase(artist, "various") && !StringUtils::EqualsNoCase(artist, "various artists") && !StringUtils::EqualsNoCase(artist, various)) // 3a compilation = false; } else if (hasAlbumArtist) // 3b compilation = false; //Such a compilation album is stored with the localized value for "various artists" as the album artist if (compilation) { CLog::Log(LOGDEBUG, "Album '%s' is a compilation as there's no overlapping tracks and %s", songsByAlbumName->first.c_str(), hasAlbumArtist ? "the album artist is 'Various'" : "there is more than one unique artist"); artists.clear(); std::vector<std::string> va; va.push_back(various); for (VECSONGS::iterator song = songs.begin(); song != songs.end(); ++song) { song->SetAlbumArtist(va); artists[various].push_back(&(*song)); } } /* We also have a compilation album when album name is non-empty and ALL tracks are marked as part of a compilation even if an album artist is given, or all songs have the same primary artist. For example an anthology - a collection of recordings from various old sources combined together such as a "best of", retrospective or rarities type release. Such an anthology compilation will not have been caught by the previous tests as it fails 3a and 3b. The album artist can be determined just like any normal album. */ if (!compilation && !songsByAlbumName->first.empty() && isCompilation) { compilation = true; CLog::Log(LOGDEBUG, "Album '%s' is a compilation as all songs are marked as part of a compilation", songsByAlbumName->first.c_str()); } /* Step 3: Find the common albumartist for each song and assign albumartist to those tracks that don't have it set. */ for (std::map<std::string, std::vector<CSong *> >::iterator j = artists.begin(); j != artists.end(); ++j) { // find the common artist for these songs std::vector<CSong *> &artistSongs = j->second; std::vector<std::string> common = artistSongs.front()->GetAlbumArtist().empty() ? artistSongs.front()->GetArtist() : artistSongs.front()->GetAlbumArtist(); for (std::vector<CSong *>::iterator k = artistSongs.begin() + 1; k != artistSongs.end(); ++k) { unsigned int match = 0; std::vector<std::string> compare = (*k)->GetAlbumArtist().empty() ? (*k)->GetArtist() : (*k)->GetAlbumArtist(); for (; match < common.size() && match < compare.size(); match++) { if (compare[match] != common[match]) break; } common.erase(common.begin() + match, common.end()); } /* Step 4: Assign the album artist for each song that doesn't have it set and add to the album vector */ CAlbum album; album.strAlbum = songsByAlbumName->first; for (std::vector<std::string>::iterator it = common.begin(); it != common.end(); ++it) { album.artistCredits.emplace_back(StringUtils::Trim(*it)); } album.bCompilation = compilation; for (std::vector<CSong *>::iterator k = artistSongs.begin(); k != artistSongs.end(); ++k) { if ((*k)->GetAlbumArtist().empty()) (*k)->SetAlbumArtist(common); //! @todo in future we may wish to union up the genres, for now we assume they're the same album.genre = (*k)->genre; // in addition, we may want to use year as discriminating for albums album.iYear = (*k)->iYear; album.strLabel = (*k)->strRecordLabel; album.strType = (*k)->strAlbumType; album.songs.push_back(**k); } albums.push_back(album); } } }