diff options
author | piernov <piernov@piernov.org> | 2014-03-13 16:48:55 +0100 |
---|---|---|
committer | piernov <piernov@piernov.org> | 2014-03-13 16:48:55 +0100 |
commit | c9af7bb67bfefc3f37b4cdd4a066a29138f202b3 (patch) | |
tree | 93bbee2437b6f78fb7f46d1193d00279b6852c35 /Processing-js/libs/inc/jasmid/stream.js | |
download | Synthe3000-master.tar.gz Synthe3000-master.tar.bz2 Synthe3000-master.tar.xz Synthe3000-master.zip |
Diffstat (limited to 'Processing-js/libs/inc/jasmid/stream.js')
-rw-r--r-- | Processing-js/libs/inc/jasmid/stream.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/Processing-js/libs/inc/jasmid/stream.js b/Processing-js/libs/inc/jasmid/stream.js new file mode 100644 index 0000000..27c69ba --- /dev/null +++ b/Processing-js/libs/inc/jasmid/stream.js @@ -0,0 +1,69 @@ +/* Wrapper for accessing strings through sequential reads */ +function Stream(str) { + var position = 0; + + function read(length) { + var result = str.substr(position, length); + position += length; + return result; + } + + /* read a big-endian 32-bit integer */ + function readInt32() { + var result = ( + (str.charCodeAt(position) << 24) + + (str.charCodeAt(position + 1) << 16) + + (str.charCodeAt(position + 2) << 8) + + str.charCodeAt(position + 3)); + position += 4; + return result; + } + + /* read a big-endian 16-bit integer */ + function readInt16() { + var result = ( + (str.charCodeAt(position) << 8) + + str.charCodeAt(position + 1)); + position += 2; + return result; + } + + /* read an 8-bit integer */ + function readInt8(signed) { + var result = str.charCodeAt(position); + if (signed && result > 127) result -= 256; + position += 1; + return result; + } + + function eof() { + return position >= str.length; + } + + /* read a MIDI-style variable-length integer + (big-endian value in groups of 7 bits, + with top bit set to signify that another byte follows) + */ + function readVarInt() { + var result = 0; + while (true) { + var b = readInt8(); + if (b & 0x80) { + result += (b & 0x7f); + result <<= 7; + } else { + /* b is the last byte */ + return result + b; + } + } + } + + return { + 'eof': eof, + 'read': read, + 'readInt32': readInt32, + 'readInt16': readInt16, + 'readInt8': readInt8, + 'readVarInt': readVarInt + } +}
\ No newline at end of file |