Esempio n. 1
0
 def __init__(self, fileobj):
     header = fileobj.read(76)
     if len(header) != 76 or not header.startswith(b'MAC '):
         raise MonkeysAudioHeaderError("not a Monkey's Audio file")
     self.version = cdata.ushort_le(header[4:6])
     if self.version >= 3980:
         (blocks_per_frame, final_frame_blocks, total_frames,
          self.bits_per_sample, self.channels,
          self.sample_rate) = struct.unpack("<IIIHHI", header[56:76])
     else:
         compression_level = cdata.ushort_le(header[6:8])
         self.channels, self.sample_rate = struct.unpack(
             "<HI", header[10:16])
         total_frames, final_frame_blocks = struct.unpack(
             "<II", header[24:32])
         if self.version >= 3950:
             blocks_per_frame = 73728 * 4
         elif self.version >= 3900 or (self.version >= 3800
                                       and compression_level == 4):
             blocks_per_frame = 73728
         else:
             blocks_per_frame = 9216
     self.version /= 1000.0
     self.length = 0.0
     if (self.sample_rate != 0) and (total_frames > 0):
         total_blocks = ((total_frames - 1) * blocks_per_frame +
                         final_frame_blocks)
         self.length = float(total_blocks) / self.sample_rate
Esempio n. 2
0
 def __init__(self, fileobj):
     header = fileobj.read(76)
     if len(header) != 76 or not header.startswith("MAC "):
         raise MonkeysAudioHeaderError("not a Monkey's Audio file")
     self.version = cdata.ushort_le(header[4:6])
     if self.version >= 3980:
         (blocks_per_frame, final_frame_blocks, total_frames,
          self.bits_per_sample, self.channels,
          self.sample_rate) = struct.unpack("<IIIHHI", header[56:76])
     else:
         compression_level = cdata.ushort_le(header[6:8])
         self.channels, self.sample_rate = struct.unpack(
             "<HI", header[10:16])
         total_frames, final_frame_blocks = struct.unpack(
             "<II", header[24:32])
         if self.version >= 3950:
             blocks_per_frame = 73728 * 4
         elif self.version >= 3900 or (self.version >= 3800 and
                                       compression_level == 4):
             blocks_per_frame = 73728
         else:
             blocks_per_frame = 9216
     self.version /= 1000.0
     self.length = 0.0
     if self.sample_rate != 0 and total_frames > 0:
         total_blocks = ((total_frames - 1) * blocks_per_frame +
                         final_frame_blocks)
         self.length = float(total_blocks) / self.sample_rate
Esempio n. 3
0
    def test_ushort(self):
        self.failUnlessEqual(cdata.ushort_le(self.ZERO(2)), 0)
        self.failUnlessEqual(cdata.ushort_le(self.LEONE(2)), 1)
        self.failUnlessEqual(cdata.ushort_le(self.BEONE(2)), 2 ** 16 >> 8)
        self.failUnlessEqual(cdata.ushort_le(self.NEGONE(2)), 65535)
        self.assertTrue(cdata.ushort_le is cdata.uint16_le)

        self.failUnlessEqual(cdata.ushort_be(self.ZERO(2)), 0)
        self.failUnlessEqual(cdata.ushort_be(self.LEONE(2)), 2 ** 16 >> 8)
        self.failUnlessEqual(cdata.ushort_be(self.BEONE(2)), 1)
        self.failUnlessEqual(cdata.ushort_be(self.NEGONE(2)), 65535)
        self.assertTrue(cdata.ushort_be is cdata.uint16_be)
Esempio n. 4
0
    def test_ushort(self):
        self.failUnlessEqual(cdata.ushort_le(self.ZERO(2)), 0)
        self.failUnlessEqual(cdata.ushort_le(self.LEONE(2)), 1)
        self.failUnlessEqual(cdata.ushort_le(self.BEONE(2)), 2**16 >> 8)
        self.failUnlessEqual(cdata.ushort_le(self.NEGONE(2)), 65535)
        self.assertTrue(cdata.ushort_le is cdata.uint16_le)

        self.failUnlessEqual(cdata.ushort_be(self.ZERO(2)), 0)
        self.failUnlessEqual(cdata.ushort_be(self.LEONE(2)), 2**16 >> 8)
        self.failUnlessEqual(cdata.ushort_be(self.BEONE(2)), 1)
        self.failUnlessEqual(cdata.ushort_be(self.NEGONE(2)), 65535)
        self.assertTrue(cdata.ushort_be is cdata.uint16_be)
