/*
 * Copyright (c) 2007-2011 Madhav Vaidyanathan
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */


package com.jinmingyunle.midiplaylib.midifile;

import java.util.Comparator;

/** @class MidiEvent
 * 事件中的一个MidiEvent表示单个事件(如EventNoteOn)
 * Midi文件。 它包括事件的时间增量
 *
 * A MidiEvent represents a single event (such as EventNoteOn) in the
 * Midi file. It includes the delta time of the event.
 */
public class MidiEvent implements Comparator<MidiEvent> {

    public int    DeltaTime;     /** The time between the previous event and this on 上次事件和这次事件之间的时间  */
    public int    StartTime;     /** The absolute time this event occurs 事件发生的绝对时间 */
    public boolean HasEventflag; /** False if this is using the previous eventflag  如果使用的是前一个事件标志，则为False*/
    public byte   EventFlag;     /** NoteOn, NoteOff, etc.  Full list is in class MidiFile 件NoteOn, NoteOff等等。 完整的列表在midfile类中*/
    public byte   Channel;       /** The channel this event occurs on  发生此事件的频道*/

    public byte   Notenumber;    /** The note number 音符数量 */
    public byte   Velocity;      /** The volume of the note 音量*/
    public byte   Instrument;    /** The instrument 乐器 */
    public byte   KeyPressure;   /** The key pressure 压力值*/
    public byte   ChanPressure;  /** The channel pressure 压力;频道 */
    public byte   ControlNum;    /** The controller number 控制器 编号*/
    public byte   ControlValue;  /** The controller value 控制器值 */
    public short PitchBend;      /** The pitch bend value 音高的滑动 */
    public byte   Numerator;     /** The numerator分子, for TimeSignature拍号 meta events(重大事件) */
    public byte   Denominator;   /** The denominator分母, for TimeSignature meta events */
    public int    Tempo;         /** The tempo节奏, for Tempo meta events */
    public byte   Metaevent;     /** The metaevent事件, used if eventflag is MetaEvent */
    public int    Metalength;    /** The metaevent length  */
    public byte[] Value;         /** The raw 原始 byte value, for Sysex and meta events */

    public MidiEvent() {
    }

    /** Return a copy of this event */
    public MidiEvent Clone() {
        MidiEvent mevent= new MidiEvent();
        mevent.DeltaTime = DeltaTime;
        mevent.StartTime = StartTime;
        mevent.HasEventflag = HasEventflag;
        mevent.EventFlag = EventFlag;
        mevent.Channel = Channel;
        mevent.Notenumber = Notenumber;
        mevent.Velocity = Velocity;
        mevent.Instrument = Instrument;
        mevent.KeyPressure = KeyPressure;
        mevent.ChanPressure = ChanPressure;
        mevent.ControlNum = ControlNum;
        mevent.ControlValue = ControlValue;
        mevent.PitchBend = PitchBend;
        mevent.Numerator = Numerator;
        mevent.Denominator = Denominator;
        mevent.Tempo = Tempo;
        mevent.Metaevent = Metaevent;
        mevent.Metalength = Metalength;
        mevent.Value = Value;
        return mevent;
    }

    /** Compare two MidiEvents based on their start times. */
    public int compare(MidiEvent x, MidiEvent y) {
        if (x.StartTime == y.StartTime) {
            if (x.EventFlag == y.EventFlag) {
                return x.Notenumber - y.Notenumber;
            }
            else {
                return x.EventFlag - y.EventFlag;
            }
        }
        else {
            return x.StartTime - y.StartTime;
        }
    }

}


