Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

encoder: getting things closer to working now

  • Loading branch information...
commit a678a1781e07639759942f5af95fafa20e735f32 1 parent c58cd80
@TooTallNate authored
Showing with 27 additions and 13 deletions.
  1. +27 −13 lib/encoder.js
View
40 lib/encoder.js
@@ -126,17 +126,17 @@ Encoder.prototype._writeHeader = function (output, fn) {
var sampleRate = this.sampleRate;
// TODO: async maybe?
r = binding.vorbis_encode_init_vbr(this.vi, channels, sampleRate, this._quality);
+ debug('vorbis_encode_init_vbr() return = %d', r);
if (0 !== r) return fn(new Error(r));
- // constant: number of 'bytes per sample'
- this.blockAlign = this.bitDepth / 8 * this.channels;
-
// synthesis init
this.vd = new Buffer(binding.sizeof_vorbis_dsp_state);
this.vb = new Buffer(binding.sizeof_vorbis_block);
var r = binding.vorbis_analysis_init(this.vd, this.vi);
+ debug('vorbis_analysis_init() return = %d', r);
if (0 !== r) return fn(new Error(r));
r = binding.vorbis_block_init(this.vd, this.vb);
+ debug('vorbis_block_init() return = %d', r);
if (0 !== r) return fn(new Error(r));
// create first 3 packets
@@ -145,15 +145,16 @@ Encoder.prototype._writeHeader = function (output, fn) {
var op_comments = new Buffer(binding.sizeof_ogg_packet);
var op_code = new Buffer(binding.sizeof_ogg_packet);
r = binding.vorbis_analysis_headerout(this.vd, this.vc, op_header, op_comments, op_code);
+ debug('vorbis_analysis_headerout() return = %d', r);
if (0 !== r) return fn(new Error(r));
output(op_header); // automatically gets placed in its own page
output(op_comments);
+ // specify that a page flush() call is required after this packet
+ op_code.flush = true;
output(op_code);
- // specify that a page flush() call is required at this point
- output({ flush: true });
-
+ // don't call this function again
this._headerWritten = true;
process.nextTick(fn);
@@ -178,6 +179,8 @@ Encoder.prototype._writepcm = function (buf, fn) {
}
binding.vorbis_analysis_write(this.vd, buf, channels, samples, function (rtn) {
+ debug('vorbis_analysis_write() return = %d', rtn);
+
buf = buf; // keep ref to "buf" for the async call...
if (0 === rtn) {
@@ -241,11 +244,12 @@ Encoder.prototype._flushpacket = function (output, fn) {
var self = this;
var packet = new Buffer(binding.sizeof_ogg_packet);
binding.vorbis_bitrate_flushpacket(this.vd, packet, function (rtn) {
+ debug('vorbis_bitrate_flushpacket() return = %d', rtn);
if (1 === rtn) {
// got a packet, output it
+ // the consumer should call `pageout()` after this packet
+ packet.pageout = true;
output(packet);
- // the consumer should call `pageout()` at this point
- output({ pageout: true });
// attempt to get another `ogg_packet`...
self._flushpacket(output, fn);
@@ -269,11 +273,21 @@ Encoder.prototype._flushpacket = function (output, fn) {
Encoder.prototype._flush = function (output, fn) {
debug('_onflush()');
- var r = binding.vorbis_analysis_eos(this.vd, 0);
- if (0 === r) {
- this._blockout(output, fn);
+
+ // ensure the vorbis header has been output first
+ if (this._headerWritten) {
+ process.call(this);
} else {
- // error code
- fn(new Error('vorbis_analysis_eos() error: ' + r));
+ this._writeHeader(output, process);
+ }
+
+ function process () {
+ var r = binding.vorbis_analysis_eos(this.vd, 0);
+ if (0 === r) {
+ this._blockout(output, fn);
+ } else {
+ // error code
+ fn(new Error('vorbis_analysis_eos() error: ' + r));
+ }
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.