
Untitled
By: a guest on
Apr 30th, 2012 | syntax:
Python | size: 2.14 KB | hits: 44 | expires: Never
import os
import struct
import subprocess
def convert_to_lwav(filename,loop_start,loop_end):
_file = open(filename,'rb')
content = _file.read()
_file.close()
#Append smpl chunk
smpl_chunk_size = 60
bytes_after_chunk_size = '\x00' * 28 + struct.pack('<i', 1) + '\x00' * 12
ending_bytes = '\x00' * 8
content += 'smpl' + struct.pack('<i', smpl_chunk_size) + bytes_after_chunk_size
content += struct.pack('<i',loop_start) + struct.pack('<i',loop_end)
content += ending_bytes
#Update the RIFF header chunk size
riff_index = content.index('RIFF')
riff_header_chunk_size = struct.unpack('<i', content[riff_index + len('RIFF'):riff_index + len('RIFF') + 4])
riff_header_chunk_size = int(riff_header_chunk_size[0])
riff_header_chunk_size += len('smpl') + 4 + smpl_chunk_size
new_content = content[0:riff_index + len('RIFF')] + struct.pack('<i', riff_header_chunk_size)
new_content += content[len(new_content):]
_file = open(filename,'wb')
_file.write(new_content)
_file.close()
def get_info(file_info, required_info):
info = file_info[file_info.index(required_info):]
info = info[:info.index('\n')]
info = int(info.strip().split('=')[1])
return info
def main():
print 'Conversion starting...'
filenames = [filename for filename in os.listdir('.') if os.path.splitext(filename)[1] == '.brstm']
for filename in filenames:
print 'Processing {0}...'.format(filename)
file_info = subprocess.check_output(['test','-b',filename])
number_of_channels = get_info(file_info, 'chan')
loop_start = get_info(file_info, 'lstart')
loop_end = get_info(file_info, 'lend')
for i in range(0,number_of_channels/2):
out_filename = os.path.splitext(filename)[0] + '_{0}.lwav'.format(i)
subprocess.call(['test',
'-o',out_filename,
'-l','1',
'-f','0',
'-d','0',
'-2',str(i),
filename])
convert_to_lwav(out_filename,loop_start,loop_end)
if __name__ == '__main__':
main()