     
     public let type: Type
     
-    public let time: TimeInterval
-    
-    public init(_ type: Type, time: TimeInterval = 0.0) {
+    public let delay: TimeInterval
+    
+    public init(_ type: Type, delay: TimeInterval = 0.0) {
         self.type = type
-        self.time = time
+        self.delay = delay
     }
 
 }


extension MIDIPacket {
    
    fileprivate init(time: TimeInterval, status: UInt8, channel: UInt8) {
        self.init()
        self.timeStamp = mach_absolute_time() + MIDITimeStamp(time * 1_000_000_000)
        self.length = 1
        self.data.0 = (status << 4) | (channel & 0b00001111)
    }
    
    fileprivate init(time: TimeInterval, status: UInt8, channel: UInt8, data1: UInt8) {
        self.init()
        self.timeStamp = mach_absolute_time() + MIDITimeStamp(time * 1_000_000_000)
        self.length = 2
        self.data.0 = (status << 4) | (channel & 0b00001111)
        self.data.1 = data1 & 0b01111111
    }
    
    fileprivate init(time: TimeInterval, status: UInt8, channel: UInt8, data1: UInt8, data2: UInt8) {
        self.init()
        self.timeStamp = mach_absolute_time() + MIDITimeStamp(time * 1_000_000_000)
        self.length = 3
        self.data.0 = (status << 4) | (channel & 0b00001111)
        self.data.1 = data1 & 0b01111111
        self.data.2 = data2 & 0b01111111
    }
    
    fileprivate var time: TimeInterval {
        return TimeInterval(timeStamp) / 1_000_000_000
    }
    

    internal init(_ packet: MIDIPacket) {
        switch packet.status {
        case 8:
            self.init(.noteOff(channel: packet.channel, key: packet.data1, velocity: packet.data2), delay: packet.delay)
        case 9:
            self.init(.noteOn(channel: packet.channel, key: packet.data1, velocity: packet.data2), delay: packet.delay)
        case 10:
            self.init(.polyphonicKeyPressure(channel: packet.channel, key: packet.data1, pressure: packet.data2), delay: packet.delay)
        case 11:
            switch (packet.data1, packet.data2) {
            case (0...119, _):
                self.init(.controlChange(channel: packet.channel, controller: packet.data1, value: packet.data2), delay: packet.delay)
            case (120, 0):
                self.init(.channelMode(channel: packet.channel, type: .allSoundOff), delay: packet.delay)
            case (121, 0):
                self.init(.channelMode(channel: packet.channel, type: .resetAllControllers), delay: packet.delay)
            case (122, 0):
                self.init(.channelMode(channel: packet.channel, type: .localControlOff), delay: packet.delay)
            case (122, 127):
                self.init(.channelMode(channel: packet.channel, type: .localControlOn), delay: packet.delay)
            case (123, 0):
                self.init(.channelMode(channel: packet.channel, type: .allNotesOff), delay: packet.delay)
            case (124, 0):
                self.init(.channelMode(channel: packet.channel, type: .omniModeOff), delay: packet.delay)
            case (125, 0):
                self.init(.channelMode(channel: packet.channel, type: .omniModeOn), delay: packet.delay)
            case (126, _):
                self.init(.channelMode(channel: packet.channel, type: .monoModeOn(channels: packet.data2)), delay: packet.delay)
            case (127, 0):
                self.init(.channelMode(channel: packet.channel, type: .polyModeOn), delay: packet.delay)
            default:
                self.init(.unknown, delay: packet.delay)
            }
        case 12:
            self.init(.programChange(channel: packet.channel, number: packet.data1), delay: packet.delay)
        case 13:
            self.init(.channelPressure(channel: packet.channel, pressure: packet.data1), delay: packet.delay)
        case 14:
            self.init(.pitchBendChange(channel: packet.channel, leastSignificantBits: packet.data1, mostSignificantBits: packet.data2), delay: packet.delay)
        case 15:
            switch packet.channel {
            case 0:
                self.init(.systemCommon(type: .systemExclusive), delay: packet.delay)
            case 1:
                self.init(.systemCommon(type: .midiTimeCodeQuarterFrame(type: packet.data1 & 0b01110000 >> 4, values: packet.data1 & 0b00001111)), delay: packet.delay)
            case 2:
                self.init(.systemCommon(type: .songPositionPointer(leastSignificantBits: packet.data1, mostSignificantBits: packet.data2)), delay: packet.delay)
            case 3:
                self.init(.systemCommon(type: .songSelect(song: packet.data1)), delay: packet.delay)
            case 5:
                self.init(.systemCommon(type: .tuneRequest), delay: packet.delay)
            case 8:
                self.init(.systemRealTime(type: .timingClock), delay: packet.delay)
            case 10:
                self.init(.systemRealTime(type: .start), delay: packet.delay)
            case 11:
                self.init(.systemRealTime(type: .continue), delay: packet.delay)
            case 12:
                self.init(.systemRealTime(type: .stop), delay: packet.delay)
            case 14:
                self.init(.systemRealTime(type: .activeSensing), delay: packet.delay)
            case 15:
                self.init(.systemRealTime(type: .reset), delay: packet.delay)
            default:
                self.init(.unknown, delay: packet.delay)
            }
        default:
            self.init(.unknown, delay: packet.delay)
        }
    }

