gasman / jasmid

UNMAINTAINED. A MIDI file reader and synthesiser in Javascript
http://matt.west.co.tt/music/jasmid-midi-synthesis-with-javascript-and-html5-audio/
Other
416 stars 89 forks source link

stream.js array buffer ajax #27

Open annopnod opened 6 years ago

annopnod commented 6 years ago

IO.PrimitiveReader = function (data) { var arr = Array.from(new Uint8Array(data)); var fn = {}; var position = 0;

    fn.read = function (length) {
        var result = arr.slice(position, position + length);
        position += length;
        return result;
    };

    /* read a big-endian 32-bit integer */
    fn.readInt32 = function () {
        var result = (
                (arr[position] << 24)
                + (arr[position + 1] << 16)
                + (arr[position + 2] << 8)
                + arr[position + 3]);
        position += 4;
        return result;
    }

    /* read a big-endian 16-bit integer */
    fn.readInt16 = function () {
        var result = (
                (arr[position] << 8)
                + arr[position + 1]);
        position += 2;
        return result;
    }

    /* read an 8-bit integer */
    fn.readInt8 = function (signed) {
        var result = arr[position];
        if (signed && result > 127)
            result -= 256;
        position += 1;
        return result;
    }

    fn.eof = function () {
        return position >= arr.length;
    }

    fn.readVarInt = function () {
        var result = 0;
        while (true) {
            var b = fn.readInt8();
            if (b & 0x80) {
                result += (b & 0x7f);
                result <<= 7;
            } else {
                /* b is the last byte */
                return result + b;
            }
        }
    };
    return fn;

};
annopnod commented 6 years ago

change midifile.js

if (trackChunk.id != 'MTrk') { throw "Unexpected chunk - expected MTrk, got "+ trackChunk.id; }

if (headerChunk.id != 'MThd' || headerChunk.length != 6) { throw "Bad .mid file - header not found"; }

change trackChunk.id

String.fromCharCode.apply(null, trackChunk.id)

annopnod commented 6 years ago

work it