def test_get_block(self):
     with open("./test_data/blocks/blocks_9_8", "rb") as f:
         data = f.read()
     c = Chunk(data)
     for x in range(32):
         for y in range(32):
             c.get_block(x, y)
Example #2
0
 def initfp(self, file):
     self._convert = None
     self._soundpos = 0
     self._file = Chunk(file, bigendian = 0)
     if self._file.getname() != b'RIFF':
         raise Error('file does not start with RIFF id')
     if self._file.read(4) != b'WAVE':
         raise Error('not a WAVE file')
     self._fmt_chunk_read = 0
     self._data_chunk = None
     while 1:
         self._data_seek_needed = 1
         try:
             chunk = Chunk(self._file, bigendian = 0)
         except EOFError:
             break
         chunkname = chunk.getname()
         if chunkname == b'fmt ':
             self._read_fmt_chunk(chunk)
             self._fmt_chunk_read = 1
         elif chunkname == b'data':
             if not self._fmt_chunk_read:
                 raise Error('data chunk before fmt chunk')
             self._data_chunk = chunk
             self._nframes = chunk.chunksize // self._framesize
             self._data_seek_needed = 0
             break
         chunk.skip()
     if not self._fmt_chunk_read or not self._data_chunk:
         raise Error('fmt chunk and/or data chunk missing')
Example #3
0
    def exitTl69asmLine(self, ctx: tl69asmParser.Tl69asmLineContext):
        if ctx.label():
            label_name = ctx.label().label_name
            self.label_dict[label_name] = self.get_line_number()

        if ctx.instruction():
            self.lines[-1].instructions.append(ctx.instruction().instruction)
        elif ctx.directive():
            directive = ctx.directive().directive
            value = None
            if directive.expr[0] == OperandType.LABEL:
                label_name = directive.expr[1]
                if label_name in self.label_dict:
                    value = self.label_dict[label_name]
                else:
                    print("TODO: proper label resolution")
                    return
            elif directive.expr[0] == OperandType.INTEGER:
                value = directive.expr[1]

            if not value:
                return

            if directive.directive_type == DirectiveType.ORG:
                if self.lines[-1].empty():
                    self.lines[-1] = Chunk(value)
                else:
                    self.lines.append(Chunk(value))
            elif directive.directive_type == DirectiveType.DW:
                dw_instruction = UnformedInstruction(
                    "DW", [Operand(OperandType.INTEGER, value)])
                self.lines[-1].instructions.append(dw_instruction)
            elif directive.directive_type == DirectiveType.EQU:
                self.label_dict[directive.label] = value
Example #4
0
def v3_score_par(profile, candidate, real_chunk_sizes, vehicle_used, roads):

    chunks = []
    cons = 0

    for i in range(0, len(profile) - 1):
        lcl_slope = ((profile[i+1] - profile[i])/(real_chunk_sizes[i+1] - real_chunk_sizes[i])) * 100
        #print("{:0.1f}-".format(lcl_slope), end='', flush=True)

        if i==0:
            chunks.append(Chunk(0, candidate[0][0], lcl_slope, (real_chunk_sizes[i+1] - real_chunk_sizes[i])))
        else:
            chunks.append(Chunk(chunks[-1].v1, candidate[0][i], lcl_slope, (real_chunk_sizes[i+1] - real_chunk_sizes[i])))

        d = (chunks[-1].v0**2) - (2*chunks[-1].accel*chunks[-1].space)
        time1 = ((-1 * chunks[-1].v0) - cmath.sqrt(d)) / chunks[-1].accel
        time2 = ((-1 * chunks[-1].v0) + cmath.sqrt(d)) / chunks[-1].accel

        #print("{},{},{} ".format(chunks[-1].v0, chunks[-1].accel, chunks[-1].space), end='')
        chunks[-1].v1 = math.sqrt(max(0,(chunks[-1].v0**2) + (2*chunks[-1].accel*chunks[-1].space)))
        chunks[-1].est_time_s = abs((chunks[-1].v1 - chunks[-1].v0) / chunks[-1].accel)

        chunks[-1].calculate_CPEM_kwh(vehicle_used)
        if chunks[-1].est_cons > 0:
            cons += chunks[-1].est_cons
        #print(" --> {}".format(chunks[-1].est_cons))

    #print("Cons (kWh): {}".format(cons/1000))
    if (not v2_check_valid_par(chunks, roads)):
        return -1

    return cons
Example #5
0
 def generate(self, x, z):
     chunk = Chunk()
     for y in range(64):
         for x in range(16):
             for z in range(16):
                 chunk.block(x, y, z)[0] = '\xff'
     return chunk
Example #6
0
 def __read_until(self, name):
     if self.__chunk is not None:
         self.__chunk.skip()
         self.__chunk = None
     while 1:
         try:
             chunk = Chunk(self.__file)
         except EOFError:
             raise audio.Error, 'chunk %s not found' % name
         else:
             self.__chunk = chunk
         chunkname = chunk.getname()
         if chunkname == 'COMM':
             self.__read_comm_chunk(chunk)
         elif chunkname == 'SSND':
             self.__read_ssnd_chunk(chunk)
         elif chunkname == 'FVER':
             self.__read_vfer_chunk(chunk)
         elif chunkname == 'MARK':
             self.__read_mark_chunk(chunk)
         elif chunkname in ('COMT', 'INST', 'MIDI', 'AESD', 'APPL', 'NAME',
                            'AUTH', '(c) ', 'ANNO'):
             self.__skip_chunk(chunk)
         else:
             raise audio.Error, 'unrecognized chunk type ' + chunkname
         if name == chunkname:
             return
         chunk.skip()
         self.__chunk = None
Example #7
0
    def create_chunks(self):
        _pos = self.pos
        _len = len
        _length = self.length
        chunks = []

        words = self.find_match_word(_pos)
        for w0 in words:
            index0 = _pos + _len(w0)
            if index0 < _length:
                words1 = self.find_match_word(index0)
                for w1 in words1:
                    index1 = index0 + _len(w1)
                    if index1 < _length:
                        words2 = self.find_match_word(index1)
                        for w2 in words2:
                            if w2.wtype == UNRECOGNIZED:
                                chunks.append(Chunk([w0, w1]))
                            else:
                                chunks.append(Chunk([w0, w1, w2]))
                    else:
                        chunks.append(Chunk([w0, w1]))
            else:
                chunks.append(Chunk([w0]))
        return chunks
Example #8
0
 def initfp(self, file):
     self._convert = None
     self._soundpos = 0
     self._file = Chunk(file, bigendian=0)
     if self._file.getname() != "RIFF":
         raise Error, "file does not start with RIFF id"
     if self._file.read(4) != "WAVE":
         raise Error, "not a WAVE file"
     self._fmt_chunk_read = 0
     self._data_chunk = None
     while 1:
         self._data_seek_needed = 1
         try:
             chunk = Chunk(self._file, bigendian=0)
         except EOFError:
             break
         chunkname = chunk.getname()
         if chunkname == "fmt ":
             self._read_fmt_chunk(chunk)
             self._fmt_chunk_read = 1
         elif chunkname == "data":
             if not self._fmt_chunk_read:
                 raise Error, "data chunk before fmt chunk"
             self._data_chunk = chunk
             self._nframes = chunk.chunksize // self._framesize
             self._data_seek_needed = 0
             break
         chunk.skip()
     if not self._fmt_chunk_read or not self._data_chunk:
         raise Error, "fmt chunk and/or data chunk missing"
Example #9
0
def parse():
    cabocha = CaboCha.Parser()
    result = []
    with open('neko.txt') as input_data:
        for line in input_data:
            line = line.strip()
            parsed = cabocha.parse(line).toString(CaboCha.FORMAT_LATTICE)
            chunks = {}
            for sentence_str in parsed.split('* '):
                sentence_analysis = sentence_str.split('\n')
                affliation_str = sentence_analysis.pop(0)
                if affliation_str in ['', 'EOS']:
                    continue
                morph_analysis = affliation_str.split(' ')
                chunk = Chunk()
                chunk.id = int(morph_analysis[0])
                chunk.srcs = int(morph_analysis[1][:-1])
                morphs = []
                for morph_str in sentence_analysis:
                    if morph_str in ['', 'EOS']:
                        continue
                    surface, right = morph_str.split('\t')
                    morph_items = right.split(',')
                    morphs.append(Morph(surface, morph_items[6],
                                        morph_items[0], morph_items[1]))
                chunk.morphs = morphs
                chunks[chunk.id] = chunk
            for i, chunk in chunks.items():
                if chunk.srcs > 0:
                    chunks[chunk.srcs].dst.append(i)
            result.append(chunks)
    return result
