def __init__(self, filename): self.lstSourceMIDI = {} self.sourceMIDI = None # rpp = open(filename, "r") rpp = rpp.read() rpp = rpp.split("\n") rpp.reverse() self.rppRoot = RppTree() self.lineNum = 0 self.decodeRppTree(rpp, self.rppRoot) self.rppRoot = self.rppRoot.branch[0] for rT in self.rppRoot.branch: if rT.getTag() == chunkDefines.TRACK: for rT2 in rT.branch: if rT2.getTag() == chunkDefines.ITEM: for lf in rT2.leaves: if lf.getTag() == chunkDefines.NAME: print lf.getName() srcMIDI = rT2.getBranch(chunkDefines.SOURCE_MIDI) print srcMIDI self.sourceMIDI = SourceMIDI() # self.sourceMIDI.NAME = lf.getName() self.lstSourceMIDI[lf.getName()] = self.sourceMIDI for lfMIDI in srcMIDI.leaves: tag = lfMIDI.getTag() p = lfMIDI.getParams() if tag == chunkDefines.HASDATA: self.PPQ = int(p[1]) print self.PPQ if tag == chunkDefines.NOTE_SELECTED or tag == chunkDefines.NOTE_UNSELECTED: state = int(p[1], 16) & 0xF0 if state != EnumMsg.NOTE_OFF and state != EnumMsg.NOTE_ON: continue delta = int(p[0]) channel = int(p[1], 16) & 0x0F pitch = int(p[2], 16) velocity = int(p[3], 16) self.sourceMIDI.addMsgNote(MsgNote(delta, state, channel, pitch, velocity)) pass pass for rTag in self.rppRoot.leaves: if rTag.getTag() == chunkDefines.TEMPO: params = rTag.getParams() self.tempo = float(params[0]) # todo 4/4 params[1]/params[2] print "tempo", rTag.getTag(), rTag.getParams()
class RppReader: lstSourceMIDI = {} PPQ = 0 def __init__(self, filename): self.lstSourceMIDI = {} self.sourceMIDI = None # rpp = open(filename, "r") rpp = rpp.read() rpp = rpp.split("\n") rpp.reverse() self.rppRoot = RppTree() self.lineNum = 0 self.decodeRppTree(rpp, self.rppRoot) self.rppRoot = self.rppRoot.branch[0] for rT in self.rppRoot.branch: if rT.getTag() == chunkDefines.TRACK: for rT2 in rT.branch: if rT2.getTag() == chunkDefines.ITEM: for lf in rT2.leaves: if lf.getTag() == chunkDefines.NAME: print lf.getName() srcMIDI = rT2.getBranch(chunkDefines.SOURCE_MIDI) print srcMIDI self.sourceMIDI = SourceMIDI() # self.sourceMIDI.NAME = lf.getName() self.lstSourceMIDI[lf.getName()] = self.sourceMIDI for lfMIDI in srcMIDI.leaves: tag = lfMIDI.getTag() p = lfMIDI.getParams() if tag == chunkDefines.HASDATA: self.PPQ = int(p[1]) print self.PPQ if tag == chunkDefines.NOTE_SELECTED or tag == chunkDefines.NOTE_UNSELECTED: state = int(p[1], 16) & 0xF0 if state != EnumMsg.NOTE_OFF and state != EnumMsg.NOTE_ON: continue delta = int(p[0]) channel = int(p[1], 16) & 0x0F pitch = int(p[2], 16) velocity = int(p[3], 16) self.sourceMIDI.addMsgNote(MsgNote(delta, state, channel, pitch, velocity)) pass pass for rTag in self.rppRoot.leaves: if rTag.getTag() == chunkDefines.TEMPO: params = rTag.getParams() self.tempo = float(params[0]) # todo 4/4 params[1]/params[2] print "tempo", rTag.getTag(), rTag.getParams() pass def decodeRppTree(self, toRead, tree): rpp = toRead rppTrees = tree while rpp: line = rpp.pop() line = line.strip() if line: self.lineNum += 1 if line[0] == "<": line = line.split("<") newTree = RppTree() newTree.setSection(line[1]) tree.branch.append(newTree) self.decodeRppTree(rpp, newTree) continue if line[0] == ">": break rppTrees.leaves.append(RppLeaf(line)) pass def getSourceMIDIList(self): return self.lstSourceMIDI.keys() def getSourceMIDIbyName(self, itemName): for sourceMIDI in self.lstSourceMIDI: if sourceMIDI.NAME == itemName: return sourceMIDI def getPPQ(self): return self.PPQ def getJNoteVOs(self, name, ch=0): lstMsg = self.lstSourceMIDI[name].getSMMsOnChannel(ch) lstNoteOn = {} length = len(lstMsg) lstJNote = [] position = 0 for i in range(0, length): msgNote = lstMsg[i] position += msgNote.delta if msgNote.state == EnumMsg.NOTE_ON: jNoteVO = JNoteVO() jNoteVO.deltaStart = position jNoteVO.ch = msgNote.channel jNoteVO.pitch = msgNote.pitch jNoteVO.velocity = msgNote.velocity lstNoteOn[msgNote.pitch] = jNoteVO if msgNote.state == EnumMsg.NOTE_OFF: jNoteVO = lstNoteOn[msgNote.pitch] jNoteVO.deltaEnd = position jNoteVO.index = len(lstJNote) jNoteVO.idGroup = jNoteVO.index lstJNote.append(jNoteVO) print jNoteVO pass return lstJNote