I've been working on a audio-recognize demo for some time, and the api needs me to pass an .wav file with sample rate of 8000 or 16000, so I have to downsample it. I have tried 2 algorithms as following. Though none of them solves the problem as I wish, there's some differences of the results and I hope that will make it more clear.
This is my first try, and it works fine when sampleRate % outputSampleRate = 0, however when outputSampleRate = 8000 or 1600, the outcome audio file is silent(which means the value of every element of the output array is 0):
function interleave(inputL){
var compression = sampleRate / outputSampleRate;
var length = inputL.length / compression;
var result = new Float32Array(length);
var index = 0,
inputIndex = 0;
while (index < length){
result[index++] = inputL[inputIndex];
inputIndex += compression;
}
return result;
}
So here's my second try which comes from a giant company, and it doesn't work too. What's more, when I set sampleRate % outputSampleRate = 0 it still output a silent file:
function interleave(e){
var t = e.length;
var n = new Float32Array(t),
r = 0,
i;
for (i = 0; i < e.length; i++){
n[r] = e[i];
r += e[i].length;
}
sampleRate += 0.0;
outputSampleRate += 0.0;
var s = 0,
o = sampleRate / outputSampleRate,
u = Math.ceil(t * outputSampleRate / sampleRate),
a = new Float32Array(u);
for (i = 0; i < u; i++) {
a[i] = n[Math.floor(s)];
s += o;
}
return a
}
In case my settings were wrong, here's the encodeWAV function:
function encodeWAV(samples){
var sampleBits = 16;
var dataLength = samples.length*(sampleBits/8);
var buffer = new ArrayBuffer(44 + dataLength);
var view = new DataView(buffer);
var offset = 0;
/* RIFF identifier */
writeString(view, offset, 'RIFF'); offset += 4;
/* file length */
view.setUint32(offset, 32 + dataLength, true); offset += 4;
/* RIFF type */
writeString(view, offset, 'WAVE'); offset += 4;
/* format chunk identifier */
writeString(view, offset, 'fmt '); offset += 4;
/* format chunk length */
view.setUint32(offset, 16, true); offset += 4;
/* sample format (raw) */
view.setUint16(offset, 1, true); offset += 2;
/* channel count */
view.setUint16(offset, outputChannels, true); offset += 2;
/* sample rate */
view.setUint32(offset, outputSampleRate, true); offset += 4;
/* byte rate (sample rate * block align) */
view.setUint32(offset, outputSampleRate*outputChannels*(sampleBits/8), true); offset += 4;
/* block align (channel count * bytes per sample) */
view.setUint16(offset, outputChannels*(sampleBits/8), true); offset += 2;
/* bits per sample */
view.setUint16(offset, sampleBits, true); offset += 2;
/* data chunk identifier */
writeString(view, offset, 'data'); offset += 4;
/* data chunk length */
view.setUint32(offset, dataLength, true); offset += 4;
floatTo16BitPCM(view, offset, samples);
return view;
}
It has confused me for a very long time, please let me know what I missed...
-----------------------------AFTER IT'S SOLVED--------------------------------
I'm glad it's running well now and here's the right edition of function interleave():
function interleave(e){
var t = e.length;
sampleRate += 0.0;
outputSampleRate += 0.0;
var s = 0,
o = sampleRate / outputSampleRate,
u = Math.ceil(t * outputSampleRate / sampleRate),
a = new Float32Array(u);
for (i = 0; i < u; i++) {
a[i] = e[Math.floor(s)];
s += o;
}
return a;
}
So you can see it's the variable that I passed to it was not of the proper type~ And thanks again for dear @jaket and other friends~ Though I figured it out myslf someway, they let me know the original things better~~~ :)