def parse(self, jpegdata): self._jpegdata = jpegdata reader = IO_Bit() reader.input(jpegdata) while reader.hasNextData(2): chunk = self._parseChunk(reader) self.chunks.append(chunk)
def build(self, opts = {}): writer = IO_Bit() self._buildChunk(writer, self.header, opts) for track in self.tracks: self._buildChunk(writer, track, opts) if self.xfinfo: self._buildChunk(writer, self.xfinfo, opts) if self.xfkaraoke: self._buildChunk(writer, self.xfkaraoke, opts) return writer.output()
def parse(self, mididata): self._mididata = mididata reader = IO_Bit() reader.input(mididata) while reader.hasNextData(4): chunk = self._parseChunk(reader) if chunk['type'] == b'MThd': self.header = chunk elif chunk['type'] == b'MTrk': self.tracks.append(chunk) elif chunk['type'] == b'XFIH': self.xfinfo = chunk elif chunk['type'] == b'XFKM': self.xfkaraoke = chunk else: sys.stderr.write("Can't parse chunk.") break
def _buildChunk(self, writer, chunk, opts): type = chunk['type'] writerChunk = IO_Bit() if type == 'MThd': self._buildChunkHeader(writerChunk, chunk['header'], opts) elif type == 'MTrk': self._buildChunkTrack(writerChunk, chunk['track'], opts) elif type == 'XFIH': self._buildChunkXFInfo(writerChunk, chunk['xfinfo'], opts) elif type == 'XFKM': self._buildChunkXFKaraoke(writerChunk, chunk['xfkaraoke'], opts) else: raise Exception("Unknown chunk (type=type)\n") chunkData = writerChunk.output() length = len(chunkData) writer.putData(type , 4) writer.putUI32BE(length) writer.putData(chunkData, length)
def dump(self, fp = sys.stdout, opts = {}): if ('hexdump' in opts) and opts['hexdump']: bitio = IO_Bit() bitio.input(self._mididata) fp.write("HEADER:\n") for key, value in self.header['header'].items(): fp.write(" {0}: {1}\n".format(key, value)) if ('hexdump' in opts) and opts['hexdump']: bitio.hexdump(0, self.header['length'] + 8) xfkaraoke_with_track = {} for idx, value in enumerate(self.tracks): xfkaraoke_with_track["{0}".format(idx)] = value; if not self.xfkaraoke is None: xfkaraoke_with_track["karaoke"] = self.xfkaraoke xfkaraoke_with_track["karaoke"]["track"] = self.xfkaraoke["xfkaraoke"] for idx, track in xfkaraoke_with_track.items(): scaleCharactors = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'] fp.write("TRACK[{0}]:\n".format(idx)) if ('hexdump' in opts) and opts['hexdump']: bitio.hexdump(track['_offset'], 8) for idx2, chunk in enumerate(track['track']): fp.write(" [{0}:".format(idx2)) meta_event_type = -1 for key, value in chunk.items(): if key == 'EventType': if value < 0xFF: eventname = self.event_name[value] else: eventname = "Meta Event" fp.write(" {0}:{1}({2}),".format(key, value, eventname)) elif key == 'MetaEventType': meta_event_type = value if value in self.meta_event_name: eventname = self.meta_event_name[value] fp.write(" {0}:{1}({2}),".format(key, value, eventname)) else: fp.write(" {0}:{1},".format(key, value)) elif key == 'ControllerType': typename = self.controller_type_name[value] fp.write(" {0}:{1}({2}),".format(key, value, typename)) elif key == 'SystemEx' or key == 'SystemExCont' or key == 'MetaEventData': fp.write(" {0}:".format(key)) dataLen = len(value) if key == 'MetaEventData': if meta_event_type == 0x05: fp.write(value.decode('SJIS')) elif meta_event_type == 0x07: fp.write(value) fp.write("(") i = 0 while i < dataLen: fp.write("{:02x}".format(ord(value[i:i+1]))) i += 1 fp.write(")") elif key == 'NoteNumber': noteoct = math.floor(value / 12) notekey = scaleCharactors[value % 12] fp.write(" {:s}:{:d}({:s}{:.0f}),".format(key, value, notekey, noteoct)) else: if (key[0:1] != '_') or (('verbose' in opts) and opts['verbose']): fp.write(" {0}:{1},".format(key, value)) fp.write("\n") if ('hexdump' in opts) and opts['hexdump']: bitio.hexdump(chunk['_offset'], chunk['_length'])