Index: /trunk/ChangeLog
===================================================================
--- /trunk/ChangeLog	(revision 417)
+++ /trunk/ChangeLog	(revision 420)
@@ -1,2 +1,10 @@
+2011-01-02  Thomas Vander Stichele  <thomas at apestaart dot org>
+
+	* morituri/common/program.py:
+	* morituri/image/table.py:
+	* morituri/rip/cd.py:
+	  Get CDDB disc id.  Use it to print info when not found on
+	  MusicBrainz.
+
 2011-01-01  Thomas Vander Stichele  <thomas at apestaart dot org>
 
Index: /trunk/morituri/image/table.py
===================================================================
--- /trunk/morituri/image/table.py	(revision 369)
+++ /trunk/morituri/image/table.py	(revision 420)
@@ -240,11 +240,68 @@
         return ret
 
-    def getCDDBDiscId(self):
-        """
-        Calculate the CDDB disc ID.
-
-        @rtype:   str
-        @returns: the 8-character hexadecimal disc ID
-        """
+    def _getCDDBValues(self):
+        """
+        Get all CDDB values needed to calculate disc id and lookup URL.
+
+        This includes:
+         - CDDB disc id
+         - number of audio tracks
+         - offset of index 1 of each track
+         - length of disc in seconds
+
+        @rtype:   list of int
+        """
+        result = []
+
+        # number of first track
+        result.append(1)
+
+        # number of last audio track
+        result.append(self.getAudioTracks())
+
+        leadout = self.leadout
+        # if the disc is multi-session, last track is the data track,
+        # and we should subtract 11250 + 150 from the last track's offset
+        # for the leadout
+        if self.hasDataTracks():
+            assert not self.tracks[-1].audio
+            leadout = self.tracks[-1].getIndex(1).absolute - 11250 - 150
+
+        # treat leadout offset as track 0 offset
+        result.append(150 + leadout)
+
+        # offsets of tracks
+        for i in range(1, 100):
+            try:
+                track = self.tracks[i - 1]
+                if not track.audio:
+                    continue
+                offset = track.getIndex(1).absolute + 150
+                result.append(offset)
+            except IndexError:
+                pass
+
+
+        self.debug('CDDB values: %r', result)
+        return result
+
+
+
+    def getCDDBValues(self):
+        """
+        Get all CDDB values needed to calculate disc id and lookup URL.
+
+        This includes:
+         - CDDB disc id
+         - number of audio tracks
+         - offset of index 1 of each track
+         - length of disc in seconds
+
+        @rtype:   list of int
+        """
+        result = []
+
+        result.append(self.getAudioTracks())
+
         # cddb disc id takes into account data tracks
         # last byte is the number of tracks on the CD
@@ -260,4 +317,5 @@
         for track in self.tracks:
             offset = self.getTrackStart(track.number) + delta
+            result.append(offset)
             debug.append(str(offset))
             seconds = offset / common.FRAMES_PER_SECOND
@@ -273,12 +331,28 @@
         t = leadoutSeconds - startSeconds
         debug.append(str(leadoutSeconds + 2)) # 2 is the 150 frame cddb offset
+        result.append(leadoutSeconds)
 
         value = (n % 0xff) << 24 | t << 8 | len(self.tracks)
+        result.insert(0, value)
 
         # compare this debug line to cd-discid output
+        self.debug('cddb values: %r', result)
+
         self.debug('cddb disc id debug: %s',
             " ".join(["%08x" % value, ] + debug))
         
-        return "%08x" % value
+        return result
+
+
+    def getCDDBDiscId(self):
+        """
+        Calculate the CDDB disc ID.
+
+        @rtype:   str
+        @returns: the 8-character hexadecimal disc ID
+        """
+        values = self.getCDDBValues()
+        return "%08x" % values[0]
+
 
     def getMusicBrainzDiscId(self):
Index: /trunk/morituri/common/program.py
===================================================================
--- /trunk/morituri/common/program.py	(revision 416)
+++ /trunk/morituri/common/program.py	(revision 420)
@@ -301,4 +301,20 @@
 
         return os.path.join(outdir, template % v)
+
+    def getCDDB(self, cddbdiscid):
+        """
+        @param cddbdiscid: list of id, tracks, offsets, seconds
+
+        @rtype: str
+        """
+        # FIXME: convert to nonblocking?
+        import CDDB
+        code, md = CDDB.query(cddbdiscid) 
+        self.debug('CDDB query result: %r, %r', code, md)
+        if code == 200:
+            return md['title']
+
+        return None
+
 
     def getMusicBrainz(self, ittoc, mbdiscid):
Index: /trunk/morituri/rip/cd.py
===================================================================
--- /trunk/morituri/rip/cd.py	(revision 419)
+++ /trunk/morituri/rip/cd.py	(revision 420)
@@ -138,8 +138,14 @@
         prog.metadata = prog.getMusicBrainz(ittoc, mbdiscid)
 
-        # stop if the cd is unknown and we don't want to continue
-        if not prog.metadata and not self.options.unknown:
-            prog.ejectDevice(device)
-            return -1
+        if not prog.metadata:
+            # fall back to FreeDB for lookup
+            cddbid = ittoc.getCDDBValues()
+            cddbmd = prog.getCDDB(cddbid)
+            if cddbmd:
+                print 'FreeDB identifies disc as %s' % cddbmd
+
+            if not self.options.unknown:
+                prog.ejectDevice(device)
+                return -1
 
         # now, read the complete index table, which is slower