Example #10
0
 def get_chunk_in_pos(self,
                      pos: (),
                      addx=0,
                      addy=0,
                      addz=0):  # retourne le chunk à la position donnée
     tpos = (to_chunk(pos[0]) + addx, to_chunk(pos[1]) + addy,
             to_chunk(pos[2]) + addz)
     ret = self.chunks.get(tpos)
     if ret is None:
         world_file = open(DEFAULT_WORLD_FILE, "br")
         size = int.from_bytes(world_file.read(4), "big")
         chunks = world_file.read()[size * 16:]
         pos_ = (to_chunk(pos[0]) + addx, to_chunk(pos[1]) + addy,
                 to_chunk(pos[2]) + addz)
         if pos_ in list(self.chunks_indexes.keys()):
             chunk = chunks[self.chunks_indexes[pos_] *
                            BLOCKS_FOR_CHUNK:self.chunks_indexes[pos_] *
                            BLOCKS_FOR_CHUNK + BLOCKS_FOR_CHUNK]
             chunk_ = Chunk(pos_, self.noise, self)
             for block_ in range(len(chunk)):
                 chunk_.blocks[block_].type = BLOCKS_ID[chunk[block_]]
             ret = chunk_
     if ret is None:
         self.chunks[tpos] = Chunk(tpos, self.noise, self)
         self.chunks_indexes[tpos] = len(self.chunks_indexes) + 1
         chunk = self.chunks[tpos]
         chunk.gen_chunk()
         ret = chunk
     return ret
Example #11
0
class TestChunk(unittest.TestCase):
    """Tests for the chunk data structure"""

    def setUp(self):
        load_block_types("code/pynecraft/blocks.xml")
        self.chunk = Chunk(CatchAll(), None, 32, 0, 0, 0)

    def test_is_empty_block(self):
        for x in xrange(32):
            for y in xrange(32):
                for z in xrange(32):
                    self.assertTrue(self.chunk.is_empty_block(x, y, z))
        self.chunk.add_block_without_rebuild(BLOCK_TYPES.Wood, 5, 5, 5)
        self.assertFalse(self.chunk.is_empty_block(5, 5, 5))

    def test_valid_coordinates(self):
        size = self.chunk.size
        self.assertTrue(self.chunk.valid_coordinates(0, 0, 0))
        self.assertFalse(self.chunk.valid_coordinates(size, size, size))
        self.assertTrue(self.chunk.valid_coordinates(size - 1, size - 1, size - 1))

    def test_grid_coords_inside(self):
        size = self.chunk.size
        self.assertTrue(self.chunk.grid_coords_inside(0, 0, 0))
        self.assertFalse(self.chunk.grid_coords_inside(size, size, size))
        self.assertTrue(self.chunk.grid_coords_inside(size - 1, size - 1, size - 1))
Example #12
0
	def generate(self, x, z):
		chunk = Chunk()
		for y in range(64):
			for x in range(16):
				for z in range(16):
					chunk.block(x, y, z)[0] = '\xff'
		return chunk
Example #13
0
 def _create_chunk(self):
     """
     Instantiate chunk object for crawler to use to create headers and documents. Create chunk file to be writen to.
     :return: Chunk object
     """
     self.chunk = Chunk(self.chunk_id)
     self.chunk.create_chunk()
     return self.chunk
Example #14
0
    def test_init_chunk(self):
        chunk = Chunk()
        chunk.set_block(0, 0, 0, Block(config.BlockType.Grass))
        res = self.world_client.init_chunk(0, 0, chunk)
        self.assertFalse(res)

        res = self.world_client.init_chunk(0, 0, chunk)
        self.assertTrue(res)
    def parse_chunk_header(self, line):
        chunk = Chunk()

        # FIXME: If you delete these lines, chunks.morphs references the old list of Morph
        chunk.morphs = []
        chunk.srcs = []

        chunk.dst = int(line.split()[2][:-1])
        return chunk
Example #16
0
    def initfp(self, file):
        self._version = 0
        self._decomp = None
        self._convert = None
        self._markers = []
        self._soundpos = 0
        self._file = file
        chunk = Chunk(file)
        if chunk.getname() != 'FORM':
            raise Error, 'file does not start with FORM id'
        formdata = chunk.read(4)
        if formdata == 'AIFF':
            self._aifc = 0
        elif formdata == 'AIFC':
            self._aifc = 1
        else:
            raise Error, 'not an AIFF or AIFF-C file'
        self._comm_chunk_read = 0
        while 1:
            self._ssnd_seek_needed = 1
            try:
                chunk = Chunk(self._file)
            except EOFError:
                break

            chunkname = chunk.getname()
            if chunkname == 'COMM':
                self._read_comm_chunk(chunk)
                self._comm_chunk_read = 1
            elif chunkname == 'SSND':
                self._ssnd_chunk = chunk
                dummy = chunk.read(8)
                self._ssnd_seek_needed = 0
            elif chunkname == 'FVER':
                self._version = _read_ulong(chunk)
            elif chunkname == 'MARK':
                self._readmark(chunk)
            chunk.skip()

        if not self._comm_chunk_read or not self._ssnd_chunk:
            raise Error, 'COMM chunk and/or SSND chunk missing'
        if self._aifc and self._decomp:
            import cl
            params = [cl.ORIGINAL_FORMAT,
             0,
             cl.BITS_PER_COMPONENT,
             self._sampwidth * 8,
             cl.FRAME_RATE,
             self._framerate]
            if self._nchannels == 1:
                params[1] = cl.MONO
            elif self._nchannels == 2:
                params[1] = cl.STEREO_INTERLEAVED
            else:
                raise Error, 'cannot compress more than 2 channels'
            self._decomp.SetParams(params)
        return
Example #17
0
    def generate(cls, cx, cy, world_client):
        chunk = Chunk()
        column_heights = cls.column_heights(cx, cy)

        for bx, bz in chunk.all_columns():
            lowest = max(0, column_heights[bx, bz]-PerlinHeight.DEPTH)
            for by in range(lowest, column_heights[bx, bz]):
                chunk.set_block(bx, by, bz, Block(config.BlockType.Grass))
        return chunk
Example #18
0
File: iff.py Project: metrasynth/rv
def chunks(f):
    """Yield (name, data) chunks read from file f."""
    while True:
        try:
            c = Chunk(f, align=False, bigendian=False)
            yield (c.getname(), c.read())
            c.skip()
        except EOFError:
            break
Example #19
0
class waveread(wave.Wave_read):
    def initfp(self, file):
        self._convert = None
        self._soundpos = 0
        self._cue = []
        self._loops = []
        self._ieee = False
        self._file = Chunk(file, bigendian=0)
        if self._file.getname() != b'RIFF':
            raise exceptions_samplerbox.WaveReadError(
                'file does not start with RIFF id')
        if self._file.read(4) != b'WAVE':
            raise exceptions_samplerbox.WaveReadError('not a WAVE file')
        self._fmt_chunk_read = 0
        self._data_chunk = None
        while 1:
            self._data_seek_needed = 1
            try:
                chunk = Chunk(self._file, bigendian=0)
            except EOFError:
                break
            chunkname = chunk.getname()
            if chunkname == b'fmt ':
                self._read_fmt_chunk(chunk)
                self._fmt_chunk_read = 1
            elif chunkname == b'data':
                if not self._fmt_chunk_read:
                    raise exceptions_samplerbox.WaveReadError(
                        'data chunk before fmt chunk')
                self._data_chunk = chunk
                self._nframes = chunk.chunksize // self._framesize
                self._data_seek_needed = 0
            elif chunkname == b'cue ':
                numcue = struct.unpack('<i', chunk.read(4))[0]
                for i in range(numcue):
                    id, position, datachunkid, chunkstart, blockstart, sampleoffset = struct.unpack(
                        '<iiiiii', chunk.read(24))
                    self._cue.append(sampleoffset)
            elif chunkname == b'smpl':
                manuf, prod, sampleperiod, midiunitynote, midipitchfraction, smptefmt, smpteoffs, numsampleloops, samplerdata = struct.unpack(
                    '<iiiiiiiii', chunk.read(36))
                for i in range(numsampleloops):
                    cuepointid, type, start, end, fraction, playcount = struct.unpack(
                        '<iiiiii', chunk.read(24))
                    self._loops.append([start, end])
            chunk.skip()
        if not self._fmt_chunk_read or not self._data_chunk:
            raise exceptions_samplerbox.WaveReadError(
                'fmt chunk and/or data chunk missing')

    def getmarkers(self):
        return self._cue

    def getloops(self):
        if gv.sample_mode == gv.PLAYLIVE or gv.sample_mode == gv.PLAYLOOP or gv.sample_mode == gv.PLAYLO2X:
            return self._loops
 def test_read_and_export(self):
     with open("./test_data/blocks/blocks_9_8", "rb") as f:
         data = f.read()
     c = Chunk(data)
     r1 = repr(c)
     exported = c.export()
     f = io.BytesIO(exported)
     c2 = Chunk.from_compressed_file(f)
     r2 = repr(c2)
     assert r1 == r2
