Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
Fix encoder errors
Browse files Browse the repository at this point in the history
Encoded audio duration fixed
granulepos fixed
Pre-skip size fixed (defaults to the recommended 3840)
  • Loading branch information
doomjs committed Aug 2, 2016
1 parent f3d1db5 commit be1ea15
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@
*.pcm
*.opus
node_modules/
.vscode
9 changes: 9 additions & 0 deletions .npmignore
@@ -0,0 +1,9 @@
*.swp
/build
/deps/build
*.pcm
*.opus
.vscode
.gitignore
.jshintrc
.travis.yml
30 changes: 14 additions & 16 deletions lib/Encoder.js
Expand Up @@ -55,7 +55,7 @@ Encoder.prototype._writeHeader = function() {
var data = new Buffer([
0x01, // version
this.channels,
0x00, 0x00, // Preskip (TODO: how do we get this?)
0x00, 0x0f, // Preskip (default and recommended 3840)
( ( this.rate & 0x000000ff ) >> 0 ),
( ( this.rate & 0x0000ff00 ) >> 8 ),
( ( this.rate & 0x00ff0000 ) >> 16 ),
Expand All @@ -74,9 +74,9 @@ Encoder.prototype._writeHeader = function() {
packet.e_o_s = 0;
packet.granulepos = 0;
packet.packetno = this.pos++;
//packet.flush = true;

this.push( packet );
this.samplesWritten += header.length;

// OpusTags packet
magicSignature = new Buffer( 'OpusTags', 'ascii' );
Expand All @@ -95,7 +95,7 @@ Encoder.prototype._writeHeader = function() {
packet.bytes = header.length;
packet.b_o_s = 0;
packet.e_o_s = 0;
packet.granulepos = 0;
packet.granulepos = this.samplesWritten;
packet.packetno = this.pos++;
packet.flush = true;

Expand Down Expand Up @@ -149,8 +149,14 @@ Encoder.prototype._processOutput = function( buf ) {

Encoder.prototype._flushFrame = function( frame, end ) {

if( this.lastPacket ) {
this.push( this.lastPacket );
}

var encoded = this.encoder.encode( frame );

this.samplesWritten += this.frameSize;

var packet = new ogg_packet();
packet.packet = encoded;
packet.bytes = encoded.length,
Expand All @@ -160,23 +166,15 @@ Encoder.prototype._flushFrame = function( frame, end ) {
packet.packetno = this.pos++;
packet.flush = true;

this.samplesWritten += this.frameSize;

this.push( packet );
this.lastPacket = packet;
};

Encoder.prototype._flush = function( done ) {

var packet = new ogg_packet();
packet.packet = new Buffer(0);
packet.bytes = 0;
packet.b_o_s = 0;
packet.e_o_s = 1;
packet.granulepos = this.pos;
packet.packetno = this.pos++;
packet.flush = true;

this.push( packet );
if( this.lastPacket ) {
this.lastPacket.e_o_s = 1;
this.push( this.lastPacket );
}

done();
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "node-opus",
"description": "NodeJS native binding to OPUS",
"version": "0.2.1",
"version": "0.2.2",
"author": "Mikko Rantanen <jubjub@jubjubnest.net>",
"homepage": "https://github.com/Rantanen/node-opus",
"bugs": "https://github.com/Rantanen/node-opus/issues",
Expand Down

0 comments on commit be1ea15

Please sign in to comment.