<commit_before>//
//  AKMIDIFile.swift
//  AudioKit
//
//  Created by <NAME> on 11/5/18.
//  Copyright © 2018 AudioKit. All rights reserved.
//

import Foundation

public struct AKMIDIFile {
    
    public var filename: String
    var chunks: [AKMIDIFileChunk] = []

    var headerChunk: MIDIFileHeaderChunk? {
        return chunks.first(where: { $0.isHeader }) as? MIDIFileHeaderChunk
    }

    public var trackChunks: [MIDIFileTrackChunk] {
        return Array(chunks.drop(while: { $0.isHeader && $0.isValid })) as? [MIDIFileTrackChunk] ?? []
    }

    public var tempoTrack: AKMIDIFileTempoTrack? {
        if format == 1, let tempoTrackChunk = trackChunks.first {
            return AKMIDIFileTempoTrack(trackChunk: tempoTrackChunk)
        }
        return nil
    }

    public var tracks: [AKMIDIFileTrack] {
        var tracks = trackChunks
        if format == 1 {
            tracks = Array(tracks.dropFirst()) // drop tempo track
        }
        return tracks.compactMap({ AKMIDIFileTrack(chunk: $0) })
    }

    public var format: Int {
        return headerChunk?.format ?? 0
    }

    public var numberOfTracks: Int {
        return headerChunk?.numTracks ?? 0
    }

    public var timeFormat: MIDITimeFormat? {
        return headerChunk?.timeFormat
    }

    public var ticksPerBeat: Int? {
        return headerChunk?.ticksPerBeat
    }

    public var framesPerSecond: Int? {
        return headerChunk?.framesPerSecond
    }

    public var ticksPerFrame: Int? {
        return headerChunk?.ticksPerFrame
    }

    public var timeDivision: UInt16 {
        return headerChunk?.timeDivision ?? 0
    }

    public init(url: URL) {
        filename = url.lastPathComponent
        if let midiData = try? Data(contentsOf: url) {
            let dataSize = midiData.count
            var chunks = [AKMIDIFileChunk]()
            var processedBytes = 0
            while processedBytes < dataSize {
                let data = Array(midiData.map({ $0 }).suffix(from: processedBytes))
                if let headerChunk = MIDIFileHeaderChunk(data: data) {
                    print("got header chunk")
                    chunks.append(headerChunk)
                    processedBytes += headerChunk.rawData.count
                } else if let trackChunk = MIDIFileTrackChunk(data: data) {
                    print("got track chunk")
                    chunks.append(trackChunk)
                    processedBytes += trackChunk.rawData.count
                }
            }
            self.chunks = chunks
        }
    }

    public init(path: String) {
        self.init(url: URL(fileURLWithPath: path))
    }
}<commit_msg>remove redundant map<commit_after>//
//  AKMIDIFile.swift
//  AudioKit
//
//  Created by <NAME> on 11/5/18.
//  Copyright © 2018 AudioKit. All rights reserved.
//

import Foundation

public struct AKMIDIFile {
    
    public var filename: String
    var chunks: [AKMIDIFileChunk] = []

    var headerChunk: MIDIFileHeaderChunk? {
        return chunks.first(where: { $0.isHeader }) as? MIDIFileHeaderChunk
    }

    public var trackChunks: [MIDIFileTrackChunk] {
        return Array(chunks.drop(while: { $0.isHeader && $0.isValid })) as? [MIDIFileTrackChunk] ?? []
    }

    public var tempoTrack: AKMIDIFileTempoTrack? {
        if format == 1, let tempoTrackChunk = trackChunks.first {
            return AKMIDIFileTempoTrack(trackChunk: tempoTrackChunk)
        }
        return nil
    }

    public var tracks: [AKMIDIFileTrack] {
        var tracks = trackChunks
        if format == 1 {
            tracks = Array(tracks.dropFirst()) // drop tempo track
        }
        return tracks.compactMap({ AKMIDIFileTrack(chunk: $0) })
    }

    public var format: Int {
        return headerChunk?.format ?? 0
    }

    public var numberOfTracks: Int {
        return headerChunk?.numTracks ?? 0
    }

    public var timeFormat: MIDITimeFormat? {
        return headerChunk?.timeFormat
    }

    public var ticksPerBeat: Int? {
        return headerChunk?.ticksPerBeat
    }

    public var framesPerSecond: Int? {
        return headerChunk?.framesPerSecond
    }

    public var ticksPerFrame: Int? {
        return headerChunk?.ticksPerFrame
    }

    public var timeDivision: UInt16 {
        return headerChunk?.timeDivision ?? 0
    }

    public init(url: URL) {
        filename = url.lastPathComponent
        if let midiData = try? Data(contentsOf: url) {
            let dataSize = midiData.count
            var chunks = [AKMIDIFileChunk]()
            var processedBytes = 0
            while processedBytes < dataSize {
                let data = Array(midiData.suffix(from: processedBytes))
                if let headerChunk = MIDIFileHeaderChunk(data: data) {
                    print("got header chunk")
                    chunks.append(headerChunk)
                    processedBytes += headerChunk.rawData.count
                } else if let trackChunk = MIDIFileTrackChunk(data: data) {
                    print("got track chunk")
                    chunks.append(trackChunk)
                    processedBytes += trackChunk.rawData.count
                }
            }
            self.chunks = chunks
        }
    }

    public init(path: String) {
        self.init(url: URL(fileURLWithPath: path))
    }
}
{'fraction_non_alphanumeric': 0.09175004719652634, 'fraction_numerical': 0.008117802529733811, 'mean_word_length': 1.993220338983051, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4930552', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1484, 'n_words': 368}