I want to use the yamnet TensorFlow model, but it needs to tensor between [-1, 1]
to enter in the model.
How can I do that? I want to transform it to [-1, 1]
?
Option #1
const readWavAudio = async () => {
wavfiles = await fs.readFileSync('archive/dataset/1/1_484.wav')
const wav = new wavefile.WaveFile();
wav.fromBuffer(wavfiles)
wav.toSampleRate(16000);
return await wav.getSamples(false, Float32Array)
}
Option #2
async function readbuf_async() {
data = fs.readFileSync("teste.wav");
buffer = [];
for (o = 0; o < data.length - 4 * 1; o += 4 * 1) {
buffer.push(data.readFloatBE(o));
}
return buffer;
}
2
Answers
You can scale the vector to a range between
[-1, 1]
by utilizing linear interpolation:Full example
Here is a Node.js module that creates a JSON data file containing the desired values.
Output
The Node.js module script will create a JSON file that contains an array of values between
[-1, 1]
.Optimization
Here is a slightly optimized version of
normalizeVector
:Note: The
2
is the result ofmin - max
of the adjusted scale i.e.1 - (-1)
.I was able to make use of wavefile for this. In my case, I was starting with base64 encoded mulaw audio from Twilio. Dividing by 32768 (the max 16-bit signed integer) is what gets you values between [-1, 1].