Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.83 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. #parts of code inspired by cseq2midi
  3.  
  4. import os, sys, struct, io
  5.  
  6. blockIds = {0x5000:'DATA', 0x5001:'LABL'}
  7.  
  8. class argTypes:
  9.     UINT8 = 0
  10.     INT8 = 1
  11.     UINT16 = 2
  12.     INT16 = 3
  13.     RANDOM = 4
  14.     VARIABLE = 5
  15.     VARLEN = 6
  16.  
  17. class BCSEQ:
  18.     def __init__(self, filename):
  19.         self.blocks = {} #Dicts containing a dict with keys 'offset' and 'size'
  20.         self.labels = []
  21.         self.commands = []
  22.         self.commandOffsets = {}
  23.        
  24.         self.filename = filename
  25.         with open(self.filename, 'rb') as fh:
  26.             self.bcseq = io.BytesIO(fh.read()) #store file in memory as file type object
  27.             self.bcseq.seek(0)
  28.         if self.bcseq.read(4) != b'CSEQ':
  29.             sys.exit('Not a BCSEQ file')
  30.         self.endianness = readBytes(self.bcseq, 2)
  31.         self.headerSize = readBytes(self.bcseq, 2)
  32.         self.version = readBytes(self.bcseq, 4)
  33.         if self.version != 0x01010000: #1.1.0.0
  34.             sys.exit('Encountered unknown CSEQ version: %X' % (self.version))
  35.         self.bcseqSize = readBytes(self.bcseq, 4)
  36.         self.blockCount = readBytes(self.bcseq, 4)
  37.        
  38.         for x in xrange(self.blockCount):
  39.             bId = readBytes(self.bcseq, 4)
  40.             if bId not in blockIds:
  41.                 sys.exit('Encountered unknown block id: %X' % (bId))
  42.             bLabel = blockIds[bId]
  43.             bOffs = readBytes(self.bcseq, 4)
  44.             bSize = readBytes(self.bcseq, 4)
  45.             self.blocks[bLabel] = {'offset':bOffs, 'size':bSize}
  46.             #print "Found '%s' block. Offset: 0x%X, Size: 0x%X" % (bLabel, bOffs, bSize)
  47.        
  48.         if not self.blocks['DATA'] or not self.blocks['LABL']:
  49.             sys.exit('Missing DATA or LABL block.')
  50.        
  51.         self.parseLablBlock()
  52.         self.parseLabels()
  53.    
  54.     def parseLablBlock(self):
  55.         """
  56.        Returns a sorted list of dicts containing labels found in the label block
  57.        Dict keys are 'offset', 'label', and 'checked'
  58.        """
  59.         labels = []
  60.        
  61.         startPos = self.bcseq.tell()
  62.         self.bcseq.seek(self.blocks['LABL']['offset'])
  63.        
  64.         bLabel = self.bcseq.read(4)
  65.         bSize = readBytes(self.bcseq, 4)
  66.         labelInfoCount = readBytes(self.bcseq, 4)
  67.        
  68.         for x in xrange(labelInfoCount):
  69.             labelInfoId = readBytes(self.bcseq, 4)
  70.             if labelInfoId != 0x5100:
  71.                 sys.exit("Unknown ID in 'LABL' block: 0x%X" % (labelInfoId))
  72.             labelInfoOffs = readBytes(self.bcseq, 4)
  73.             tmp = self.bcseq.tell()
  74.             self.bcseq.seek(self.blocks['LABL']['offset'] + 8 + labelInfoOffs) #Offset is relative to after the 8 byte block header
  75.             tmpId = readBytes(self.bcseq, 4) #Should be 0x1F00, but not checking for that
  76.             labelDataOffs = readBytes(self.bcseq, 4) #Relative to after 8 byte header of DATA block
  77.             labelStrSize = readBytes(self.bcseq, 4)
  78.             labelStr = self.bcseq.read(labelStrSize) #Assume label string size >0
  79.             self.addLabel(labelDataOffs, labelStr)
  80.             self.bcseq.seek(tmp)
  81.        
  82.         self.bcseq.seek(startPos)
  83.    
  84.     def addLabel(self, offset, label):
  85.         """Adds a label to a list of labels if one doesn't already exist for that offset, and sorts them by offset"""
  86.         if not any(x['offset'] == offset for x in self.labels):
  87.             self.labels.append({'offset':offset, 'label':label, 'checked':False})
  88.             self.labels.sort(key=lambda x: int(x['offset']))
  89.    
  90.     def labelFromOffset(self, offset, str):
  91.         #Is this the best way to do this?
  92.         label = next( (x['label'] for x in self.labels if x['offset'] == offset), None)
  93.         if label == None:
  94.             label = '%s_%06X' % (str, offset)
  95.             self.addLabel(offset, label)
  96.         return label
  97.    
  98.     def parseLabels(self):
  99.         """Iterates over every label(including ones generated here) and decompiles their commands"""
  100.         while 1:
  101.             label = next( (x for x in self.labels if x['checked'] == False), None)
  102.             if label == None:
  103.                 self.sortCommands()
  104.                 break
  105.             self.parseLabelCommands(label)
  106.             label['checked'] = True
  107.    
  108.     def parseLabelCommands(self, label):
  109.         startPos = self.bcseq.tell()
  110.         self.bcseq.seek(self.blocks['DATA']['offset'] + 8 + label['offset'])
  111.        
  112.         parsing = True
  113.         while parsing:
  114.             tmpcmd = {'offset':0, 'name':'UNDEFINED', 'suffix1':'', 'suffix2':'', 'suffix3':'', 'arg1Type':None, 'arg2Type':None, 'args':[]}
  115.             tmpcmd['offset'] = self.bcseq.tell() - self.blocks['DATA']['offset'] - 8
  116.            
  117.             cmd = readBytes(self.bcseq, 1)
  118.            
  119.             #Parsing code checks for '_if' first, then one of ['_t', '_tr', '_tv'], then one of ['_r', '_v']
  120.             if cmd == 0xA2: #_if
  121.                 tmpcmd['suffix3'] = '_if'
  122.                 cmd = readBytes(self.bcseq, 1)
  123.            
  124.             if cmd == 0xA3: #_t
  125.                 tmpcmd['suffix2'] = '_t'
  126.                 tmpcmd['arg2Type'] = argTypes.INT16
  127.                 cmd = readBytes(self.bcseq, 1)
  128.             elif cmd == 0xA4: #_tr
  129.                 tmpcmd['suffix2'] = '_tr'
  130.                 tmpcmd['arg2Type'] = argTypes.RANDOM
  131.                 cmd = readBytes(self.bcseq, 1)
  132.             elif cmd == 0xA5: #_tv
  133.                 tmpcmd['suffix2'] = '_tv'
  134.                 tmpcmd['arg2Type'] = argTypes.VARIABLE
  135.                 cmd = readBytes(self.bcseq, 1)
  136.            
  137.             if cmd == 0xA0: #_r
  138.                 tmpcmd['suffix1'] = '_r'
  139.                 tmpcmd['arg1Type'] = argTypes.RANDOM
  140.                 cmd = readBytes(self.bcseq, 1)
  141.             elif cmd == 0xA1: #_v
  142.                 tmpcmd['suffix1'] = '_v'
  143.                 tmpcmd['arg1Type'] = argTypes.VARIABLE
  144.                 cmd = readBytes(self.bcseq, 1)
  145.            
  146.            
  147.             if cmd < 0x80: #note
  148.                 tmpcmd['name'] = keyToStr(cmd)
  149.                 tmpcmd['args'].append(str(readBytes(self.bcseq, 1)))
  150.                 if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.VARLEN
  151.                 tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  152.            
  153.             elif cmd == 0x80: #wait
  154.                 tmpcmd['name'] = 'wait'
  155.                 if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.VARLEN
  156.                 tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  157.            
  158.             elif cmd == 0x81: #prg
  159.                 tmpcmd['name'] = 'prg'
  160.                 if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.VARLEN
  161.                 tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  162.            
  163.             elif cmd == 0x88: #opentrack
  164.                 tmpcmd['name'] = 'opentrack'
  165.                 tmpcmd['args'].append(str(readBytes(self.bcseq, 1)))
  166.                 label = self.labelFromOffset(readBytes(self.bcseq, 3, endian='big'), 'track')
  167.                 tmpcmd['args'].append(label)
  168.            
  169.             elif cmd == 0x89: #jump
  170.                 tmpcmd['name'] = 'jump'
  171.                 label = self.labelFromOffset(readBytes(self.bcseq, 3, endian='big'), 'loc')
  172.                 tmpcmd['args'].append(label)
  173.                 if tmpcmd['suffix3'] == '':
  174.                     parsing = False
  175.            
  176.             elif cmd == 0x8A: #call
  177.                 tmpcmd['name'] = 'call'
  178.                 label = self.labelFromOffset(readBytes(self.bcseq, 3, endian='big'), 'sub')
  179.                 tmpcmd['args'].append(label)
  180.            
  181.             elif 0xB0 <= cmd <= 0xDF and cmd not in [0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xDE]:
  182.                 if cmd == 0xB1: #env_hold
  183.                     tmpcmd['name'] = 'env_hold'
  184.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT8
  185.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  186.                
  187.                 elif cmd == 0xB2: #monophonic
  188.                     suf = 'off' if readBytes(self.bcseq, 1) == 0 else 'on'
  189.                     tmpcmd['name'] = 'monophonic_%s' % (suf)
  190.                
  191.                 elif cmd == 0xBF: #frontbypass
  192.                     suf = 'off' if readBytes(self.bcseq, 1) == 0 else 'on'
  193.                     tmpcmd['name'] = 'frontbypass_%s' % (suf)
  194.                
  195.                 elif cmd == 0xC3: #transpose
  196.                     tmpcmd['name'] = 'transpose'
  197.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT8
  198.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  199.                
  200.                 elif cmd == 0xC4: #pitchbend
  201.                     tmpcmd['name'] = 'pitchbend'
  202.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT8
  203.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  204.                
  205.                 elif cmd == 0xC7: #notewait
  206.                     suf = 'off' if readBytes(self.bcseq, 1) == 0 else 'on'
  207.                     tmpcmd['name'] = 'notewait_%s' % (suf)
  208.                
  209.                 elif cmd == 0xC8: #tie
  210.                     suf = 'off' if readBytes(self.bcseq, 1) == 0 else 'on'
  211.                     tmpcmd['name'] = 'tie%s' % (suf)
  212.                
  213.                 elif cmd == 0xC9: #porta
  214.                     tmpcmd['name'] = 'porta'
  215.                     tmpcmd['args'].append(keyToStr(readBytes(self.bcseq, 1)))
  216.                
  217.                 elif cmd == 0xCC: #mod_type
  218.                     tmpcmd['name'] = 'mod_type'
  219.                     modType = readBytes(self.bcseq, 1)
  220.                     if modType > 2:
  221.                         sys.exit("Unrecognized 'mod_type' value: 0x%02X" % (modType))
  222.                     tmpcmd['args'].append(['MOD_TYPE_PITCH', 'MOD_TYPE_VOLUME', 'MOD_TYPE_PAN'][modType])
  223.                
  224.                 elif cmd == 0xCE: #porta
  225.                     suf = 'off' if readBytes(self.bcseq, 1) == 0 else 'on'
  226.                     tmpcmd['name'] = 'porta_%s' % (suf)
  227.                
  228.                 elif 0xD0 <= cmd <= 0xD3: #Attack, decay, sustain, release
  229.                     tmpcmd['name'] = ['attack', 'decay', 'sustain', 'release'][cmd - 0xD0]
  230.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT8
  231.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  232.                
  233.                 elif cmd == 0xD6: #printvar
  234.                     tmpcmd['name'] = 'printvar'
  235.                     tmpcmd['args'] += readArg(self.bcseq, argTypes.VARIABLE)
  236.                
  237.                 elif cmd == 0xDF: #damper
  238.                     suf = 'off' if readBytes(self.bcseq, 1) == 0 else 'on'
  239.                     tmpcmd['name'] = 'damper_%s' % (suf)
  240.  
  241.                 else:
  242.                     b0Range = {
  243.                         0xB0:'timebase', 0xB3:'velocity_range', 0xB4:'biquad_type', 0xB5:'biquad_value', 0xB6:'bank_select',
  244.                         0xBD:'mod_phase', 0xBE:'mod_curve', 0xC0:'pan', 0xC1:'volume', 0xC2:'main_volume',
  245.                         0xC5:'bendrange', 0xC6:'prio', 0xCA:'mod_depth', 0xCB:'mod_speed', 0xCD:'mod_range',
  246.                         0xCF:'porta_time', 0xD4:'loop_start', 0xD5:'volume2', 0xD7:'span', 0xD8:'lpf_cutoff',
  247.                         0xD9:'fxsend_a', 0xDA:'fxsend_b', 0xDB:'mainsend', 0xDC:'init_pan', 0xDD:'mute'
  248.                     }
  249.                     tmpcmd['name'] = b0Range[cmd]
  250.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.UINT8
  251.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  252.                    
  253.                 if tmpcmd['arg2Type'] != None:
  254.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg2Type'])
  255.            
  256.             elif cmd == 0xE0: #mod_delay
  257.                 tmpcmd['name'] = 'mod_delay'
  258.                 if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT16
  259.                 tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  260.            
  261.             elif cmd == 0xE1: #tempo
  262.                 tmpcmd['name'] = 'tempo'
  263.                 if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT16
  264.                 tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  265.            
  266.             elif cmd == 0xE3: #sweep_pitch
  267.                 tmpcmd['name'] = 'sweep_pitch'
  268.                 if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT16
  269.                 tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  270.            
  271.             elif cmd == 0xE4: #mod_period
  272.                 tmpcmd['name'] = 'mod_period'
  273.                 if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT16
  274.                 tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  275.            
  276.             elif cmd == 0xF0: #extended
  277.                 cmd = readBytes(self.bcseq, 1)
  278.                 if 0x80 <= cmd <= 0x8B or 0x90 <= cmd <= 0x95:
  279.                     f080Range = {
  280.                         0x80:'setvar', 0x81:'addvar', 0x82:'subvar', 0x83:'mulvar', 0x84:'divvar',
  281.                         0x85:'shiftvar', 0x86:'randvar', 0x87:'andvar', 0x88:'orvar', 0x89:'xorvar',
  282.                         0x8A:'notvar', 0x8B:'modvar', 0x90:'cmp_eq', 0x91:'cmp_ge', 0x92:'cmp_gt',
  283.                         0x93:'cmp_le', 0x94:'cmp_lt', 0x95:'cmp_ne'
  284.                     }
  285.                     tmpcmd['name'] = f080Range[cmd]
  286.                     tmpcmd['args'] += readArg(self.bcseq, argTypes.VARIABLE)
  287.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT16
  288.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  289.                
  290.                 elif cmd == 0xA4: #mod2_type
  291.                     tmpcmd['name'] = 'mod2_type'
  292.                     modType = readBytes(self.bcseq, 1)
  293.                     if modType > 2:
  294.                         sys.exit("Unrecognized 'mod2_type' value: 0x%02X" % (modType))
  295.                     tmpcmd['args'].append(['MOD_TYPE_PITCH', 'MOD_TYPE_VOLUME', 'MOD_TYPE_PAN'][modType])
  296.                
  297.                 elif cmd == 0xAA: #mod3_type
  298.                     tmpcmd['name'] = 'mod3_type'
  299.                     modType = readBytes(self.bcseq, 1)
  300.                     if modType > 2:
  301.                         sys.exit("Unrecognized 'mod3_type' value: 0x%02X" % (modType))
  302.                     tmpcmd['args'].append(['MOD_TYPE_PITCH', 'MOD_TYPE_VOLUME', 'MOD_TYPE_PAN'][modType])
  303.                
  304.                 elif cmd == 0xB0: #mod4_type
  305.                     tmpcmd['name'] = 'mod4_type'
  306.                     modType = readBytes(self.bcseq, 1)
  307.                     if modType > 2:
  308.                         sys.exit("Unrecognized 'mod4_type' value: 0x%02X" % (modType))
  309.                     tmpcmd['args'].append(['MOD_TYPE_PITCH', 'MOD_TYPE_VOLUME', 'MOD_TYPE_PAN'][modType])
  310.                
  311.                 elif 0xA0 <= cmd <= 0xB1:
  312.                     f0a0Range = {
  313.                         0xA0:'mod2_curve', 0xA1:'mod2_phase', 0xA2:'mod2_depth', 0xA3:'mod2_speed', 0xA5:'mod2_range',
  314.                         0xA6:'mod3_curve', 0xA7:'mod3_phase', 0xA8:'mod3_depth', 0xA9:'mod3_speed', 0xAB:'mod3_range',
  315.                         0xAC:'mod4_curve', 0xAD:'mod4_phase', 0xAE:'mod4_depth', 0xAF:'mod4_speed', 0xB1:'mod4_range'
  316.                     }
  317.                     tmpcmd['name'] = f0a0Range[cmd]
  318.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.UINT8
  319.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  320.                
  321.                 elif cmd == 0xE0: #userproc
  322.                     tmpcmd['name'] = 'userproc'
  323.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.UINT16
  324.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  325.                
  326.                 elif 0xE1 <= cmd <= 0xE6:
  327.                     f0e0Range = {
  328.                         0xE1:'mod2_delay', 0xE2:'mod2_period', 0xE3:'mod3_delay',
  329.                         0xE4:'mod3_period', 0xE5:'mod4_delay', 0xE6:'mod4_period'
  330.                     }
  331.                     tmpcmd['name'] = f0e0Range[cmd]
  332.                     if tmpcmd['arg1Type'] == None: tmpcmd['arg1Type'] = argTypes.INT16
  333.                     tmpcmd['args'] += readArg(self.bcseq, tmpcmd['arg1Type'])
  334.                
  335.                 else:
  336.                     sys.exit('Unrecognized extended command: 0xFF 0x%X, at 0x%X' % (cmd, self.blocks['DATA']['offset'] + 8 + tmpcmd['offset']))
  337.            
  338.             elif cmd == 0xFB: #env_reset
  339.                 tmpcmd['name'] = 'env_reset'
  340.            
  341.             elif cmd == 0xFC: #loop_end
  342.                 tmpcmd['name'] = 'loop_end'
  343.            
  344.             elif cmd == 0xFD: #ret
  345.                 tmpcmd['name'] = 'ret'
  346.                 if tmpcmd['suffix3'] == '':
  347.                     parsing = False
  348.            
  349.             elif cmd == 0xFE: #alloctrack
  350.                 tmpcmd['name'] = 'alloctrack'
  351.                 tmpcmd['args'].append('0x%X' % (readBytes(self.bcseq, 2, endian='big'))) #TODO REPLACE
  352.            
  353.             elif cmd == 0xFF: #fin
  354.                 tmpcmd['name'] = 'fin'
  355.                 if tmpcmd['suffix3'] == '':
  356.                     parsing = False
  357.            
  358.             else:
  359.                 sys.exit('Unrecognized command: 0x%X, at 0x%X' % (cmd, self.blocks['DATA']['offset'] + 8 + tmpcmd['offset']))
  360.            
  361.             if self.commandOffsets.get(tmpcmd['offset'], False) == True:
  362.                 parsing = False
  363.             else:
  364.                 self.commands.append(tmpcmd)
  365.                 self.commandOffsets[tmpcmd['offset']] = True
  366.        
  367.         self.bcseq.seek(startPos)
  368.    
  369.     def sortCommands(self):
  370.         self.commands.sort(key=lambda x: int(x['offset']))
  371.    
  372.     def genCseq(self):
  373.         out = ''
  374.         out += ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n'
  375.         out += '; Input file: %s\n' % (os.path.basename(self.filename))
  376.         out += ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n'
  377.         for command in self.commands:
  378.             line = ''
  379.             label = next( (x for x in self.labels if x['offset'] == command['offset']), None)
  380.             if label != None:
  381.                 line += label['label'] + ':\n'
  382.             line += '    '
  383.             line += command['name'] + command['suffix1'] + command['suffix2'] + command['suffix3']
  384.             if command['args']:
  385.                 line += ' ' + ', '.join(x for x in command['args'])
  386.             line += '\n'
  387.             if command['name'] == 'jump' or command['name'] == 'ret' or command['name'] == 'fin':
  388.                 line += '\n'
  389.            
  390.             out += line
  391.         return out
  392.  
  393. #Yuck, just wanted a generic function to handle this...
  394. def readBytes(file, bytes, endian='little', signed=False):
  395.     val = 0
  396.    
  397.     for x in xrange(bytes):
  398.         if endian == 'little':
  399.             SHIFT = (x * 8)
  400.         else: #Assume 'big' if not 'little'
  401.             SHIFT = ((bytes - 1 - x) * 8)
  402.         val |= struct.unpack('B', file.read(1))[0] << SHIFT
  403.    
  404.     if signed == True:
  405.         if val >= (1 << ((bytes*8)-1)):
  406.             val = val - (1 << (bytes*8))
  407.    
  408.     return val
  409.  
  410. def readVarLen(file):
  411.     val = 0
  412.    
  413.     temp = 0x80
  414.     while temp & 0x80 != 0:
  415.         temp = struct.unpack('B', file.read(1))[0]
  416.         val = (val << 7) | (temp & 0x7F)
  417.    
  418.     return val
  419.  
  420. def readArg(file, argType):
  421.     """Returns a list because the RANDOM argType has two values"""
  422.     val = []
  423.    
  424.     if argType == argTypes.UINT8:
  425.         val.append(str(readBytes(file, 1)))
  426.     elif argType == argTypes.INT8:
  427.         val.append(str(readBytes(file, 1, signed=True)))
  428.     elif argType == argTypes.UINT16:
  429.         val.append(str(readBytes(file, 2, endian='big')))
  430.     elif argType == argTypes.INT16:
  431.         val.append(str(readBytes(file, 2, endian='big', signed=True)))
  432.     elif argType == argTypes.RANDOM:
  433.         val.append(str(readBytes(file, 2, endian='big', signed=True)))
  434.         val.append(str(readBytes(file, 2, endian='big', signed=True)))
  435.     elif argType == argTypes.VARIABLE:
  436.         val.append(varToStr(readBytes(file, 1)))
  437.     elif argType == argTypes.VARLEN:
  438.         val.append(str(readVarLen(file)))
  439.    
  440.     return val
  441.  
  442. def keyToStr(inp):
  443.     keys = ['cn', 'cs', 'dn', 'ds', 'en', 'fn', 'fs', 'gn', 'gs', 'an', 'as', 'bn']
  444.     key = keys[inp % 12]
  445.     octave = (inp // 12) - 1 #key 0x00 = C(-1), cnm1 in text CSEQ format
  446.     suffix = 'm' if octave < 0 else ''
  447.     return key + suffix + str(abs(octave))
  448.  
  449. def varToStr(num):
  450.     prefixes = ['', 'G', 'T']
  451.     return '%sVAR_%d' % (prefixes[num // 16], (num % 16))
  452.  
  453. def main(argc, argv):
  454.     if argc < 2 or not os.path.isfile(argv[1]):
  455.         sys.exit('usage: %s file.bcseq' % (os.path.basename(__file__)))
  456.    
  457.     cseq = BCSEQ(argv[1])
  458.    
  459.     #for label in cseq.labels:
  460.     #    print label
  461.    
  462.     print cseq.genCseq()
  463.  
  464. if __name__ == '__main__':
  465.     main(len(sys.argv), sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement