Skip to content
Browse files

encoder: allow the pcm formatting to be configurable

Only the `channels` and `sampleRate` really.
Signed 32-bit float samples in native endianness are required other than that.
  • Loading branch information...
1 parent 5743f18 commit a5047c29698b38fd534d85cf2d22a74c7d6e2d88 @TooTallNate committed
Showing with 92 additions and 7 deletions.
  1. +92 −7 lib/encoder.js
View
99 lib/encoder.js
@@ -3,11 +3,17 @@
* Module dependencies.
*/
-var debug = require('debug')('vorbis:encoder');
+var os = require('os');
var binding = require('./binding');
var inherits = require('util').inherits;
var Transform = require('stream').Transform;
var ogg_packet = require('ogg').ogg_packet;
+var debug = require('debug')('vorbis:encoder');
+
+// determine the native host endianness, the only supported encoding endianness
+var endianness = 'function' == os.endianness ?
+ os.endianness() :
+ 'LE'; // assume little-endian for older versions of node.js
// node v0.8.x compat
if (!Transform) Transform = require('readable-stream/transform');
@@ -49,12 +55,11 @@ function Encoder (opts) {
throw new Error('"quality" must be in the range -0.1...1.0, got ' + this.quality);
}
- // PCM formatting options
- this.channels = 2;
- this.sampleRate = 44100;
- this.float = true;
- this.signed = true;
- this.bitDepth = 32; // sizeof(float) * 8
+ // set PCM formatting options
+ this._format({ channels: 2, sampleRate: 44100, float: true,
+ endianness: endianness, signed: true, bitDepth: 32 }); // defaults
+ this._format(opts);
+ this.on('pipe', this._pipe);
this.vi = new Buffer(binding.sizeof_vorbis_info);
this.vc = new Buffer(binding.sizeof_vorbis_comment);
@@ -309,3 +314,83 @@ Encoder.prototype._flush = function (output, fn) {
}
}
};
+
+/**
+ * Set given PCM formatting options. Called during instantiation on the passed in
+ * options object, on the stream given to the "pipe" event, and a final time if
+ * that stream emits a "format" event.
+ *
+ * @param {Object} opts
+ * @api private
+ */
+
+
+Encoder.prototype._format = function (opts) {
+ debug('format(keys = %j)', Object.keys(opts));
+
+ // channels is configurable
+ if (null != opts.channels) {
+ debug('setting "channels"', opts.channels);
+ this.channels = opts.channels;
+ }
+
+ // the sample rate is configurable
+ if (null != opts.sampleRate) {
+ debug('setting "sampleRate"', opts.sampleRate);
+ this.sampleRate = opts.sampleRate;
+ }
+
+ // only signed 32-bit float samples are supported
+ if (null != opts.bitDepth) {
+ if (32 == opts.bitDepth) {
+ debug('setting "bitDepth"', opts.bitDepth);
+ this.bitDepth = opts.bitDepth;
+ } else {
+ return this.emit('error', new Error('only signed `32-bit` float samples are supported, got "' + opts.bitDepth + '"'));
+ }
+ }
+
+ // only signed 32-bit float samples are supported
+ if (null != opts.float) {
+ if (opts.float) {
+ debug('setting "float"', opts.float);
+ this.float = opts.float;
+ } else {
+ return this.emit('error', new Error('only signed 32-bit `float` samples are supported, got "' + opts.float + '"'));
+ }
+ }
+
+ // only signed 32-bit float samples are supported
+ if (null != opts.signed) {
+ if (opts.signed) {
+ debug('setting "signed"', opts.signed);
+ this.signed = opts.signed;
+ } else {
+ return this.emit('error', new Error('only `signed` 32-bit float samples are supported, got "' + opts.signed + '"'));
+ }
+ }
+
+ // only native endianness is supported
+ if (null != opts.endianness) {
+ if (opts.endianness == endianness) {
+ debug('setting "endianness"', endianness);
+ this.endianness = endianness;
+ } else {
+ return this.emit('error', new Error('only native endianness ("' + endianness + '") is supported, got "' + opts.endianness + '"'));
+ }
+ }
+};
+
+/**
+ * Called when this stream is pipe()d to from another readable stream.
+ * If the "sampleRate", "channels", "bitDepth", "signed", etc. properties are
+ * set, then they will be used over the currently set values.
+ *
+ * @api private
+ */
+
+Encoder.prototype._pipe = function (source) {
+ debug('_pipe()');
+ this._format(source);
+ source.once('format', this._format.bind(this));
+};

0 comments on commit a5047c2

Please sign in to comment.
Something went wrong with that request. Please try again.