summaryrefslogtreecommitdiffstats
path: root/xbmc/music/Song.cpp
blob: 165889ca924445e733b3454f0b760f99ee2058fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "Song.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "music/tags/MusicInfoTag.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include "utils/log.h"

using namespace MUSIC_INFO;

CSong::CSong(CFileItem& item)
{
  CMusicInfoTag& tag = *item.GetMusicInfoTag();
  strTitle = tag.GetTitle();
  genre = tag.GetGenre();
  strArtistDesc = tag.GetArtistString();
  //Set sort string before processing artist credits
  strArtistSort = tag.GetArtistSort();
  m_strComposerSort = tag.GetComposerSort();

  // Determine artist credits from various tag arrays
  SetArtistCredits(tag.GetArtist(), tag.GetMusicBrainzArtistHints(), tag.GetMusicBrainzArtistID());

  strAlbum = tag.GetAlbum();
  m_albumArtist = tag.GetAlbumArtist();
  // Separate album artist names further, if possible, and trim blank space.
  if (tag.GetMusicBrainzAlbumArtistHints().size() > m_albumArtist.size())
    // Make use of hints (ALBUMARTISTS tag), when present, to separate artist names
    m_albumArtist = tag.GetMusicBrainzAlbumArtistHints();
  else
    // Split album artist names further using multiple possible delimiters, over single separator applied in Tag loader
    m_albumArtist = StringUtils::SplitMulti(m_albumArtist, CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_musicArtistSeparators);
  for (auto artistname : m_albumArtist)
    StringUtils::Trim(artistname);
  m_strAlbumArtistSort = tag.GetAlbumArtistSort();

  strMusicBrainzTrackID = tag.GetMusicBrainzTrackID();
  m_musicRoles = tag.GetContributors();
  strComment = tag.GetComment();
  strCueSheet = tag.GetCueSheet();
  strMood = tag.GetMood();
  rating = tag.GetRating();
  userrating = tag.GetUserrating();
  votes = tag.GetVotes();
  strOrigReleaseDate = tag.GetOriginalDate();
  strReleaseDate = tag.GetReleaseDate();
  strDiscSubtitle = tag.GetDiscSubtitle();
  iTrack = tag.GetTrackAndDiscNumber();
  iDuration = tag.GetDuration();
  strRecordLabel = tag.GetRecordLabel();
  strAlbumType = tag.GetMusicBrainzReleaseType();
  bCompilation = tag.GetCompilation();
  embeddedArt = tag.GetCoverArtInfo();
  strFileName = tag.GetURL().empty() ? item.GetPath() : tag.GetURL();
  dateAdded = tag.GetDateAdded();
  replayGain = tag.GetReplayGain();
  strThumb = item.GetUserMusicThumb(true);
  iStartOffset = static_cast<int>(item.GetStartOffset());
  iEndOffset = static_cast<int>(item.GetEndOffset());
  idSong = -1;
  iTimesPlayed = 0;
  idAlbum = -1;
  iBPM = tag.GetBPM();
  iSampleRate = tag.GetSampleRate();
  iBitRate = tag.GetBitRate();
  iChannels = tag.GetNoOfChannels();
}

CSong::CSong()
{
  Clear();
}

