class Musepack8StreamReader: """An object for parsing Musepack SV8 streams.""" NUT_HEADER = Con.Struct('nut_header', Con.String('key', 2), NutValue('length')) def __init__(self, stream): """Initialized with a file object.""" self.stream = stream def packets(self): """Yields a set of (key, data) tuples.""" import string UPPERCASE = frozenset(string.ascii_uppercase) while (True): try: frame_header = self.NUT_HEADER.parse_stream(self.stream) except Con.core.FieldError: break if (not frozenset(frame_header.key).issubset(UPPERCASE)): break yield (frame_header.key, self.stream.read(frame_header.length - len(self.NUT_HEADER.build(frame_header))))
def delete_metadata(self): """Deletes the track's MetaData. This removes or unsets tags as necessary in order to remove all data. Raises IOError if unable to write the file.""" import tempfile new_aiff = tempfile.TemporaryFile() new_aiff.seek(12, 0) for (chunk_id, chunk_length, chunk_file) in self.chunk_files(): if (chunk_id != 'ID3 '): new_aiff.write( self.CHUNK_HEADER.build( Con.Container(chunk_id=chunk_id, chunk_length=chunk_length))) transfer_data(chunk_file.read, new_aiff.write) header = Con.Container(aiff_id='FORM', aiff_size=new_aiff.tell() - 8, aiff_type='AIFF') new_aiff.seek(0, 0) new_aiff.write(self.AIFF_HEADER.build(header)) new_aiff.seek(0, 0) f = open(self.filename, 'wb') transfer_data(new_aiff.read, f.write) new_aiff.close() f.close()
def set_metadata(self, metadata): """Takes a MetaData object and sets this track's metadata. This metadata includes track name, album name, and so on. Raises IOError if unable to write the file.""" if (metadata is None): return import tempfile id3_chunk = ID3v22Comment.converted(metadata).build() new_aiff = tempfile.TemporaryFile() new_aiff.seek(12, 0) id3_found = False for (chunk_id, chunk_length, chunk_file) in self.chunk_files(): if (chunk_id != 'ID3 '): new_aiff.write( self.CHUNK_HEADER.build( Con.Container(chunk_id=chunk_id, chunk_length=chunk_length))) transfer_data(chunk_file.read, new_aiff.write) else: new_aiff.write( self.CHUNK_HEADER.build( Con.Container(chunk_id='ID3 ', chunk_length=len(id3_chunk)))) new_aiff.write(id3_chunk) id3_found = True if (not id3_found): new_aiff.write( self.CHUNK_HEADER.build( Con.Container(chunk_id='ID3 ', chunk_length=len(id3_chunk)))) new_aiff.write(id3_chunk) header = Con.Container(aiff_id='FORM', aiff_size=new_aiff.tell() - 8, aiff_type='AIFF') new_aiff.seek(0, 0) new_aiff.write(self.AIFF_HEADER.build(header)) new_aiff.seek(0, 0) f = open(self.filename, 'wb') transfer_data(new_aiff.read, f.write) new_aiff.close() f.close()
def from_pcm(cls, filename, pcmreader, compression=None): """Encodes a new file from PCM data. Takes a filename string, PCMReader object and optional compression level string. Encodes a new audio file from pcmreader's data at the given filename with the specified compression level and returns a new AuAudio object.""" if (pcmreader.bits_per_sample not in (8, 16, 24)): raise InvalidFormat( _(u"Unsupported bits per sample %s") % (pcmreader.bits_per_sample)) bytes_per_sample = pcmreader.bits_per_sample / 8 header = Con.Container(magic_number='.snd', data_offset=0, data_size=0, encoding_format={ 8: 2, 16: 3, 24: 4 }[pcmreader.bits_per_sample], sample_rate=pcmreader.sample_rate, channels=pcmreader.channels) try: f = file(filename, 'wb') except IOError, err: raise EncodingError(str(err))
class UnframedVorbisComment(VorbisComment): """An implementation of VorbisComment without the framing bit.""" VORBIS_COMMENT = Con.Struct( "vorbis_comment", Con.PascalString("vendor_string", length_field=Con.ULInt32("length")), Con.PrefixedArray(length_field=Con.ULInt32("length"), subcon=Con.PascalString( "value", length_field=Con.ULInt32("length"))))
class AtomListAdapter(Con.Adapter): """An adapter for turning an Atom into a list of atoms. This works by parsing its data contents with Atom.""" ATOM_LIST = Con.GreedyRepeater(Atom("atoms")) def _encode(self, obj, context): obj.data = self.ATOM_LIST.build(obj.data) return obj def _decode(self, obj, context): obj.data = self.ATOM_LIST.parse(obj.data) return obj
def l_tag_value(cls, file, tag): subtype = {1: Con.Byte("data"), 2: Con.CString("data"), 3: Con.ULInt16("data"), 4: Con.ULInt32("data"), 5: Con.Struct("data", Con.ULInt32("high"), Con.ULInt32("low"))}[tag.type] data = Con.StrictRepeater(tag.count, subtype) if ((tag.type != 2) and (data.sizeof() <= 4)): return tag.offset else: file.seek(tag.offset, 0) return data.parse_stream(file)
def __init__(self, name): Con.Adapter.__init__( self, Con.Struct( name, Con.Embed( Con.BitStruct(None, Con.Flag("signed"), Con.Bits("exponent", 15))), Con.UBInt64("mantissa")))
def __str__(self): def __count_digits__(i): if (i == 0): return 0 else: return (i % 10) + __count_digits__(i / 10) disc_id = Con.Container() disc_id.track_count = len(self.tracks) disc_id.length = self.length() / 75 disc_id.digit_sum = sum( [__count_digits__(o / 75) for o in self.offsets()]) % 0xFF return DiscID.DISCID.build(disc_id).encode('hex')
def build(self): """Returns this VorbisComment as a binary string.""" comment = Con.Container(vendor_string=self.vendor_string, framing=1, value=[]) for (key, values) in self.items(): for value in values: if ((value != u"") and not ((key in ("TRACKNUMBER", "TRACKTOTAL", "DISCNUMBER", "DISCTOTAL")) and (value == u"0"))): comment.value.append("%s=%s" % (key, value.encode('utf-8'))) return self.VORBIS_COMMENT.build(comment)
class AuAudio(AudioFile): """A Sun AU audio file.""" SUFFIX = "au" NAME = SUFFIX AU_HEADER = Con.Struct('header', Con.Const(Con.String('magic_number', 4), '.snd'), Con.UBInt32('data_offset'), Con.UBInt32('data_size'), Con.UBInt32('encoding_format'), Con.UBInt32('sample_rate'), Con.UBInt32('channels')) def __init__(self, filename): AudioFile.__init__(self, filename) try: f = file(filename, 'rb') except IOError, msg: raise InvalidAU(str(msg)) try: header = AuAudio.AU_HEADER.parse_stream(f) if (header.encoding_format not in (2, 3, 4)): raise InvalidFile(_(u"Unsupported Sun AU encoding format")) self.__bits_per_sample__ = { 2: 8, 3: 16, 4: 24 }[header.encoding_format] self.__channels__ = header.channels self.__sample_rate__ = header.sample_rate self.__total_frames__ = header.data_size / \ (self.__bits_per_sample__ / 8) / \ self.__channels__ self.__data_offset__ = header.data_offset self.__data_size__ = header.data_size except Con.ConstError: raise InvalidFile(_(u"Invalid Sun AU header")) except Con.FieldError: raise InvalidAU(_(u"Invalid Sun AU header"))
def _encode(self, value, context): import math if (value < 0): signed = True value *= -1 else: signed = False (fmant, exponent) = math.frexp(value) if ((exponent > 16384) or (fmant >= 1)): exponent = 0x7FFF mantissa = 0 else: exponent += 16382 mantissa = fmant * (2**64) return Con.Container(signed=signed, exponent=exponent, mantissa=mantissa)
def build_id3v1(cls, song_title, artist, album, year, comment, track_number): """Turns fields into a complete ID3v1 binary tag string. All fields are unicode except for track_number, an int.""" def __s_pad__(s, length): if (len(s) < length): return s + chr(0) * (length - len(s)) else: s = s[0:length].rstrip() return s + chr(0) * (length - len(s)) c = Con.Container() c.identifier = 'TAG' c.song_title = __s_pad__(song_title.encode('ascii', 'replace'), 30) c.artist = __s_pad__(artist.encode('ascii', 'replace'), 30) c.album = __s_pad__(album.encode('ascii', 'replace'), 30) c.year = __s_pad__(year.encode('ascii', 'replace'), 4) c.comment = __s_pad__(comment.encode('ascii', 'replace'), 28) c.track_number = int(track_number) c.genre = 0 return ID3v1Comment.ID3v1.build(c)
class VorbisComment(MetaData, dict): """A complete Vorbis Comment tag.""" VORBIS_COMMENT = Con.Struct( "vorbis_comment", Con.PascalString("vendor_string", length_field=Con.ULInt32("length")), Con.PrefixedArray(length_field=Con.ULInt32("length"), subcon=Con.PascalString( "value", length_field=Con.ULInt32("length"))), Con.Const(Con.Byte("framing"), 1)) ATTRIBUTE_MAP = { 'track_name': 'TITLE', 'track_number': 'TRACKNUMBER', 'track_total': 'TRACKTOTAL', 'album_name': 'ALBUM', 'artist_name': 'ARTIST', 'performer_name': 'PERFORMER', 'composer_name': 'COMPOSER', 'conductor_name': 'CONDUCTOR', 'media': 'SOURCE MEDIUM', 'ISRC': 'ISRC', 'catalog': 'CATALOG', 'copyright': 'COPYRIGHT', 'publisher': 'PUBLISHER', 'year': 'DATE', 'album_number': 'DISCNUMBER', 'album_total': 'DISCTOTAL', 'comment': 'COMMENT' } ITEM_MAP = dict(map(reversed, ATTRIBUTE_MAP.items())) def __init__(self, vorbis_data, vendor_string=u""): """Initialized with a key->[value1,value2] dict. keys are generally upper case. values are unicode string. vendor_string is an optional unicode string.""" dict.__init__(self, [(key.upper(), values) for (key, values) in vorbis_data.items()]) self.vendor_string = vendor_string def __setitem__(self, key, value): dict.__setitem__(self, key.upper(), value) def __getattr__(self, key): if (key == 'track_number'): match = re.match(r'^\d+$', self.get('TRACKNUMBER', [u''])[0]) if (match): return int(match.group(0)) else: match = re.match('^(\d+)/\d+$', self.get('TRACKNUMBER', [u''])[0]) if (match): return int(match.group(1)) else: return 0 elif (key == 'track_total'): match = re.match(r'^\d+$', self.get('TRACKTOTAL', [u''])[0]) if (match): return int(match.group(0)) else: match = re.match('^\d+/(\d+)$', self.get('TRACKNUMBER', [u''])[0]) if (match): return int(match.group(1)) else: return 0 elif (key == 'album_number'): match = re.match(r'^\d+$', self.get('DISCNUMBER', [u''])[0]) if (match): return int(match.group(0)) else: match = re.match('^(\d+)/\d+$', self.get('DISCNUMBER', [u''])[0]) if (match): return int(match.group(1)) else: return 0 elif (key == 'album_total'): match = re.match(r'^\d+$', self.get('DISCTOTAL', [u''])[0]) if (match): return int(match.group(0)) else: match = re.match('^\d+/(\d+)$', self.get('DISCNUMBER', [u''])[0]) if (match): return int(match.group(1)) else: return 0 elif (key in self.ATTRIBUTE_MAP): return self.get(self.ATTRIBUTE_MAP[key], [u''])[0] elif (key in MetaData.__FIELDS__): return u'' else: try: return self.__dict__[key] except KeyError: raise AttributeError(key) def __delattr__(self, key): if (key == 'track_number'): track_number = self.get('TRACKNUMBER', [u''])[0] if (re.match(r'^\d+$', track_number)): del (self['TRACKNUMBER']) elif (re.match('^\d+/(\d+)$', track_number)): self['TRACKNUMBER'] = u"0/%s" % (re.match( '^\d+/(\d+)$', track_number).group(1)) elif (key == 'track_total'): track_number = self.get('TRACKNUMBER', [u''])[0] if (re.match('^(\d+)/\d+$', track_number)): self['TRACKNUMBER'] = u"%s" % (re.match( '^(\d+)/\d+$', track_number).group(1)) if ('TRACKTOTAL' in self): del (self['TRACKTOTAL']) elif (key == 'album_number'): album_number = self.get('DISCNUMBER', [u''])[0] if (re.match(r'^\d+$', album_number)): del (self['DISCNUMBER']) elif (re.match('^\d+/(\d+)$', album_number)): self['DISCNUMBER'] = u"0/%s" % (re.match( '^\d+/(\d+)$', album_number).group(1)) elif (key == 'album_total'): album_number = self.get('DISCNUMBER', [u''])[0] if (re.match('^(\d+)/\d+$', album_number)): self['DISCNUMBER'] = u"%s" % (re.match('^(\d+)/\d+$', album_number).group(1)) if ('DISCTOTAL' in self): del (self['DISCTOTAL']) elif (key in self.ATTRIBUTE_MAP): if (self.ATTRIBUTE_MAP[key] in self): del (self[self.ATTRIBUTE_MAP[key]]) elif (key in MetaData.__FIELDS__): pass else: try: del (self.__dict__[key]) except KeyError: raise AttributeError(key) @classmethod def supports_images(cls): """Returns False.""" #There's actually a (proposed?) standard to add embedded covers #to Vorbis Comments by base64 encoding them. #This strikes me as messy and convoluted. #In addition, I'd have to perform a special case of #image extraction and re-insertion whenever converting #to FlacMetaData. The whole thought gives me a headache. return False def images(self): """Returns an empty list of Image objects.""" return list() #if an attribute is updated (e.g. self.track_name) #make sure to update the corresponding dict pair def __setattr__(self, key, value): if (key in self.ATTRIBUTE_MAP): if (key not in MetaData.__INTEGER_FIELDS__): self[self.ATTRIBUTE_MAP[key]] = [value] else: self[self.ATTRIBUTE_MAP[key]] = [unicode(value)] else: self.__dict__[key] = value @classmethod def converted(cls, metadata): """Converts a MetaData object to a VorbisComment object.""" if ((metadata is None) or (isinstance(metadata, VorbisComment))): return metadata elif (metadata.__class__.__name__ == 'FlacMetaData'): return cls(vorbis_data=dict(metadata.vorbis_comment.items()), vendor_string=metadata.vorbis_comment.vendor_string) else: values = {} for key in cls.ATTRIBUTE_MAP.keys(): if (key in cls.__INTEGER_FIELDS__): if (getattr(metadata, key) != 0): values[cls.ATTRIBUTE_MAP[key]] = \ [unicode(getattr(metadata, key))] elif (getattr(metadata, key) != u""): values[cls.ATTRIBUTE_MAP[key]] = \ [unicode(getattr(metadata, key))] return VorbisComment(values) def merge(self, metadata): """Updates any currently empty entries from metadata's values.""" metadata = self.__class__.converted(metadata) if (metadata is None): return for (key, values) in metadata.items(): if ((len(values) > 0) and (len(self.get(key, [])) == 0)): self[key] = values def __comment_name__(self): return u'Vorbis' #takes two (key,value) vorbiscomment pairs #returns cmp on the weighted set of them #(title first, then artist, album, tracknumber, ... , replaygain) @classmethod def __by_pair__(cls, pair1, pair2): KEY_MAP = { "TITLE": 1, "ALBUM": 2, "TRACKNUMBER": 3, "TRACKTOTAL": 4, "DISCNUMBER": 5, "DISCTOTAL": 6, "ARTIST": 7, "PERFORMER": 8, "COMPOSER": 9, "CONDUCTOR": 10, "CATALOG": 11, "PUBLISHER": 12, "ISRC": 13, "SOURCE MEDIUM": 14, #"YEAR": 15, "DATE": 16, "COPYRIGHT": 17, "REPLAYGAIN_ALBUM_GAIN": 19, "REPLAYGAIN_ALBUM_PEAK": 19, "REPLAYGAIN_TRACK_GAIN": 19, "REPLAYGAIN_TRACK_PEAK": 19, "REPLAYGAIN_REFERENCE_LOUDNESS": 20 } return cmp( (KEY_MAP.get(pair1[0].upper(), 18), pair1[0].upper(), pair1[1]), (KEY_MAP.get(pair2[0].upper(), 18), pair2[0].upper(), pair2[1])) def __comment_pairs__(self): pairs = [] for (key, values) in self.items(): for value in values: pairs.append((key, value)) pairs.sort(VorbisComment.__by_pair__) return pairs def build(self): """Returns this VorbisComment as a binary string.""" comment = Con.Container(vendor_string=self.vendor_string, framing=1, value=[]) for (key, values) in self.items(): for value in values: if ((value != u"") and not ((key in ("TRACKNUMBER", "TRACKTOTAL", "DISCNUMBER", "DISCTOTAL")) and (value == u"0"))): comment.value.append("%s=%s" % (key, value.encode('utf-8'))) return self.VORBIS_COMMENT.build(comment)
class __BMP__(ImageMetrics): HEADER = Con.Struct('bmp_header', Con.Const(Con.String('magic_number', 2), 'BM'), Con.ULInt32('file_size'), Con.ULInt16('reserved1'), Con.ULInt16('reserved2'), Con.ULInt32('bitmap_data_offset')) INFORMATION = Con.Struct('bmp_information', Con.ULInt32('header_size'), Con.ULInt32('width'), Con.ULInt32('height'), Con.ULInt16('color_planes'), Con.ULInt16('bits_per_pixel'), Con.ULInt32('compression_method'), Con.ULInt32('image_size'), Con.ULInt32('horizontal_resolution'), Con.ULInt32('vertical_resolution'), Con.ULInt32('colors_used'), Con.ULInt32('important_colors_used')) def __init__(self, width, height, bits_per_pixel, color_count): ImageMetrics.__init__(self, width, height, bits_per_pixel, color_count, u'image/x-ms-bmp') @classmethod def parse(cls, file): try: header = cls.HEADER.parse_stream(file) information = cls.INFORMATION.parse_stream(file) return __BMP__(information.width, information.height, information.bits_per_pixel, information.colors_used) except Con.ConstError: raise InvalidBMP(_(u'Invalid BMP'))
class AiffAudio(AudioFile): """An AIFF audio file.""" SUFFIX = "aiff" NAME = SUFFIX AIFF_HEADER = Con.Struct("aiff_header", Con.Const(Con.Bytes("aiff_id", 4), "FORM"), Con.UBInt32("aiff_size"), Con.Const(Con.Bytes("aiff_type", 4), "AIFF")) CHUNK_HEADER = Con.Struct("chunk_header", Con.Bytes("chunk_id", 4), Con.UBInt32("chunk_length")) COMM_CHUNK = Con.Struct("comm", Con.UBInt16("channels"), Con.UBInt32("total_sample_frames"), Con.UBInt16("sample_size"), IEEE_Extended("sample_rate")) SSND_ALIGN = Con.Struct("ssnd", Con.UBInt32("offset"), Con.UBInt32("blocksize")) PRINTABLE_ASCII = set([chr(i) for i in xrange(0x20, 0x7E + 1)]) def __init__(self, filename): """filename is a plain string.""" self.filename = filename comm_found = False ssnd_found = False try: f = open(self.filename, 'rb') for (chunk_id, chunk_length, chunk_offset) in self.chunks(): if (chunk_id == 'COMM'): f.seek(chunk_offset, 0) comm = self.COMM_CHUNK.parse(f.read(chunk_length)) self.__channels__ = comm.channels self.__total_sample_frames__ = comm.total_sample_frames self.__sample_size__ = comm.sample_size self.__sample_rate__ = int(comm.sample_rate) comm_found = True elif (chunk_id == 'SSND'): f.seek(chunk_offset, 0) ssnd = self.SSND_ALIGN.parse_stream(f) ssnd_found = True elif (not set(chunk_id).issubset(self.PRINTABLE_ASCII)): raise InvalidAIFF(_("chunk header not ASCII")) if (not comm_found): raise InvalidAIFF(_("no COMM chunk found")) if (not ssnd_found): raise InvalidAIFF(_("no SSND chunk found")) f.close() except IOError, msg: raise InvalidAIFF(str(msg)) except Con.FieldError: raise InvalidAIFF(_("invalid COMM or SSND chunk"))
if (int(pcmreader.channel_mask) in ( 0x4, # FC 0x3, # FL, FR 0x7, # FL, FR, FC 0x33, # FL, FR, BL, BR 0x707)): # FL, SL, FC, FR, SR, BC standard_channel_mask = ChannelMask(pcmreader.channel_mask) aiff_channel_mask = AIFFChannelMask(standard_channel_mask) pcmreader = ReorderedPCMReader(pcmreader, [ standard_channel_mask.channels().index(channel) for channel in aiff_channel_mask.channels() ]) try: aiff_header = Con.Container(aiff_id='FORM', aiff_size=4, aiff_type='AIFF') comm_chunk = Con.Container(channels=pcmreader.channels, total_sample_frames=0, sample_size=pcmreader.bits_per_sample, sample_rate=float( pcmreader.sample_rate)) ssnd_header = Con.Container(chunk_id='SSND', chunk_length=0) ssnd_alignment = Con.Container(offset=0, blocksize=0) #skip ahead to the start of the SSND chunk f.seek( cls.AIFF_HEADER.sizeof() + cls.CHUNK_HEADER.sizeof() + cls.COMM_CHUNK.sizeof() + cls.CHUNK_HEADER.sizeof(), 0)
class __GIF__(ImageMetrics): HEADER = Con.Struct('header', Con.Const(Con.String('gif', 3), 'GIF'), Con.String('version', 3)) SCREEN_DESCRIPTOR = Con.Struct('logical_screen_descriptor', Con.ULInt16('width'), Con.ULInt16('height'), Con.Embed( Con.BitStruct('packed_fields', Con.Flag('global_color_table'), Con.Bits('color_resolution', 3), Con.Flag('sort'), Con.Bits('global_color_table_size', 3))), Con.Byte('background_color_index'), Con.Byte('pixel_aspect_ratio')) def __init__(self, width, height, color_count): ImageMetrics.__init__(self, width, height, 8, color_count, u'image/gif') @classmethod def parse(cls, file): try: header = cls.HEADER.parse_stream(file) descriptor = cls.SCREEN_DESCRIPTOR.parse_stream(file) return __GIF__(descriptor.width, descriptor.height, 2 ** (descriptor.global_color_table_size + 1)) except Con.ConstError: raise InvalidGIF(_(u'Invalid GIF'))
class __PNG__(ImageMetrics): HEADER = Con.Const(Con.String('header', 8), '89504e470d0a1a0a'.decode('hex')) CHUNK_HEADER = Con.Struct('chunk', Con.UBInt32('length'), Con.String('type', 4)) CHUNK_FOOTER = Con.Struct('crc32', Con.UBInt32('crc')) IHDR = Con.Struct('IHDR', Con.UBInt32('width'), Con.UBInt32('height'), Con.Byte('bit_depth'), Con.Byte('color_type'), Con.Byte('compression_method'), Con.Byte('filter_method'), Con.Byte('interlace_method')) def __init__(self, width, height, bits_per_pixel, color_count): ImageMetrics.__init__(self, width, height, bits_per_pixel, color_count, u'image/png') @classmethod def parse(cls, file): ihdr = None plte = None try: header = cls.HEADER.parse_stream(file) chunk_header = cls.CHUNK_HEADER.parse_stream(file) data = file.read(chunk_header.length) chunk_footer = cls.CHUNK_FOOTER.parse_stream(file) while (chunk_header.type != 'IEND'): if (chunk_header.type == 'IHDR'): ihdr = cls.IHDR.parse(data) elif (chunk_header.type == 'PLTE'): plte = data chunk_header = cls.CHUNK_HEADER.parse_stream(file) data = file.read(chunk_header.length) chunk_footer = cls.CHUNK_FOOTER.parse_stream(file) if (ihdr.color_type == 0): # grayscale bits_per_pixel = ihdr.bit_depth color_count = 0 elif (ihdr.color_type == 2): # RGB bits_per_pixel = ihdr.bit_depth * 3 color_count = 0 elif (ihdr.color_type == 3): # palette bits_per_pixel = 8 if ((len(plte) % 3) != 0): raise InvalidPNG(_(u'Invalid PLTE chunk length')) else: color_count = len(plte) / 3 elif (ihdr.color_type == 4): # grayscale + alpha bits_per_pixel = ihdr.bit_depth * 2 color_count = 0 elif (ihdr.color_type == 6): # RGB + alpha bits_per_pixel = ihdr.bit_depth * 4 color_count = 0 return __PNG__(ihdr.width, ihdr.height, bits_per_pixel, color_count) except Con.ConstError: raise InvalidPNG(_(u'Invalid PNG'))
class SpeexAudio(VorbisAudio): """An Ogg Speex audio file.""" SUFFIX = "spx" NAME = SUFFIX DEFAULT_COMPRESSION = "8" COMPRESSION_MODES = tuple([str(i) for i in range(0, 11)]) BINARIES = ("speexenc", "speexdec") REPLAYGAIN_BINARIES = tuple() SPEEX_HEADER = Con.Struct('speex_header', Con.String('speex_string', 8), Con.String('speex_version', 20), Con.ULInt32('speex_version_id'), Con.ULInt32('header_size'), Con.ULInt32('sampling_rate'), Con.ULInt32('mode'), Con.ULInt32('mode_bitstream_version'), Con.ULInt32('channels'), Con.ULInt32('bitrate'), Con.ULInt32('frame_size'), Con.ULInt32('vbr'), Con.ULInt32('frame_per_packet'), Con.ULInt32('extra_headers'), Con.ULInt32('reserved1'), Con.ULInt32('reserved2')) def __init__(self, filename): """filename is a plain string.""" AudioFile.__init__(self, filename) try: self.__read_metadata__() except IOError, msg: raise InvalidSpeex(str(msg))
header = self.header.parse_stream(stream) return self.sub_atom.parse_stream(stream) def _build(self, obj, stream, context): data = self.sub_atom.build(obj) stream.write( self.header.build( Con.Container(type=self.atom_name, size=len(data) + 8))) stream.write(data) def _sizeof(self, context): return self.sub_atom.sizeof(context) + 8 ATOM_FTYP = Con.Struct("ftyp", Con.String("major_brand", 4), Con.UBInt32("major_brand_version"), Con.GreedyRepeater(Con.String("compatible_brands", 4))) ATOM_MVHD = Con.Struct( "mvhd", Con.Byte("version"), Con.String("flags", 3), VersionLength("created_mac_UTC_date"), VersionLength("modified_mac_UTC_date"), Con.UBInt32("time_scale"), VersionLength("duration"), Con.UBInt32("playback_speed"), Con.UBInt16("user_volume"), Con.Padding(10), Con.Struct("windows", Con.UBInt32("geometry_matrix_a"), Con.UBInt32("geometry_matrix_b"), Con.UBInt32("geometry_matrix_u"), Con.UBInt32("geometry_matrix_c"), Con.UBInt32("geometry_matrix_d"), Con.UBInt32("geometry_matrix_v"), Con.UBInt32("geometry_matrix_x"),
class __TIFF__(ImageMetrics): HEADER = Con.Struct('header', Con.String('byte_order', 2), Con.Switch('order', lambda ctx: ctx['byte_order'], {"II": Con.Embed( Con.Struct('little_endian', Con.Const(Con.ULInt16('version'), 42), Con.ULInt32('offset'))), "MM": Con.Embed( Con.Struct('big_endian', Con.Const(Con.UBInt16('version'), 42), Con.UBInt32('offset')))})) L_IFD = Con.Struct('ifd', Con.PrefixedArray( length_field=Con.ULInt16('length'), subcon=Con.Struct('tags', Con.ULInt16('id'), Con.ULInt16('type'), Con.ULInt32('count'), Con.ULInt32('offset'))), Con.ULInt32('next')) B_IFD = Con.Struct('ifd', Con.PrefixedArray( length_field=Con.UBInt16('length'), subcon=Con.Struct('tags', Con.UBInt16('id'), Con.UBInt16('type'), Con.UBInt32('count'), Con.UBInt32('offset'))), Con.UBInt32('next')) def __init__(self, width, height, bits_per_pixel, color_count): ImageMetrics.__init__(self, width, height, bits_per_pixel, color_count, u'image/tiff') @classmethod def b_tag_value(cls, file, tag): subtype = {1: Con.Byte("data"), 2: Con.CString("data"), 3: Con.UBInt16("data"), 4: Con.UBInt32("data"), 5: Con.Struct("data", Con.UBInt32("high"), Con.UBInt32("low"))}[tag.type] data = Con.StrictRepeater(tag.count, subtype) if ((tag.type != 2) and (data.sizeof() <= 4)): return tag.offset else: file.seek(tag.offset, 0) return data.parse_stream(file) @classmethod def l_tag_value(cls, file, tag): subtype = {1: Con.Byte("data"), 2: Con.CString("data"), 3: Con.ULInt16("data"), 4: Con.ULInt32("data"), 5: Con.Struct("data", Con.ULInt32("high"), Con.ULInt32("low"))}[tag.type] data = Con.StrictRepeater(tag.count, subtype) if ((tag.type != 2) and (data.sizeof() <= 4)): return tag.offset else: file.seek(tag.offset, 0) return data.parse_stream(file) @classmethod def parse(cls, file): width = 0 height = 0 bits_per_sample = 0 color_count = 0 try: header = cls.HEADER.parse_stream(file) if (header.byte_order == 'II'): IFD = cls.L_IFD tag_value = cls.l_tag_value elif (header.byte_order == 'MM'): IFD = cls.B_IFD tag_value = cls.b_tag_value else: raise InvalidTIFF(_(u'Invalid byte order')) file.seek(header.offset, 0) ifd = IFD.parse_stream(file) while (True): for tag in ifd.tags: if (tag.id == 0x0100): width = tag_value(file, tag) elif (tag.id == 0x0101): height = tag_value(file, tag) elif (tag.id == 0x0102): try: bits_per_sample = sum(tag_value(file, tag)) except TypeError: bits_per_sample = tag_value(file, tag) elif (tag.id == 0x0140): color_count = tag.count / 3 else: pass if (ifd.next == 0x00): break else: file.seek(ifd.next, 0) ifd = IFD.parse_stream(file) return __TIFF__(width, height, bits_per_sample, color_count) except Con.ConstError: raise InvalidTIFF(_(u'Invalid TIFF'))
def __init__(self, atom_name, sub_atom): Con.Struct.__init__(self, atom_name) self.atom_name = atom_name self.sub_atom = sub_atom self.header = Con.Struct(atom_name, Con.UBInt32("size"), Con.Const(Con.String("type", 4), atom_name))
class __JPEG__(ImageMetrics): SEGMENT_HEADER = Con.Struct('segment_header', Con.Const(Con.Byte('header'), 0xFF), Con.Byte('type'), Con.If( lambda ctx: ctx['type'] not in (0xD8, 0xD9), Con.UBInt16('length'))) APP0 = Con.Struct('JFIF_segment_marker', Con.String('identifier', 5), Con.Byte('major_version'), Con.Byte('minor_version'), Con.Byte('density_units'), Con.UBInt16('x_density'), Con.UBInt16('y_density'), Con.Byte('thumbnail_width'), Con.Byte('thumbnail_height')) SOF = Con.Struct('start_of_frame', Con.Byte('data_precision'), Con.UBInt16('image_height'), Con.UBInt16('image_width'), Con.Byte('components')) def __init__(self, width, height, bits_per_pixel): ImageMetrics.__init__(self, width, height, bits_per_pixel, 0, u'image/jpeg') @classmethod def parse(cls, file): try: header = cls.SEGMENT_HEADER.parse_stream(file) if (header.type != 0xD8): raise InvalidJPEG(_(u'Invalid JPEG header')) segment = cls.SEGMENT_HEADER.parse_stream(file) while (segment.type != 0xD9): if (segment.type == 0xDA): break if (segment.type in (0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0XC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF)): # start of frame segment_data = cStringIO.StringIO( file.read(segment.length - 2)) frame0 = cls.SOF.parse_stream(segment_data) segment_data.close() return __JPEG__(width=frame0.image_width, height=frame0.image_height, bits_per_pixel=(frame0.data_precision * frame0.components)) else: file.seek(segment.length - 2, 1) segment = cls.SEGMENT_HEADER.parse_stream(file) raise InvalidJPEG(_(u'Start of frame not found')) except Con.ConstError: raise InvalidJPEG(_(u"Invalid JPEG segment marker at 0x%X") % \ (file.tell()))
class ID3v1Comment(MetaData, list): """A complete ID3v1 tag.""" ID3v1 = Con.Struct("id3v1", Con.Const(Con.String("identifier", 3), 'TAG'), Con.String("song_title", 30), Con.String("artist", 30), Con.String("album", 30), Con.String("year", 4), Con.String("comment", 28), Con.Padding(1), Con.Byte("track_number"), Con.Byte("genre")) ID3v1_NO_TRACKNUMBER = Con.Struct( "id3v1_notracknumber", Con.Const(Con.String("identifier", 3), 'TAG'), Con.String("song_title", 30), Con.String("artist", 30), Con.String("album", 30), Con.String("year", 4), Con.String("comment", 30), Con.Byte("genre")) ATTRIBUTES = [ 'track_name', 'artist_name', 'album_name', 'year', 'comment', 'track_number' ] @classmethod def read_id3v1_comment(cls, mp3filename): """Reads a ID3v1Comment data from an MP3 filename. Returns a (song title, artist, album, year, comment, track number) tuple. If no ID3v1 tag is present, returns a tuple with those fields blank. All text is in unicode. If track number is -1, the id3v1 comment could not be found. """ mp3file = file(mp3filename, "rb") try: mp3file.seek(-128, 2) try: id3v1 = ID3v1Comment.ID3v1.parse(mp3file.read()) except Con.adapters.PaddingError: mp3file.seek(-128, 2) id3v1 = ID3v1Comment.ID3v1_NO_TRACKNUMBER.parse(mp3file.read()) id3v1.track_number = 0 except Con.ConstError: return tuple([u""] * 5 + [-1]) field_list = (id3v1.song_title, id3v1.artist, id3v1.album, id3v1.year, id3v1.comment) return tuple( map(lambda t: t.rstrip('\x00').decode('ascii', 'replace'), field_list) + [id3v1.track_number]) finally: mp3file.close() @classmethod def build_id3v1(cls, song_title, artist, album, year, comment, track_number): """Turns fields into a complete ID3v1 binary tag string. All fields are unicode except for track_number, an int.""" def __s_pad__(s, length): if (len(s) < length): return s + chr(0) * (length - len(s)) else: s = s[0:length].rstrip() return s + chr(0) * (length - len(s)) c = Con.Container() c.identifier = 'TAG' c.song_title = __s_pad__(song_title.encode('ascii', 'replace'), 30) c.artist = __s_pad__(artist.encode('ascii', 'replace'), 30) c.album = __s_pad__(album.encode('ascii', 'replace'), 30) c.year = __s_pad__(year.encode('ascii', 'replace'), 4) c.comment = __s_pad__(comment.encode('ascii', 'replace'), 28) c.track_number = int(track_number) c.genre = 0 return ID3v1Comment.ID3v1.build(c) def __init__(self, metadata): """Initialized with a read_id3v1_comment tuple. Fields are (title,artist,album,year,comment,tracknum)""" list.__init__(self, metadata) def supports_images(self): """Returns False.""" return False #if an attribute is updated (e.g. self.track_name) #make sure to update the corresponding list item def __setattr__(self, key, value): if (key in self.ATTRIBUTES): if (key != 'track_number'): self[self.ATTRIBUTES.index(key)] = value else: self[self.ATTRIBUTES.index(key)] = int(value) elif (key in MetaData.__FIELDS__): pass else: self.__dict__[key] = value def __delattr__(self, key): if (key == 'track_number'): setattr(self, key, 0) elif (key in self.ATTRIBUTES): setattr(self, key, u"") def __getattr__(self, key): if (key in self.ATTRIBUTES): return self[self.ATTRIBUTES.index(key)] elif (key in MetaData.__INTEGER_FIELDS__): return 0 elif (key in MetaData.__FIELDS__): return u"" else: raise AttributeError(key) @classmethod def converted(cls, metadata): """Converts a MetaData object to an ID3v1Comment object.""" if ((metadata is None) or (isinstance(metadata, ID3v1Comment))): return metadata return ID3v1Comment( (metadata.track_name, metadata.artist_name, metadata.album_name, metadata.year, metadata.comment, int(metadata.track_number))) def __comment_name__(self): return u'ID3v1' def __comment_pairs__(self): return zip(('Title', 'Artist', 'Album', 'Year', 'Comment', 'Tracknum'), self) def build_tag(self): """Returns a binary string of this tag's data.""" return self.build_id3v1(self.track_name, self.artist_name, self.album_name, self.year, self.comment, self.track_number) def images(self): """Returns an empty list of Image objects.""" return []
def _build(self, obj, stream, context): data = self.sub_atom.build(obj) stream.write( self.header.build( Con.Container(type=self.atom_name, size=len(data) + 8))) stream.write(data)
def __init__(self, name): Con.Adapter.__init__( self, Con.RepeatUntil(lambda obj, ctx: (obj & 0x80) == 0x00, Con.UBInt8(name)))
def VersionLength(name): """A struct for 32 or 64 bit fields, depending on version field.""" return Con.IfThenElse(name, lambda ctx: ctx["version"] == 0, Con.UBInt32(None), Con.UBInt64(None))
class MusepackAudio(ApeTaggedAudio, AudioFile): """A Musepack audio file.""" SUFFIX = "mpc" NAME = SUFFIX DEFAULT_COMPRESSION = "standard" COMPRESSION_MODES = ("thumb", "radio", "standard", "extreme", "insane") ###Musepack SV7### #BINARIES = ('mppdec','mppenc') ###Musepack SV8### BINARIES = ('mpcdec', 'mpcenc') MUSEPACK8_HEADER = Con.Struct('musepack8_header', Con.UBInt32('crc32'), Con.Byte('bitstream_version'), NutValue('sample_count'), NutValue('beginning_silence'), Con.Embed(Con.BitStruct( 'flags', Con.Bits('sample_frequency', 3), Con.Bits('max_used_bands', 5), Con.Bits('channel_count', 4), Con.Flag('mid_side_used'), Con.Bits('audio_block_frames', 3)))) #not sure about some of the flag locations #Musepack 7's header is very unusual MUSEPACK7_HEADER = Con.Struct('musepack7_header', Con.Const(Con.String('signature', 3), 'MP+'), Con.Byte('version'), Con.ULInt32('frame_count'), Con.ULInt16('max_level'), Con.Embed( Con.BitStruct('flags', Con.Bits('profile', 4), Con.Bits('link', 2), Con.Bits('sample_frequency', 2), Con.Flag('intensity_stereo'), Con.Flag('midside_stereo'), Con.Bits('maxband', 6))), Con.ULInt16('title_gain'), Con.ULInt16('title_peak'), Con.ULInt16('album_gain'), Con.ULInt16('album_peak'), Con.Embed( Con.BitStruct('more_flags', Con.Bits('unused1', 16), Con.Bits('last_frame_length_low', 4), Con.Flag('true_gapless'), Con.Bits('unused2', 3), Con.Flag('fast_seeking'), Con.Bits('last_frame_length_high', 7))), Con.Bytes('unknown', 3), Con.Byte('encoder_version')) def __init__(self, filename): """filename is a plain string.""" AudioFile.__init__(self, filename) f = file(filename, 'rb') try: if (f.read(4) == 'MPCK'): # a Musepack 8 stream for (key, packet) in Musepack8StreamReader(f).packets(): if (key == 'SH'): header = MusepackAudio.MUSEPACK8_HEADER.parse(packet) self.__sample_rate__ = (44100, 48000, 37800, 32000)[ header.sample_frequency] self.__total_frames__ = header.sample_count self.__channels__ = header.channel_count + 1 break elif (key == 'SE'): raise InvalidFile(_(u'No Musepack header found')) else: # a Musepack 7 stream f.seek(0, 0) try: header = MusepackAudio.MUSEPACK7_HEADER.parse_stream(f) except Con.ConstError: raise InvalidFile(_(u'Musepack signature incorrect')) header.last_frame_length = \ (header.last_frame_length_high << 4) | \ header.last_frame_length_low self.__sample_rate__ = (44100, 48000, 37800, 32000)[header.sample_frequency] self.__total_frames__ = (((header.frame_count - 1) * 1152) + header.last_frame_length) self.__channels__ = 2 finally: f.close() @classmethod def from_pcm(cls, filename, pcmreader, compression=None): """Encodes a new file from PCM data. Takes a filename string, PCMReader object and optional compression level string. Encodes a new audio file from pcmreader's data at the given filename with the specified compression level and returns a new MusepackAudio object.""" import tempfile import bisect if (str(compression) not in cls.COMPRESSION_MODES): compression = cls.DEFAULT_COMPRESSION if ((pcmreader.channels > 2) or (pcmreader.sample_rate not in (44100, 48000, 37800, 32000)) or (pcmreader.bits_per_sample != 16)): pcmreader = PCMConverter( pcmreader, sample_rate=[32000, 32000, 37800, 44100, 48000][bisect.bisect( [32000, 37800, 44100, 48000], pcmreader.sample_rate)], channels=min(pcmreader.channels, 2), bits_per_sample=16) f = tempfile.NamedTemporaryFile(suffix=".wav") w = WaveAudio.from_pcm(f.name, pcmreader) try: return cls.__from_wave__(filename, f.name, compression) finally: del(w) f.close() #While Musepack needs to pipe things through WAVE, #not all WAVEs are acceptable. #Use the *_pcm() methods first. def __to_wave__(self, wave_filename): devnull = file(os.devnull, "wb") try: sub = subprocess.Popen([BIN['mpcdec'], self.filename, wave_filename], stdout=devnull, stderr=devnull) #FIXME - small files (~5 seconds) result in an error by mpcdec, #even if they decode correctly. #Not much we can do except try to workaround its bugs. if (sub.wait() not in [0, 250]): raise DecodingError() finally: devnull.close() @classmethod def __from_wave__(cls, filename, wave_filename, compression=None): if (str(compression) not in cls.COMPRESSION_MODES): compression = cls.DEFAULT_COMPRESSION #mppenc requires files to end with .mpc for some reason if (not filename.endswith(".mpc")): import tempfile actual_filename = filename tempfile = tempfile.NamedTemporaryFile(suffix=".mpc") filename = tempfile.name else: actual_filename = tempfile = None ###Musepack SV7### #sub = subprocess.Popen([BIN['mppenc'], # "--silent", # "--overwrite", # "--%s" % (compression), # wave_filename, # filename], # preexec_fn=ignore_sigint) ###Musepack SV8### sub = subprocess.Popen([BIN['mpcenc'], "--silent", "--overwrite", "--%s" % (compression), wave_filename, filename]) if (sub.wait() == 0): if (tempfile is not None): filename = actual_filename f = file(filename, 'wb') tempfile.seek(0, 0) transfer_data(tempfile.read, f.write) f.close() tempfile.close() return MusepackAudio(filename) else: if (tempfile is not None): tempfile.close() raise EncodingError(u"error encoding file with mpcenc") @classmethod def is_type(cls, file): """Returns True if the given file object describes this format. Takes a seekable file pointer rewound to the start of the file.""" header = file.read(4) ###Musepack SV7### #return header == 'MP+\x07' ###Musepack SV8### return (header == 'MP+\x07') or (header == 'MPCK') def sample_rate(self): """Returns the rate of the track's audio as an integer number of Hz.""" return self.__sample_rate__ def total_frames(self): """Returns the total PCM frames of the track as an integer.""" return self.__total_frames__ def channels(self): """Returns an integer number of channels this track contains.""" return self.__channels__ def bits_per_sample(self): """Returns an integer number of bits-per-sample this track contains.""" return 16 def lossless(self): """Returns False.""" return False
def Atom(name): """A basic QuickTime atom struct.""" return AtomAdapter( Con.Struct(name, Con.UBInt32("size"), Con.String("type", 4), Con.String("data", lambda ctx: ctx["size"] - 8)))