Last active
January 14, 2016 15:25
-
-
Save jakubfiala/cb9de100fdd4f043d46e to your computer and use it in GitHub Desktop.
This is a simple JS class using the wav module in node to read a wav file into a Float32Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
WavManager | |
========== | |
simple class to open and stream a WAV file as a Float32Array | |
by jakub fiala | |
goldsmiths computing, 2015 | |
*/ | |
var WavManager = function(data_callback, end_callback) { | |
var source = new Buffer(1) | |
var fs = require('fs') | |
var wav = require('wav') | |
var _dcb = data_callback | |
var _ecb = end_callback | |
var _bitDepth = 16 | |
var _numBytesPerSample = 2 | |
var _endian = 'LE' | |
var _format = 'unknown' | |
this.format = function() { | |
return _format | |
} | |
this.open = function (path) { | |
var file = fs.createReadStream(path) | |
source = new Buffer(1); //empty the current source if needed | |
var reader = new wav.Reader() | |
reader.on('format', function(format) { | |
_bitDepth = format.bitDepth | |
_numBytesPerSample = _bitDepth/8 | |
_endian = format.endianness | |
_format = format | |
}) | |
reader.on('data', function(_d) { | |
source = Buffer.concat([source, _d], source.length + _d.length) | |
var output = new Float32Array(_d.length/_numBytesPerSample) | |
for (var i = 0; i < _d.length/_numBytesPerSample; i += _numBytesPerSample) { | |
if (_endian == 'BE') | |
output[i] = _d.readInt16BE(i) | |
else | |
output[i] = _d.readInt16LE(i) | |
} | |
if (_dcb) _dcb(output) | |
}) | |
reader.on('end', function() { | |
var output = new Float32Array(source.length/_numBytesPerSample) | |
for (var i = 0; i < source.length/_numBytesPerSample; i += _numBytesPerSample) { | |
if (_endian == 'BE') | |
output[i] = source.readInt16BE(i) | |
else | |
output[i] = source.readInt16LE(i) | |
} | |
if (_ecb) _ecb(output) | |
}) | |
file.pipe(reader) | |
} | |
} | |
module.exports = WavManager |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment