<commit_before>/*eslint no-underscore-dangle:0 */

/**
 * @module midijs/lib/file
 */

'use strict';

var buffer = require('buffer');
var util = require('util');
var stream = require('stream');
var BufferCursor = require('buffercursor');

var Header = require('./file/header').Header;
var Event = require('./file/event').Event;
var Track = require('./file/track').Track;

var parseHeader = require('./file/parser/header').parseHeader;
var parseTrack = require('./file/parser/track').parseTrack;

/**
 * MIDI file reader and writer
 *
 * Parse and encode MIDI data
 *
 * @constructor
 * @param {String|Buffer|void} data - MIDI data as a Buffer or as a string
 * @property {Buffer} data - Raw data of this file
 * @property {module:midijs/lib/file/header.Header} header - File header
 * @property {Array<module:midijs/lib/file/track.Track>} tracks
 * List of file tracks
 */
function File(data) {
    stream.Writable.call(this);
    
    if (!data) {
        this.data = new buffer.Buffer([]);
    } else if (buffer.Buffer.isBuffer(data)) {
        this.data = data;
    } else {
        this.data = new buffer.Buffer(data, 'utf8');
    }
    
    this.header = new Header();
    this.tracks = [];
}

util.inherits(File, stream.Writable);

File.Header = Header;
File.Event = Event;
File.Track = Track;

exports.File = File;

/**
 * Parse data
 * 
 * Will be called automatically by .end()
 *
 * @param {Function} callback - Called when the file is parsed
 * @return {void}
 */
File.prototype.parse = function (callback) {
    var header, tracks = [],
        cursor, length, i;
    
    callback = callback || function () {
        return undefined;
    };
    
    try {
        cursor = new BufferCursor(this.data);
        header = parseHeader(cursor);

        if (header.fileType === 0 && header.trackCount > 1) {
            throw new Error('Invalid file type "0" with more than one track');
        }

        length = header.trackCount;<commit_msg>:bulb: Expose MetaEvent, SysexEvent, ChannelEvent<commit_after>/*eslint no-underscore-dangle:0 */

/**
 * @module midijs/lib/file
 */

'use strict';

var buffer = require('buffer');
var util = require('util');
var stream = require('stream');
var BufferCursor = require('buffercursor');

var Header = require('./file/header').Header;
var Event = require('./file/event').Event;
var MetaEvent = require('./file/event').MetaEvent;
var SysexEvent = require('./file/event').SysexEvent;
var ChannelEvent = require('./file/event').ChannelEvent;
var Track = require('./file/track').Track;

var parseHeader = require('./file/parser/header').parseHeader;
var parseTrack = require('./file/parser/track').parseTrack;

/**
 * MIDI file reader and writer
 *
 * Parse and encode MIDI data
 *
 * @constructor
 * @param {String|Buffer|void} data - MIDI data as a Buffer or as a string
 * @property {Buffer} data - Raw data of this file
 * @property {module:midijs/lib/file/header.Header} header - File header
 * @property {Array<module:midijs/lib/file/track.Track>} tracks
 * List of file tracks
 */
function File(data) {
    stream.Writable.call(this);
    
    if (!data) {
        this.data = new buffer.Buffer([]);
    } else if (buffer.Buffer.isBuffer(data)) {
        this.data = data;
    } else {
        this.data = new buffer.Buffer(data, 'utf8');
    }
    
    this.header = new Header();
    this.tracks = [];
}

util.inherits(File, stream.Writable);

File.Header = Header;
File.Event = Event;
File.MetaEvent = MetaEvent;
File.SysexEvent = SysexEvent;
File.ChannelEvent = ChannelEvent;
File.Track = Track;

exports.File = File;

/**
 * Parse data
 * 
 * Will be called automatically by .end()
 *
 * @param {Function} callback - Called when the file is parsed
 * @return {void}
 */
File.prototype.parse = function (callback) {
    var header, tracks = [],
        cursor, length, i;
    
    callback = callback || function () {
        return undefined;
    };
    
    try {
        cursor = new BufferCursor(this.data);
        header = parseHeader(cursor);

        if (header.fileType === 0 && header.trackCount > 1) {
            throw new Error('Invalid file type "0" with more than one track');
        }

        length = header.trackCount;
{'fraction_non_alphanumeric': 0.1489158923040267, 'fraction_numerical': 0.002382654276864427, 'mean_word_length': 3.063891577928364, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4278630', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1257, 'n_words': 348}