Bug 139707

Summary: Problem with m_length in cue files having only the INDEX01 token, without the INDEX00 one present
Product: [Applications] amarok Reporter: Dawid Wróbel <me>
Component: generalAssignee: Amarok Developers <amarok-bugs-dist>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: Unlisted Binaries   
OS: Linux   
Latest Commit: Version Fixed In:
Attachments: Fixes the problem
Little update
Previous patch somehow got malformed, this one should be fine.

Description Dawid Wróbel 2007-01-07 03:01:03 UTC
Version:           svn 620648 (using KDE KDE 3.5.5)
Installed from:    Unspecified Linux

The cue files that happen to only have INDEX01 tokens with no corresponding INDEX00 token are treaten by amarok in a wrong way. Currenyly in such cases m_length is set to 0, which is wrong. The patch attached provides the fix for this. It also fixes the bug with m_length beeing set to 0 for the final track.
Comment 1 Dawid Wróbel 2007-01-07 03:02:34 UTC
Created attachment 19145 [details]
Fixes the problem

The patch fixes the problem for both bugs. Tested with different and weird cue
files (having mixed INDEX00 and INDEX01 sets).
Comment 2 Dawid Wróbel 2007-01-07 03:40:02 UTC
Created attachment 19148 [details]
Little update

Updated a bit, actually just added a one more handy debug().
Comment 3 Dawid Wróbel 2007-01-07 04:01:32 UTC
Created attachment 19149 [details]
Previous patch somehow got malformed, this one should be fine.
Comment 4 Alexandre Oliveira 2007-01-07 04:11:22 UTC
SVN commit 620705 by aoliveira:

Length of tracks wouldn't be shown correctly for some cue files.
Patch by Dawid Wróbel <dawid@klej.net>
BUG: 139707


 M  +2 -0      ChangeLog  
 M  +2 -2      src/contextbrowser.cpp  
 M  +29 -9     src/cuefile.cpp  
 M  +1 -1      src/cuefile.h  


--- trunk/extragear/multimedia/amarok/ChangeLog #620704:620705
@@ -94,6 +94,8 @@
     * Amarok now saves playlists with relative paths by default.
 
   BUGFIXES:
+    * Length of tracks wouldn't be shown correctly for some cue files.
+      Patch by Dawid Wróbel <dawid@klej.net> (BR 139707)
     * Assume that all dots but the last in script executable files belong to
       the script name. (BR 139460)
     * Don't crash when quitting while initially loading the playlist.
--- trunk/extragear/multimedia/amarok/src/contextbrowser.cpp #620704:620705
@@ -666,7 +666,7 @@
 
         m_cuefile->setCueFileName( cueFile );
 
-        if( m_cuefile->load() )
+        if( m_cuefile->load( bundle.length() ) )
             debug() << "[CUEFILE]: " << cueFile << " - Shoot blindly, found and loaded. " << endl;
 
         // if unlucky, let's have a look inside cue files, if any
@@ -704,7 +704,7 @@
                                     cueFile = dir.filePath(*it);
                                     foundCueFile = true;
                                     m_cuefile->setCueFileName( cueFile );
-                                    if( m_cuefile->load() )
+                                    if( m_cuefile->load( bundle.length() ) )
                                         debug() << "[CUEFILE]: " << cueFile << " - Looked inside cue files, found and loaded proper one" << endl;
                                 }
                             }
--- trunk/extragear/multimedia/amarok/src/cuefile.cpp #620704:620705
@@ -59,7 +59,7 @@
 /**
 * @return true if the cuefile could be successfully loaded
 */
-bool CueFile::load()
+bool CueFile::load(int mediaLength)
 {
     clear();
     m_lastSeekPos = -1;
@@ -74,6 +74,7 @@
         QString title = QString::null;
         long length = 0;
         long prevIndex = -1;
+        bool index00Present = false;
         long index = -1;
 
         int mode = BEGIN;
@@ -130,7 +131,6 @@
                         // add previous entry to map
                         insert( index, CueFileItem( title, artist, defaultAlbum, track, index ) );
                         prevIndex = index;
-                        index  = -1;
                         title  = QString::null;
                         artist = QString::null;
                         track  = 0;
@@ -145,28 +145,38 @@
                     {
                         int indexNo = line.section(' ',1,1).toInt();
 
-                        if(indexNo == 1)
+                        if( indexNo == 1 )
                         {
                             QStringList time = QStringList::split( QChar(':'),line.section (' ',-1,-1) );
 
                             index = time[0].toLong()*60*1000 + time[1].toLong()*1000 + time[2].toLong()*1000/75; //75 frames per second
+
+                             if( prevIndex != -1 && !index00Present ) // set the prev track's length if there is INDEX01 present, but no INDEX00
+                            {
+                            	length = index - prevIndex; 
+                            	debug() << "Setting length of track " << (*this)[prevIndex].getTitle() << " to " << length << " msecs." << endl;
+                            	(*this)[prevIndex].setLength(length);
+                            }
+
+                            index00Present = false;
                             mode = INDEX_FOUND;
                             length = 0;
                         }
-                        else if(indexNo == 0) // gap, use to calc prev track length
+
+                        else if( indexNo == 0 ) // gap, use to calc prev track length
                         {
                             QStringList time = QStringList::split( QChar(':'),line.section (' ',-1,-1) );
                             length = time[0].toLong()*60*1000 + time[1].toLong()*1000 + time[2].toLong()*1000/75; //75 frames per second
 
-                            if(prevIndex != -1)
+                            if( prevIndex != -1 )
                             {
                             	length -= prevIndex; //this[prevIndex].getIndex();
                             	debug() << "Setting length of track " << (*this)[prevIndex].getTitle() << " to " << length << " msecs." << endl;
                             	(*this)[prevIndex].setLength(length);
-                            	prevIndex = -1; // FIXME safety?
+                            	index00Present = true;
                             }
                             else
-                            	length = 0;
+                                length =  0;
                         }
                         else
                         {
@@ -192,11 +202,21 @@
             insert( index, CueFileItem( title, artist, defaultAlbum, track, index ) );
             file.close();
         }
+
+        /**
+            *  Because there is no way to set the length for the last track in a normal way,
+            *  we have to do some magic here. Having the total length of the media file given
+            *  we can set the lenth for the last track after all the cue file was loaded into array.
+            */
+
+        (*this)[index].setLength(mediaLength*1000 - index);
+        debug() << "Setting length of track " << (*this)[index].getTitle() << " to " << mediaLength*1000 - index << " msecs." << endl;
+
         return true;
     }
-    else {
+
+    else 
         return false;
-    }
 }
 
 void CueFile::engineTrackPositionChanged( long position, bool userSeek )
--- trunk/extragear/multimedia/amarok/src/cuefile.h #620704:620705
@@ -51,7 +51,7 @@
         static CueFile *instance();
 
         void setCueFileName( QString name ) { m_cueFileName = name; };
-        bool load();
+        bool load(int mediaLength);
 
         // EngineObserver
         virtual void engineTrackPositionChanged( long /*position*/ , bool /*userSeek*/ );