Skip to content

Commit

Permalink
use 64-bit integer for granule position
Browse files Browse the repository at this point in the history
  • Loading branch information
anthumchris committed Nov 2, 2018
1 parent 4d0fda6 commit 315293d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
18 changes: 16 additions & 2 deletions dist/test-seekable-opus-stream-decoder.html
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@
this.filePos = posFromFileStart;
this.filePosHex = posFromFileStart.toString(16); // used for debugging

// TODO this shoudl be read as 64-bit, which isn't supported
this.granule = view.getUint32(viewOffset+6, true);
this.granule = view.getUint64(viewOffset+6, true);

this.serial = view.getUint32(viewOffset+14, true);
this.page = view.getUint32(viewOffset+18, true);
Expand All @@ -280,6 +279,21 @@
channelMappingFamily: dataView.getUint8(14, true),
};
}

// getUint64() polyfill from https://stackoverflow.com/questions/53103695/
DataView.prototype.getUint64 = function(byteOffset, littleEndian) {
// split 64-bit number into two 32-bit parts
const left = this.getUint32(byteOffset, littleEndian);
const right = this.getUint32(byteOffset+4, littleEndian);

// combine the two 32-bit values
const combined = littleEndian? left + 2**32*right : 2**32*left + right;

if (!Number.isSafeInteger(combined))
console.warn(combined, 'exceeds MAX_SAFE_INTEGER. Precision may be lost');

return combined;
}
</script>
</body>
</html>
18 changes: 16 additions & 2 deletions src/test-seekable-opus-stream-decoder.html
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@
this.filePos = posFromFileStart;
this.filePosHex = posFromFileStart.toString(16); // used for debugging

// TODO this shoudl be read as 64-bit, which isn't supported
this.granule = view.getUint32(viewOffset+6, true);
this.granule = view.getUint64(viewOffset+6, true);

this.serial = view.getUint32(viewOffset+14, true);
this.page = view.getUint32(viewOffset+18, true);
Expand All @@ -280,6 +279,21 @@
channelMappingFamily: dataView.getUint8(14, true),
};
}

// getUint64() polyfill from https://stackoverflow.com/questions/53103695/
DataView.prototype.getUint64 = function(byteOffset, littleEndian) {
// split 64-bit number into two 32-bit parts
const left = this.getUint32(byteOffset, littleEndian);
const right = this.getUint32(byteOffset+4, littleEndian);

// combine the two 32-bit values
const combined = littleEndian? left + 2**32*right : 2**32*left + right;

if (!Number.isSafeInteger(combined))
console.warn(combined, 'exceeds MAX_SAFE_INTEGER. Precision may be lost');

return combined;
}
</script>
</body>
</html>

0 comments on commit 315293d

Please sign in to comment.