Rename "morituri" module to "whipper".
Fixes https://github.com/JoeLametta/whipper/issues/100
This commit is contained in:
0
whipper/image/__init__.py
Normal file
0
whipper/image/__init__.py
Normal file
207
whipper/image/cue.py
Normal file
207
whipper/image/cue.py
Normal file
@@ -0,0 +1,207 @@
|
||||
# -*- Mode: Python; test-case-name: whipper.test.test_image_cue -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
# Morituri - for those about to RIP
|
||||
|
||||
# Copyright (C) 2009 Thomas Vander Stichele
|
||||
|
||||
# This file is part of whipper.
|
||||
#
|
||||
# whipper is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# whipper is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with whipper. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
Reading .cue files
|
||||
|
||||
See http://digitalx.org/cuesheetsyntax.php
|
||||
"""
|
||||
|
||||
import re
|
||||
import codecs
|
||||
|
||||
from whipper.common import common
|
||||
from whipper.image import table
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_REM_RE = re.compile("^REM\s(\w+)\s(.*)$")
|
||||
_PERFORMER_RE = re.compile("^PERFORMER\s(.*)$")
|
||||
_TITLE_RE = re.compile("^TITLE\s(.*)$")
|
||||
|
||||
_FILE_RE = re.compile(r"""
|
||||
^FILE # FILE
|
||||
\s+"(?P<name>.*)" # 'file name' in quotes
|
||||
\s+(?P<format>\w+)$ # format (WAVE/MP3/AIFF/...)
|
||||
""", re.VERBOSE)
|
||||
|
||||
_TRACK_RE = re.compile(r"""
|
||||
^\s+TRACK # TRACK
|
||||
\s+(?P<track>\d\d) # two-digit track number
|
||||
\s+(?P<mode>.+)$ # mode (AUDIO, MODEx/2xxx, ...)
|
||||
""", re.VERBOSE)
|
||||
|
||||
_INDEX_RE = re.compile(r"""
|
||||
^\s+INDEX # INDEX
|
||||
\s+(\d\d) # two-digit index number
|
||||
\s+(\d\d) # minutes
|
||||
:(\d\d) # seconds
|
||||
:(\d\d)$ # frames
|
||||
""", re.VERBOSE)
|
||||
|
||||
|
||||
class CueFile(object):
|
||||
"""
|
||||
I represent a .cue file as an object.
|
||||
|
||||
@type table: L{table.Table}
|
||||
@ivar table: the index table.
|
||||
"""
|
||||
logCategory = 'CueFile'
|
||||
|
||||
def __init__(self, path):
|
||||
"""
|
||||
@type path: unicode
|
||||
"""
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
|
||||
self._path = path
|
||||
self._rems = {}
|
||||
self._messages = []
|
||||
self.leadout = None
|
||||
self.table = table.Table()
|
||||
|
||||
def parse(self):
|
||||
state = 'HEADER'
|
||||
currentFile = None
|
||||
currentTrack = None
|
||||
counter = 0
|
||||
|
||||
logger.info('Parsing .cue file %r', self._path)
|
||||
handle = codecs.open(self._path, 'r', 'utf-8')
|
||||
|
||||
for number, line in enumerate(handle.readlines()):
|
||||
line = line.rstrip()
|
||||
|
||||
m = _REM_RE.search(line)
|
||||
if m:
|
||||
tag = m.expand('\\1')
|
||||
value = m.expand('\\2')
|
||||
if state != 'HEADER':
|
||||
self.message(number, 'REM %s outside of header' % tag)
|
||||
else:
|
||||
self._rems[tag] = value
|
||||
continue
|
||||
|
||||
# look for FILE lines
|
||||
m = _FILE_RE.search(line)
|
||||
if m:
|
||||
counter += 1
|
||||
filePath = m.group('name')
|
||||
fileFormat = m.group('format')
|
||||
currentFile = File(filePath, fileFormat)
|
||||
|
||||
# look for TRACK lines
|
||||
m = _TRACK_RE.search(line)
|
||||
if m:
|
||||
if not currentFile:
|
||||
self.message(number, 'TRACK without preceding FILE')
|
||||
continue
|
||||
|
||||
state = 'TRACK'
|
||||
|
||||
trackNumber = int(m.group('track'))
|
||||
#trackMode = m.group('mode')
|
||||
|
||||
logger.debug('found track %d', trackNumber)
|
||||
currentTrack = table.Track(trackNumber)
|
||||
self.table.tracks.append(currentTrack)
|
||||
continue
|
||||
|
||||
# look for INDEX lines
|
||||
m = _INDEX_RE.search(line)
|
||||
if m:
|
||||
if not currentTrack:
|
||||
self.message(number, 'INDEX without preceding TRACK')
|
||||
print 'ouch'
|
||||
continue
|
||||
|
||||
indexNumber = int(m.expand('\\1'))
|
||||
minutes = int(m.expand('\\2'))
|
||||
seconds = int(m.expand('\\3'))
|
||||
frames = int(m.expand('\\4'))
|
||||
frameOffset = frames \
|
||||
+ seconds * common.FRAMES_PER_SECOND \
|
||||
+ minutes * common.FRAMES_PER_SECOND * 60
|
||||
|
||||
logger.debug('found index %d of track %r in %r:%d',
|
||||
indexNumber, currentTrack, currentFile.path, frameOffset)
|
||||
# FIXME: what do we do about File's FORMAT ?
|
||||
currentTrack.index(indexNumber,
|
||||
path=currentFile.path, relative=frameOffset,
|
||||
counter=counter)
|
||||
continue
|
||||
|
||||
def message(self, number, message):
|
||||
"""
|
||||
Add a message about a given line in the cue file.
|
||||
|
||||
@param number: line number, counting from 0.
|
||||
"""
|
||||
self._messages.append((number + 1, message))
|
||||
|
||||
def getTrackLength(self, track):
|
||||
# returns track length in frames, or -1 if can't be determined and
|
||||
# complete file should be assumed
|
||||
# FIXME: this assumes a track can only be in one file; is this true ?
|
||||
i = self.table.tracks.index(track)
|
||||
if i == len(self.table.tracks) - 1:
|
||||
# last track, so no length known
|
||||
return -1
|
||||
|
||||
thisIndex = track.indexes[1] # FIXME: could be more
|
||||
nextIndex = self.table.tracks[i + 1].indexes[1] # FIXME: could be 0
|
||||
|
||||
c = thisIndex.counter
|
||||
if c is not None and c == nextIndex.counter:
|
||||
# they belong to the same source, so their relative delta is length
|
||||
return nextIndex.relative - thisIndex.relative
|
||||
|
||||
# FIXME: more logic
|
||||
return -1
|
||||
|
||||
def getRealPath(self, path):
|
||||
"""
|
||||
Translate the .cue's FILE to an existing path.
|
||||
|
||||
@type path: unicode
|
||||
"""
|
||||
return common.getRealPath(self._path, path)
|
||||
|
||||
|
||||
class File:
|
||||
"""
|
||||
I represent a FILE line in a cue file.
|
||||
"""
|
||||
|
||||
def __init__(self, path, format):
|
||||
"""
|
||||
@type path: unicode
|
||||
"""
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
|
||||
self.path = path
|
||||
self.format = format
|
||||
|
||||
def __repr__(self):
|
||||
return '<File %r of format %s>' % (self.path, self.format)
|
||||
255
whipper/image/image.py
Normal file
255
whipper/image/image.py
Normal file
@@ -0,0 +1,255 @@
|
||||
# -*- Mode: Python; test-case-name: whipper.test.test_image_image -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
# Morituri - for those about to RIP
|
||||
|
||||
# Copyright (C) 2009 Thomas Vander Stichele
|
||||
|
||||
# This file is part of whipper.
|
||||
#
|
||||
# whipper is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# whipper is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with whipper. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
Wrap on-disk CD images based on the .cue file.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from whipper.common import encode
|
||||
from whipper.common import common
|
||||
from whipper.common import checksum
|
||||
from whipper.image import cue, table
|
||||
from whipper.extern.task import task
|
||||
from whipper.program.soxi import AudioLengthTask
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Image(object):
|
||||
"""
|
||||
@ivar table: The Table of Contents for this image.
|
||||
@type table: L{table.Table}
|
||||
"""
|
||||
logCategory = 'Image'
|
||||
|
||||
def __init__(self, path):
|
||||
"""
|
||||
@type path: unicode
|
||||
@param path: .cue path
|
||||
"""
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
|
||||
self._path = path
|
||||
self.cue = cue.CueFile(path)
|
||||
self.cue.parse()
|
||||
self._offsets = [] # 0 .. trackCount - 1
|
||||
self._lengths = [] # 0 .. trackCount - 1
|
||||
|
||||
self.table = None
|
||||
|
||||
def getRealPath(self, path):
|
||||
"""
|
||||
Translate the .cue's FILE to an existing path.
|
||||
|
||||
@param path: .cue path
|
||||
"""
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
|
||||
return self.cue.getRealPath(path)
|
||||
|
||||
def setup(self, runner):
|
||||
"""
|
||||
Do initial setup, like figuring out track lengths, and
|
||||
constructing the Table of Contents.
|
||||
"""
|
||||
logger.debug('setup image start')
|
||||
verify = ImageVerifyTask(self)
|
||||
logger.debug('verifying image')
|
||||
runner.run(verify)
|
||||
logger.debug('verified image')
|
||||
|
||||
# calculate offset and length for each track
|
||||
|
||||
# CD's have a standard lead-in time of 2 seconds;
|
||||
# checksums that use it should add it there
|
||||
if verify.lengths.has_key(0):
|
||||
offset = verify.lengths[0]
|
||||
else:
|
||||
offset = self.cue.table.tracks[0].getIndex(1).relative
|
||||
|
||||
tracks = []
|
||||
|
||||
for i in range(len(self.cue.table.tracks)):
|
||||
length = self.cue.getTrackLength(self.cue.table.tracks[i])
|
||||
if length == -1:
|
||||
length = verify.lengths[i + 1]
|
||||
t = table.Track(i + 1, audio=True)
|
||||
tracks.append(t)
|
||||
# FIXME: this probably only works for non-compliant .CUE files
|
||||
# where pregap is put at end of previous file
|
||||
t.index(1, absolute=offset,
|
||||
path=self.cue.table.tracks[i].getIndex(1).path,
|
||||
relative=0)
|
||||
|
||||
offset += length
|
||||
|
||||
self.table = table.Table(tracks)
|
||||
self.table.leadout = offset
|
||||
logger.debug('setup image done')
|
||||
|
||||
|
||||
class AccurateRipChecksumTask(task.MultiSeparateTask):
|
||||
"""
|
||||
I calculate the AccurateRip checksums of all tracks.
|
||||
"""
|
||||
|
||||
description = "Checksumming tracks"
|
||||
|
||||
# TODO MW: Update this further for V2 code
|
||||
def __init__(self, image):
|
||||
task.MultiSeparateTask.__init__(self)
|
||||
|
||||
self._image = image
|
||||
cue = image.cue
|
||||
self.checksums = []
|
||||
|
||||
logger.debug('Checksumming %d tracks' % len(cue.table.tracks))
|
||||
for trackIndex, track in enumerate(cue.table.tracks):
|
||||
index = track.indexes[1]
|
||||
length = cue.getTrackLength(track)
|
||||
if length < 0:
|
||||
logger.debug('track %d has unknown length' % (trackIndex + 1, ))
|
||||
else:
|
||||
logger.debug('track %d is %d samples long' % (
|
||||
trackIndex + 1, length))
|
||||
|
||||
path = image.getRealPath(index.path)
|
||||
|
||||
|
||||
checksumTask = checksum.FastAccurateRipChecksumTask(path,
|
||||
trackNumber=trackIndex + 1, trackCount=len(cue.table.tracks),
|
||||
wave=True, v2=False)
|
||||
|
||||
self.addTask(checksumTask)
|
||||
|
||||
def stop(self):
|
||||
self.checksums = [t.checksum for t in self.tasks]
|
||||
task.MultiSeparateTask.stop(self)
|
||||
|
||||
|
||||
class ImageVerifyTask(task.MultiSeparateTask):
|
||||
"""
|
||||
I verify a disk image and get the necessary track lengths.
|
||||
"""
|
||||
|
||||
logCategory = 'ImageVerifyTask'
|
||||
|
||||
description = "Checking tracks"
|
||||
lengths = None
|
||||
|
||||
def __init__(self, image):
|
||||
task.MultiSeparateTask.__init__(self)
|
||||
|
||||
self._image = image
|
||||
cue = image.cue
|
||||
self._tasks = []
|
||||
self.lengths = {}
|
||||
|
||||
try:
|
||||
htoa = cue.table.tracks[0].indexes[0]
|
||||
track = cue.table.tracks[0]
|
||||
path = image.getRealPath(htoa.path)
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
logger.debug('schedule scan of audio length of %r', path)
|
||||
taskk = AudioLengthTask(path)
|
||||
self.addTask(taskk)
|
||||
self._tasks.append((0, track, taskk))
|
||||
except (KeyError, IndexError):
|
||||
logger.debug('no htoa track')
|
||||
|
||||
for trackIndex, track in enumerate(cue.table.tracks):
|
||||
logger.debug('verifying track %d', trackIndex + 1)
|
||||
index = track.indexes[1]
|
||||
length = cue.getTrackLength(track)
|
||||
|
||||
if length == -1:
|
||||
path = image.getRealPath(index.path)
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
logger.debug('schedule scan of audio length of %r', path)
|
||||
taskk = AudioLengthTask(path)
|
||||
self.addTask(taskk)
|
||||
self._tasks.append((trackIndex + 1, track, taskk))
|
||||
else:
|
||||
logger.debug('track %d has length %d', trackIndex + 1, length)
|
||||
|
||||
def stop(self):
|
||||
for trackIndex, track, taskk in self._tasks:
|
||||
if taskk.exception:
|
||||
logger.debug('subtask %r had exception %r, shutting down' % (
|
||||
taskk, taskk.exception))
|
||||
self.setException(taskk.exception)
|
||||
break
|
||||
|
||||
if taskk.length is None:
|
||||
raise ValueError("Track length was not found; look for "
|
||||
"earlier errors in debug log (set RIP_DEBUG=4)")
|
||||
# print '%d has length %d' % (trackIndex, taskk.length)
|
||||
index = track.indexes[1]
|
||||
assert taskk.length % common.SAMPLES_PER_FRAME == 0
|
||||
end = taskk.length / common.SAMPLES_PER_FRAME
|
||||
self.lengths[trackIndex] = end - index.relative
|
||||
|
||||
task.MultiSeparateTask.stop(self)
|
||||
|
||||
|
||||
class ImageEncodeTask(task.MultiSeparateTask):
|
||||
"""
|
||||
I encode a disk image to a different format.
|
||||
"""
|
||||
|
||||
description = "Encoding tracks"
|
||||
|
||||
def __init__(self, image, outdir):
|
||||
task.MultiSeparateTask.__init__(self)
|
||||
|
||||
self._image = image
|
||||
cue = image.cue
|
||||
self._tasks = []
|
||||
self.lengths = {}
|
||||
|
||||
def add(index):
|
||||
|
||||
path = image.getRealPath(index.path)
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
logger.debug('schedule encode of %r', path)
|
||||
root, ext = os.path.splitext(os.path.basename(path))
|
||||
outpath = os.path.join(outdir, root + '.' + 'flac')
|
||||
logger.debug('schedule encode to %r', outpath)
|
||||
taskk = encode.FlacEncodeTask(path, os.path.join(outdir,
|
||||
root + '.' + 'flac'))
|
||||
self.addTask(taskk)
|
||||
|
||||
try:
|
||||
htoa = cue.table.tracks[0].indexes[0]
|
||||
logger.debug('encoding htoa track')
|
||||
add(htoa)
|
||||
except (KeyError, IndexError):
|
||||
logger.debug('no htoa track')
|
||||
pass
|
||||
|
||||
for trackIndex, track in enumerate(cue.table.tracks):
|
||||
logger.debug('encoding track %d', trackIndex + 1)
|
||||
index = track.indexes[1]
|
||||
add(index)
|
||||
871
whipper/image/table.py
Normal file
871
whipper/image/table.py
Normal file
@@ -0,0 +1,871 @@
|
||||
# -*- Mode: Python; test-case-name: whipper.test.test_image_table -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
# Morituri - for those about to RIP
|
||||
|
||||
# Copyright (C) 2009 Thomas Vander Stichele
|
||||
|
||||
# This file is part of whipper.
|
||||
#
|
||||
# whipper is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# whipper is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with whipper. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
Wrap Table of Contents.
|
||||
"""
|
||||
|
||||
import copy
|
||||
import urllib
|
||||
import urlparse
|
||||
|
||||
import whipper
|
||||
|
||||
from whipper.common import common
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# FIXME: taken from libcdio, but no reference found for these
|
||||
|
||||
CDTEXT_FIELDS = [
|
||||
'ARRANGER',
|
||||
'COMPOSER',
|
||||
'DISCID',
|
||||
'GENRE',
|
||||
'MESSAGE',
|
||||
'ISRC',
|
||||
'PERFORMER',
|
||||
'SIZE_INFO',
|
||||
'SONGWRITER',
|
||||
'TITLE',
|
||||
'TOC_INFO',
|
||||
'TOC_INFO2',
|
||||
'UPC_EAN',
|
||||
]
|
||||
|
||||
|
||||
class Track:
|
||||
"""
|
||||
I represent a track entry in an Table.
|
||||
|
||||
@ivar number: track number (1-based)
|
||||
@type number: int
|
||||
@ivar audio: whether the track is audio
|
||||
@type audio: bool
|
||||
@type indexes: dict of number -> L{Index}
|
||||
@ivar isrc: ISRC code (12 alphanumeric characters)
|
||||
@type isrc: str
|
||||
@ivar cdtext: dictionary of CD Text information; see L{CDTEXT_KEYS}.
|
||||
@type cdtext: str -> unicode
|
||||
@ivar pre_emphasis: whether track is pre-emphasised
|
||||
@type pre_emphasis: bool
|
||||
"""
|
||||
|
||||
number = None
|
||||
audio = None
|
||||
indexes = None
|
||||
isrc = None
|
||||
cdtext = None
|
||||
session = None
|
||||
pre_emphasis = None
|
||||
|
||||
def __repr__(self):
|
||||
return '<Track %02d>' % self.number
|
||||
|
||||
def __init__(self, number, audio=True, session=None):
|
||||
self.number = number
|
||||
self.audio = audio
|
||||
self.indexes = {}
|
||||
self.cdtext = {}
|
||||
|
||||
def index(self, number, absolute=None, path=None, relative=None,
|
||||
counter=None):
|
||||
"""
|
||||
@type path: unicode or None
|
||||
"""
|
||||
if path is not None:
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
|
||||
i = Index(number, absolute, path, relative, counter)
|
||||
self.indexes[number] = i
|
||||
|
||||
def getIndex(self, number):
|
||||
return self.indexes[number]
|
||||
|
||||
def getFirstIndex(self):
|
||||
"""
|
||||
Get the first chronological index for this track.
|
||||
|
||||
Typically this is INDEX 01; but it could be INDEX 00 if there's
|
||||
a pre-gap.
|
||||
"""
|
||||
indexes = self.indexes.keys()
|
||||
indexes.sort()
|
||||
return self.indexes[indexes[0]]
|
||||
|
||||
def getLastIndex(self):
|
||||
indexes = self.indexes.keys()
|
||||
indexes.sort()
|
||||
return self.indexes[indexes[-1]]
|
||||
|
||||
def getPregap(self):
|
||||
"""
|
||||
Returns the length of the pregap for this track.
|
||||
|
||||
The pregap is 0 if there is no index 0, and the difference between
|
||||
index 1 and index 0 if there is.
|
||||
"""
|
||||
if 0 not in self.indexes:
|
||||
return 0
|
||||
|
||||
return self.indexes[1].absolute - self.indexes[0].absolute
|
||||
|
||||
|
||||
class Index:
|
||||
"""
|
||||
@ivar counter: counter for the index source; distinguishes between
|
||||
the matching FILE lines in .cue files for example
|
||||
@type path: unicode or None
|
||||
"""
|
||||
number = None
|
||||
absolute = None
|
||||
path = None
|
||||
relative = None
|
||||
counter = None
|
||||
|
||||
def __init__(self, number, absolute=None, path=None, relative=None,
|
||||
counter=None):
|
||||
|
||||
if path is not None:
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
|
||||
self.number = number
|
||||
self.absolute = absolute
|
||||
self.path = path
|
||||
self.relative = relative
|
||||
self.counter = counter
|
||||
|
||||
def __repr__(self):
|
||||
return '<Index %02d absolute %r path %r relative %r counter %r>' % (
|
||||
self.number, self.absolute, self.path, self.relative, self.counter)
|
||||
|
||||
|
||||
class Table(object):
|
||||
"""
|
||||
I represent a table of indexes on a CD.
|
||||
|
||||
@ivar tracks: tracks on this CD
|
||||
@type tracks: list of L{Track}
|
||||
@ivar catalog: catalog number
|
||||
@type catalog: str
|
||||
@type cdtext: dict of str -> str
|
||||
"""
|
||||
|
||||
tracks = None # list of Track
|
||||
leadout = None # offset where the leadout starts
|
||||
catalog = None # catalog number; FIXME: is this UPC ?
|
||||
cdtext = None
|
||||
mbdiscid = None
|
||||
|
||||
classVersion = 4
|
||||
|
||||
def __init__(self, tracks=None):
|
||||
if not tracks:
|
||||
tracks = []
|
||||
|
||||
self.tracks = tracks
|
||||
self.cdtext = {}
|
||||
# done this way because just having a class-defined instance var
|
||||
# gets overridden when unpickling
|
||||
self.instanceVersion = self.classVersion
|
||||
self.unpickled()
|
||||
|
||||
def unpickled(self):
|
||||
self.logName = "Table 0x%08x v%d" % (id(self), self.instanceVersion)
|
||||
logger.debug('set logName')
|
||||
|
||||
def getTrackStart(self, number):
|
||||
"""
|
||||
@param number: the track number, 1-based
|
||||
@type number: int
|
||||
|
||||
@returns: the start of the given track number's index 1, in CD frames
|
||||
@rtype: int
|
||||
"""
|
||||
track = self.tracks[number - 1]
|
||||
return track.getIndex(1).absolute
|
||||
|
||||
def getTrackEnd(self, number):
|
||||
"""
|
||||
@param number: the track number, 1-based
|
||||
@type number: int
|
||||
|
||||
@returns: the end of the given track number (ie index 1 of next track)
|
||||
@rtype: int
|
||||
"""
|
||||
# default to end of disc
|
||||
end = self.leadout - 1
|
||||
|
||||
# if not last track, calculate it from the next track
|
||||
if number < len(self.tracks):
|
||||
end = self.tracks[number].getIndex(1).absolute - 1
|
||||
|
||||
# if on a session border, subtract the session leadin
|
||||
thisTrack = self.tracks[number - 1]
|
||||
nextTrack = self.tracks[number]
|
||||
if nextTrack.session > thisTrack.session:
|
||||
gap = self._getSessionGap(nextTrack.session)
|
||||
end -= gap
|
||||
|
||||
return end
|
||||
|
||||
def getTrackLength(self, number):
|
||||
"""
|
||||
@param number: the track number, 1-based
|
||||
@type number: int
|
||||
|
||||
@returns: the length of the given track number, in CD frames
|
||||
@rtype: int
|
||||
"""
|
||||
return self.getTrackEnd(number) - self.getTrackStart(number) + 1
|
||||
|
||||
def getAudioTracks(self):
|
||||
"""
|
||||
@returns: the number of audio tracks on the CD
|
||||
@rtype: int
|
||||
"""
|
||||
return len([t for t in self.tracks if t.audio])
|
||||
|
||||
def hasDataTracks(self):
|
||||
"""
|
||||
@returns: whether this disc contains data tracks
|
||||
"""
|
||||
return len([t for t in self.tracks if not t.audio]) > 0
|
||||
|
||||
def _cddbSum(self, i):
|
||||
ret = 0
|
||||
while i > 0:
|
||||
ret += (i % 10)
|
||||
i /= 10
|
||||
|
||||
return ret
|
||||
|
||||
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 (including data track)
|
||||
|
||||
@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
|
||||
n = 0
|
||||
|
||||
# CD's have a standard lead-in time of 2 seconds
|
||||
# which gets added for CDDB disc id's
|
||||
delta = 2 * common.FRAMES_PER_SECOND
|
||||
#if self.getTrackStart(1) > 0:
|
||||
# delta = 0
|
||||
|
||||
debug = [str(len(self.tracks))]
|
||||
for track in self.tracks:
|
||||
offset = self.getTrackStart(track.number) + delta
|
||||
result.append(offset)
|
||||
debug.append(str(offset))
|
||||
seconds = offset / common.FRAMES_PER_SECOND
|
||||
n += self._cddbSum(seconds)
|
||||
|
||||
# the 'real' leadout, not offset by 150 frames
|
||||
# print 'THOMAS: disc leadout', self.leadout
|
||||
last = self.tracks[-1]
|
||||
leadout = self.getTrackEnd(last.number) + 1
|
||||
logger.debug('leadout LBA: %d', leadout)
|
||||
|
||||
# FIXME: we can't replace these calculations with the getFrameLength
|
||||
# call because the start and leadout in the algorithm get rounded
|
||||
# before making the difference
|
||||
startSeconds = self.getTrackStart(1) / common.FRAMES_PER_SECOND
|
||||
leadoutSeconds = leadout / common.FRAMES_PER_SECOND
|
||||
t = leadoutSeconds - startSeconds
|
||||
# durationFrames = self.getFrameLength(data=True)
|
||||
# duration = durationFrames / common.FRAMES_PER_SECOND
|
||||
# assert t == duration, "%r != %r" % (t, duration)
|
||||
|
||||
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
|
||||
logger.debug('cddb values: %r', result)
|
||||
|
||||
logger.debug('cddb disc id debug: %s',
|
||||
" ".join(["%08x" % value, ] + debug))
|
||||
|
||||
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):
|
||||
"""
|
||||
Calculate the MusicBrainz disc ID.
|
||||
|
||||
@rtype: str
|
||||
@returns: the 28-character base64-encoded disc ID
|
||||
"""
|
||||
if self.mbdiscid:
|
||||
logger.debug('getMusicBrainzDiscId: returning cached %r'
|
||||
% self.mbdiscid)
|
||||
return self.mbdiscid
|
||||
values = self._getMusicBrainzValues()
|
||||
|
||||
# MusicBrainz disc id does not take into account data tracks
|
||||
# P2.3
|
||||
try:
|
||||
import hashlib
|
||||
sha1 = hashlib.sha1
|
||||
except ImportError:
|
||||
from sha import sha as sha1
|
||||
import base64
|
||||
|
||||
sha = sha1()
|
||||
|
||||
# number of first track
|
||||
sha.update("%02X" % values[0])
|
||||
|
||||
# number of last track
|
||||
sha.update("%02X" % values[1])
|
||||
|
||||
sha.update("%08X" % values[2])
|
||||
|
||||
# offsets of tracks
|
||||
for i in range(1, 100):
|
||||
try:
|
||||
offset = values[2 + i]
|
||||
except IndexError:
|
||||
#print 'track', i - 1, '0 offset'
|
||||
offset = 0
|
||||
sha.update("%08X" % offset)
|
||||
|
||||
digest = sha.digest()
|
||||
assert len(digest) == 20, \
|
||||
"digest should be 20 chars, not %d" % len(digest)
|
||||
|
||||
# The RFC822 spec uses +, /, and = characters, all of which are special
|
||||
# HTTP/URL characters. To avoid the problems with dealing with that, I
|
||||
# (Rob) used ., _, and -
|
||||
|
||||
# base64 altchars specify replacements for + and /
|
||||
result = base64.b64encode(digest, '._')
|
||||
|
||||
# now replace =
|
||||
result = "-".join(result.split("="))
|
||||
assert len(result) == 28, \
|
||||
"Result should be 28 characters, not %d" % len(result)
|
||||
|
||||
logger.debug('getMusicBrainzDiscId: returning %r' % result)
|
||||
self.mbdiscid = result
|
||||
return result
|
||||
|
||||
def getMusicBrainzSubmitURL(self):
|
||||
host = 'musicbrainz.org'
|
||||
|
||||
discid = self.getMusicBrainzDiscId()
|
||||
values = self._getMusicBrainzValues()
|
||||
|
||||
query = urllib.urlencode({
|
||||
'id': discid,
|
||||
'toc': ' '.join([str(v) for v in values]),
|
||||
'tracks': self.getAudioTracks(),
|
||||
})
|
||||
|
||||
return urlparse.urlunparse((
|
||||
'https', host, '/cdtoc/attach', '', query, ''))
|
||||
|
||||
def getFrameLength(self, data=False):
|
||||
"""
|
||||
Get the length in frames (excluding HTOA)
|
||||
|
||||
@param data: whether to include the data tracks in the length
|
||||
"""
|
||||
# the 'real' leadout, not offset by 150 frames
|
||||
if data:
|
||||
last = self.tracks[-1]
|
||||
else:
|
||||
last = self.tracks[self.getAudioTracks() - 1]
|
||||
|
||||
leadout = self.getTrackEnd(last.number) + 1
|
||||
logger.debug('leadout LBA: %d', leadout)
|
||||
durationFrames = leadout - self.getTrackStart(1)
|
||||
|
||||
return durationFrames
|
||||
|
||||
def duration(self):
|
||||
"""
|
||||
Get the duration in ms for all audio tracks (excluding HTOA).
|
||||
"""
|
||||
return int(self.getFrameLength() * 1000.0 / common.FRAMES_PER_SECOND)
|
||||
|
||||
def _getMusicBrainzValues(self):
|
||||
"""
|
||||
Get all MusicBrainz values needed to calculate disc id and submit URL.
|
||||
|
||||
This includes:
|
||||
- track number of first track
|
||||
- number of audio tracks
|
||||
- leadout of disc
|
||||
- offset of index 1 of each track
|
||||
|
||||
@rtype: list of int
|
||||
"""
|
||||
# MusicBrainz disc id does not take into account data tracks
|
||||
|
||||
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
|
||||
|
||||
|
||||
logger.debug('Musicbrainz values: %r', result)
|
||||
return result
|
||||
|
||||
def getAccurateRipIds(self):
|
||||
"""
|
||||
Calculate the two AccurateRip ID's.
|
||||
|
||||
@returns: the two 8-character hexadecimal disc ID's
|
||||
@rtype: tuple of (str, str)
|
||||
"""
|
||||
# AccurateRip does not take into account data tracks,
|
||||
# but does count the data track to determine the leadout offset
|
||||
discId1 = 0
|
||||
discId2 = 0
|
||||
|
||||
for track in self.tracks:
|
||||
if not track.audio:
|
||||
continue
|
||||
offset = self.getTrackStart(track.number)
|
||||
discId1 += offset
|
||||
discId2 += (offset or 1) * track.number
|
||||
|
||||
# also add end values, where leadout offset is one past the end
|
||||
# of the last track
|
||||
last = self.tracks[-1]
|
||||
offset = self.getTrackEnd(last.number) + 1
|
||||
discId1 += offset
|
||||
discId2 += offset * (self.getAudioTracks() + 1)
|
||||
|
||||
discId1 &= 0xffffffff
|
||||
discId2 &= 0xffffffff
|
||||
|
||||
return ("%08x" % discId1, "%08x" % discId2)
|
||||
|
||||
def getAccurateRipURL(self):
|
||||
"""
|
||||
Return the full AccurateRip URL.
|
||||
|
||||
@returns: the AccurateRip URL
|
||||
@rtype: str
|
||||
"""
|
||||
discId1, discId2 = self.getAccurateRipIds()
|
||||
|
||||
return "http://www.accuraterip.com/accuraterip/" \
|
||||
"%s/%s/%s/dBAR-%.3d-%s-%s-%s.bin" % (
|
||||
discId1[-1], discId1[-2], discId1[-3],
|
||||
self.getAudioTracks(), discId1, discId2, self.getCDDBDiscId())
|
||||
|
||||
def cue(self, cuePath='', program='whipper'):
|
||||
"""
|
||||
@param cuePath: path to the cue file to be written. If empty,
|
||||
will treat paths as if in current directory.
|
||||
|
||||
|
||||
Dump our internal representation to a .cue file content.
|
||||
|
||||
@rtype: C{unicode}
|
||||
"""
|
||||
logger.debug('generating .cue for cuePath %r', cuePath)
|
||||
|
||||
lines = []
|
||||
|
||||
def writeFile(path):
|
||||
targetPath = common.getRelativePath(path, cuePath)
|
||||
line = 'FILE "%s" WAVE' % targetPath
|
||||
lines.append(line)
|
||||
logger.debug('writeFile: %r' % line)
|
||||
|
||||
# header
|
||||
main = ['PERFORMER', 'TITLE']
|
||||
|
||||
for key in CDTEXT_FIELDS:
|
||||
if key not in main and key in self.cdtext:
|
||||
lines.append(" %s %s" % (key, self.cdtext[key]))
|
||||
|
||||
assert self.hasTOC(), "Table does not represent a full CD TOC"
|
||||
lines.append('REM DISCID %s' % self.getCDDBDiscId().upper())
|
||||
lines.append('REM COMMENT "%s %s"' % (program, whipper.__version__))
|
||||
|
||||
if self.catalog:
|
||||
lines.append("CATALOG %s" % self.catalog)
|
||||
|
||||
for key in main:
|
||||
if key in self.cdtext:
|
||||
lines.append('%s "%s"' % (key, self.cdtext[key]))
|
||||
|
||||
# FIXME:
|
||||
# - the first FILE statement goes before the first TRACK, even if
|
||||
# there is a non-file-using PREGAP
|
||||
# - the following FILE statements come after the last INDEX that
|
||||
# use that FILE; so before a next TRACK, PREGAP silence, ...
|
||||
|
||||
# add the first FILE line; EAC always puts the first FILE
|
||||
# statement before TRACK 01 and any possible PRE-GAP
|
||||
firstTrack = self.tracks[0]
|
||||
index = firstTrack.getFirstIndex()
|
||||
indexOne = firstTrack.getIndex(1)
|
||||
counter = index.counter
|
||||
track = firstTrack
|
||||
|
||||
while not index.path:
|
||||
t, i = self.getNextTrackIndex(track.number, index.number)
|
||||
track = self.tracks[t - 1]
|
||||
index = track.getIndex(i)
|
||||
counter = index.counter
|
||||
|
||||
if index.path:
|
||||
logger.debug('counter %d, writeFile' % counter)
|
||||
writeFile(index.path)
|
||||
|
||||
for i, track in enumerate(self.tracks):
|
||||
logger.debug('track i %r, track %r' % (i, track))
|
||||
# FIXME: skip data tracks for now
|
||||
if not track.audio:
|
||||
continue
|
||||
|
||||
indexes = track.indexes.keys()
|
||||
indexes.sort()
|
||||
|
||||
wroteTrack = False
|
||||
|
||||
for number in indexes:
|
||||
index = track.indexes[number]
|
||||
logger.debug('index %r, %r' % (number, index))
|
||||
|
||||
# any time the source counter changes to a higher value,
|
||||
# write a FILE statement
|
||||
# it has to be higher, because we can run into the HTOA
|
||||
# at counter 0 here
|
||||
if index.counter > counter:
|
||||
if index.path:
|
||||
logger.debug('counter %d, writeFile' % counter)
|
||||
writeFile(index.path)
|
||||
logger.debug('setting counter to index.counter %r' %
|
||||
index.counter)
|
||||
counter = index.counter
|
||||
|
||||
# any time we hit the first index, write a TRACK statement
|
||||
if not wroteTrack:
|
||||
wroteTrack = True
|
||||
line = " TRACK %02d %s" % (i + 1, 'AUDIO')
|
||||
lines.append(line)
|
||||
logger.debug('%r' % line)
|
||||
|
||||
for key in CDTEXT_FIELDS:
|
||||
if key in track.cdtext:
|
||||
lines.append(' %s "%s"' % (
|
||||
key, track.cdtext[key]))
|
||||
|
||||
if track.isrc is not None:
|
||||
lines.append(" ISRC %s" % track.isrc)
|
||||
|
||||
if track.pre_emphasis is not None:
|
||||
lines.append(" FLAGS PRE")
|
||||
|
||||
# handle TRACK 01 INDEX 00 specially
|
||||
if 0 in indexes:
|
||||
index00 = track.indexes[0]
|
||||
if i == 0:
|
||||
# if we have a silent pre-gap, output it
|
||||
if not index00.path:
|
||||
length = indexOne.absolute - index00.absolute
|
||||
lines.append(" PREGAP %s" %
|
||||
common.framesToMSF(length))
|
||||
continue
|
||||
|
||||
# handle any other INDEX 00 after its TRACK
|
||||
lines.append(" INDEX %02d %s" % (0,
|
||||
common.framesToMSF(index00.relative)))
|
||||
|
||||
if number > 0:
|
||||
# index 00 is output after TRACK up above
|
||||
lines.append(" INDEX %02d %s" % (number,
|
||||
common.framesToMSF(index.relative)))
|
||||
|
||||
lines.append("")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
### methods that modify the table
|
||||
|
||||
def clearFiles(self):
|
||||
"""
|
||||
Clear all file backings.
|
||||
Resets indexes paths and relative offsets.
|
||||
"""
|
||||
# FIXME: do a loop over track indexes better, with a pythonic
|
||||
# construct that allows you to do for t, i in ...
|
||||
t = self.tracks[0].number
|
||||
index = self.tracks[0].getFirstIndex()
|
||||
i = index.number
|
||||
|
||||
logger.debug('clearing path')
|
||||
while True:
|
||||
track = self.tracks[t - 1]
|
||||
index = track.getIndex(i)
|
||||
logger.debug('Clearing path on track %d, index %d', t, i)
|
||||
index.path = None
|
||||
index.relative = None
|
||||
try:
|
||||
t, i = self.getNextTrackIndex(t, i)
|
||||
except IndexError:
|
||||
break
|
||||
|
||||
def setFile(self, track, index, path, length, counter=None):
|
||||
"""
|
||||
Sets the given file as the source from the given index on.
|
||||
Will loop over all indexes that fall within the given length,
|
||||
to adjust the path.
|
||||
|
||||
Assumes all indexes have an absolute offset and will raise if not.
|
||||
|
||||
@type track: C{int}
|
||||
@type index: C{int}
|
||||
"""
|
||||
logger.debug('setFile: track %d, index %d, path %r, '
|
||||
'length %r, counter %r', track, index, path, length, counter)
|
||||
|
||||
t = self.tracks[track - 1]
|
||||
i = t.indexes[index]
|
||||
start = i.absolute
|
||||
assert start is not None, "index %r is missing absolute offset" % i
|
||||
end = start + length - 1 # last sector that should come from this file
|
||||
|
||||
# FIXME: check border conditions here, esp. wrt. toc's off-by-one bug
|
||||
while i.absolute <= end:
|
||||
i.path = path
|
||||
i.relative = i.absolute - start
|
||||
i.counter = counter
|
||||
logger.debug('Setting path %r, relative %r on '
|
||||
'track %d, index %d, counter %r',
|
||||
path, i.relative, track, index, counter)
|
||||
try:
|
||||
track, index = self.getNextTrackIndex(track, index)
|
||||
t = self.tracks[track - 1]
|
||||
i = t.indexes[index]
|
||||
except IndexError:
|
||||
break
|
||||
|
||||
def absolutize(self):
|
||||
"""
|
||||
Calculate absolute offsets on indexes as much as possible.
|
||||
Only possible for as long as tracks draw from the same file.
|
||||
"""
|
||||
t = self.tracks[0].number
|
||||
index = self.tracks[0].getFirstIndex()
|
||||
i = index.number
|
||||
# the first cut is the deepest
|
||||
counter = index.counter
|
||||
|
||||
#for t in self.tracks: print t, t.indexes
|
||||
logger.debug('absolutizing')
|
||||
while True:
|
||||
track = self.tracks[t - 1]
|
||||
index = track.getIndex(i)
|
||||
assert track.number == t
|
||||
assert index.number == i
|
||||
if index.counter is None:
|
||||
logger.debug('Track %d, index %d has no counter', t, i)
|
||||
break
|
||||
if index.counter != counter:
|
||||
logger.debug('Track %d, index %d has a different counter', t, i)
|
||||
break
|
||||
logger.debug('Setting absolute offset %d on track %d, index %d',
|
||||
index.relative, t, i)
|
||||
if index.absolute is not None:
|
||||
if index.absolute != index.relative:
|
||||
msg = 'Track %d, index %d had absolute %d,' \
|
||||
' overriding with %d' % (
|
||||
t, i, index.absolute, index.relative)
|
||||
raise ValueError(msg)
|
||||
index.absolute = index.relative
|
||||
try:
|
||||
t, i = self.getNextTrackIndex(t, i)
|
||||
except IndexError:
|
||||
break
|
||||
|
||||
def merge(self, other, session=2):
|
||||
"""
|
||||
Merges the given table at the end.
|
||||
The other table is assumed to be from an additional session,
|
||||
|
||||
|
||||
@type other: L{Table}
|
||||
"""
|
||||
gap = self._getSessionGap(session)
|
||||
|
||||
trackCount = len(self.tracks)
|
||||
sourceCounter = self.tracks[-1].getLastIndex().counter
|
||||
|
||||
for track in other.tracks:
|
||||
t = copy.deepcopy(track)
|
||||
t.number = track.number + trackCount
|
||||
t.session = session
|
||||
for i in t.indexes.values():
|
||||
if i.absolute is not None:
|
||||
i.absolute += self.leadout + gap
|
||||
logger.debug('Fixing track %02d, index %02d, absolute %d' % (
|
||||
t.number, i.number, i.absolute))
|
||||
if i.counter is not None:
|
||||
i.counter += sourceCounter
|
||||
logger.debug('Fixing track %02d, index %02d, counter %d' % (
|
||||
t.number, i.number, i.counter))
|
||||
self.tracks.append(t)
|
||||
|
||||
self.leadout += other.leadout + gap # FIXME
|
||||
logger.debug('Fixing leadout, now %d', self.leadout)
|
||||
|
||||
def _getSessionGap(self, session):
|
||||
# From cdrecord multi-session info:
|
||||
# For the first additional session this is 11250 sectors
|
||||
# lead-out/lead-in overhead + 150 sectors for the pre-gap of the first
|
||||
# track after the lead-in = 11400 sectos.
|
||||
|
||||
# For all further session this is 6750 sectors lead-out/lead-in
|
||||
# overhead + 150 sectors for the pre-gap of the first track after the
|
||||
# lead-in = 6900 sectors.
|
||||
|
||||
gap = 11400
|
||||
if session > 2:
|
||||
gap = 6900
|
||||
return gap
|
||||
|
||||
### lookups
|
||||
|
||||
def getNextTrackIndex(self, track, index):
|
||||
"""
|
||||
Return the next track and index.
|
||||
|
||||
@param track: track number, 1-based
|
||||
|
||||
@raises IndexError: on last index
|
||||
|
||||
@rtype: tuple of (int, int)
|
||||
"""
|
||||
t = self.tracks[track - 1]
|
||||
indexes = t.indexes.keys()
|
||||
position = indexes.index(index)
|
||||
|
||||
if position + 1 < len(indexes):
|
||||
return track, indexes[position + 1]
|
||||
|
||||
track += 1
|
||||
if track > len(self.tracks):
|
||||
raise IndexError("No index beyond track %d, index %d" % (
|
||||
track - 1, index))
|
||||
|
||||
t = self.tracks[track - 1]
|
||||
indexes = t.indexes.keys()
|
||||
|
||||
return track, indexes[0]
|
||||
|
||||
# various tests for types of Table
|
||||
|
||||
def hasTOC(self):
|
||||
"""
|
||||
Check if the Table has a complete TOC.
|
||||
a TOC is a list of all tracks and their Index 01, with absolute
|
||||
offsets, as well as the leadout.
|
||||
"""
|
||||
if not self.leadout:
|
||||
logger.debug('no leadout, no TOC')
|
||||
return False
|
||||
|
||||
for t in self.tracks:
|
||||
if 1 not in t.indexes.keys():
|
||||
logger.debug('no index 1, no TOC')
|
||||
return False
|
||||
if t.indexes[1].absolute is None:
|
||||
logger.debug('no absolute index 1, no TOC')
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def canCue(self):
|
||||
"""
|
||||
Check if this table can be used to generate a .cue file
|
||||
"""
|
||||
if not self.hasTOC():
|
||||
logger.debug('No TOC, cannot cue')
|
||||
return False
|
||||
|
||||
for t in self.tracks:
|
||||
for i in t.indexes.values():
|
||||
if i.relative is None:
|
||||
logger.debug('Track %02d, Index %02d does not have relative',
|
||||
t.number, i.number)
|
||||
return False
|
||||
|
||||
return True
|
||||
445
whipper/image/toc.py
Normal file
445
whipper/image/toc.py
Normal file
@@ -0,0 +1,445 @@
|
||||
# -*- Mode: Python; test-case-name: whipper.test.test_image_toc -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
# Morituri - for those about to RIP
|
||||
|
||||
# Copyright (C) 2009 Thomas Vander Stichele
|
||||
|
||||
# This file is part of whipper.
|
||||
#
|
||||
# whipper is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# whipper is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with whipper. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
Reading .toc files
|
||||
|
||||
The .toc file format is described in the man page of cdrdao
|
||||
"""
|
||||
|
||||
import re
|
||||
import codecs
|
||||
|
||||
from whipper.common import common
|
||||
from whipper.image import table
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# shared
|
||||
_CDTEXT_CANDIDATE_RE = re.compile(r'(?P<key>\w+) "(?P<value>.+)"')
|
||||
|
||||
# header
|
||||
_CATALOG_RE = re.compile(r'^CATALOG "(?P<catalog>\d+)"$')
|
||||
|
||||
# pre emphasis
|
||||
_PRE_EMPHASIS_RE = re.compile(r'^PRE_EMPHASIS$')
|
||||
|
||||
# records
|
||||
_TRACK_RE = re.compile(r"""
|
||||
^TRACK # TRACK
|
||||
\s(?P<mode>.+)$ # mode (AUDIO, MODE2_FORM_MIX, MODEx/2xxx, ...)
|
||||
""", re.VERBOSE)
|
||||
|
||||
_ISRC_RE = re.compile(r'^ISRC "(?P<isrc>\w+)"$')
|
||||
|
||||
# a HTOA is marked in the cdrdao's TOC as SILENCE
|
||||
_SILENCE_RE = re.compile(r"""
|
||||
^SILENCE # SILENCE
|
||||
\s(?P<length>.*)$ # pre-gap length
|
||||
""", re.VERBOSE)
|
||||
|
||||
# ZERO is used as pre-gap source when switching mode
|
||||
_ZERO_RE = re.compile(r"""
|
||||
^ZERO # ZERO
|
||||
\s(?P<mode>.+) # mode (AUDIO, MODEx/2xxx, ...)
|
||||
\s(?P<length>.*)$ # zero length
|
||||
""", re.VERBOSE)
|
||||
|
||||
|
||||
_FILE_RE = re.compile(r"""
|
||||
^FILE # FILE
|
||||
\s+"(?P<name>.*)" # 'file name' in quotes
|
||||
\s+(?P<start>.+) # start offset
|
||||
\s(?P<length>.+)$ # length in frames of section
|
||||
""", re.VERBOSE)
|
||||
|
||||
_DATAFILE_RE = re.compile(r"""
|
||||
^DATAFILE # DATA FILE
|
||||
\s+"(?P<name>.*)" # 'file name' in quotes
|
||||
\s+(?P<length>\S+) # start offset
|
||||
\s*.* # possible // comment
|
||||
""", re.VERBOSE)
|
||||
|
||||
|
||||
# FIXME: start can be 0
|
||||
_START_RE = re.compile(r"""
|
||||
^START # START
|
||||
\s(?P<length>.*)$ # pre-gap length
|
||||
""", re.VERBOSE)
|
||||
|
||||
|
||||
_INDEX_RE = re.compile(r"""
|
||||
^INDEX # INDEX
|
||||
\s(?P<offset>.+)$ # start offset
|
||||
""", re.VERBOSE)
|
||||
|
||||
|
||||
class Sources:
|
||||
"""
|
||||
I represent the list of sources used in the .toc file.
|
||||
Each SILENCE and each FILE is a source.
|
||||
If the filename for FILE doesn't change, the counter is not increased.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._sources = []
|
||||
|
||||
def append(self, counter, offset, source):
|
||||
"""
|
||||
@param counter: the source counter; updates for each different
|
||||
data source (silence or different file path)
|
||||
@type counter: int
|
||||
@param offset: the absolute disc offset where this source starts
|
||||
"""
|
||||
logger.debug('Appending source, counter %d, abs offset %d, source %r' % (
|
||||
counter, offset, source))
|
||||
self._sources.append((counter, offset, source))
|
||||
|
||||
def get(self, offset):
|
||||
"""
|
||||
Retrieve the source used at the given offset.
|
||||
"""
|
||||
for i, (c, o, s) in enumerate(self._sources):
|
||||
if offset < o:
|
||||
return self._sources[i - 1]
|
||||
|
||||
return self._sources[-1]
|
||||
|
||||
def getCounterStart(self, counter):
|
||||
"""
|
||||
Retrieve the absolute offset of the first source for this counter
|
||||
"""
|
||||
for i, (c, o, s) in enumerate(self._sources):
|
||||
if c == counter:
|
||||
return self._sources[i][1]
|
||||
|
||||
return self._sources[-1][1]
|
||||
|
||||
|
||||
class TocFile(object):
|
||||
|
||||
def __init__(self, path):
|
||||
"""
|
||||
@type path: unicode
|
||||
"""
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
self._path = path
|
||||
self._messages = []
|
||||
self.table = table.Table()
|
||||
self.logName = '<TocFile %08x>' % id(self)
|
||||
|
||||
self._sources = Sources()
|
||||
|
||||
def _index(self, currentTrack, i, absoluteOffset, trackOffset):
|
||||
absolute = absoluteOffset + trackOffset
|
||||
# this may be in a new source, so calculate relative
|
||||
c, o, s = self._sources.get(absolute)
|
||||
logger.debug('at abs offset %d, we are in source %r' % (
|
||||
absolute, s))
|
||||
counterStart = self._sources.getCounterStart(c)
|
||||
relative = absolute - counterStart
|
||||
|
||||
currentTrack.index(i, path=s.path,
|
||||
absolute=absolute,
|
||||
relative=relative,
|
||||
counter=c)
|
||||
logger.debug(
|
||||
'[track %02d index %02d] trackOffset %r, added %r',
|
||||
currentTrack.number, i, trackOffset,
|
||||
currentTrack.getIndex(i))
|
||||
|
||||
|
||||
def parse(self):
|
||||
# these two objects start as None then get set as real objects,
|
||||
# so no need to complain about them here
|
||||
__pychecker__ = 'no-objattrs'
|
||||
currentFile = None
|
||||
currentTrack = None
|
||||
|
||||
state = 'HEADER'
|
||||
counter = 0 # counts sources for audio data; SILENCE/ZERO/FILE
|
||||
trackNumber = 0
|
||||
indexNumber = 0
|
||||
absoluteOffset = 0 # running absolute offset of where each track starts
|
||||
relativeOffset = 0 # running relative offset, relative to counter src
|
||||
currentLength = 0 # accrued during TRACK record parsing;
|
||||
# length of current track as parsed so far;
|
||||
# reset on each TRACK statement
|
||||
totalLength = 0 # accrued during TRACK record parsing, total disc
|
||||
pregapLength = 0 # length of the pre-gap, current track in for loop
|
||||
|
||||
# the first track's INDEX 1 can only be gotten from the .toc
|
||||
# file once the first pregap is calculated; so we add INDEX 1
|
||||
# at the end of each parsed TRACK record
|
||||
handle = codecs.open(self._path, "r", "utf-8")
|
||||
|
||||
for number, line in enumerate(handle.readlines()):
|
||||
line = line.rstrip()
|
||||
|
||||
# look for CDTEXT stuff in either header or tracks
|
||||
m = _CDTEXT_CANDIDATE_RE.search(line)
|
||||
if m:
|
||||
key = m.group('key')
|
||||
value = m.group('value')
|
||||
# usually, value is encoded with octal escapes and in latin-1
|
||||
# FIXME: other encodings are possible, does cdrdao handle
|
||||
# them ?
|
||||
value = value.decode('string-escape').decode('latin-1')
|
||||
if key in table.CDTEXT_FIELDS:
|
||||
# FIXME: consider ISRC separate for now, but this
|
||||
# is a limitation of our parser approach
|
||||
if state == 'HEADER':
|
||||
self.table.cdtext[key] = value
|
||||
logger.debug('Found disc CD-Text %s: %r', key, value)
|
||||
elif state == 'TRACK':
|
||||
if key != 'ISRC' or not currentTrack \
|
||||
or currentTrack.isrc is not None:
|
||||
logger.debug('Found track CD-Text %s: %r',
|
||||
key, value)
|
||||
currentTrack.cdtext[key] = value
|
||||
|
||||
# look for header elements
|
||||
m = _CATALOG_RE.search(line)
|
||||
if m:
|
||||
self.table.catalog = m.group('catalog')
|
||||
logger.debug("Found catalog number %s", self.table.catalog)
|
||||
|
||||
# look for TRACK lines
|
||||
m = _TRACK_RE.search(line)
|
||||
if m:
|
||||
state = 'TRACK'
|
||||
|
||||
# set index 1 of previous track if there was one, using
|
||||
# pregapLength if applicable
|
||||
if currentTrack:
|
||||
self._index(currentTrack, 1, absoluteOffset, pregapLength)
|
||||
|
||||
# create a new track to be filled by later lines
|
||||
trackNumber += 1
|
||||
trackMode = m.group('mode')
|
||||
audio = trackMode == 'AUDIO'
|
||||
currentTrack = table.Track(trackNumber, audio=audio)
|
||||
self.table.tracks.append(currentTrack)
|
||||
|
||||
# update running totals
|
||||
absoluteOffset += currentLength
|
||||
relativeOffset += currentLength
|
||||
totalLength += currentLength
|
||||
|
||||
# FIXME: track mode
|
||||
logger.debug('found track %d, mode %s, at absoluteOffset %d',
|
||||
trackNumber, trackMode, absoluteOffset)
|
||||
|
||||
# reset counters relative to a track
|
||||
currentLength = 0
|
||||
indexNumber = 1
|
||||
pregapLength = 0
|
||||
|
||||
continue
|
||||
|
||||
# look for PRE_EMPHASIS lines
|
||||
m = _PRE_EMPHASIS_RE.search(line)
|
||||
if m:
|
||||
currentTrack.pre_emphasis = True
|
||||
logger.debug('Track has PRE_EMPHASIS')
|
||||
|
||||
# look for ISRC lines
|
||||
m = _ISRC_RE.search(line)
|
||||
if m:
|
||||
isrc = m.group('isrc')
|
||||
currentTrack.isrc = isrc
|
||||
logger.debug('Found ISRC code %s', isrc)
|
||||
|
||||
# look for SILENCE lines
|
||||
m = _SILENCE_RE.search(line)
|
||||
if m:
|
||||
length = m.group('length')
|
||||
logger.debug('SILENCE of %r', length)
|
||||
self._sources.append(counter, absoluteOffset, None)
|
||||
if currentFile is not None:
|
||||
logger.debug('SILENCE after FILE, increasing counter')
|
||||
counter += 1
|
||||
relativeOffset = 0
|
||||
currentFile = None
|
||||
currentLength += common.msfToFrames(length)
|
||||
|
||||
# look for ZERO lines
|
||||
m = _ZERO_RE.search(line)
|
||||
if m:
|
||||
if currentFile is not None:
|
||||
logger.debug('ZERO after FILE, increasing counter')
|
||||
counter += 1
|
||||
relativeOffset = 0
|
||||
currentFile = None
|
||||
length = m.group('length')
|
||||
currentLength += common.msfToFrames(length)
|
||||
|
||||
# look for FILE lines
|
||||
m = _FILE_RE.search(line)
|
||||
if m:
|
||||
filePath = m.group('name')
|
||||
start = m.group('start')
|
||||
length = m.group('length')
|
||||
logger.debug('FILE %s, start %r, length %r',
|
||||
filePath, common.msfToFrames(start),
|
||||
common.msfToFrames(length))
|
||||
if not currentFile or filePath != currentFile.path:
|
||||
counter += 1
|
||||
relativeOffset = 0
|
||||
logger.debug('track %d, switched to new FILE, '
|
||||
'increased counter to %d',
|
||||
trackNumber, counter)
|
||||
currentFile = File(filePath, common.msfToFrames(start),
|
||||
common.msfToFrames(length))
|
||||
self._sources.append(counter, absoluteOffset + currentLength,
|
||||
currentFile)
|
||||
#absoluteOffset += common.msfToFrames(start)
|
||||
currentLength += common.msfToFrames(length)
|
||||
|
||||
# look for DATAFILE lines
|
||||
m = _DATAFILE_RE.search(line)
|
||||
if m:
|
||||
filePath = m.group('name')
|
||||
length = m.group('length')
|
||||
# print 'THOMAS', length
|
||||
logger.debug('FILE %s, length %r',
|
||||
filePath, common.msfToFrames(length))
|
||||
if not currentFile or filePath != currentFile.path:
|
||||
counter += 1
|
||||
relativeOffset = 0
|
||||
logger.debug('track %d, switched to new FILE, '
|
||||
'increased counter to %d',
|
||||
trackNumber, counter)
|
||||
# FIXME: assume that a MODE2_FORM_MIX track always starts at 0
|
||||
currentFile = File(filePath, 0, common.msfToFrames(length))
|
||||
self._sources.append(counter, absoluteOffset + currentLength,
|
||||
currentFile)
|
||||
#absoluteOffset += common.msfToFrames(start)
|
||||
currentLength += common.msfToFrames(length)
|
||||
|
||||
|
||||
# look for START lines
|
||||
m = _START_RE.search(line)
|
||||
if m:
|
||||
if not currentTrack:
|
||||
self.message(number, 'START without preceding TRACK')
|
||||
print 'ouch'
|
||||
continue
|
||||
|
||||
length = common.msfToFrames(m.group('length'))
|
||||
c, o, s = self._sources.get(absoluteOffset)
|
||||
logger.debug('at abs offset %d, we are in source %r' % (
|
||||
absoluteOffset, s))
|
||||
counterStart = self._sources.getCounterStart(c)
|
||||
relativeOffset = absoluteOffset - counterStart
|
||||
|
||||
currentTrack.index(0, path=s and s.path or None,
|
||||
absolute=absoluteOffset,
|
||||
relative=relativeOffset, counter=c)
|
||||
logger.debug('[track %02d index 00] added %r',
|
||||
currentTrack.number, currentTrack.getIndex(0))
|
||||
# store the pregapLength to add it when we index 1 for this
|
||||
# track on the next iteration
|
||||
pregapLength = length
|
||||
|
||||
# look for INDEX lines
|
||||
m = _INDEX_RE.search(line)
|
||||
if m:
|
||||
if not currentTrack:
|
||||
self.message(number, 'INDEX without preceding TRACK')
|
||||
print 'ouch'
|
||||
continue
|
||||
|
||||
indexNumber += 1
|
||||
offset = common.msfToFrames(m.group('offset'))
|
||||
self._index(currentTrack, indexNumber, absoluteOffset, offset)
|
||||
|
||||
# handle index 1 of final track, if any
|
||||
if currentTrack:
|
||||
self._index(currentTrack, 1, absoluteOffset, pregapLength)
|
||||
|
||||
# totalLength was added up to the penultimate track
|
||||
self.table.leadout = totalLength + currentLength
|
||||
logger.debug('parse: leadout: %r', self.table.leadout)
|
||||
|
||||
def message(self, number, message):
|
||||
"""
|
||||
Add a message about a given line in the cue file.
|
||||
|
||||
@param number: line number, counting from 0.
|
||||
"""
|
||||
self._messages.append((number + 1, message))
|
||||
|
||||
def getTrackLength(self, track):
|
||||
"""
|
||||
Returns the length of the given track, from its INDEX 01 to the next
|
||||
track's INDEX 01
|
||||
"""
|
||||
# returns track length in frames, or -1 if can't be determined and
|
||||
# complete file should be assumed
|
||||
# FIXME: this assumes a track can only be in one file; is this true ?
|
||||
i = self.table.tracks.index(track)
|
||||
if i == len(self.table.tracks) - 1:
|
||||
# last track, so no length known
|
||||
return -1
|
||||
|
||||
thisIndex = track.indexes[1] # FIXME: could be more
|
||||
nextIndex = self.table.tracks[i + 1].indexes[1] # FIXME: could be 0
|
||||
|
||||
c = thisIndex.counter
|
||||
if c is not None and c == nextIndex.counter:
|
||||
# they belong to the same source, so their relative delta is length
|
||||
return nextIndex.relative - thisIndex.relative
|
||||
|
||||
# FIXME: more logic
|
||||
return -1
|
||||
|
||||
def getRealPath(self, path):
|
||||
"""
|
||||
Translate the .toc's FILE to an existing path.
|
||||
|
||||
@type path: unicode
|
||||
"""
|
||||
return common.getRealPath(self._path, path)
|
||||
|
||||
|
||||
class File:
|
||||
"""
|
||||
I represent a FILE line in a .toc file.
|
||||
"""
|
||||
|
||||
def __init__(self, path, start, length):
|
||||
"""
|
||||
@type path: C{unicode}
|
||||
@type start: C{int}
|
||||
@param start: starting point for the track in this file, in frames
|
||||
@param length: length for the track in this file, in frames
|
||||
"""
|
||||
assert type(path) is unicode, "%r is not unicode" % path
|
||||
|
||||
self.path = path
|
||||
self.start = start
|
||||
self.length = length
|
||||
|
||||
def __repr__(self):
|
||||
return '<File %r>' % (self.path, )
|
||||
Reference in New Issue
Block a user