Esempio n. 5
0
    def __init__(self, fileobj):
        header = bytearray(fileobj.read(32))
        if len(header) != 32:
            raise MusepackHeaderError("not a Musepack file")
        # Skip ID3v2 tags
        if header[:3] == b"ID3":
            size = 10 + BitPaddedInt(header[6:10])
            fileobj.seek(size)
            header = bytearray(fileobj.read(32))
            if len(header) != 32:
                raise MusepackHeaderError("not a Musepack file")
        # SV7
        if header.startswith(b"MP+"):
            self.version = header[3] & 0xF
            if self.version < 7:
                raise MusepackHeaderError("not a Musepack file")
            frames = cdata.uint_le(header[4:8])
            flags = cdata.uint_le(header[8:12])

            self.title_peak, self.title_gain = struct_unpack(
                "<Hh", header[12:16])
            self.album_peak, self.album_gain = struct_unpack(
                "<Hh", header[16:20])
            self.title_gain /= 100.0
            self.album_gain /= 100.0
            self.title_peak /= 65535.0
            self.album_peak /= 65535.0

            self.sample_rate = RATES[(flags >> 16) & 0x0003]
            self.bitrate = 0
        # SV4-SV6
        else:
            header_dword = cdata.uint_le(header[0:4])
            self.version = (header_dword >> 11) & 0x03FF;
            if self.version < 4 or self.version > 6:
                raise MusepackHeaderError("not a Musepack file")
            self.bitrate = (header_dword >> 23) & 0x01FF;
            self.sample_rate = 44100
            if self.version >= 5:
                frames = cdata.uint_le(header[4:8])
            else:
                frames = cdata.ushort_le(header[6:8])
            if self.version < 6:
                frames -= 1
        self.channels = 2
        self.length = float(frames * 1152 - 576) / self.sample_rate
        if not self.bitrate and self.length != 0:
            fileobj.seek(0, 2)
            self.bitrate = int(fileobj.tell() * 8 / (self.length * 1000) + 0.5)
Esempio n. 6
0
    def __init__(self, fileobj):
        header = bytearray(fileobj.read(32))
        if len(header) != 32:
            raise MusepackHeaderError("not a Musepack file")
        # Skip ID3v2 tags
        if header[:3] == b"ID3":
            size = 10 + BitPaddedInt(header[6:10])
            fileobj.seek(size)
            header = bytearray(fileobj.read(32))
            if len(header) != 32:
                raise MusepackHeaderError("not a Musepack file")
        # SV7
        if header.startswith(b"MP+"):
            self.version = header[3] & 0xF
            if self.version < 7:
                raise MusepackHeaderError("not a Musepack file")
            frames = cdata.uint_le(header[4:8])
            flags = cdata.uint_le(header[8:12])

            self.title_peak, self.title_gain = struct_unpack(
                "<Hh", header[12:16])
            self.album_peak, self.album_gain = struct_unpack(
                "<Hh", header[16:20])
            self.title_gain /= 100.0
            self.album_gain /= 100.0
            self.title_peak /= 65535.0
            self.album_peak /= 65535.0

            self.sample_rate = RATES[(flags >> 16) & 0x0003]
            self.bitrate = 0
        # SV4-SV6
        else:
            header_dword = cdata.uint_le(header[0:4])
            self.version = (header_dword >> 11) & 0x03FF
            if self.version < 4 or self.version > 6:
                raise MusepackHeaderError("not a Musepack file")
            self.bitrate = (header_dword >> 23) & 0x01FF
            self.sample_rate = 44100
            if self.version >= 5:
                frames = cdata.uint_le(header[4:8])
            else:
                frames = cdata.ushort_le(header[6:8])
            if self.version < 6:
                frames -= 1
        self.channels = 2
        self.length = float(frames * 1152 - 576) / self.sample_rate
        if not self.bitrate and self.length != 0:
            fileobj.seek(0, 2)
            self.bitrate = int(fileobj.tell() * 8 / (self.length * 1000) + 0.5)
