def load(self): data = self.fileobj.read(FormatChunk.CHUNK_SIZE) if len(data) != FormatChunk.CHUNK_SIZE: raise error("DSF chunk truncated") self.chunk_header = data[0:4] if self.chunk_header != b"fmt ": raise error("DSF fmt header not found") self.chunk_size = cdata.ulonglong_le(data[4:12]) if self.chunk_size != FormatChunk.CHUNK_SIZE: raise error("DSF dsd header size mismatch") self.format_version = cdata.uint_le(data[12:16]) if self.format_version != FormatChunk.VERSION: raise error("Unsupported format version") self.format_id = cdata.uint_le(data[16:20]) if self.format_id != FormatChunk.FORMAT_DSD_RAW: raise error("Unsupported format ID") self.channel_type = cdata.uint_le(data[20:24]) self.channel_num = cdata.uint_le(data[24:28]) self.sampling_frequency = cdata.uint_le(data[28:32]) self.bits_per_sample = cdata.uint_le(data[32:36]) self.sample_count = cdata.ulonglong_le(data[36:44])
def test_ulonglong(self): self.failUnlessEqual(cdata.ulonglong_le(self.ZERO(8)), 0) self.failUnlessEqual(cdata.ulonglong_le(self.LEONE(8)), 1) self.failUnlessEqual(cdata.longlong_le(self.BEONE(8)), 2 ** 64 >> 8) self.failUnlessEqual(cdata.ulonglong_le(self.NEGONE(8)), 2 ** 64 - 1) self.assertTrue(cdata.ulonglong_le is cdata.uint64_le) self.failUnlessEqual(cdata.ulonglong_be(self.ZERO(8)), 0) self.failUnlessEqual(cdata.ulonglong_be(self.LEONE(8)), 2 ** 64 >> 8) self.failUnlessEqual(cdata.longlong_be(self.BEONE(8)), 1) self.failUnlessEqual(cdata.ulonglong_be(self.NEGONE(8)), 2 ** 64 - 1) self.assertTrue(cdata.ulonglong_be is cdata.uint64_be)
def test_ulonglong(self): self.failUnlessEqual(cdata.ulonglong_le(self.ZERO(8)), 0) self.failUnlessEqual(cdata.ulonglong_le(self.LEONE(8)), 1) self.failUnlessEqual(cdata.longlong_le(self.BEONE(8)), 2**64 >> 8) self.failUnlessEqual(cdata.ulonglong_le(self.NEGONE(8)), 2**64 - 1) self.assertTrue(cdata.ulonglong_le is cdata.uint64_le) self.failUnlessEqual(cdata.ulonglong_be(self.ZERO(8)), 0) self.failUnlessEqual(cdata.ulonglong_be(self.LEONE(8)), 2**64 >> 8) self.failUnlessEqual(cdata.longlong_be(self.BEONE(8)), 1) self.failUnlessEqual(cdata.ulonglong_be(self.NEGONE(8)), 2**64 - 1) self.assertTrue(cdata.ulonglong_be is cdata.uint64_be)
def load(self): data = self.fileobj.read(DSDChunk.CHUNK_SIZE) self.chunk_header = data[0:4] if self.chunk_header != b"DSD ": raise error("DSF dsd header not found") self.chunk_size = cdata.ulonglong_le(data[4:12]) if self.chunk_size != DSDChunk.CHUNK_SIZE: raise error("DSF dsd header size mismatch") self.total_size = cdata.ulonglong_le(data[12:20]) self.offset_metdata_chunk = cdata.ulonglong_le(data[20:28])
def load(self): data = self.fileobj.read(DSDChunk.CHUNK_SIZE) if len(data) != DSDChunk.CHUNK_SIZE: raise error("DSF chunk truncated") self.chunk_header = data[0:4] if self.chunk_header != b"DSD ": raise error("DSF dsd header not found") self.chunk_size = cdata.ulonglong_le(data[4:12]) if self.chunk_size != DSDChunk.CHUNK_SIZE: raise error("DSF dsd header size mismatch") self.total_size = cdata.ulonglong_le(data[12:20]) self.offset_metdata_chunk = cdata.ulonglong_le(data[20:28])
def __init__(self, fileobj, offset=None): # Warning, offset ignored! fileobj.seek(0) dsd_header = fileobj.read(28) if len(dsd_header) != 28 or not dsd_header.startswith("DSD "): raise DSFHeaderError("DSF dsd header not found") self.file_size = cdata.ulonglong_le(dsd_header[12:20]) self.id3_location = cdata.ulonglong_le(dsd_header[20:28]) fmt_header = fileobj.read(52) if len(fmt_header) != 52 or not fmt_header.startswith("fmt "): raise DSFHeaderError("DSF fmt header not found") self.format_version = cdata.uint_le(fmt_header[12:16]) self.format_id = cdata.uint_le(fmt_header[16:20]) self.channel_type = cdata.uint_le(fmt_header[20:24]) self.channel_num = cdata.uint_le(fmt_header[24:28]) self.sample_rate = cdata.uint_le(fmt_header[28:32]) self.bits_per_sample = cdata.uint_le(fmt_header[32:36]) samples = cdata.ulonglong_le(fmt_header[36:44]) self.length = float(samples) / self.sample_rate
def load(self): data = self.fileobj.read(DataChunk.CHUNK_SIZE) self.chunk_header = data[0:4] if self.chunk_header != b"data": raise error("DSF data header not found") self.chunk_size = cdata.ulonglong_le(data[4:12]) if self.chunk_size < DataChunk.CHUNK_SIZE: raise error("DSF data header size mismatch")
def load(self): data = self.fileobj.read(DataChunk.CHUNK_SIZE) if len(data) != DataChunk.CHUNK_SIZE: raise error("DSF chunk truncated") self.chunk_header = data[0:4] if self.chunk_header != b"data": raise error("DSF data header not found") self.chunk_size = cdata.ulonglong_le(data[4:12]) if self.chunk_size < DataChunk.CHUNK_SIZE: raise error("DSF data header size mismatch")
def test_ulonglong_le(self): self.failUnlessEqual(cdata.ulonglong_le(self.ZERO * 2), 0) self.failUnlessEqual(cdata.ulonglong_le(self.LEONE + self.ZERO), 1) self.failUnlessEqual(cdata.ulonglong_le(self.NEGONE * 2), 2**64-1)
def test_ulonglong_le(self): self.failUnlessEqual(cdata.ulonglong_le(self.ZERO * 2), 0) self.failUnlessEqual(cdata.ulonglong_le(self.LEONE + self.ZERO), 1) self.failUnlessEqual(cdata.ulonglong_le(self.NEGONE * 2), 2**64 - 1)
return framedata = ''.join(framedata) framesize = len(framedata) if filename is None: filename = self.filename try: f = open(filename, 'rb+') except IOError, err: from errno import ENOENT if err.errno != ENOENT: raise f = open(filename, 'ab') # create, then reopen f = open(filename, 'rb+') try: # DSF stores the ID3 at the end of the filename dsd_header = f.read(28) id3_location = cdata.ulonglong_le(dsd_header[20:28]) if id3_location == 0 : # we can make a new entry at the end of the file f.seek(0,2) # go to the end of the file id3_location = f.tell() # this is where we are going to put the id3 f.seek(20) f.write(struct.pack('Q',id3_location)) # write the location outsize = (framesize + 1023) & ~0x3FF framedata += '\x00' * (outsize - framesize) framesize = BitPaddedInt.to_str(outsize, width=4) flags = 0 header = pack('>3sBBB4s', 'ID3', v2, 0, flags, framesize) data = header + framedata try: f.seek(id3_location) except EnvironmentError: raise err, None, stack
framedata = ''.join(framedata) framesize = len(framedata) if filename is None: filename = self.filename try: f = open(filename, 'rb+') except IOError, err: from errno import ENOENT if err.errno != ENOENT: raise f = open(filename, 'ab') # create, then reopen f = open(filename, 'rb+') try: # DSF stores the ID3 at the end of the filename dsd_header = f.read(28) id3_location = cdata.ulonglong_le(dsd_header[20:28]) if id3_location == 0: # we can make a new entry at the end of the file f.seek(0, 2) # go to the end of the file id3_location = f.tell( ) # this is where we are going to put the id3 f.seek(20) f.write(struct.pack('Q', id3_location)) # write the location outsize = (framesize + 1023) & ~0x3FF framedata += '\x00' * (outsize - framesize) framesize = BitPaddedInt.to_str(outsize, width=4) flags = 0 header = pack('>3sBBB4s', 'ID3', v2, 0, flags, framesize) data = header + framedata try: