UNPKG

midi-file-io

Version:

Reads, parses and writes MIDI files. Fork of NHQ's midi-file-parser.

70 lines (69 loc) 1.99 kB
"use strict"; /** * Date: 8/22/19 * Time: 9:27 PM * @license MIT (see project's LICENSE file) */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ReadStream = void 0; /** * Wrapper for accessing strings through sequential reads */ class ReadStream { constructor(buffer) { this.position = 0; this.buffer = buffer; } read(length) { const result = this.buffer.substr(this.position, length); this.position += length; return result; } /* read a big-endian 32-bit integer */ readInt32() { const result = ((this.buffer.charCodeAt(this.position) << 24) + (this.buffer.charCodeAt(this.position + 1) << 16) + (this.buffer.charCodeAt(this.position + 2) << 8) + this.buffer.charCodeAt(this.position + 3)); this.position += 4; return result; } /* read a big-endian 16-bit integer */ readInt16() { const result = (this.buffer.charCodeAt(this.position) << 8) + this.buffer.charCodeAt(this.position + 1); this.position += 2; return result; } /* read an 8-bit integer */ readInt8(signed = false) { let result = this.buffer.charCodeAt(this.position); if (signed && result > 127) { result -= 256; } this.position += 1; return result; } eof() { return this.position >= this.buffer.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) */ readVarInt() { var result = 0; while (true) { var b = this.readInt8(); if (b & 0x80) { result += (b & 0x7f); result <<= 7; } else { /* b is the last byte */ return result + b; } } } } exports.ReadStream = ReadStream;