source: gs3-extensions/web-audio/trunk/js-mad/script/bit.js@ 28388

Last change on this file since 28388 was 28388, checked in by davidb, 11 years ago

Set of JS, CSS, PNG etc web resources to support a mixture of audio player/document display capabilities

File size: 3.3 KB
Line 
1
2// Well duh.
3var CHAR_BIT = 8;
4
5/*
6 * NAME: bit.init()
7 * DESCRIPTION: initialize bit pointer struct
8 */
9Mad.Bit = function (stream, offset) {
10 if (typeof(stream) === 'string') {
11 this.stream = new Mad.BinaryStrings.StringStream(stream);
12 } else if(stream instanceof Uint8Array) {
13 this.stream = new Mad.ArrayBuffers.ArrayStream(stream);
14 } else {
15 this.stream = stream;
16 }
17
18 this.offset = offset;
19
20 this.cache = 0;
21 this.left = CHAR_BIT;
22}
23
24Mad.Bit.prototype.clone = function() {
25 var c = new Mad.Bit(this.stream, this.offset);
26
27 c.cache = this.cache;
28 c.left = this.left;
29
30 return c;
31}
32
33/*
34 * NAME: bit.length()
35 * DESCRIPTION: return number of bits between start and end points
36 */
37Mad.Bit.prototype.length = function(end) {
38 return this.left + CHAR_BIT * (end.offset - (this.offset + 1)) + (CHAR_BIT - end.left);
39}
40
41/*
42 * NAME: bit.nextbyte()
43 * DESCRIPTION: return pointer to next unprocessed byte
44 */
45Mad.Bit.prototype.nextbyte = function() {
46 return this.left === CHAR_BIT ? this.offset : this.offset + 1;
47}
48
49/*
50 * NAME: bit.skip()
51 * DESCRIPTION: advance bit pointer
52 */
53Mad.Bit.prototype.skip = function(len) {
54 this.offset += (len / CHAR_BIT) >> 0; // javascript trick to get integer divison
55 this.left -= len % CHAR_BIT;
56
57 if (this.left > CHAR_BIT) {
58 this.offset++;
59 this.left += CHAR_BIT;
60 }
61
62 if (this.left < CHAR_BIT) {
63 this.cache = this.stream.getU8(this.offset);
64 }
65}
66
67/*
68 * NAME: bit.read()
69 * DESCRIPTION: read an arbitrary number of bits and return their UIMSBF value
70 */
71Mad.Bit.prototype.read = function(len) {
72 if(len > 16) {
73 return this.readBig(len);
74 }
75
76 var value = 0;
77
78 if (this.left === CHAR_BIT) {
79 this.cache = this.stream.getU8(this.offset);
80 }
81
82 if (len < this.left) {
83 value = (this.cache & ((1 << this.left) - 1)) >> (this.left - len);
84 this.left -= len;
85
86 return value;
87 }
88
89 /* remaining bits in current byte */
90 value = this.cache & ((1 << this.left) - 1);
91 len -= this.left;
92
93 this.offset++;
94 this.left = CHAR_BIT;
95
96 /* more bytes */
97 while (len >= CHAR_BIT) {
98 value = (value << CHAR_BIT) | this.stream.getU8(this.offset++);
99 len -= CHAR_BIT;
100 }
101
102 if (len > 0) {
103 this.cache = this.stream.getU8(this.offset);
104
105 value = (value << len) | (this.cache >> (CHAR_BIT - len));
106 this.left -= len;
107 }
108
109 return value;
110}
111
112Mad.Bit.prototype.readBig = function(len) {
113 var value = 0;
114
115 if (this.left === CHAR_BIT) {
116 this.cache = this.stream.getU8(this.offset);
117 }
118
119 if (len < this.left) {
120 value = (this.cache & ((1 << this.left) - 1)) >> (this.left - len);
121 this.left -= len;
122
123 return value;
124 }
125
126 /* remaining bits in current byte */
127 value = this.cache & ((1 << this.left) - 1);
128 len -= this.left;
129
130 this.offset++;
131 this.left = CHAR_BIT;
132
133 /* more bytes */
134 while (len >= CHAR_BIT) {
135 value = Mad.bitwiseOr(Mad.lshift(value, CHAR_BIT), this.stream.getU8(this.offset++));
136 len -= CHAR_BIT;
137 }
138
139 if (len > 0) {
140 this.cache = this.stream.getU8(this.offset);
141
142 value = Mad.bitwiseOr(Mad.lshift(value, len), (this.cache >> (CHAR_BIT - len)));
143 this.left -= len;
144 }
145
146 return value;
147}
148
Note: See TracBrowser for help on using the repository browser.