Example #21
0
	def generate_chunk (self, cx, cy, cz):
	
		res = Chunk ()
		if cy < 0:
			for z in range(16):
				for y in range (16):
					for x in range (16):
						res.put (x, y, z, 1)
		
		return res
Example #22
0
 def _read_wav_header(self):
     """ Read RIFF WAV header. """
     ch = Chunk(self.wav, bigendian=0)
     if ch.getname() != 'RIFF' or ch.read(4) != 'WAVE':
         logging.debug('Not a WAV file.')
         return False
     # this would normally be 0
     self.riff_pos = self.wav.tell() - 12
     riff_size = ch.getsize()
     self.sampwidth, self.nchannels, self.framerate = 0, 0, 0
     while True:
         try:
             chunk = Chunk(ch, bigendian=0)
         except EOFError:
             logging.debug('No data chunk found in WAV file.')
             return False
         chunkname = chunk.getname()
         if chunkname == 'fmt ':
             format_tag, self.nchannels, self.framerate, _, _ = struct.unpack('<HHLLH', chunk.read(14))
             if format_tag == 1:
                 sampwidth = struct.unpack('<H', chunk.read(2))[0]
                 self.sampwidth = (sampwidth + 7) // 8
             else:
                 logging.debug('WAV file not in uncompressed PCM format.')
                 return False
         elif chunkname == 'data':
             if not self.sampwidth:
                 logging.debug('Format chunk not found.')
                 return False
             self.data_pos = self.wav.tell() - 4
             #self.wav.read(4)
             self.start = self.wav.tell()
             return True
         chunk.skip()
Example #23
0
class waveread(wave.Wave_read):
    def initfp(self, file):
        self._convert = None
        self._soundpos = 0
        self._cue = []
        self._loops = []
        self._ieee = False
        self._file = Chunk(file, bigendian=0)
        if self._file.getname() != "RIFF":
            raise Error, "file does not start with RIFF id"
        if self._file.read(4) != "WAVE":
            raise Error, "not a WAVE file"
        self._fmt_chunk_read = 0
        self._data_chunk = None
        while 1:
            self._data_seek_needed = 1
            try:
                chunk = Chunk(self._file, bigendian=0)
            except EOFError:
                break
            chunkname = chunk.getname()
            if chunkname == "fmt ":
                self._read_fmt_chunk(chunk)
                self._fmt_chunk_read = 1
            elif chunkname == "data":
                if not self._fmt_chunk_read:
                    raise Error, "data chunk before fmt chunk"
                self._data_chunk = chunk
                self._nframes = chunk.chunksize // self._framesize
                self._data_seek_needed = 0
            elif chunkname == "cue ":
                numcue = struct.unpack("<i", chunk.read(4))[0]
                for i in range(numcue):
                    id, position, datachunkid, chunkstart, blockstart, sampleoffset = struct.unpack(
                        "<iiiiii", chunk.read(24)
                    )
                    self._cue.append(sampleoffset)
            elif chunkname == "smpl":
                manuf, prod, sampleperiod, midiunitynote, midipitchfraction, smptefmt, smpteoffs, numsampleloops, samplerdata = struct.unpack(
                    "<iiiiiiiii", chunk.read(36)
                )
                for i in range(numsampleloops):
                    cuepointid, type, start, end, fraction, playcount = struct.unpack("<iiiiii", chunk.read(24))
                    self._loops.append([start, end])
            chunk.skip()
        if not self._fmt_chunk_read or not self._data_chunk:
            raise Error, "fmt chunk and/or data chunk missing"

    def getmarkers(self):
        return self._cue

    def getloops(self):
        return self._loops
Example #24
0
def parse_quetzal(fp) -> HeaderData:
    """Reads a Quetzal save file, and returns information about the associated game."""
    if type(fp) != str and not hasattr(fp, 'read'):
        raise TypeError("file is not a string or a bytes-like object.")

    if type(fp) == str:
        if not os.path.isfile(fp):
            raise Exception("File provided isn't a file, or doesn't exist.")

        # Open file as bytes
        qzl = open(fp, 'rb')
    else:
        qzl = fp

    header = qzl.read(4)

    # Header checking yay.
    if header != b"FORM":
        raise Exception("Invalid file format.")

    qzl.read(4) # Skip some bytes we don't care about.

    ftype = qzl.read(4)

    # More header checking yay.
    if ftype != b"IFZS":
        raise Exception("Invalid file format.")

    chunk = Chunk(qzl)
    name = chunk.getname()
    size = chunk.getsize()
    data = chunk.read(size).decode("latin_1")

    # Make sure first chunk is IFhd.
    if name != b"IFhd":
        raise Exception("File does not start with an IFhd chunk.")
    elif size != 13:
        raise Exception("Invalid size for IFhd chunk: " + str(size))

    try:
        # Bitwise magic to get data.
        release = (ord(data[0]) << 8) + ord(data[1])
        serial = int(data[2:8])
        checksum = (ord(data[8]) << 8) + ord(data[9])
        # pc = (ord(data[10]) << 16) + (ord(data[11]) << 8) + ord(data[12]) # This isn't needed rn, but it's commented just in case.
    except ValueError as e:
        print(data)
        print(release)
        print(data[2:8])
        print((ord(data[8]) << 8) + ord(data[9]))
        raise e

    return HeaderData(release, serial, checksum)
Example #25
0
    def test_is_generated(self):
        res = self.world_client.is_generated(0, 0)
        self.assertFalse(
            res,
            "World Client says an ungenerated chunk is already generated.")

        chunk = Chunk()
        chunk.set_block(0, 0, 0, Block(config.BlockType.Grass))
        res = self.world_client.init_chunk(0, 0, chunk)
        self.assertFalse(res, "Couldn't init the chunk.")

        res = self.world_client.is_generated(0, 0)
        self.assertTrue(res)
Example #26
0
    def test_setget_chunk(self):
        # Create a chunk with a non-default blocks
        chunk = Chunk()
        chunk.set_block(0, 0, 0, Block(config.BlockType.Grass))

        # Ensure that the block the server sets the chunk without error.
        res = self.world_client.set_chunk(0, 0, chunk)
        self.assertFalse(res)

        # Ensure the chunk it gives back to us is the same one we but in.
        res = self.world_client.get_chunk(0,
                                          0)[config.WorldRequestData.ChunkData]
        self.assertIsNotNone(res, "get_chunk failed")
        self.assertEqual(chunk, res)
Example #27
0
 def _read_header(self, f: BinaryIO) -> None:
     """ Read Bank Header (BKHD) chunk. """
     chunk = Chunk(f, bigendian=False, align=False)
     if chunk.getname() != b"BKHD":
         raise exception.AudioKineticFormatError(
             "Not an AudioKinetic WWise SoundBank file.")
     self._soundbank.version = self._read_uint32(chunk)
     self._soundbank.id = self._read_uint32(chunk)
     # There are additional 6 unknown uint32 values.
     chunk.skip()
     # Get name from SoundBankInfo
     info = self.soundbankinfo.get(self._soundbank.id)
     self._soundbank.name = info.name if isinstance(
         info, soundbanksinfo.SoundBankInfo) else ""
Example #28
0
 def initfp(self, file):
     self._convert = None
     self._soundpos = 0
     self._file = Chunk(file, bigendian=0)
     if self._file.getname() != b'RIFF':
         raise Error('file does not start with RIFF id')
     if self._file.read(4) != b'WAVE':
         raise Error('not a WAVE file')
     self._fmt_chunk_read = 0
     self._data_chunk = None
     while 1:
         self._data_seek_needed = 1
         try:
             chunk = Chunk(self._file, bigendian=0)
         except EOFError:
             break
         chunkname = chunk.getname()
         if chunkname == b'fmt ':
             self._read_fmt_chunk(chunk)
             self._fmt_chunk_read = 1
         elif chunkname == b'data':
             if not self._fmt_chunk_read:
                 raise Error('data chunk before fmt chunk')
             self._data_chunk = chunk
             self._nframes = chunk.chunksize // self._framesize
             self._data_seek_needed = 0
             break
         else:
             self.other_chunks.append(chunk.read())
         chunk.skip()
     if not self._fmt_chunk_read or not self._data_chunk:
         raise Error('fmt chunk and/or data chunk missing')
Example #29
0
File: iff.py Project: qznc/ik
def read_iff(fh,keys=["FORM"]):
   while True:
      try:
         ch = Chunk(fh, align=False)
      except EOFError:
         return
      name = ch.getname()
      if name == "FORM":
         typ = ch.read(4) # TODO return somehow
         for x in read_iff(ch):
            yield x
      else:
         data = ch.read(ch.getsize())
         yield name, data
