I am trying to translate a function that interleaves the data of a Wav file from Python to JavaScript using the Wavefile library in JavaScript. My translation of the function interleaves the files but does not output the correct Wav file.
Here is my code:
function load_to_buffer_arrays(list_of_files) {
const output_data = [];
const raw_data_array = [];
const num_files = list_of_files.length;
for (let i = 0; i < num_files; i++) {
const file = list_of_files[i];
try {
const buffer = fs.readFileSync(file);
const wav_temp = new WaveFile(buffer);
raw_data_array.push(wav_temp.getSamples(false, Int16Array));
} catch (err) {
console.error(err);
}
}
const num_samples = raw_data_array[0].length;
for (let j = 0; j < num_samples; j++) {
for (let i = 0; i < num_files; i++) {
let sample = raw_data_array[i][j];
output_data.push(sample);
}
}
return output_data;
}
Here is the Python code:
def load_to_buffer_arrays(list_of_files):
"""Load files to buffer arrays and interleave the data into one single entity and pass it back"""
raw_data_array = []
for i, file in enumerate(list_of_files):
sound_file = AudioSegment.from_file(file)
sound_samples = sound_file.get_array_of_samples()
raw_data_array.append(sound_samples)
output_data = []
number_of_files = len(list_of_files)
length_of_one_raw_data = len(raw_data_array[0])
for i in range(0, length_of_one_raw_data):
for j in range(0,number_of_files):
sample = raw_data_array[j][i]
output_data.append(sample)
return output_data
Python uses the
array.arrayclass to store the raw sample data for each file. In JS, you're usingInt16Arraywhich while correct, you're not creating an array to store the sample data for each file. Instead, you're pushing the same sample data for each file in the dir to theraw_data_arrayarray.You'll need to create a new
Int16Arrayfor each file and push that intoraw_data_arrayinstead.