我正在尝试使用 JavaScript 中的 Wavefile 库翻译一个将 Wav 文件的数据从 Python 交错到 JavaScript 的函数。我对该函数的翻译交织了文件,但没有输出正确的 Wav 文件。
这是我的代码:
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;
}
这里是Python代码:
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