Skip to content

Instantly share code, notes, and snippets.

@ipiyer
Created November 4, 2017 18:29
Show Gist options
  • Select an option

  • Save ipiyer/286f18e629303510bd83fc8572f6c560 to your computer and use it in GitHub Desktop.

Select an option

Save ipiyer/286f18e629303510bd83fc8572f6c560 to your computer and use it in GitHub Desktop.
calculate pcm audio length
const { Transform } = require("readable-stream");
const fs = require('fs');
class PCMTime extends Transform {
constructor() {
super();
this.length = 0;
// bitsPerSample * samplesPerSecond * channels
this.bytesPerSec = (16 * 44100 * 1) / 8;
}
_transform(chunk, env, cb) {
this.length += chunk.length;
cb(null, chunk);
}
end() {
console.log("pcm audio length:", this.bytesPerSec / this.length);
}
}
var test = fs
.createReadStream("asdf_in.pcm")
.pipe(new PCMTime)
.pipe(fs.createWriteStream('test.pcm'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment