SHARE
TWEET

gs2c.py

a guest Aug 13th, 2016 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4.  
  5. import sys
  6. import os.path
  7.  
  8. def parseint(s):
  9.     if s.startswith('$'):
  10.         return int(s[1:], 16)
  11.     return int(s)
  12.  
  13. def hexint(n):
  14.     return '$' + hex(n)[2:].upper()
  15.  
  16. def convert(filename):
  17.     lines = []
  18.     with open(filename, 'r') as f:
  19.         for line in f:
  20.             line = line.strip()
  21.             line = convert_line(line)
  22.             if not(line.startswith(';') or line.endswith(':') or not line):
  23.                 line = '\t\t' + line
  24.             lines.append(line)
  25.     return '\n'.join(lines)
  26.  
  27. def convert_line(line):
  28.     # Example: db oct0 == octave 1
  29.     # Example: db oct5 == octave 6
  30.     if line.startswith('db oct'):
  31.         octave = parseint(line[6:]) + 1
  32.         return 'octave %s' % (octave,)
  33.  
  34.     # Example: db (ntA# | 0) == note A#, 1
  35.     # Example: db (ntRst | 3) == note __, 4
  36.     if line.startswith('db (nt'):
  37.         _, note, _, length = line.split(None, 3)
  38.         note = note.lstrip('(')[2:]
  39.         length = length.rstrip(')')
  40.         if note == 'Rst':
  41.             note = '__'
  42.         elif not note.endswith('#'):
  43.             note += '_'
  44.         length = parseint(length) + 1
  45.         return 'note %s, %s' % (note, length)
  46.  
  47.     # Example: pkmsSetVel 1, 3 == intensity $13
  48.     if line.startswith('pkmsSetVel'):
  49.         _, vel, length = line.split(None, 2)
  50.         vel = parseint(vel.rstrip(','))
  51.         length = parseint(length)
  52.         intensity = hexint((vel << 4) | length)
  53.         return 'intensity %s' % (intensity,)
  54.  
  55.     # Example: pkmsSetNtr $C, 9, 4 == notetype $C, $94
  56.     if line.startswith('pkmsSetNtr'):
  57.         _, speed, vel, length = line.split(None, 3)
  58.         speed = parseint(speed.rstrip(','))
  59.         vel = parseint(vel.rstrip(','))
  60.         length = parseint(length)
  61.         intensity = hexint((vel << 4) | length)
  62.         return 'notetype %s, %s' % (speed, intensity)
  63.  
  64.     # Example: pkmsSetDSpeed $C == notetype $C
  65.     if line.startswith('pkmsSetDSpeed'):
  66.         _, speed = line.split(None, 1)
  67.         return 'notetype %s' % (speed,)
  68.  
  69.     # Example: pkmsEndSound == endchannel
  70.     if line.startswith('pkmsEndSound'):
  71.         return 'endchannel'
  72.  
  73.     # Example: pkmsSetMod $C, 1, 2 == vibrato $C, $12
  74.     if line.startswith('pkmsSetMod'):
  75.         _, delay, depth, rate = line.split(None, 3)
  76.         delay = delay.rstrip(',')
  77.         depth = parseint(depth.rstrip(','))
  78.         rate = parseint(rate)
  79.         extent = hexint((depth << 4) | rate)
  80.         return 'notetype %s, %s' % (delay, extent)
  81.  
  82.     # Example: pkmsSetDuty 2 == dutycycle 2
  83.     if line.startswith('pkmsSetDuty'):
  84.         _, duty = line.split(None, 1)
  85.         return 'dutycycle %s' % (duty,)
  86.  
  87.     # Example: pkmsSetDrums 5 == togglenoise 5
  88.     if line.startswith('pkmsSetDrums'):
  89.         _, drums = line.split(None, 1)
  90.         return 'togglenoise %s' % (drums,)
  91.  
  92.     # Example: pkmsSetArp $C == unknownmusic0xde $C
  93.     if line.startswith('pkmsSetArp'):
  94.         _, x = line.split(None, 1)
  95.         return 'unknownmusic0xde %s' % (x,)
  96.  
  97.     # Example: pkmsSetTempo 0, $80 == tempo $80
  98.     if line.startswith('pkmsSetTempo'):
  99.         _, divider, modifier = line.split(None, 2)
  100.         divider = divider.rstrip(',')
  101.         hi = parseint(divider)
  102.         lo = parseint(modifier)
  103.         tempo = hexint((hi << 8) | lo)
  104.         return 'tempo %s' % (tempo,)
  105.  
  106.     # Example: pkmsSetVolume $77 == volume $77
  107.     if line.startswith('pkmsSetVolume'):
  108.         _, volume = line.split(None, 1)
  109.         return 'volume %s' % (volume,)
  110.  
  111.     # Example: pkmsCall NewSong_Channel1 + SONG_START == callchannel NewSong_Channel1
  112.     if line.startswith('pkmsCall'):
  113.         _, offset = line.split(None, 1)
  114.         offset = offset.replace('+ SONG_START', '')
  115.         return 'callchannel %s' % (offset,)
  116.  
  117.     # Example: pkmsJump Channel1_Loop + SONG_START == loopchannel 0, Channel1_Loop
  118.     if line.startswith('pkmsJump'):
  119.         _, offset = line.split(None, 1)
  120.         offset = offset.replace('+ SONG_START', '')
  121.         return 'loopchannel 0, %s' % (offset,)
  122.  
  123.     if line.startswith(';') or line.endswith(':') or not line:
  124.         return line
  125.  
  126.     return line + ' ; WARNING: unconverted'
  127.  
  128. def write(content, old_filename):
  129.     name, ext = os.path.splitext(old_filename)
  130.     new_filename = name + '_c' + '.' + ext
  131.     with open(new_filename, 'w') as f:
  132.         f.write(content)
  133.  
  134. def main():
  135.     if len(sys.argv) < 2:
  136.         print('Usage: gs2c.py filename.asm', file=sys.stderr)
  137.         sys.exit(1)
  138.     filename = sys.argv[1]
  139.     content = convert(filename)
  140.     write(content, filename)
  141.  
  142. if __name__ == '__main__':
  143.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top