Esempio n. 7
0
    def __parse_sv467(self, fileobj):
        fileobj.seek(-4, 1)
        header = fileobj.read(32)
        if len(header) != 32:
            raise MusepackHeaderError("not a Musepack file")

        # SV7
        if header.startswith(b"MP+"):
            self.version = bytearray(header)[3] & 0xF
            if self.version < 7:
                raise MusepackHeaderError("not a Musepack file")
            frames = cdata.uint_le(header[4:8])
            flags = cdata.uint_le(header[8:12])

            self.title_peak, self.title_gain = struct.unpack(
                "<Hh", header[12:16])
            self.album_peak, self.album_gain = struct.unpack(
                "<Hh", header[16:20])
            self.title_gain /= 100.0
            self.album_gain /= 100.0
            self.title_peak /= 65535.0
            self.album_peak /= 65535.0

            self.sample_rate = RATES[(flags >> 16) & 0x0003]
            self.bitrate = 0
        # SV4-SV6
        else:
            header_dword = cdata.uint_le(header[0:4])
            self.version = (header_dword >> 11) & 0x03FF
            if self.version < 4 or self.version > 6:
                raise MusepackHeaderError("not a Musepack file")
            self.bitrate = (header_dword >> 23) & 0x01FF
            self.sample_rate = 44100
            if self.version >= 5:
                frames = cdata.uint_le(header[4:8])
            else:
                frames = cdata.ushort_le(header[6:8])
            if self.version < 6:
                frames -= 1
        self.channels = 2
        self.length = float(frames * 1152 - 576) / self.sample_rate
Esempio n. 8
0
    def __parse_sv467(self, fileobj):
        fileobj.seek(-4, 1)
        header = fileobj.read(32)
        if len(header) != 32:
            raise MusepackHeaderError("not a Musepack file")

        # SV7
        if header.startswith(b"MP+"):
            self.version = bytearray(header)[3] & 0xF
            if self.version < 7:
                raise MusepackHeaderError("not a Musepack file")
            frames = cdata.uint_le(header[4:8])
            flags = cdata.uint_le(header[8:12])

            self.title_peak, self.title_gain = struct.unpack(
                "<Hh", header[12:16])
            self.album_peak, self.album_gain = struct.unpack(
                "<Hh", header[16:20])
            self.title_gain /= 100.0
            self.album_gain /= 100.0
            self.title_peak /= 65535.0
            self.album_peak /= 65535.0

            self.sample_rate = RATES[(flags >> 16) & 0x0003]
            self.bitrate = 0
        # SV4-SV6
        else:
            header_dword = cdata.uint_le(header[0:4])
            self.version = (header_dword >> 11) & 0x03FF
            if self.version < 4 or self.version > 6:
                raise MusepackHeaderError("not a Musepack file")
            self.bitrate = (header_dword >> 23) & 0x01FF
            self.sample_rate = 44100
            if self.version >= 5:
                frames = cdata.uint_le(header[4:8])
            else:
                frames = cdata.ushort_le(header[6:8])
            if self.version < 6:
                frames -= 1
        self.channels = 2
        self.length = float(frames * 1152 - 576) / self.sample_rate
Esempio n. 9
0
    def from_fileobj(cls, fileobj):
        """A new _WavPackHeader or raises WavPackHeaderError"""

        header = fileobj.read(32)
        if len(header) != 32 or not header.startswith(b"wvpk"):
            raise WavPackHeaderError("not a WavPack header: %r" % header)

        block_size = cdata.uint_le(header[4:8])
        version = cdata.ushort_le(header[8:10])
        track_no = ord(header[10:11])
        index_no = ord(header[11:12])
        samples = cdata.uint_le(header[12:16])
        if samples == 2**32 - 1:
            samples = -1
        block_index = cdata.uint_le(header[16:20])
        block_samples = cdata.uint_le(header[20:24])
        flags = cdata.uint_le(header[24:28])
        crc = cdata.uint_le(header[28:32])

        return _WavPackHeader(block_size, version, track_no, index_no, samples,
                              block_index, block_samples, flags, crc)
Esempio n. 10
0
    def from_fileobj(cls, fileobj):
        """A new _WavPackHeader or raises WavPackHeaderError"""

        header = fileobj.read(32)
        if len(header) != 32 or not header.startswith(b"wvpk"):
            raise WavPackHeaderError("not a WavPack header: %r" % header)

        block_size = cdata.uint_le(header[4:8])
        version = cdata.ushort_le(header[8:10])
        track_no = ord(header[10:11])
        index_no = ord(header[11:12])
        samples = cdata.uint_le(header[12:16])
        if samples == 2 ** 32 - 1:
            samples = -1
        block_index = cdata.uint_le(header[16:20])
        block_samples = cdata.uint_le(header[20:24])
        flags = cdata.uint_le(header[24:28])
        crc = cdata.uint_le(header[28:32])

        return _WavPackHeader(block_size, version, track_no, index_no,
                              samples, block_index, block_samples, flags, crc)