Example #30
0
 def _read_data_index(self, chunk: Chunk) -> None:
     """ Read Data Index (DIDX) chunk."""
     num_objects = chunk.getsize() // 12
     for i in range(num_objects):
         try:
             file_id, offset, length = struct.unpack("<LLL", chunk.read(12))
         except struct.error as e:
             raise exception.AudioKineticIOError(
                 "Failed to read Data Index (DIDX) chunk.") from e
         assert file_id
         assert length
         assert file_id not in self._soundbank.objs
         self._soundbank.objs[file_id] = File(self._soundbank.id, file_id,
                                              offset, length)
Example #31
0
class Request:
    '''Detect HTTP request header fields'''
    def __init__(self, parser: Parser):
        self.parser = parser
        self.buf = Chunk()
        self.start_parsed = False
        self.headers_parsed = False
        self.body_unparsed = 0

    def push(self, data: bytes):
        '''Push received data at the end of buffer'''
        self.buf.push(data)
        self.parse()

    def parse(self):
        '''Parse so far received buffer'''
        if not self.start_parsed:
            self.parse_start()
        elif not self.headers_parsed:
            self.parse_headers()
        elif self.body_unparsed:
            data = self.buf.pop_all()
            self.body_unparsed -= len(data)
            self.parser.on_body(data)
            self.parse()
        else:
            self.parser.on_finished()

    def parse_start(self):
        '''Parse the starting line of the HTTP request'''
        line = self.buf.pop(separator=b"\r\n")
        if line is not None:
            method, url, version = line.strip().split()
            self.start_parsed = True
            self.parser.on_method(method, url, version)
            self.parse()

    def parse_headers(self):
        '''Parse the subsequent header name-value pairs of the HTTP request'''
        line = self.buf.pop(separator=b"\r\n")
        if line is not None:
            if line:
                name, value = line.strip().split(b": ", maxsplit=1)
                if name.lower() == b"content-length":
                    self.body_unparsed = int(value.decode("utf-8"))
                self.parser.on_header(name, value)
            else:
                self.headers_parsed = True
            self.parse()
Example #32
0
 def __init__(self, size, chunks_no, name, IP, port, peer):
     self.size = size
     self.chunk_total = chunks_no
     self.file_name = name
     self.owners.append(peer)
     for i in range(chunks_no):
         self.chunks.append(Chunk(i, IP, port))
Example #33
0
def threaded_update():
    while executing:

        player_pos_chunk = utils.player_to_chunk(player.position)
        for j in range(
                int(player_pos_chunk.z) - render_distance,
                int(player_pos_chunk.z) + render_distance):
            for i in range(
                    int(player_pos_chunk.x) - render_distance,
                    int(player_pos_chunk.x) + render_distance):
                if not (Vec3(i, 0, j) in world):
                    world.update(
                        {Vec3(i, 0, j): Chunk(position=Vec3(i, 0, j))})
                    #print("Generating new chunk in {}, {}, {}".format(i,0,j))

        for vec in world:
            if utils.vec2dist(
                    utils.player_to_chunk(player.position).xz,
                    vec.xz) > render_distance + 1:
                world[vec].destroy()
                todelete.append(vec)
            else:
                world[vec].compute_mesh()
                #update_queue.put(world[vec])

        for vec in todelete:
            del world[vec]
        todelete.clear()

        time.sleep(0.5)
Example #34
0
class Compiler():
    
    chunk = Chunk()
    scope_depth = 0

    global_variables = []
    local_variables  = []
Example #35
0
    def payload(self):
        c = Chunk(self.mem,self.argv,self.envp)
        print "Type\tOffset  VirtAddr  PsychAddr  FileSz   MemSz   Flg   Align"
        for ph in self.elf.enum_phdr():
            print ph
            if ph.p_type in  [ph.PT_LOAD,ph.PT_GNU_STACK] :
                c.append(ph)

        c.append(self.elf)

        for a in c.chunks:
            print hex(len(a))


#        needle = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'
        return self.loader() + ''.join(c.chunks)
Example #36
0
 def get_block(self, x, y):
     assert 0 <= x < (self._data["world_db"]["main"] \
         ["worldv2"]["worldWidthMacro"] << 5) and 0 <= y < 1024
     name = "%d_%d" % (x >> 5, y >> 5)
     if not isinstance(self.chunks[name], Chunk):
         self.chunks[name] = Chunk(self.chunks[name]._data[0])
     return self.chunks[name].get_block(x & 31, y & 31)
Example #37
0
 def get_chunk(self, x, y):
     assert 0 <= x < self._data["world_db"]["main"] \
         ["worldv2"]["worldWidthMacro"] and 0 <= y < 32
     name = "%d_%d" % (x, y)
     if not isinstance(self.chunks[name], Chunk):
         self.chunks[name] = Chunk(self.chunks[name]._data[0])
     return self.chunks[name]
Example #38
0
def dump(node):
    debugcnt = 0
    sentence_id = -1
    for c in node:
        if c.tag == "sentence":
            sentence = []
            sentence_id += 1
            chunk_dict = {} # chunkは1sentenceに複数
            token_dict = {} #tokenは1chunkに複数
            chunk_iter = c.getiterator('chunk')
            for chunk in chunk_iter:
                morphs = []
                tok_iter = chunk.getiterator('tok')
                chunk_dict['id'] = chunk.get("id")
                chunk_dict['link'] = chunk.get("link")
                chunk_dict['rel'] = chunk.get("rel")
                chunk_dict['score'] = chunk.get("score")
                chunk_dict['head'] = chunk.get("head")
                chunk_dict['func'] = chunk.get("func")
                for tok in tok_iter:
                    tok_id = tok.get('id')
                    tok_feature = tok.get('feature')
                    morph = Morph(sentence_id, chunk_dict['id'], tok_id, tok_feature, tok.text)
                    morphs.append(morph)
                    tok_content = tok.text
                a_chunk = Chunk(sentence_id,chunk_dict['id'], chunk_dict['link'], chunk_dict['rel'], \
                chunk_dict['score'], chunk_dict['head'], chunk_dict['func'], morphs)
                # chuncks.append(a_chunk)
                sentence.append(a_chunk)
            sents_list.append(sentence)
        dump(c)
Example #39
0
    def __init__(self, parent_log):
        """parent_log: The logging.getLogger() object that will be the parent for this log."""
        super(WorldDataServer, self).__init__()

        self.__running__ = mp.Value('b', True)
        self.__main_pipe_pub__, self.__main_pipe__ = mp.Pipe(True)
        self.__chunks__ = defaultdict(lambda: Chunk())
        self.__connections__ = {
            self.__main_pipe__: config.WorldDataServer.MainConnectionName,
        }
        self.parent_log = parent_log

        #self.log = logging.getLogger('WorldDataServer')
        self.log = parent_log.getChild('WorldDataServer')
        self.log.setLevel(config.WorldDataServer.LogLevel)

        #self.__handler__ = logging.StreamHandler()

        # DON'T DELETE ME. This is the format I want for the main log.
        #self.__formatter__ = logging.Formatter('%(asctime)s %(processName)-6s %(filename)s:%(funcName)s[%(lineno)s] %(levelname)-8s %(message)s')

        #self.__handler__.setFormatter(self.__formatter__)
        #self.log.addHandler(self.__handler__)
        #self.log.setLevel(log_level)
        self.log.info("WorldDataServer is ready.")
Example #40
0
 def __init__(self, save_file, machine):
     f = open(save_file, 'rb')
     form = Chunk(f)
     self.machine = machine
     if form.read(4) != 'IFZS':
         raise QuetzalError, "File is not a quetzal save file!"
     
     # Read the thing
     while True:
         try:
             chunk = Chunk(form)
         except EOFError:
             break
         function = "chunk_%s" % chunk.getname().strip()
         if hasattr(self, function):
             self.__getattribute__(function)(chunk)
Example #41
0
 def __init__(self, file):
     if type(file) == type(''):
         self.__filename = file  # only needed for __repr__
         self.__file = file = open(file, 'rb')
     else:
         self.__filename = '<unknown filename>'
         self.__file = file
     # initialization
     self.__chunk = None  # the chunk currently being read
     self.__format = None
     self.__ssnd_chunk = None
     self.__version = None
     self.__markers = None
     self.__soundpos = 0
     self.__framesread = 0
     # start parsing
     self.__file = file = Chunk(file)
     if file.getname() != 'FORM':
         raise audio.Error, 'file does not start with FORM id'
     formdata = file.read(4)
     if formdata == 'AIFF':
         self.__aifc = 0
     elif formdata == 'AIFC':
         self.__aifc = 1
     else:
         raise audio.Error, 'not an AIFF or AIFF-C file'
