scratch-audio/src/ArrayBufferStream.js

108 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-02-02 14:53:17 -05:00
/**
* ArrayBufferStream wraps the built-in javascript ArrayBuffer, adding the ability to access
* data in it like a stream, tracking its position.
* You can request to read a value from the front of the array, and it will keep track of the position
* within the byte array, so that successive reads are consecutive.
* The available types to read include:
* Uint8, Uint8String, Int16, Uint16, Int32, Uint32
* @param {ArrayBuffer} arrayBuffer - array to use as a stream
* @constructor
*/
const ArrayBufferStream = function (arrayBuffer) {
2016-11-16 17:06:34 -05:00
this.arrayBuffer = arrayBuffer;
this.position = 0;
};
2016-11-16 17:06:34 -05:00
2017-02-02 14:53:17 -05:00
/**
* Return a new ArrayBufferStream that is a slice of the existing one
* @param {number} length - the number of bytes of extract
* @return {ArrayBufferStream} the extracted stream
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.extract = function (length) {
2017-04-17 11:22:16 -04:00
const slicedArrayBuffer = this.arrayBuffer.slice(this.position, this.position + length);
const newStream = new ArrayBufferStream(slicedArrayBuffer);
2016-11-16 17:06:34 -05:00
return newStream;
};
2017-02-02 14:53:17 -05:00
/**
* @return {number} the length of the stream in bytes
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.getLength = function () {
return this.arrayBuffer.byteLength;
};
2017-02-02 14:53:17 -05:00
/**
* @return {number} the number of bytes available after the current position in the stream
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.getBytesAvailable = function () {
return (this.arrayBuffer.byteLength - this.position);
};
2017-02-02 14:53:17 -05:00
/**
* Read an unsigned 8 bit integer from the stream
* @return {number} the next 8 bit integer in the stream
2017-02-02 14:53:17 -05:00
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.readUint8 = function () {
2017-04-17 11:22:16 -04:00
const val = new Uint8Array(this.arrayBuffer, this.position, 1)[0];
2016-11-16 17:06:34 -05:00
this.position += 1;
return val;
};
2017-02-02 14:53:17 -05:00
/**
* Read a sequence of bytes of the given length and convert to a string.
* This is a convenience method for use with short strings.
* @param {number} length - the number of bytes to convert
* @return {string} a String made by concatenating the chars in the input
2017-02-02 14:53:17 -05:00
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.readUint8String = function (length) {
2017-04-17 11:22:16 -04:00
const arr = new Uint8Array(this.arrayBuffer, this.position, length);
2016-11-16 17:06:34 -05:00
this.position += length;
2017-04-17 11:22:16 -04:00
let str = '';
for (let i = 0; i < arr.length; i++) {
2016-11-16 17:06:34 -05:00
str += String.fromCharCode(arr[i]);
}
return str;
};
2017-02-02 14:53:17 -05:00
/**
* Read a 16 bit integer from the stream
* @return {number} the next 16 bit integer in the stream
2017-02-02 14:53:17 -05:00
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.readInt16 = function () {
2017-04-17 11:22:16 -04:00
const val = new Int16Array(this.arrayBuffer, this.position, 1)[0];
2016-11-16 17:06:34 -05:00
this.position += 2; // one 16 bit int is 2 bytes
return val;
};
2017-02-02 14:53:17 -05:00
/**
* Read an unsigned 16 bit integer from the stream
* @return {number} the next unsigned 16 bit integer in the stream
2017-02-02 14:53:17 -05:00
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.readUint16 = function () {
2017-04-17 11:22:16 -04:00
const val = new Uint16Array(this.arrayBuffer, this.position, 1)[0];
2016-11-16 17:06:34 -05:00
this.position += 2; // one 16 bit int is 2 bytes
return val;
};
2017-02-02 14:53:17 -05:00
/**
* Read a 32 bit integer from the stream
* @return {number} the next 32 bit integer in the stream
2017-02-02 14:53:17 -05:00
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.readInt32 = function () {
2017-04-17 11:22:16 -04:00
const val = new Int32Array(this.arrayBuffer, this.position, 1)[0];
2016-11-16 17:06:34 -05:00
this.position += 4; // one 32 bit int is 4 bytes
return val;
};
2017-02-02 14:53:17 -05:00
/**
* Read an unsigned 32 bit integer from the stream
* @return {number} the next unsigned 32 bit integer in the stream
2017-02-02 14:53:17 -05:00
*/
2016-11-16 17:06:34 -05:00
ArrayBufferStream.prototype.readUint32 = function () {
2017-04-17 11:22:16 -04:00
const val = new Uint32Array(this.arrayBuffer, this.position, 1)[0];
2016-11-16 17:06:34 -05:00
this.position += 4; // one 32 bit int is 4 bytes
return val;
};
module.exports = ArrayBufferStream;