Open maxpain opened 1 year ago
I wasn't able to get the above working for me.
But I was able to use @wxfred 's suggestion to get it working using the following:
// Where wav is a correctly created WaveFile:
const duration: number = wav.chunkSize / wav.fmt.numChannels / wav.fmt.sampleRate / (wav.fmt.bitsPerSample / 8)
// Some debug to validate values are coming back as expected.
console.log(
'wav.chunkSize', wav.chunkSize,
'wav.fmt.numChannels', wav.fmt.numChannels,
'wav.fmt.sampleRate', wav.fmt.sampleRate,
'wav.fmt.bitsPerSample', wav.fmt.bitsPerSample
);
If you save the wave file, on MacOS you can validate the calculated duration of a final output using the built in utility:
afinfo filename.wav | grep duration
I found they were very close, but not exactly the same.
I calculate it in ms like this:
var duration = wav.data.chunkSize * 1000 / wav.fmt.blockAlign / wav.fmt.sampleRate / wav.fmt.numChannels;
Try this