Advertisement
Guest User

Converting a BRSTM to LWAV

a guest
Apr 26th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. import os
  2. import struct
  3. import subprocess
  4.  
  5. def convert_to_lwav(filename,loop_start,loop_end):
  6.     _file = open(filename,'rb')
  7.     content = _file.read()
  8.     _file.close()
  9.     #Append smpl chunk
  10.     smpl_chunk_size = 60
  11.     bytes_after_chunk_size = '\x00' * 28 + struct.pack('<i', 1) + '\x00' * 12
  12.     ending_bytes = '\x00' * 8
  13.    
  14.     content += 'smpl' + struct.pack('<i', smpl_chunk_size) + bytes_after_chunk_size
  15.     content += struct.pack('<i',loop_start) + struct.pack('<i',loop_end)
  16.     content += ending_bytes
  17.     #Update the RIFF header chunk size
  18.     riff_index = content.index('RIFF')
  19.     riff_header_chunk_size = struct.unpack('<i', content[riff_index + len('RIFF'):riff_index + len('RIFF') + 4])
  20.     riff_header_chunk_size = int(riff_header_chunk_size[0])
  21.     riff_header_chunk_size += len('smpl') + smpl_chunk_size
  22.     new_content = content[0:riff_index + len('RIFF')] + struct.pack('<i', riff_header_chunk_size)
  23.     new_content += content[len(new_content):]
  24.     _file = open(filename,'wb')
  25.     _file.write(new_content)
  26.     _file.close()
  27.  
  28. def get_info(file_info, required_info):
  29.     info = file_info[file_info.index(required_info):]
  30.     info = info[:info.index('\n')]
  31.     info = int(info.strip().split('=')[1])
  32.     return info
  33.  
  34. def main():
  35.     for dirpath, dirnames, filenames in os.walk('.'):
  36.         pass
  37.     #filenames = [filename for filename in filenames if os.path.splitext(filename)[1] == '.brstm']
  38.     filenames = ['STRM_N_FACTORY_N.brstm']
  39.     for filename in filenames:
  40.         file_info = subprocess.check_output(['test','-b',filename])
  41.         number_of_channels = get_info(file_info, 'chan')
  42.         loop_start = get_info(file_info, 'lstart')
  43.         loop_end = get_info(file_info, 'lend')
  44.         for i in range(0,number_of_channels/2):
  45.             out_filename = os.path.splitext(filename)[0] + '_{0}.lwav'.format(i)
  46.             subprocess.call(['test',
  47.                 '-o',out_filename,
  48.                 '-l','1',
  49.                 '-f','0',
  50.                 '-d','0',
  51.                 '-2',str(i),
  52.                 filename])
  53.             convert_to_lwav(out_filename,loop_start,loop_end)
  54.  
  55. if __name__ == '__main__':
  56.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement