gpt4 book ai didi

javascript - 如何使用javascript将音频文件转换为字节数组

转载 作者:搜寻专家 更新时间:2023-11-01 00:28:21 24 4
gpt4 key购买 nike

我正在尝试将 .wav 文件转换为字节数组字符串。我需要在后端执行此操作,目标文件正在成为一个问题。

        files.forEach(file => {
let index = files.indexOf(file)
let reader = new FileReader();
reader.readAsArrayBuffer(file);
console.log(reader.result);

reader.onload = function (event) {
let byteArray = new Uint8Array(reader.result);
let FileName = file.name;
let dataAsByteArrayString = byteArray.toString();
var listHtml = $list.html();

上面的代码使用了 npm 的文件读取器,它说要定位一个文件。我很难做到这一点,因为这不是文件的前端拖放。

我生成的文件名为“response.wav”,我如何使用 JavaScript 和 Node 扩展来转换此文件?谢谢!

最佳答案

我不知道这是否有帮助,但在我工作的最后一个项目中,我们使用 Node 缓冲区 API 解析了一个 .wav 文件,并使用 Node 文件 API 编写了它。如果您对代码有更多疑问,我可以将您转给最常处理此文件的人。我希望它有所帮助 https://github.com/IntelliSound/intelliSound-Server/blob/development/lib/sound-data-parser.js

'use strict';

function ParsedWave(buffer) {

const RIFF_HEADER_OFFSET = 0;
const FILE_SIZE_OFFSET = 4;
const RIFF_FORMAT_OFFSET = 8;
const SUBCHUNK1_ID_OFFSET = 12;
const AUDIO_FORMAT_OFFSET = 20;
const NUMBER_OF_CHANNELS_OFFSET = 22;
const SAMPLE_RATE_OFFSET = 24;
const BITS_PER_SAMPLE_OFFSET = 34;
const SUBCHUNK2_ID_OFFSET = 36;
const SUBCHUNK2_SIZE_OFFSET = 40;
const DATA_OFFSET = 44;

this.buffer = buffer;
this.riff = buffer.slice(RIFF_HEADER_OFFSET, RIFF_HEADER_OFFSET + 4).toString('utf8');
this.fileSize = buffer.readUInt32LE(FILE_SIZE_OFFSET);
this.riffType = buffer.slice(RIFF_FORMAT_OFFSET, RIFF_FORMAT_OFFSET + 4).toString('utf8');
this.subChunk1Id = buffer.slice(SUBCHUNK1_ID_OFFSET, SUBCHUNK1_ID_OFFSET + 4).toString('utf8');
this.audioFormat = buffer.readUInt16LE(AUDIO_FORMAT_OFFSET);
this.numberOfChannels = buffer.readUInt16LE(NUMBER_OF_CHANNELS_OFFSET);
this.sampleRate = buffer.readUInt32LE(SAMPLE_RATE_OFFSET);
this.bitsPerSample = buffer.readUInt16LE(BITS_PER_SAMPLE_OFFSET);
this.subChunk2Id = buffer.slice(SUBCHUNK2_ID_OFFSET, SUBCHUNK2_ID_OFFSET + 4).toString('utf8');
this.subChunk2Size = buffer.readUInt32LE(SUBCHUNK2_SIZE_OFFSET);
this.data = buffer.slice(DATA_OFFSET, this.subChunk2Size + DATA_OFFSET);
}

// Andrew - The bufferMapper function is going to accept a parsed wave-file and output
// an array of values corresponding to the data subchunk in a format which can
// be accepted as input to the neural network.

const bufferMapper = parsedWave => {

const SIXTEEN_BIT_ZERO = 32768;
const SIXTEEN_BIT_MAX = 65535;

parsedWave.neuralArray = [];

for (let i = 0; i < parsedWave.data.length; i += 2) {
const sample = parsedWave.data.readInt16LE(i);
const unsignedSample = sample + SIXTEEN_BIT_ZERO;
const sigmoidSample = unsignedSample / SIXTEEN_BIT_MAX;
parsedWave.neuralArray.push(sigmoidSample);
}
return parsedWave;
};

module.exports = data => {

const parsedWaveFile = new ParsedWave(data);

if (parsedWaveFile.riff !== 'RIFF') {
throw new TypeError('incorrect file type, must be RIFF format');
}

if (parsedWaveFile.fileSize > 10000000) {
throw new TypeError('file too large, please limit file size to less than 10MB');
}

if (parsedWaveFile.riffType !== 'WAVE') {
throw new TypeError('file must be a WAVE');
}

if (parsedWaveFile.subChunk1Id !== 'fmt ') {
throw new TypeError('the first subchunk must be fmt');
}

if (parsedWaveFile.audioFormat !== 1) {
throw new TypeError('wave file must be uncompressed linear PCM');
}

if (parsedWaveFile.numberOfChannels > 2) {
throw new TypeError('wave file must have 2 or less channels');
}

if (parsedWaveFile.sampleRate > 48000) {
throw new TypeError('wave file must have sample rate of less than 48k');
}

if (parsedWaveFile.bitsPerSample !== 16) {
throw new TypeError(`file's bit depth must be 16`);
}

if (parsedWaveFile.subChunk2Id !== 'data') {
throw new TypeError('subchunk 2 must be data');
}

const neuralMappedWaveFile = bufferMapper(parsedWaveFile);

return neuralMappedWaveFile;
};

关于javascript - 如何使用javascript将音频文件转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49091040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com