mp3player
Version:
A mp3 player,get data by ajax and play by audiocontext or mediasource.it can download and reprocessing while playing
149 lines (115 loc) • 3.55 kB
JavaScript
import StringStream from './stringstream';
import Mad from './global';
// Well duh.
var CHAR_BIT = 8;
/*
* NAME: bit.init()
* DESCRIPTION: initialize bit pointer struct
*/
var Bit = function (stream, offset) {
if (typeof(stream) == 'string') {
this.stream = new StringStream(stream);
} else {
this.stream = stream;
}
this.offset = offset;
this.cache = 0;
this.left = CHAR_BIT;
}
Bit.prototype.clone = function() {
var c = new Bit(this.stream, this.offset);
c.cache = this.cache;
c.left = this.left;
return c;
}
/*
* NAME: bit.length()
* DESCRIPTION: return number of bits between start and end points
*/
Bit.prototype.length = function(end) {
return this.left + CHAR_BIT * (end.offset - (this.offset + 1)) + (CHAR_BIT - end.left);
}
/*
* NAME: bit.nextbyte()
* DESCRIPTION: return pointer to next unprocessed byte
*/
Bit.prototype.nextbyte = function() {
return this.left == CHAR_BIT ? this.offset : this.offset + 1;
}
/*
* NAME: bit.skip()
* DESCRIPTION: advance bit pointer
*/
Bit.prototype.skip = function(len) {
this.offset += (len / CHAR_BIT) >> 0; // javascript trick to get integer divison
this.left -= len % CHAR_BIT;
if (this.left > CHAR_BIT) {
this.offset++;
this.left += CHAR_BIT;
}
if (this.left < CHAR_BIT) {
this.cache = this.stream.getU8(this.offset);
}
}
/*
* NAME: bit.read()
* DESCRIPTION: read an arbitrary number of bits and return their UIMSBF value
*/
Bit.prototype.read = function(len) {
if(len > 16) {
return this.readBig(len);
}
var value = 0;
if (this.left == CHAR_BIT) {
this.cache = this.stream.getU8(this.offset);
}
if (len < this.left) {
value = (this.cache & ((1 << this.left) - 1)) >> (this.left - len);
this.left -= len;
return value;
}
/* remaining bits in current byte */
value = this.cache & ((1 << this.left) - 1);
len -= this.left;
this.offset++;
this.left = CHAR_BIT;
/* more bytes */
while (len >= CHAR_BIT) {
value = (value << CHAR_BIT) | this.stream.getU8(this.offset++);
len -= CHAR_BIT;
}
if (len > 0) {
this.cache = this.stream.getU8(this.offset);
value = (value << len) | (this.cache >> (CHAR_BIT - len));
this.left -= len;
}
return value;
}
Bit.prototype.readBig = function(len) {
var value = 0;
if (this.left == CHAR_BIT) {
this.cache = this.stream.getU8(this.offset);
}
if (len < this.left) {
value = (this.cache & ((1 << this.left) - 1)) >> (this.left - len);
this.left -= len;
return value;
}
/* remaining bits in current byte */
value = this.cache & ((1 << this.left) - 1);
len -= this.left;
this.offset++;
this.left = CHAR_BIT;
/* more bytes */
while (len >= CHAR_BIT) {
value = Mad.bitwiseOr(Mad.lshift(value, CHAR_BIT), this.stream.getU8(this.offset++));
len -= CHAR_BIT;
}
if (len > 0) {
this.cache = this.stream.getU8(this.offset);
value = Mad.bitwiseOr(Mad.lshift(value, len), (this.cache >> (CHAR_BIT - len)));
this.left -= len;
}
return value;
}
export default Bit;