lbmrn-pcm-utils
Version:
PCM audio utilities for Node.js
81 lines (70 loc) • 2.56 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var Zipper, binding, pcm, stream,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
binding = require('../build/Release/binding');
stream = require('stream');
pcm = require('./constants');
Zipper = (function(superClass) {
extend(Zipper, superClass);
function Zipper(channels, format) {
var i, ref;
this.channels = channels != null ? channels : 2;
this.format = format != null ? format : pcm.FMT_F32LE;
stream.Readable.call(this);
this.alignment = pcm.ALIGNMENTS[this.format];
this.zipper = new binding.Zipper(this.channels, this.alignment, (function(_this) {
return function(err, chunk) {
if (err != null) {
throw err;
}
return _this.push(chunk);
};
})(this));
this.bufferSize = this.zipper.samplesPerBuffer * this.alignment;
this.inputs = (function() {
var j, ref, results;
results = [];
for (i = j = 0, ref = this.channels; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
results.push((function(_this) {
return function(i) {
return (new stream.PassThrough).on('readable', function() {
return _this.readInput(i);
});
};
})(this)(i));
}
return results;
}).call(this);
if (this.channels === 1) {
this.mono = this.inputs[0];
}
if (this.channels === 2) {
ref = [this.inputs[0], this.inputs[1]], this.left = ref[0], this.right = ref[1];
}
}
Zipper.prototype.readInput = function(channel) {
var chunk;
if (this.zipper.zipping || this.zipper.channelsReady[channel]) {
return false;
}
chunk = this.inputs[channel].read(this.bufferSize);
if (chunk == null) {
return false;
}
if (chunk.length > this.bufferSize) {
this.inputs[channel].unshift(chunk.slice(this.bufferSize, chunk.length));
chunk = chunk.slice(0, this.bufferSize);
}
return this.zipper.write(channel, chunk);
};
Zipper.prototype._read = function(size) {
var i, j, ref;
for (i = j = 0, ref = this.channels; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
this.readInput(i);
}
return this.push('');
};
return Zipper;
})(stream.Readable);
module.exports = Zipper;