diff --git a/pytrax.py b/pytrax.py new file mode 100644 index 0000000..2d8425e --- /dev/null +++ b/pytrax.py @@ -0,0 +1,23 @@ +from pytrax import impulsetracker + +if __name__ == "__main__": + import sys + import pprint + if len(sys.argv) > 1: + info = impulsetracker.parse_file(sys.argv[1], with_samples=True, with_instruments=True, with_patterns=True) + instruments = info.pop("instruments", None) + samples = info.pop("samples", None) + patterns = info.pop("patterns", None) + + for i in info: + print i, "=>", info[i] + print + if instruments: + print len(instruments), "instruments" + if samples: + print len(samples), "samples" + if patterns: + print len(patterns), "patterns" + else: + print "usage:", sys.argv[0], "FILE.it" + diff --git a/pytrax/impulsetracker.py b/pytrax/impulsetracker.py index 82192aa..a80b2f2 100644 --- a/pytrax/impulsetracker.py +++ b/pytrax/impulsetracker.py @@ -3,10 +3,10 @@ import struct -IT_HEADER = '4x26s2x8H5BxHL4x128B' -IT_HEADER_INS = '4x12sx3BH6BHBx26s6x120H' -IT_HEADER_SMP = '4x12sx3B26s2B7L4B' -IT_HEADER_PAT = '2H4x' +IT_HEADER = '<4x26s2x8H5BxHL4x128B' +IT_HEADER_INS = '<4x12sx3BH6BHBx26s6x120H' +IT_HEADER_SMP = '<4x12sx3B26s2B7L4B' +IT_HEADER_PAT = '<2H4x' def parse_file(filename, with_instruments=False, @@ -42,12 +42,12 @@ def parse(file, 'pansep': data[13], 'pantable': data[16:80], 'voltable': data[80:144], - 'orders': struct.unpack('%dB' % data[1], file.read(data[1])), + 'orders': struct.unpack('<%dB' % data[1], file.read(data[1])), } - - insoffs = struct.unpack('%dL' % data[2], file.read(data[2] * 4)) - smpoffs = struct.unpack('%dL' % data[3], file.read(data[3] * 4)) - patoffs = struct.unpack('%dL' % data[4], file.read(data[4] * 4)) + + insoffs = struct.unpack('<%dL' % data[2], file.read(data[2] * 4)) + smpoffs = struct.unpack('<%dL' % data[3], file.read(data[3] * 4)) + patoffs = struct.unpack('<%dL' % data[4], file.read(data[4] * 4)) info['message'] = '' if data[8] & 0x01: @@ -57,6 +57,10 @@ def parse(file, if with_instruments: info['instruments'] = _get_instruments(file, insoffs) if with_samples: info['samples'] = _get_samples(file, smpoffs) if with_patterns: info['patterns'] = _get_patterns(file, patoffs) + + # add sample data to samples + if not (info.has_key('instruments') and info["instruments"]) and with_samples: + _load_sample_data(file, info['samples']) return info @@ -121,6 +125,12 @@ def _get_samples(file, offs): return result +def _load_sample_data(file, samples): + for s in samples: + file.seek(s['offset']) + s['sampledata'] = file.read(s['length']) + return samples + def _get_patterns(file, offs): result = [] diff --git a/setup.py b/setup.py index 6c86058..cf9e19f 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from distutils.core import setup setup(name='pytrax', - version='0.1', + version='0.2', description='Impulse Tracker parser', author='Dave Benjamin', author_email='dave@ramenlabs.com',