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