Skip to content

Commit

Permalink
Fix serial and 64bit granule pos
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rudmin committed May 22, 2018
1 parent f71b4bb commit 0b4938d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist-unminified/encoderWorker.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/encoderWorker.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opus-recorder",
"version": "4.1.1",
"version": "4.1.2",
"description": "A library for recording opus encoded audio",
"homepage": "https://github.com/chris-rudmin/opus-recorder",
"author": "Chris Rudmin",
Expand Down
12 changes: 8 additions & 4 deletions src/encoderWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var OggOpusEncoder = function( config, Module ){
numberOfChannels: 1,
originalSampleRate: 44100,
resampleQuality: 3, // Value between 0 and 10 inclusive. 10 being highest quality.
serial: Math.floor( Math.random() * Math.pow(2,32) )
serial: Math.floor( Math.random() * (Math.pow(2,32) - 1) )
}, config );

this._opus_encoder_create = Module._opus_encoder_create;
Expand Down Expand Up @@ -171,10 +171,14 @@ OggOpusEncoder.prototype.generatePage = function(){
pageBufferView.setUint8( 4, 0, true ); // Version
pageBufferView.setUint8( 5, this.headerType, true ); // 1 = continuation, 2 = beginning of stream, 4 = end of stream

// Number of samples upto and including this page at 48000Hz, into 64 bits
// Number of samples upto and including this page at 48000Hz, into signed 64 bit Little Endian integer
// Javascript Number maximum value is 53 bits or 2^53 - 1
pageBufferView.setUint32( 6, granulePosition, true );
if ( granulePosition > 4294967296 || granulePosition < 0 ) {
pageBufferView.setUint32( 10, Math.floor( granulePosition/4294967296 ), true );
if (granulePosition < 0) {
pageBufferView.setInt32( 10, Math.ceil(granulePosition/4294967297) - 1, true );
}
else {
pageBufferView.setInt32( 10, Math.floor(granulePosition/4294967296), true );
}

pageBufferView.setUint32( 14, this.config.serial, true ); // Bitstream serial number
Expand Down
126 changes: 126 additions & 0 deletions test/encoderWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,130 @@ describe('encoderWorker', function() {
getEncoder();
});

it('should set granule position to 0', function (done) {
var pageBufferCount = 0;

global.postMessage = function(page) {
pageBufferCount++;

if (pageBufferCount == 3) {
var dataView = new DataView(page.buffer);
expect(dataView.getUint32(6, true)).to.equal(0);
expect(dataView.getInt32(10, true)).to.equal(0);
done();
}
};

getEncoder().then(function(encoder) {
encoder.lastPositiveGranulePosition = 1;
encoder.granulePosition = 0;
encoder.generatePage();
});
});

it('should set granule position to -1', function (done) {
var pageBufferCount = 0;

global.postMessage = function(page) {
pageBufferCount++;

if (pageBufferCount == 3) {
var dataView = new DataView(page.buffer);
expect(dataView.getUint32(6, true)).to.equal(4294967295);
expect(dataView.getInt32(10, true)).to.equal(-1);
done();
}
};

getEncoder().then(function(encoder) {
encoder.lastPositiveGranulePosition = 1;
encoder.granulePosition = -1;
encoder.generatePage();
});
});

it('should set granule position to -2^32', function (done) {
var pageBufferCount = 0;

global.postMessage = function(page) {
pageBufferCount++;

if (pageBufferCount == 3) {
var dataView = new DataView(page.buffer);
expect(dataView.getUint32(6, true)).to.equal(0);
expect(dataView.getInt32(10, true)).to.equal(-1);
done();
}
};

getEncoder().then(function(encoder) {
encoder.lastPositiveGranulePosition = 1;
encoder.granulePosition = -4294967296;
encoder.generatePage();
});
});

it('should set granule position to -2^32 - 1', function (done) {
var pageBufferCount = 0;

global.postMessage = function(page) {
pageBufferCount++;

if (pageBufferCount == 3) {
var dataView = new DataView(page.buffer);
expect(dataView.getUint32(6, true)).to.equal(4294967295);
expect(dataView.getInt32(10, true)).to.equal(-2);
done();
}
};

getEncoder().then(function(encoder) {
encoder.lastPositiveGranulePosition = 1;
encoder.granulePosition = -4294967297;
encoder.generatePage();
});
});

it('should set granule position to 2^32 - 1', function (done) {
var pageBufferCount = 0;

global.postMessage = function(page) {
pageBufferCount++;

if (pageBufferCount == 3) {
var dataView = new DataView(page.buffer);
expect(dataView.getUint32(6, true)).to.equal(4294967295);
expect(dataView.getInt32(10, true)).to.equal(0);
done();
}
};

getEncoder().then(function(encoder) {
encoder.lastPositiveGranulePosition = 1;
encoder.granulePosition = 4294967295;
encoder.generatePage();
});
});

it('should set granule position to 2^32', function (done) {
var pageBufferCount = 0;

global.postMessage = function(page) {
pageBufferCount++;

if (pageBufferCount == 3) {
var dataView = new DataView(page.buffer);
expect(dataView.getUint32(6, true)).to.equal(0);
expect(dataView.getInt32(10, true)).to.equal(1);
done();
}
};

getEncoder().then(function(encoder) {
encoder.lastPositiveGranulePosition = 1;
encoder.granulePosition = 4294967296;
encoder.generatePage();
});
});

});

0 comments on commit 0b4938d

Please sign in to comment.