From d215562a07b7eec7d9784c282dd66faefc76eb5e Mon Sep 17 00:00:00 2001 From: Craig Drummond Date: Wed, 25 Apr 2018 18:50:24 +0100 Subject: [PATCH] Use last index value and total file duration to determine duration of last track in CUE file. Issue #1249 --- mpd-interface/cuefile.cpp | 7 ++++--- mpd-interface/cuefile.h | 2 +- mpd-interface/mpdparseutils.cpp | 15 ++++++--------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/mpd-interface/cuefile.cpp b/mpd-interface/cuefile.cpp index 76e663e09..806567297 100644 --- a/mpd-interface/cuefile.cpp +++ b/mpd-interface/cuefile.cpp @@ -170,7 +170,7 @@ static bool updateSong(const CueEntry &entry, const QString &nextIndex, Song &so // Updates the song with data from the .cue entry. This one must be used only for the // last song in the .cue file. -static bool updateLastSong(const CueEntry &entry, Song &song) +static bool updateLastSong(const CueEntry &entry, Song &song, double &lastTrackIndex) { double beginning = indexToMarker(entry.index); @@ -178,6 +178,7 @@ static bool updateLastSong(const CueEntry &entry, Song &song) if (beginning<0) { return false; } + lastTrackIndex = beginning; song.title=entry.title; song.artist=entry.artist; @@ -211,7 +212,7 @@ static const QList & codecList() return codecs; } -bool CueFile::parse(const QString &fileName, const QString &dir, QList &songList, QSet &files) +bool CueFile::parse(const QString &fileName, const QString &dir, QList &songList, QSet &files, double &lastTrackIndex) { DBUG << fileName; QScopedPointer textStream; @@ -397,7 +398,7 @@ bool CueFile::parse(const QString &fileName, const QString &dir, QList &so } } else { // incorrect index? - if (!updateLastSong(entry, song)) { + if (!updateLastSong(entry, song, lastTrackIndex)) { continue; } } diff --git a/mpd-interface/cuefile.h b/mpd-interface/cuefile.h index 73934c5bc..6eecf006c 100644 --- a/mpd-interface/cuefile.h +++ b/mpd-interface/cuefile.h @@ -37,7 +37,7 @@ namespace CueFile extern bool isCue(const QString &str); extern QByteArray getLoadLine(const QString &str); - extern bool parse(const QString &fileName, const QString &dir, QList &songList, QSet &files); + extern bool parse(const QString &fileName, const QString &dir, QList &songList, QSet &files, double &lastTrackIndex); } #endif // CUEFILE_H diff --git a/mpd-interface/mpdparseutils.cpp b/mpd-interface/mpdparseutils.cpp index 31d9a5813..7a30bb6e0 100644 --- a/mpd-interface/mpdparseutils.cpp +++ b/mpd-interface/mpdparseutils.cpp @@ -573,6 +573,7 @@ void MPDParseUtils::parseDirItems(const QByteArray &data, const QString &mpdDir, continue; } + DBUG << currentSong.file; if (Song::Playlist==currentSong.type) { // lsinfo will return all stored playlists - but this is deprecated. if (!parsePlaylists) { @@ -623,9 +624,10 @@ void MPDParseUtils::parseDirItems(const QByteArray &data, const QString &mpdDir, bool canSplitCue=mpdVersion>=CANTATA_MAKE_VERSION(0,17,0); bool parseCue=canSplitCue && currentSong.isCueFile() && !mpdDir.startsWith(constHttpProtocol) && QFile::exists(mpdDir+currentSong.file); bool cueParseStatus=false; + double lastTrackIndex=0.0; if (parseCue) { DBUG << "Parsing cue file:" << currentSong.file << "mpdDir:" << mpdDir; - cueParseStatus=CueFile::parse(currentSong.file, mpdDir, cueSongs, cueFiles); + cueParseStatus=CueFile::parse(currentSong.file, mpdDir, cueSongs, cueFiles, lastTrackIndex); if (!cueParseStatus) { DBUG << "Failed to parse cue file!"; continue; @@ -661,7 +663,6 @@ void MPDParseUtils::parseDirItems(const QByteArray &data, const QString &mpdDir, bool setTimeFromSource=origFiles.size()==cueSongs.size(); DBUG << "setTimeFromSource" << setTimeFromSource << "at" << albumTime << "#c" << cueFiles.size(); - quint32 usedAlbumTime=0; for (const Song &orig: cueSongs) { Song s=orig; Song albumSong=origFiles[s.name()]; @@ -688,14 +689,10 @@ void MPDParseUtils::parseDirItems(const QByteArray &data, const QString &mpdDir, } if (0==s.time && setTimeFromSource) { s.time=albumSong.time; - } else if (0!=albumTime && 1==cueFiles.size()) { - DBUG << s.title << s.time << albumTime << usedAlbumTime; + } else if (0==s.time && 1==cueFiles.size()) { + DBUG << "XX" << s.title << s.time << albumTime << (lastTrackIndex/1000.0); // Try to set duration of last track by subtracting previous track durations from album duration... - if (0==s.time) { - s.time=albumTime-usedAlbumTime; - } else { - usedAlbumTime+=s.time; - } + s.time=albumTime-(lastTrackIndex/1000.0); } DBUG << s.title << s.time; fixedCueSongs.append(s);