void CSong::SetArtistCredits(const std::vector<std::string>& names, const std::vector<std::string>& hints,
  const std::vector<std::string>& mbids)
{
  artistCredits.clear();
  std::vector<std::string> artistHints = hints;
  //Split the artist sort string to try and get sort names for individual artists
  std::vector<std::string> artistSort = StringUtils::Split(strArtistSort, CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_musicItemSeparator);

  if (!mbids.empty())
  { // Have musicbrainz artist info, so use it

    // Vector of possible separators in the order least likely to be part of artist name
    const std::vector<std::string> separators{ " feat. ", " ft. ", " Feat. "," Ft. ", ";", ":", "|", "#", "/", " with ", ",", "&" };

    // Establish tag consistency - do the number of musicbrainz ids and number of names in hints or artist match
    if (mbids.size() != artistHints.size() && mbids.size() != names.size())
    {
      // Tags mismatch - report it and then try to fix
      CLog::Log(LOGDEBUG, "Mismatch in song file tags: {} mbid {} names {} {}", (int)mbids.size(),
                (int)names.size(), strTitle, strArtistDesc);
      /*
        Most likely we have no hints and a single artist name like "Artist1 feat. Artist2"
        or "Composer; Conductor, Orchestra, Soloist" or "Artist1/Artist2" where the
        expected single item separator (default = space-slash-space) as not been used.
        Ampersand (&), comma and slash (no spaces) are poor delimiters as could be in name
        e.g. "AC/DC", "Earth, Wind & Fire", but here treat them as such in attempt to find artist names.
        When there are hints but count not match mbid they could be poorly formatted using unexpected
        separators so attempt to split them. Or we could have more hints or artist names than
        musicbrainz id so ignore them but raise warning.
      */
      // Do hints exist yet mismatch
      if (artistHints.size() > 0 &&
        artistHints.size() != mbids.size())
      {
        if (names.size() == mbids.size())
          // Artist name count matches, use that as hints
          artistHints = names;
        else if (artistHints.size() < mbids.size())
        { // Try splitting the hints until have matching number
          artistHints = StringUtils::SplitMulti(artistHints, separators, mbids.size());
        }
        else
          // Extra hints, discard them.
          artistHints.resize(mbids.size());
      }
      // Do hints not exist or still mismatch, try artists
      if (artistHints.size() != mbids.size())
        artistHints = names;
      // Still mismatch, try splitting the hints (now artists) until have matching number
      if (artistHints.size() < mbids.size())
      {
        artistHints = StringUtils::SplitMulti(artistHints, separators, mbids.size());
      }
    }
    else
    { // Either hints or artist names (or both) matches number of musicbrainz id
      // If hints mismatch, use artists
      if (artistHints.size() != mbids.size())
        artistHints = names;
    }

    // Try to get number of artist sort names and musicbrainz ids to match. Split sort names
    // further using multiple possible delimiters, over single separator applied in Tag loader
    if (artistSort.size() != mbids.size())
      artistSort = StringUtils::SplitMulti(artistSort, { ";", ":", "|", "#" });

    for (size_t i = 0; i < mbids.size(); i++)
    {
      std::string artistId = mbids[i];
      std::string artistName;
      /*
       We try and get the corresponding artist name from the hints list.
       Having already attempted to make the number of hints match, if they
       still don't then use musicbrainz id as the name and hope later on we
       can update that entry.
      */
      if (i < artistHints.size())
        artistName = artistHints[i];
      else
        artistName = artistId;

      // Use artist sort name providing we have as many as we have mbid,
      // otherwise something is wrong with them so ignore and leave blank
      if (artistSort.size() == mbids.size())
        artistCredits.emplace_back(StringUtils::Trim(artistName), StringUtils::Trim(artistSort[i]), artistId);
      else
        artistCredits.emplace_back(StringUtils::Trim(artistName), "", artistId);
    }
  }
  else
  { // No musicbrainz artist ids, so fill in directly
    // Separate artist names further, if possible, and trim blank space.
    std::vector<std::string> artists = names;
    if (artistHints.size() > names.size())
      // Make use of hints (ARTISTS tag), when present, to separate artist names
      artists = artistHints;
    else
      // Split artist names further using multiple possible delimiters, over single separator applied in Tag loader
      artists = StringUtils::SplitMulti(artists, CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_musicArtistSeparators);

    if (artistSort.size() != artists.size())
      // Split artist sort names further using multiple possible delimiters, over single separator applied in Tag loader
      artistSort = StringUtils::SplitMulti(artistSort, { ";", ":", "|", "#" });

    for (size_t i = 0; i < artists.size(); i++)
    {
      artistCredits.emplace_back(StringUtils::Trim(artists[i]));
      // Set artist sort name providing we have as many as we have artists,
      // otherwise something is wrong with them so ignore rather than guess.
      if (artistSort.size() == artists.size())
        artistCredits.back().SetSortName(StringUtils::Trim(artistSort[i]));
    }
  }

}

void CSong::MergeScrapedSong(const CSong& source, bool override)
{
  // Merge when MusicBrainz Track ID match (checked in CAlbum::MergeScrapedAlbum)
  if ((override && !source.strTitle.empty()) || strTitle.empty())
    strTitle = source.strTitle;
  if ((override && source.iTrack != 0) || iTrack == 0)
    iTrack = source.iTrack;
  if (override)
  {
    artistCredits = source.artistCredits; // Replace artists and store mbid returned by scraper
    strArtistDesc.clear();  // @todo: set artist display string e.g. "artist1 feat. artist2" when scraped
  }
}

