source: main/trunk/model-sites-dev/respooled/collect/popup-video-respooled/js/midi/inc/jasmid/stream.js@ 29863

Last change on this file since 29863 was 29863, checked in by davidb, 9 years ago

First cut at respooled site/collection

File size: 1.4 KB
Line 
1/* Wrapper for accessing strings through sequential reads */
2function Stream(str) {
3 var position = 0;
4
5 function read(length) {
6 var result = str.substr(position, length);
7 position += length;
8 return result;
9 }
10
11 /* read a big-endian 32-bit integer */
12 function readInt32() {
13 var result = (
14 (str.charCodeAt(position) << 24)
15 + (str.charCodeAt(position + 1) << 16)
16 + (str.charCodeAt(position + 2) << 8)
17 + str.charCodeAt(position + 3));
18 position += 4;
19 return result;
20 }
21
22 /* read a big-endian 16-bit integer */
23 function readInt16() {
24 var result = (
25 (str.charCodeAt(position) << 8)
26 + str.charCodeAt(position + 1));
27 position += 2;
28 return result;
29 }
30
31 /* read an 8-bit integer */
32 function readInt8(signed) {
33 var result = str.charCodeAt(position);
34 if (signed && result > 127) result -= 256;
35 position += 1;
36 return result;
37 }
38
39 function eof() {
40 return position >= str.length;
41 }
42
43 /* read a MIDI-style variable-length integer
44 (big-endian value in groups of 7 bits,
45 with top bit set to signify that another byte follows)
46 */
47 function readVarInt() {
48 var result = 0;
49 while (true) {
50 var b = readInt8();
51 if (b & 0x80) {
52 result += (b & 0x7f);
53 result <<= 7;
54 } else {
55 /* b is the last byte */
56 return result + b;
57 }
58 }
59 }
60
61 return {
62 'eof': eof,
63 'read': read,
64 'readInt32': readInt32,
65 'readInt16': readInt16,
66 'readInt8': readInt8,
67 'readVarInt': readVarInt
68 }
69}
Note: See TracBrowser for help on using the repository browser.