examples/MIDIFile.js
function MIDIFileHeader(buffer) {
	let a;
	// No buffer creating him
	if (!buffer) {
		a = new Uint8Array(MIDIFileHeader.HEADER_LENGTH);
		// Adding the header id (MThd)
		a[0] = 0x4D;
		a[1] = 0x54;
		a[2] = 0x68;
		a[3] = 0x64;
		// Adding the header chunk size
		a[4] = 0x00;
		a[5] = 0x00;
		a[6] = 0x00;
		a[7] = 0x06;
		// Adding the file format (1 here cause it's the most commonly used)
		a[8] = 0x00;
		a[9] = 0x01;
		// Adding the track count (1 cause it's a new file)
		a[10] = 0x00;
		a[11] = 0x01;
		// Adding the time division (192 ticks per beat)
		a[12] = 0x00;
		a[13] = 0xC0;
		// saving the buffer
		this.datas = new DataView(a.buffer, 0, MIDIFileHeader.HEADER_LENGTH);
		// Parsing the given buffer
	} else {
		if (!(buffer instanceof ArrayBuffer)) {
			throw Error('Invalid buffer received.');
		}
		this.datas = new DataView(buffer, 0, MIDIFileHeader.HEADER_LENGTH);
		// Reading MIDI header chunk
		if (!(
				'M' === String.fromCharCode(this.datas.getUint8(0)) &&
				'T' === String.fromCharCode(this.datas.getUint8(1)) &&
				'h' === String.fromCharCode(this.datas.getUint8(2)) &&
				'd' === String.fromCharCode(this.datas.getUint8(3)))) {
			throw new Error('Invalid MIDIFileHeader : MThd prefix not found');
		}
		// Reading chunk length
		if (6 !== this.datas.getUint32(4)) {
			throw new Error('Invalid MIDIFileHeader : Chunk length must be 6');
		}
	}
}
function MIDIFileHeader(buffer) {
	let a;
	// No buffer creating him
	if (!buffer) {
		a = new Uint8Array(MIDIFileHeader.HEADER_LENGTH);
		// Adding the header id (MThd)
		a[0] = 0x4D;
		a[1] = 0x54;
		a[2] = 0x68;
		a[3] = 0x64;
		// Adding the header chunk size
		a[4] = 0x00;
		a[5] = 0x00;
		a[6] = 0x00;
		a[7] = 0x06;
		// Adding the file format (1 here cause it's the most commonly used)
		a[8] = 0x00;
		a[9] = 0x01;
		// Adding the track count (1 cause it's a new file)
		a[10] = 0x00;
		a[11] = 0x01;
		// Adding the time division (192 ticks per beat)
		a[12] = 0x00;
		a[13] = 0xC0;
		// saving the buffer
		this.datas = new DataView(a.buffer, 0, MIDIFileHeader.HEADER_LENGTH);
		// Parsing the given buffer
	} else {
		if (!(buffer instanceof ArrayBuffer)) {
			throw Error('Invalid buffer received.');
		}
		this.datas = new DataView(buffer, 0, MIDIFileHeader.HEADER_LENGTH);
		// Reading MIDI header chunk
		if (!(
				'M' === String.fromCharCode(this.datas.getUint8(0)) &&
				'T' === String.fromCharCode(this.datas.getUint8(1)) &&
				'h' === String.fromCharCode(this.datas.getUint8(2)) &&
				'd' === String.fromCharCode(this.datas.getUint8(3)))) {
			throw new Error('Invalid MIDIFileHeader : MThd prefix not found');
		}
		// Reading chunk length
		if (6 !== this.datas.getUint32(4)) {
			throw new Error('Invalid MIDIFileHeader : Chunk length must be 6');
		}
	}
}
function MIDIFileHeader ( buffer ) { let a ; // No buffer creating him if ( ! buffer ) { a = new Uint8Array ( MIDIFileHeader . HEADER_LENGTH ) ; // Adding the header id (MThd) a [ 0 ] = 0x4D ; a [ 1 ] = 0x54 ; a [ 2 ] = 0x68 ; a [ 3 ] = 0x64 ; // Adding the header chunk size a [ 4 ] = 0x00 ; a [ 5 ] = 0x00 ; a [ 6 ] = 0x00 ; a [ 7 ] = 0x06 ; // Adding the file format (1 here cause it's the most commonly used) a [ 8 ] = 0x00 ; a [ 9 ] = 0x01 ; // Adding the track count (1 cause it's a new file) a [ 10 ] = 0x00 ; a [ 11 ] = 0x01 ; // Adding the time division (192 ticks per beat) a [ 12 ] = 0x00 ; a [ 13 ] = 0xC0 ; // saving the buffer this . datas = new DataView ( a . buffer , 0 , MIDIFileHeader . HEADER_LENGTH ) ; // Parsing the given buffer } else { if ( ! ( buffer instanceof ArrayBuffer ) ) { throw Error ( 'Invalid buffer received.' ) ; } this . datas = new DataView ( buffer , 0 , MIDIFileHeader . HEADER_LENGTH ) ; // Reading MIDI header chunk if ( ! ( 'M' === String . fromCharCode ( this . datas . getUint8 ( 0 ) ) && 'T' === String . fromCharCode ( this . datas . getUint8 ( 1 ) ) && 'h' === String . fromCharCode ( this . datas . getUint8 ( 2 ) ) && 'd' === String . fromCharCode ( this . datas . getUint8 ( 3 ) ) ) ) { throw new Error ( 'Invalid MIDIFileHeader : MThd prefix not found' ) ; } // Reading chunk length if ( 6 !== this . datas . getUint32 ( 4 ) ) { throw new Error ( 'Invalid MIDIFileHeader : Chunk length must be 6' ) ; } } }
/........................................... MIDIFileHeader : Read and edit a MIDI header chunk in a given ArrayBuffer
/ ........................................... MIDIFileHeader : Read and edit a MIDI header chunk in a given ArrayBuffer