Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: Python  |  size: 2.14 KB  |  hits: 44  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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') + 4 + 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.     print 'Conversion starting...'
  36.     filenames = [filename for filename in os.listdir('.') if os.path.splitext(filename)[1] == '.brstm']
  37.     for filename in filenames:
  38.         print 'Processing {0}...'.format(filename)
  39.         file_info = subprocess.check_output(['test','-b',filename])
  40.         number_of_channels = get_info(file_info, 'chan')
  41.         loop_start = get_info(file_info, 'lstart')
  42.         loop_end = get_info(file_info, 'lend')
  43.         for i in range(0,number_of_channels/2):
  44.             out_filename = os.path.splitext(filename)[0] + '_{0}.lwav'.format(i)
  45.             subprocess.call(['test',
  46.                 '-o',out_filename,
  47.                 '-l','1',
  48.                 '-f','0',
  49.                 '-d','0',
  50.                 '-2',str(i),
  51.                 filename])
  52.             convert_to_lwav(out_filename,loop_start,loop_end)
  53.  
  54. if __name__ == '__main__':
  55.     main()