Example #42
0
    def initfp(self, file):
        self._version = 0
        self._decomp = None
        self._convert = None
        self._markers = []
        self._soundpos = 0
        self._file = file
        chunk = Chunk(file)
        if chunk.getname() != 'FORM':
            raise Error, 'file does not start with FORM id'
        formdata = chunk.read(4)
        if formdata == 'AIFF':
            self._aifc = 0
        elif formdata == 'AIFC':
            self._aifc = 1
        else:
            raise Error, 'not an AIFF or AIFF-C file'
        self._comm_chunk_read = 0
        while 1:
            self._ssnd_seek_needed = 1
            try:
                chunk = Chunk(self._file)
            except EOFError:
                break

            chunkname = chunk.getname()
            if chunkname == 'COMM':
                self._read_comm_chunk(chunk)
                self._comm_chunk_read = 1
            elif chunkname == 'SSND':
                self._ssnd_chunk = chunk
                dummy = chunk.read(8)
                self._ssnd_seek_needed = 0
            elif chunkname == 'FVER':
                self._version = _read_ulong(chunk)
            elif chunkname == 'MARK':
                self._readmark(chunk)
            chunk.skip()

        if not self._comm_chunk_read or not self._ssnd_chunk:
            raise Error, 'COMM chunk and/or SSND chunk missing'
        if self._aifc and self._decomp:
            import cl
            params = [cl.ORIGINAL_FORMAT,
             0,
             cl.BITS_PER_COMPONENT,
             self._sampwidth * 8,
             cl.FRAME_RATE,
             self._framerate]
            if self._nchannels == 1:
                params[1] = cl.MONO
            elif self._nchannels == 2:
                params[1] = cl.STEREO_INTERLEAVED
            else:
                raise Error, 'cannot compress more than 2 channels'
            self._decomp.SetParams(params)
        return
Example #43
0
    def loadFile(self, filename):
        with open(filename) as iff:
            chunk = Chunk(iff)

            logging.info('Reading file "%s"' % filename)

            if chunk.getname() == "FORM" and chunk.read(4) == self._kind:
                iff.seek(12)

                while True:
                    try:
                        chunk = Chunk(iff)
                    except EOFError:
                        break

                    name = chunk.getname()
                    size = chunk.getsize()
                    data = chunk.read()

                    logging.debug("Encountered %s chunk of size %d" % (name, size))

                    self._chunks.append(self._parseChunk(name, data))
            else:
                logging.error("File %s is not of IFF/%s type." % (filename, self._kind))
                return False

        return True
Example #44
0
  def load(self, filename):
    self.chunks = []

    with open(filename) as iff:
      chunk = Chunk(iff)

      logging.info('Reading file "%s" as IFF/%s type.' % (filename, self.form))

      if chunk.getname() == 'FORM' and chunk.read(4) == self.form:
        iff.seek(12)

        while True:
          try:
            chunk = Chunk(iff)
          except EOFError:
            break

          name = chunk.getname()
          size = chunk.getsize()
          data = chunk.read()

          if name in self.ChunkBlackList:
            logging.info('Ignoring %s chunk of size %d' % (name, size))
          else:
            logging.debug('Encountered %s chunk of size %d' % (name, size))

            self.chunks.append(self.readChunk(name, data))
      else:
        logging.warn(
          'File %s is not of IFF/%s type.' % (filename, self.form))
        return False

    return True
Example #45
0
    def readChunks(self, file):
        file.seek(self.offset)
        
        self.chunks = {}
        
        done = False
        # Marathon maps are made of many types of 'chunks'
        # Lines, points, sides, mission info, etc
        while not done:            
            chunk = Chunk()
            chunk.read(file)

            file.seek(chunk.next + self.offset)
            
            if chunk.next == 0:
                done = True
            
            self.chunks[chunk.tag] = chunk
            
Example #46
0
	def parse(self, filename):
		book = []
		sentence = []
		chunk = Chunk()
		srcs = {}

		cnt_eos = 0
		for line in codecs.open(filename, "r"):
			# 文節の始まり
			if line[0] == '*':
				# print line
				column = line.split(" ")
				chunk_id = str(column[1])

				if chunk_id != '0':
					sentence.append(chunk)

				# 係り先
				dst = column[2].replace("D", "")
				if dst != '-1':
					if srcs.has_key(dst):
						srcs[dst].append(chunk_id)
					else:
						srcs[dst] = [chunk_id]

				# Chunkオブジェクト生成
				chunk = Chunk()
				chunk.chunk_id = chunk_id
				chunk.dst = dst
				chunk.srcs = srcs.get(chunk_id)

				continue

			# 一文の終わりの場合
			if line.strip() == 'EOS':
				cnt_eos += 1
				if len(sentence) > 0:
					sentence.append(chunk)
					book.append(sentence)
					# print chunk
					sentence = []
					srcs = {}

				continue

			# 文の開始または途中の場合
			column = re.split("[\t|,]", line)
			morpheme = Morph(column[0], column[7], column[1], column[2])
			chunk.addMorph(morpheme)

		self.parsed = book
Example #47
0
def build_chunk(Schema,chunk_id,cellcount,chunksize):
  chosen = []
  coords = []   # coordinates of nonempty cells
  attrvals = [] # attribute values of nonempty cells
  chunk = Chunk(Schema,chunk_id,cellcount,[],[]) # empty chunk for now

  if chunksize == 1:
    return [0]
  candidates = range(chunksize)
  if cellcount == chunksize:
    return candidates
  chosen = choose_randk(candidates,cellcount)
  #generate dimension coordinates
  for i in chosen:
    coords.append(Schema.compute_dimid(chunk_id,i))
  chunk.coordinates = coords
  #generate attribute values
  attrvals = generate_attributes(Schema.attributes,cellcount)
  chunk.attributes = attrvals
  #print "chosen:",chosen
  #print "dimensions for cell",chosen[0],"of chunk",chunk_id,":",Schema.compute_dimid(chunk_id,chosen[0])
  #print "coords for chunk",chunk_id,":",coords
  #print "attributes for chunk",chunk_id,":",attrvals
  return chunk
Example #48
0
 def _read_wav_header(self):
     """ Read RIFF WAV header. """
     try:
         ch = Chunk(self.wav, bigendian=0)
     except (EOFError):
         logging.debug("WAV file is corrupted.")
         return False
     if ch.getname() != "RIFF" or ch.read(4) != "WAVE":
         logging.debug("Not a WAV file.")
         return False
     # this would normally be 0
     self.riff_pos = self.wav.tell() - 12
     riff_size = ch.getsize()
     self.sampwidth, self.nchannels, self.framerate = 0, 0, 0
     while True:
         try:
             chunk = Chunk(ch, bigendian=0)
         except EOFError:
             logging.debug("No data chunk found in WAV file.")
             return False
         chunkname = chunk.getname()
         if chunkname == "fmt ":
             format_tag, self.nchannels, self.framerate, _, _ = struct.unpack("<HHLLH", chunk.read(14))
             if format_tag == 1:
                 sampwidth = struct.unpack("<H", chunk.read(2))[0]
                 self.sampwidth = (sampwidth + 7) // 8
             else:
                 logging.debug("WAV file not in uncompressed PCM format.")
                 return False
         elif chunkname == "data":
             if not self.sampwidth:
                 logging.debug("Format chunk not found.")
                 return False
             self.data_pos = self.wav.tell() - 4
             # self.wav.read(4)
             self.start = self.wav.tell()
             return True
         chunk.skip()
Example #49
0
    def __init__(self, path):
        super(DownmixedWavFile, self).__init__()
        self._file = None
        self._file = open(path, 'rb')
        try:
            riff = Chunk(self._file, bigendian=False)
            if riff.getname() != 'RIFF':
                raise SushiError('File does not start with RIFF id')
            if riff.read(4) != 'WAVE':
                raise SushiError('Not a WAVE file')

            fmt_chunk_read = False
            data_chink_read = False
            file_size = os.path.getsize(path)

            while True:
                try:
                    chunk = Chunk(self._file, bigendian=False)
                except EOFError:
                    break

                if chunk.getname() == 'fmt ':
                    self._read_fmt_chunk(chunk)
                    fmt_chunk_read = True
                elif chunk.getname() == 'data':
                    if file_size > 0xFFFFFFFF:
                        # large broken wav
                        self.frames_count = (file_size - self._file.tell()) // self.frame_size
                    else:
                        self.frames_count = chunk.chunksize // self.frame_size
                    data_chink_read = True
                    break
                chunk.skip()
            if not fmt_chunk_read or not data_chink_read:
                raise SushiError('Invalid WAV file')
        except:
            if self._file:
                self._file.close()
            raise
