I have access to a byte array file (float32, big endian) from a URL, and I'm trying to convert it into something processable by WaveSurfer, which needs a valid ArrayBuffer, AudioBuffer, or a URL to a wav file.
The closest I've gotten is using fetch to get the file, and trying to convert it to an ArrayBuffer, however this is not recognizable by WaveSurfer, which says it is unable to decode the data.
This is my JavaScript code:
let blob = await fetch(url);
let buffer = await blob.arrayBuffer();
this.wavesurfer.loadArrayBuffer(buffer);
I know the file I'm trying to decode is a valid audio file, as I can convert it to a wav file using the following python script:
import numpy as np
from scipy.io.wavfile import write
def main():
a = np.fromfile(filePath, '>f4')
write(outputPath, 16000, a)
Any suggestions on what I can do?
Holy crap, i've finally figured it out. My solution ended up using this package.
Starting with a binary file that i need to read in a big-endian Float32 format:
import createBuffer from "audio-buffer-from"
var res = await fetch(response.url);
var buffer = await res.arrayBuffer();
var dataview = new DataView(buffer);
var mFloatArray = new Float32Array(buffer.byteLength / 4);
for (let i = 0; i < mFloatArray.length; i++) {
mFloatArray[i] = dataview.getFloat32(i*4);
}
audioBuffer = createBuffer(mFloatArray, { sampleRate: 16000 });
this.wavesurfer.loadDecodedBuffer(audioBuffer);
This successfully loaded into wavesurfer!