from os import path
import numpy as np
inpath = 'C:\\example\\wav_four_seconds.wav'
outpath = 'C:\\example\\artificial_take_output.vr8'
input_wav_file_size = path.getsize(inpath)
# Okay, so ... the algorithm is to figure out where the header ends and strip it out.
# Then just numpy-little-to-big-endian the whole file.
def chunks_from_wav_file_after_header(filename, chunksize):
with open(filename, "rb") as f:
first_128_bytes_of_wav = f.read(128)
bytes_the_word_data_literal = b'\x64\x61\x74\x61' # "data"
start_of_data_position = first_128_bytes_of_wav.find(bytes_the_word_data_literal) + 8
f.seek(start_of_data_position) # Skip past header to data
while True:
chunk = f.read(chunksize)
if chunk:
yield chunk
else:
break
def reverse_littleendian_chunk_to_bigendian(chunk):
little_end_np_arr = np.ndarray(shape=(int(len(chunk) / 2),), dtype='<i2', buffer=chunk)
converted_big_end_bytes = little_end_np_arr.astype('>i2').tobytes()
return converted_big_end_bytes
# ####################################################
f_out = open(outpath, 'wb')
input_wav_large_chunk_generator = chunks_from_wav_file_after_header(inpath, 65536)
for chunk in input_wav_large_chunk_generator:
reversed_endianness_chunk = reverse_littleendian_chunk_to_bigendian(chunk)
f_out.write(reversed_endianness_chunk)
f_out.close()