Example #50
0
 def initfp(self, file):
     self._version = 0
     self._convert = None
     self._markers = []
     self._soundpos = 0
     self._file = file
     chunk = Chunk(file)
     if chunk.getname() != b'FORM':
         raise Error('file does not start with FORM id')
     formdata = chunk.read(4)
     if formdata == b'AIFF':
         self._aifc = 0
     elif formdata == b'AIFC':
         self._aifc = 1
     else:
         raise Error('not an AIFF or AIFF-C file')
     self._comm_chunk_read = 0
     self._ssnd_chunk = None
     while 1:
         self._ssnd_seek_needed = 1
         try:
             chunk = Chunk(self._file)
         except EOFError:
             break
         chunkname = chunk.getname()
         if chunkname == b'COMM':
             self._read_comm_chunk(chunk)
             self._comm_chunk_read = 1
         elif chunkname == b'SSND':
             self._ssnd_chunk = chunk
             dummy = chunk.read(8)
             self._ssnd_seek_needed = 0
         elif chunkname == b'FVER':
             self._version = _read_ulong(chunk)
         elif chunkname == b'MARK':
             self._readmark(chunk)
         chunk.skip()
     if not self._comm_chunk_read or not self._ssnd_chunk:
         raise Error('COMM chunk and/or SSND chunk missing')
Example #51
0
 def initfp(self, file):
     self._convert = None
     self._soundpos = 0
     self._cue = []
     self._loops = []
     self._ieee = False
     self._file = Chunk(file, bigendian=0)
     if self._file.getname() != 'RIFF':
         raise Error, 'file does not start with RIFF id'
     if self._file.read(4) != 'WAVE':
         raise Error, 'not a WAVE file'
     self._fmt_chunk_read = 0
     self._data_chunk = None
     while 1:
         self._data_seek_needed = 1
         try:
             chunk = Chunk(self._file, bigendian=0)
         except EOFError:
             break
         chunkname = chunk.getname()
         if chunkname == 'fmt ':
             self._read_fmt_chunk(chunk)
             self._fmt_chunk_read = 1
         elif chunkname == 'data':
             if not self._fmt_chunk_read:
                 raise Error, 'data chunk before fmt chunk'
             self._data_chunk = chunk
             self._nframes = chunk.chunksize // self._framesize
             self._data_seek_needed = 0
         elif chunkname == 'cue ':
             numcue = struct.unpack('<i', chunk.read(4))[0]
             for i in range(numcue):
                 id, position, datachunkid, chunkstart, blockstart, sampleoffset = struct.unpack('<iiiiii', chunk.read(24))
                 self._cue.append(sampleoffset)
         elif chunkname == 'smpl':
             manuf, prod, sampleperiod, midiunitynote, midipitchfraction, smptefmt, smpteoffs, numsampleloops, samplerdata = struct.unpack(
                 '<iiiiiiiii', chunk.read(36))
             for i in range(numsampleloops):
                 cuepointid, type, start, end, fraction, playcount = struct.unpack('<iiiiii', chunk.read(24))
                 self._loops.append([start, end])
         chunk.skip()
     if not self._fmt_chunk_read or not self._data_chunk:
         raise Error, 'fmt chunk and/or data chunk missing'
Example #52
0
 def initfp(self, file):
     self._version = 0
     self._convert = None
     self._markers = []
     self._soundpos = 0
     self._file = file
     chunk = Chunk(file)
     if chunk.getname() != b"FORM":
         raise Error("file does not start with FORM id")
     formdata = chunk.read(4)
     if formdata == b"AIFF":
         self._aifc = 0
     elif formdata == b"AIFC":
         self._aifc = 1
     else:
         raise Error("not an AIFF or AIFF-C file")
     self._comm_chunk_read = 0
     while 1:
         self._ssnd_seek_needed = 1
         try:
             chunk = Chunk(self._file)
         except EOFError:
             break
         chunkname = chunk.getname()
         if chunkname == b"COMM":
             self._read_comm_chunk(chunk)
             self._comm_chunk_read = 1
         elif chunkname == b"SSND":
             self._ssnd_chunk = chunk
             dummy = chunk.read(8)
             self._ssnd_seek_needed = 0
         elif chunkname == b"FVER":
             self._version = _read_ulong(chunk)
         elif chunkname == b"MARK":
             self._readmark(chunk)
         chunk.skip()
     if not self._comm_chunk_read or not self._ssnd_chunk:
         raise Error("COMM chunk and/or SSND chunk missing")
Example #53
0
 def parseFromFile(cls, iFile, nodeName = None):
     # parse a file generating an SF2 tree structure
     
     # read stuff from file at the current position
     aChunk = Chunk(iFile, align=True, bigendian=False)
     ckID = aChunk.getname()
     ckSize = aChunk.getsize()
     ckData = aChunk.read()
     aChunk.close()
     
     # let's create a node from this data
     node = SF2Parser.createNode(nodeName, ckID, ckSize, ckData)
     
     # depending on the type of node, children could be inside of it
     SF2Parser.parseNode(node)
     
     # return the root node
     return node
Example #54
0
 def setUp(self):
     load_block_types("code/pynecraft/blocks.xml")
     self.chunk = Chunk(CatchAll(), None, 32, 0, 0, 0)
