UNPKG

qambi

Version:

MIDI sequencer, loads MIDI files, can record and playback MIDI, uses WebMIDI and WebAudio

128 lines (104 loc) 3.5 kB
/* Wrapper for accessing bytes through sequential reads based on: https://github.com/gasman/jasmid adapted to work with ArrayBuffer -> Uint8Array */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var fcc = String.fromCharCode; var MIDIStream = function () { // buffer is Uint8Array function MIDIStream(buffer) { _classCallCheck(this, MIDIStream); this.buffer = buffer; this.position = 0; } /* read string or any number of bytes */ _createClass(MIDIStream, [{ key: 'read', value: function read(length) { var toString = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var result = void 0; if (toString) { result = ''; for (var i = 0; i < length; i++, this.position++) { result += fcc(this.buffer[this.position]); } return result; } else { result = []; for (var _i = 0; _i < length; _i++, this.position++) { result.push(this.buffer[this.position]); } return result; } } /* read a big-endian 32-bit integer */ }, { key: 'readInt32', value: function readInt32() { var result = (this.buffer[this.position] << 24) + (this.buffer[this.position + 1] << 16) + (this.buffer[this.position + 2] << 8) + this.buffer[this.position + 3]; this.position += 4; return result; } /* read a big-endian 16-bit integer */ }, { key: 'readInt16', value: function readInt16() { var result = (this.buffer[this.position] << 8) + this.buffer[this.position + 1]; this.position += 2; return result; } /* read an 8-bit integer */ }, { key: 'readInt8', value: function readInt8(signed) { var result = this.buffer[this.position]; if (signed && result > 127) { result -= 256; } this.position += 1; return result; } }, { key: 'eof', value: function eof() { return this.position >= this.buffer.length; } /* read a MIDI-style letiable-length integer (big-endian value in groups of 7 bits, with top bit set to signify that another byte follows) */ }, { key: 'readVarInt', value: function 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; } } } }, { key: 'reset', value: function reset() { this.position = 0; } }, { key: 'setPosition', value: function setPosition(p) { this.position = p; } }]); return MIDIStream; }(); exports.default = MIDIStream;