void CSong::Serialize(CVariant& value) const
{
  value["filename"] = strFileName;
  value["title"] = strTitle;
  value["artist"] = GetArtist();
  value["artistsort"] = GetArtistSort();  // a string for the song not vector of values for each artist
  value["album"] = strAlbum;
  value["albumartist"] = GetAlbumArtist();
  value["genre"] = genre;
  value["duration"] = iDuration;
  value["track"] = iTrack;
  value["year"] = atoi(strReleaseDate.c_str());;
  value["musicbrainztrackid"] = strMusicBrainzTrackID;
  value["comment"] = strComment;
  value["mood"] = strMood;
  value["rating"] = rating;
  value["userrating"] = userrating;
  value["votes"] = votes;
  value["timesplayed"] = iTimesPlayed;
  value["lastplayed"] = lastPlayed.IsValid() ? lastPlayed.GetAsDBDateTime() : "";
  value["dateadded"] = dateAdded.IsValid() ? dateAdded.GetAsDBDateTime() : "";
  value["albumid"] = idAlbum;
  value["albumreleasedate"] = strReleaseDate;
  value["bpm"] = iBPM;
  value["bitrate"] = iBitRate;
  value["samplerate"] = iSampleRate;
  value["channels"] = iChannels;
}

void CSong::Clear()
{
  strFileName.clear();
  strTitle.clear();
  strAlbum.clear();
  strArtistSort.clear();
  strArtistDesc.clear();
  m_albumArtist.clear();
  m_strAlbumArtistSort.clear();
  genre.clear();
  strThumb.clear();
  strMusicBrainzTrackID.clear();
  m_musicRoles.clear();
  strComment.clear();
  strMood.clear();
  rating = 0;
  userrating = 0;
  votes = 0;
  iTrack = 0;
  iDuration = 0;
  strOrigReleaseDate.clear();
  strReleaseDate.clear();
  strDiscSubtitle.clear();
  iStartOffset = 0;
  iEndOffset = 0;
  idSong = -1;
  iTimesPlayed = 0;
  lastPlayed.Reset();
  dateAdded.Reset();
  dateUpdated.Reset();
  dateNew.Reset();
  idAlbum = -1;
  bCompilation = false;
  embeddedArt.Clear();
  iBPM = 0;
  iBitRate = 0;
  iSampleRate = 0;
  iChannels =  0;

  replayGain = ReplayGain();
}
const std::vector<std::string> CSong::GetArtist() const
{
  //Get artist names as vector from artist credits
  std::vector<std::string> songartists;
  for (const auto& artistCredit : artistCredits)
  {
    songartists.push_back(artistCredit.GetArtist());
  }
  //When artist credits have not been populated attempt to build an artist vector from the description string
  //This is a temporary fix, in the longer term other areas should query the song_artist table and populate
  //artist credits. Note that splitting the string may not give the same artists as held in the song_artist table
  if (songartists.empty() && !strArtistDesc.empty())
    songartists = StringUtils::Split(strArtistDesc, CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_musicItemSeparator);
  return songartists;
}

const std::string CSong::GetArtistSort() const
{
  //The stored artist sort name string takes precedence but a
  //value could be created from individual sort names held in artistcredits
  if (!strArtistSort.empty())
    return strArtistSort;
  std::vector<std::string> artistvector;
  for (const auto& artistcredit : artistCredits)
    if (!artistcredit.GetSortName().empty())
      artistvector.emplace_back(artistcredit.GetSortName());
  std::string artistString;
  if (!artistvector.empty())
    artistString = StringUtils::Join(artistvector, "; ");
  return artistString;
}

const std::vector<std::string> CSong::GetMusicBrainzArtistID() const
{
  //Get artist MusicBrainz IDs as vector from artist credits
  std::vector<std::string> musicBrainzID;
  for (const auto& artistCredit : artistCredits)
  {
    musicBrainzID.push_back(artistCredit.GetMusicBrainzArtistID());
  }
  return musicBrainzID;
}

const std::string CSong::GetArtistString() const
{
  //Artist description may be different from the artists in artistcredits (see ARTISTS tag processing)
  //but is takes precedence as a string because artistcredits is not always filled during processing
  if (!strArtistDesc.empty())
    return strArtistDesc;
  std::vector<std::string> artistvector;
  for (const auto& i : artistCredits)
    artistvector.push_back(i.GetArtist());
  std::string artistString;
  if (!artistvector.empty())
    artistString = StringUtils::Join(artistvector, CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_musicItemSeparator);
  return artistString;
}

const std::vector<int> CSong::GetArtistIDArray() const
{
  // Get song artist IDs for json rpc
  std::vector<int> artistids;
  for (const auto& artistCredit : artistCredits)
    artistids.push_back(artistCredit.GetArtistId());
  return artistids;
}

void CSong::AppendArtistRole(const CMusicRole& musicRole)
{
  m_musicRoles.push_back(musicRole);
}

bool CSong::HasArt() const
{
  if (!strThumb.empty()) return true;
  if (!embeddedArt.Empty()) return true;
  return false;
}

bool CSong::ArtMatches(const CSong &right) const
{
  return (right.strThumb == strThumb &&
          embeddedArt.Matches(right.embeddedArt));
}

const std::string CSong::GetDiscSubtitle() const
{
  return strDiscSubtitle;
}