Find the same MidiEvent in track as the argument midiEvent. - Java javax.sound.midi

Java examples for javax.sound.midi:MidiEvent

Description

Find the same MidiEvent in track as the argument midiEvent.

Demo Code


import javax.sound.midi.ShortMessage;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Track;
import javax.sound.midi.Sequence;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MetaMessage;
import java.util.Vector;

public class Main{
    /**//from   www .j a  v  a 2  s .  co m
     * Find the same MidiEvent in track as the argument midiEvent.
     */
    public static MidiEvent findSameEvent(Track track, MidiEvent midiEvent)
            throws Exception {
        for (int i = 0; i < track.size(); i++) {
            MidiEvent trackEvent = track.get(i);

            if (isEqualMidiEvents(trackEvent, midiEvent)) {
                return trackEvent;
            }
        }

        System.out.println("There is no event "
                + DebugMidi.midiEventToString(midiEvent) + " in track: "
                + DebugMidi.trackEventsToString(track));
        throw new Exception();
    }
    /** 
     * Tests it two events are the same event.
     * (Their tick is the same, their note number is the same,
     * and their velocity is the same.)
     * Note: This does not make sure their channel is the same.
     */
    public static boolean isEqualMidiEvents(MidiEvent aEvent,
            MidiEvent bEvent) {
        assert aEvent != null && bEvent != null;
        //assert isNoteOnEvent(aEvent) || isNoteOffEvent(aEvent); 
        //assert isNoteOnEvent(bEvent) || isNoteOffEvent(bEvent);

        if (aEvent.getTick() != bEvent.getTick()) {
            return false;
        }

        MidiMessage a = aEvent.getMessage();
        MidiMessage b = bEvent.getMessage();

        // test if they are note on messages
        if (isNoteOnMessage(a)) {
            if (!isNoteOnMessage(b)) {
                return false;
            } else if (isNoteOnMessage(b)) {
                return getVelocity(aEvent) == getVelocity(bEvent)
                        && getNoteValue(aEvent) == getNoteValue(bEvent);
            }
        }

        // test if they are note off messages
        if (isNoteOffMessage(a)) {
            if (!isNoteOffMessage(b)) {
                return false;
            } else if (isNoteOffMessage(b)) {
                return getNoteValue(aEvent) == getNoteValue(bEvent);
            }
        }

        // test if they are metamessages
        if (a.getMessage()[0] == (byte) 255
                && b.getMessage()[0] == (byte) 255) {
            return java.util.Arrays.equals(a.getMessage(), b.getMessage());
        }

        return false;
    }
    /** 
     * Return true if message is a Note On message.
     */
    public static boolean isNoteOnMessage(MidiMessage message) {
        // The status can be a range of values, depending on what channel it is on.
        // Also, the velocity cannot be zero, otherwise it is a note off message.
        return message.getStatus() >= 144 && message.getStatus() < 160
                && getVelocity(message) > 0;
    }
    /**
     * Return the velocity for a note on or note off event.
     */
    public static int getVelocity(MidiEvent noteOnOff) {
        // I can't check for this because isNoteOnMessage calls getVelocity
        //assert isNoteOnEvent(noteOnOff) || isNoteOffEvent(noteOnOff);

        return getVelocity(noteOnOff.getMessage());
    }
    /**
     * Return the velocity for a note on or off message.
     */
    public static int getVelocity(MidiMessage noteOnOff) {
        // I can't check for this because isNoteOnMessage calls getVelocity
        //assert isNoteOnMessage(noteOnOff) || isNoteOffMessage(noteOnOff);

        return noteOnOff.getMessage()[2];
    }
    /**
     * Return the note value for a note on or note off event.
     */
    public static int getNoteValue(MidiEvent noteOnOff) {
        assert isNoteOnEvent(noteOnOff) || isNoteOffEvent(noteOnOff);

        return getNoteValue(noteOnOff.getMessage());
    }
    /**
     * Return the note value for a note on or off message.
     */
    public static int getNoteValue(MidiMessage noteOnOff) {
        assert isNoteOnMessage(noteOnOff) || isNoteOffMessage(noteOnOff);

        return noteOnOff.getMessage()[1];
    }
    /** 
     * Return true if message is a Note Off message.
     */
    public static boolean isNoteOffMessage(MidiMessage message) {
        // It is a note off event if the status indicates it is a 
        // note off message.  Or, if it is a note on message and
        // the velocity is zero.
        return (message.getStatus() >= 128 && message.getStatus() < 144)
                || (message.getStatus() >= 144 && message.getStatus() < 160 && getVelocity(message) == 0);
    }
    /** 
     * Return true if event is a Note On event.
     */
    public static boolean isNoteOnEvent(MidiEvent event) {
        return isNoteOnMessage(event.getMessage());
    }
    /** 
     * Return true if event is a Note Off event.
     */
    public static boolean isNoteOffEvent(MidiEvent event) {
        return isNoteOffMessage(event.getMessage());
    }
}

Related Tutorials