Example #55
0
"""Stuff to parse WAVE files.
Example #56
0
class Aifc_read:
    # Variables used in this class:
    #
    # These variables are available to the user though appropriate
    # methods of this class:
    # _file -- the open file with methods read(), close(), and seek()
    #       set through the __init__() method
    # _nchannels -- the number of audio channels
    #       available through the getnchannels() method
    # _nframes -- the number of audio frames
    #       available through the getnframes() method
    # _sampwidth -- the number of bytes per audio sample
    #       available through the getsampwidth() method
    # _framerate -- the sampling frequency
    #       available through the getframerate() method
    # _comptype -- the AIFF-C compression type ('NONE' if AIFF)
    #       available through the getcomptype() method
    # _compname -- the human-readable AIFF-C compression type
    #       available through the getcomptype() method
    # _markers -- the marks in the audio file
    #       available through the getmarkers() and getmark()
    #       methods
    # _soundpos -- the position in the audio stream
    #       available through the tell() method, set through the
    #       setpos() method
    #
    # These variables are used internally only:
    # _version -- the AIFF-C version number
    # _decomp -- the decompressor from builtin module cl
    # _comm_chunk_read -- 1 iff the COMM chunk has been read
    # _aifc -- 1 iff reading an AIFF-C file
    # _ssnd_seek_needed -- 1 iff positioned correctly in audio
    #       file for readframes()
    # _ssnd_chunk -- instantiation of a chunk class for the SSND chunk
    # _framesize -- size of one frame in the file

    def initfp(self, file):
        self._version = 0
        self._decomp = None
        self._convert = None
        self._markers = []
        self._soundpos = 0
        self._file = Chunk(file)
        if self._file.getname() != 'FORM':
            raise Error, 'file does not start with FORM id'
        formdata = self._file.read(4)
        if formdata == 'AIFF':
            self._aifc = 0
        elif formdata == 'AIFC':
            self._aifc = 1
        else:
            raise Error, 'not an AIFF or AIFF-C file'
        self._comm_chunk_read = 0
        while 1:
            self._ssnd_seek_needed = 1
            try:
                chunk = Chunk(self._file)
            except EOFError:
                break
            chunkname = chunk.getname()
            if chunkname == 'COMM':
                self._read_comm_chunk(chunk)
                self._comm_chunk_read = 1
            elif chunkname == 'SSND':
                self._ssnd_chunk = chunk
                dummy = chunk.read(8)
                self._ssnd_seek_needed = 0
            elif chunkname == 'FVER':
                self._version = _read_ulong(chunk)
            elif chunkname == 'MARK':
                self._readmark(chunk)
            elif chunkname in _skiplist:
                pass
            else:
                raise Error, 'unrecognized chunk type '+chunk.chunkname
            chunk.skip()
        if not self._comm_chunk_read or not self._ssnd_chunk:
            raise Error, 'COMM chunk and/or SSND chunk missing'
        if self._aifc and self._decomp:
            import cl
            params = [cl.ORIGINAL_FORMAT, 0,
                  cl.BITS_PER_COMPONENT, self._sampwidth * 8,
                  cl.FRAME_RATE, self._framerate]
            if self._nchannels == 1:
                params[1] = cl.MONO
            elif self._nchannels == 2:
                params[1] = cl.STEREO_INTERLEAVED
            else:
                raise Error, 'cannot compress more than 2 channels'
            self._decomp.SetParams(params)

    def __init__(self, f):
        if type(f) == type(''):
            f = __builtin__.open(f, 'rb')
        # else, assume it is an open file object already
        self.initfp(f)

    #
    # User visible methods.
    #
    def getfp(self):
        return self._file

    def rewind(self):
        self._ssnd_seek_needed = 1
        self._soundpos = 0

    def close(self):
        if self._decomp:
            self._decomp.CloseDecompressor()
            self._decomp = None
        self._file = None

    def tell(self):
        return self._soundpos

    def getnchannels(self):
        return self._nchannels

    def getnframes(self):
        return self._nframes

    def getsampwidth(self):
        return self._sampwidth

    def getframerate(self):
        return self._framerate

    def getcomptype(self):
        return self._comptype

    def getcompname(self):
        return self._compname

##  def getversion(self):
##      return self._version

    def getparams(self):
        return self.getnchannels(), self.getsampwidth(), \
              self.getframerate(), self.getnframes(), \
              self.getcomptype(), self.getcompname()

    def getmarkers(self):
        if len(self._markers) == 0:
            return None
        return self._markers

    def getmark(self, id):
        for marker in self._markers:
            if id == marker[0]:
                return marker
        raise Error, 'marker %r does not exist' % (id,)

    def setpos(self, pos):
        if pos < 0 or pos > self._nframes:
            raise Error, 'position not in range'
        self._soundpos = pos
        self._ssnd_seek_needed = 1

    def readframes(self, nframes):
        if self._ssnd_seek_needed:
            self._ssnd_chunk.seek(0)
            dummy = self._ssnd_chunk.read(8)
            pos = self._soundpos * self._framesize
            if pos:
                self._ssnd_chunk.seek(pos + 8)
            self._ssnd_seek_needed = 0
        if nframes == 0:
            return ''
        data = self._ssnd_chunk.read(nframes * self._framesize)
        if self._convert and data:
            data = self._convert(data)
        self._soundpos = self._soundpos + len(data) / (self._nchannels * self._sampwidth)
        return data

    #
    # Internal methods.
    #

    def _decomp_data(self, data):
        import cl
        dummy = self._decomp.SetParam(cl.FRAME_BUFFER_SIZE,
                          len(data) * 2)
        return self._decomp.Decompress(len(data) / self._nchannels,
                           data)

    def _ulaw2lin(self, data):
        import audioop
        return audioop.ulaw2lin(data, 2)

    def _adpcm2lin(self, data):
        import audioop
        if not hasattr(self, '_adpcmstate'):
            # first time
            self._adpcmstate = None
        data, self._adpcmstate = audioop.adpcm2lin(data, 2,
                               self._adpcmstate)
        return data

    def _read_comm_chunk(self, chunk):
        self._nchannels = _read_short(chunk)
        self._nframes = _read_long(chunk)
        self._sampwidth = (_read_short(chunk) + 7) / 8
        self._framerate = int(_read_float(chunk))
        self._framesize = self._nchannels * self._sampwidth
        if self._aifc:
            #DEBUG: SGI's soundeditor produces a bad size :-(
            kludge = 0
            if chunk.chunksize == 18:
                kludge = 1
                print 'Warning: bad COMM chunk size'
                chunk.chunksize = 23
            #DEBUG end
            self._comptype = chunk.read(4)
            #DEBUG start
            if kludge:
                length = ord(chunk.file.read(1))
                if length & 1 == 0:
                    length = length + 1
                chunk.chunksize = chunk.chunksize + length
                chunk.file.seek(-1, 1)
            #DEBUG end
            self._compname = _read_string(chunk)
            if self._comptype != 'NONE':
                if self._comptype == 'G722':
                    try:
                        import audioop
                    except ImportError:
                        pass
                    else:
                        self._convert = self._adpcm2lin
                        self._framesize = self._framesize / 4
                        return
                # for ULAW and ALAW try Compression Library
                try:
                    import cl
                except ImportError:
                    if self._comptype == 'ULAW':
                        try:
                            import audioop
                            self._convert = self._ulaw2lin
                            self._framesize = self._framesize / 2
                            return
                        except ImportError:
                            pass
                    raise Error, 'cannot read compressed AIFF-C files'
                if self._comptype == 'ULAW':
                    scheme = cl.G711_ULAW
                    self._framesize = self._framesize / 2
                elif self._comptype == 'ALAW':
                    scheme = cl.G711_ALAW
                    self._framesize = self._framesize / 2
                #else:
                #    raise Error, 'unsupported compression type'
                self._decomp = cl.OpenDecompressor(scheme)
                self._convert = self._decomp_data
        else:
            self._comptype = 'NONE'
            self._compname = 'not compressed'

    def _readmark(self, chunk):
        nmarkers = _read_short(chunk)
        # Some files appear to contain invalid counts.
        # Cope with this by testing for EOF.
        try:
            for i in range(nmarkers):
                id = _read_short(chunk)
                pos = _read_long(chunk)
                name = _read_string(chunk)
                if pos or name:
                    # some files appear to have
                    # dummy markers consisting of
                    # a position 0 and name ''
                    self._markers.append((id, pos, name))
        except EOFError:
            print 'Warning: MARK chunk contains only',
            print len(self._markers),
            if len(self._markers) == 1: print 'marker',
            else: print 'markers',
            print 'instead of', nmarkers
Example #57
0
from chunk import Chunk

c = Chunk()
c.load("test.dat")
print(c)
c[0, 0] = 1
print(c)
c.save("test.dat")
Example #58
0
class Wave_read:
    """Variables used in this class:
    
    These variables are available to the user though appropriate
    methods of this class:
    _file -- the open file with methods read(), close(), and seek()
              set through the __init__() method
    _nchannels -- the number of audio channels
              available through the getnchannels() method
    _nframes -- the number of audio frames
              available through the getnframes() method
    _sampwidth -- the number of bytes per audio sample
              available through the getsampwidth() method
    _framerate -- the sampling frequency
              available through the getframerate() method
    _comptype -- the AIFF-C compression type ('NONE' if AIFF)
              available through the getcomptype() method
    _compname -- the human-readable AIFF-C compression type
              available through the getcomptype() method
    _soundpos -- the position in the audio stream
              available through the tell() method, set through the
              setpos() method
    
    These variables are used internally only:
    _fmt_chunk_read -- 1 iff the FMT chunk has been read
    _data_seek_needed -- 1 iff positioned correctly in audio
              file for readframes()
    _data_chunk -- instantiation of a chunk class for the DATA chunk
    _framesize -- size of one frame in the file
    """

    def initfp(self, file):
        self._convert = None
        self._soundpos = 0
        self._file = Chunk(file, bigendian=0)
        if self._file.getname() != 'RIFF':
            raise Error, 'file does not start with RIFF id'
        if self._file.read(4) != 'WAVE':
            raise Error, 'not a WAVE file'
        self._fmt_chunk_read = 0
        self._data_chunk = None
        while 1:
            self._data_seek_needed = 1
            try:
                chunk = Chunk(self._file, bigendian=0)
            except EOFError:
                break

            chunkname = chunk.getname()
            if chunkname == 'fmt ':
                self._read_fmt_chunk(chunk)
                self._fmt_chunk_read = 1
            elif chunkname == 'data':
                if not self._fmt_chunk_read:
                    raise Error, 'data chunk before fmt chunk'
                self._data_chunk = chunk
                self._nframes = chunk.chunksize // self._framesize
                self._data_seek_needed = 0
                break
            chunk.skip()

        if not self._fmt_chunk_read or not self._data_chunk:
            raise Error, 'fmt chunk and/or data chunk missing'
        return

    def __init__(self, f):
        self._i_opened_the_file = None
        if isinstance(f, basestring):
            f = __builtin__.open(f, 'rb')
            self._i_opened_the_file = f
        try:
            self.initfp(f)
        except:
            if self._i_opened_the_file:
                f.close()
            raise

        return

    def __del__(self):
        self.close()

    def getfp(self):
        return self._file

    def rewind(self):
        self._data_seek_needed = 1
        self._soundpos = 0

    def close(self):
        if self._i_opened_the_file:
            self._i_opened_the_file.close()
            self._i_opened_the_file = None
        self._file = None
        return

    def tell(self):
        return self._soundpos

    def getnchannels(self):
        return self._nchannels

    def getnframes(self):
        return self._nframes

    def getsampwidth(self):
        return self._sampwidth

    def getframerate(self):
        return self._framerate

    def getcomptype(self):
        return self._comptype

    def getcompname(self):
        return self._compname

    def getparams(self):
        return (self.getnchannels(),
         self.getsampwidth(),
         self.getframerate(),
         self.getnframes(),
         self.getcomptype(),
         self.getcompname())

    def getmarkers(self):
        return None

    def getmark(self, id):
        raise Error, 'no marks'

    def setpos(self, pos):
        if pos < 0 or pos > self._nframes:
            raise Error, 'position not in range'
        self._soundpos = pos
        self._data_seek_needed = 1

    def readframes(self, nframes):
        if self._data_seek_needed:
            self._data_chunk.seek(0, 0)
            pos = self._soundpos * self._framesize
            if pos:
                self._data_chunk.seek(pos, 0)
            self._data_seek_needed = 0
        if nframes == 0:
            return ''
        if self._sampwidth in (2, 4) and sys.byteorder == 'big':
            import array
            chunk = self._data_chunk
            data = array.array(_array_fmts[self._sampwidth])
            if not data.itemsize == self._sampwidth:
                raise AssertionError
                nitems = nframes * self._nchannels
                if nitems * self._sampwidth > chunk.chunksize - chunk.size_read:
                    nitems = (chunk.chunksize - chunk.size_read) / self._sampwidth
                data.fromfile(chunk.file.file, nitems)
                chunk.size_read = chunk.size_read + nitems * self._sampwidth
                chunk = chunk.file
                chunk.size_read = chunk.size_read + nitems * self._sampwidth
                data.byteswap()
                data = data.tostring()
            else:
                data = self._data_chunk.read(nframes * self._framesize)
                if self._sampwidth == 3 and sys.byteorder == 'big':
                    data = _byteswap3(data)
            data = self._convert and data and self._convert(data)
        self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
        return data

    def _read_fmt_chunk(self, chunk):
        wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<HHLLH', chunk.read(14))
        if wFormatTag == WAVE_FORMAT_PCM:
            sampwidth = struct.unpack('<H', chunk.read(2))[0]
            self._sampwidth = (sampwidth + 7) // 8
        else:
            raise Error, 'unknown format: %r' % (wFormatTag,)
        self._framesize = self._nchannels * self._sampwidth
        self._comptype = 'NONE'
        self._compname = 'not compressed'
Example #59
0
class Wave_read:
    """Variables used in this class:

    These variables are available to the user though appropriate
    methods of this class:
    _file -- the open file with methods read(), close(), and seek()
              set through the __init__() method
    _nchannels -- the number of audio channels
              available through the getnchannels() method
    _nframes -- the number of audio frames
              available through the getnframes() method
    _sampwidth -- the number of bytes per audio sample
              available through the getsampwidth() method
    _framerate -- the sampling frequency
              available through the getframerate() method
    _comptype -- the AIFF-C compression type ('NONE' if AIFF)
              available through the getcomptype() method
    _compname -- the human-readable AIFF-C compression type
              available through the getcomptype() method
    _soundpos -- the position in the audio stream
              available through the tell() method, set through the
              setpos() method

    These variables are used internally only:
    _fmt_chunk_read -- 1 iff the FMT chunk has been read
    _data_seek_needed -- 1 iff positioned correctly in audio
              file for readframes()
    _data_chunk -- instantiation of a chunk class for the DATA chunk
    _framesize -- size of one frame in the file
    """

    def initfp(self, file):
        self._convert = None
        self._soundpos = 0
        self._file = Chunk(file, bigendian = 0)
        if self._file.getname() != b'RIFF':
            raise Error('file does not start with RIFF id')
        if self._file.read(4) != b'WAVE':
            raise Error('not a WAVE file')
        self._fmt_chunk_read = 0
        self._data_chunk = None
        while 1:
            self._data_seek_needed = 1
            try:
                chunk = Chunk(self._file, bigendian = 0)
            except EOFError:
                break
            chunkname = chunk.getname()
            if chunkname == b'fmt ':
                self._read_fmt_chunk(chunk)
                self._fmt_chunk_read = 1
            elif chunkname == b'data':
                if not self._fmt_chunk_read:
                    raise Error('data chunk before fmt chunk')
                self._data_chunk = chunk
                self._nframes = chunk.chunksize // self._framesize
                self._data_seek_needed = 0
                break
            chunk.skip()
        if not self._fmt_chunk_read or not self._data_chunk:
            raise Error('fmt chunk and/or data chunk missing')

    def __init__(self, f):
        self._i_opened_the_file = None
        if isinstance(f, str):
            f = builtins.open(f, 'rb')
            self._i_opened_the_file = f
        # else, assume it is an open file object already
        try:
            self.initfp(f)
        except:
            if self._i_opened_the_file:
                f.close()
            raise

    def __del__(self):
        self.close()
    #
    # User visible methods.
    #
    def getfp(self):
        return self._file

    def rewind(self):
        self._data_seek_needed = 1
        self._soundpos = 0

    def close(self):
        if self._i_opened_the_file:
            self._i_opened_the_file.close()
            self._i_opened_the_file = None
        self._file = None

    def tell(self):
        return self._soundpos

    def getnchannels(self):
        return self._nchannels

    def getnframes(self):
        return self._nframes

    def getsampwidth(self):
        return self._sampwidth

    def getframerate(self):
        return self._framerate

    def getcomptype(self):
        return self._comptype

    def getcompname(self):
        return self._compname

    def getparams(self):
        return self.getnchannels(), self.getsampwidth(), \
               self.getframerate(), self.getnframes(), \
               self.getcomptype(), self.getcompname()

    def getmarkers(self):
        return None

    def getmark(self, id):
        raise Error('no marks')

    def setpos(self, pos):
        if pos < 0 or pos > self._nframes:
            raise Error('position not in range')
        self._soundpos = pos
        self._data_seek_needed = 1

    def readframes(self, nframes):
        if self._data_seek_needed:
            self._data_chunk.seek(0, 0)
            pos = self._soundpos * self._framesize
            if pos:
                self._data_chunk.seek(pos, 0)
            self._data_seek_needed = 0
        if nframes == 0:
            return b''
        if self._sampwidth > 1 and big_endian:
            # unfortunately the fromfile() method does not take
            # something that only looks like a file object, so
            # we have to reach into the innards of the chunk object
            import array
            chunk = self._data_chunk
            data = array.array(_array_fmts[self._sampwidth])
            nitems = nframes * self._nchannels
            if nitems * self._sampwidth > chunk.chunksize - chunk.size_read:
                nitems = (chunk.chunksize - chunk.size_read) // self._sampwidth
            data.fromfile(chunk.file.file, nitems)
            # "tell" data chunk how much was read
            chunk.size_read = chunk.size_read + nitems * self._sampwidth
            # do the same for the outermost chunk
            chunk = chunk.file
            chunk.size_read = chunk.size_read + nitems * self._sampwidth
            data.byteswap()
            data = data.tobytes()
        else:
            data = self._data_chunk.read(nframes * self._framesize)
        if self._convert and data:
            data = self._convert(data)
        self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
        return data

    #
    # Internal methods.
    #

    def _read_fmt_chunk(self, chunk):
        wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<hhllh', chunk.read(14))
        if wFormatTag == WAVE_FORMAT_PCM:
            sampwidth = struct.unpack_from('<h', chunk.read(2))[0]
            self._sampwidth = (sampwidth + 7) // 8
        else:
            raise Error('unknown format: %r' % (wFormatTag,))
        self._framesize = self._nchannels * self._sampwidth
        self._comptype = 'NONE'
        self._compname = 'not compressed'
    def __init__(self, filename, outfilename=None):
        self.chunks = []
        self.chunkmap = {}
        self.chunkatpos = {}
        self.usages = []
        self.usagemap = {}
        
        self.filename = filename
        self.outfilename = outfilename
        if (not self.outfilename):
            self.outfilename = self.filename

        if (not self.filename):
            # No loading; create an empty file.
            self.file = None
            self.formchunk = None
            self.changed = True
            chunk = BlorbChunk(self, b'RIdx', -1, 4)
            chunk.literaldata = struct.pack('>I', 0)
            self.add_chunk(chunk, None, None, 0)
            return
            
        self.changed = False

        self.file = open(filename, 'rb')
        formchunk = Chunk(self.file)
        self.formchunk = formchunk
        
        if (formchunk.getname() != b'FORM'):
            raise Exception('This does not appear to be a Blorb file.')
        formtype = formchunk.read(4)
        if (formtype != b'IFRS'):
            raise Exception('This does not appear to be a Blorb file.')
        formlen = formchunk.getsize()
        while formchunk.tell() < formlen:
            chunk = Chunk(formchunk)
            start = formchunk.tell()
            size = chunk.getsize()
            formtype = None
            if chunk.getname() == b'FORM':
                formtype = chunk.read(4)
            subchunk = BlorbChunk(self, chunk.getname(), start, size, formtype)
            self.chunks.append(subchunk)
            chunk.skip()
            chunk.close()

        for chunk in self.chunks:
            self.chunkatpos[chunk.start] = chunk
            dict_append(self.chunkmap, chunk.type, chunk)

        # Sanity checks. Also get the usage list.
        
        ls = self.chunkmap.get(b'RIdx')
        if (not ls):
            raise Exception('No resource index chunk!')
        elif (len(ls) != 1):
            print('Warning: too many resource index chunks!')
        else:
            chunk = ls[0]
            if (self.chunks[0] is not chunk):
                print('Warning: resource index chunk is not first!')
            dat = chunk.data()
            numres = struct.unpack('>I', dat[0:4])[0]
            if (numres*12+4 != chunk.len):
                print('Warning: resource index chunk has wrong size!')
            for ix in range(numres):
                subdat = dat[4+ix*12 : 16+ix*12]
                typ = struct.unpack('>4c', subdat[0:4])
                typ = b''.join(typ)
                num = struct.unpack('>I', subdat[4:8])[0]
                start = struct.unpack('>I', subdat[8:12])[0]
                subchunk = self.chunkatpos.get(start)
                if (not subchunk):
                    print('Warning: resource (%s, %d) refers to a nonexistent chunk!' % (typestring(typ), num))
                self.usages.append( (typ, num, subchunk) )
                self.usagemap[(typ, num)] = subchunk