Example usage for javax.sound.midi MidiEvent getTick

List of usage examples for javax.sound.midi MidiEvent getTick

Introduction

In this page you can find the example usage for javax.sound.midi MidiEvent getTick.

Prototype

public long getTick() 

Source Link

Document

Obtains the time-stamp for the event, in MIDI ticks.

Usage

From source file:at.ofai.music.util.WormFileParseException.java

public static EventList readMidiFile(String fileName, int skipTrackFlag) {
    EventList list = new EventList();
    Sequence s;//from   w w w . jav a2s. co m
    try {
        s = MidiSystem.getSequence(new File(fileName));
    } catch (Exception e) {
        e.printStackTrace();
        return list;
    }
    double midiTempo = 500000;
    double tempoFactor = midiTempo / s.getResolution() / 1000000.0;
    // System.err.println(tempoFactor);
    Event[][] noteOns = new Event[128][16];
    Track[] tracks = s.getTracks();
    for (int t = 0; t < tracks.length; t++, skipTrackFlag >>= 1) {
        if ((skipTrackFlag & 1) == 1)
            continue;
        for (int e = 0; e < tracks[t].size(); e++) {
            MidiEvent me = tracks[t].get(e);
            MidiMessage mm = me.getMessage();
            double time = me.getTick() * tempoFactor;
            byte[] mesg = mm.getMessage();
            int channel = mesg[0] & 0x0F;
            int command = mesg[0] & 0xF0;
            if (command == ShortMessage.NOTE_ON) {
                int pitch = mesg[1] & 0x7F;
                int velocity = mesg[2] & 0x7F;
                if (noteOns[pitch][channel] != null) {
                    if (velocity == 0) { // NOTE_OFF in disguise :(
                        noteOns[pitch][channel].keyUp = time;
                        noteOns[pitch][channel].pedalUp = time;
                        noteOns[pitch][channel] = null;
                    } else
                        System.err.println("Double note on: n=" + pitch + " c=" + channel + " t1="
                                + noteOns[pitch][channel] + " t2=" + time);
                } else {
                    Event n = new Event(time, 0, 0, pitch, velocity, -1, -1, 0, ShortMessage.NOTE_ON, channel,
                            t);
                    noteOns[pitch][channel] = n;
                    list.add(n);
                }
            } else if (command == ShortMessage.NOTE_OFF) {
                int pitch = mesg[1] & 0x7F;
                noteOns[pitch][channel].keyUp = time;
                noteOns[pitch][channel].pedalUp = time;
                noteOns[pitch][channel] = null;
            } else if (command == 0xF0) {
                if ((channel == 0x0F) && (mesg[1] == 0x51)) {
                    midiTempo = (mesg[5] & 0xFF) | ((mesg[4] & 0xFF) << 8) | ((mesg[3] & 0xFF) << 16);
                    tempoFactor = midiTempo / s.getResolution() / 1000000.0;
                    //   System.err.println("Info: Tempo change: " + midiTempo +
                    //                  "  tf=" + tempoFactor);
                }
            } else if (mesg.length > 3) {
                System.err.println("midi message too long: " + mesg.length);
                System.err.println("\tFirst byte: " + mesg[0]);
            } else {
                int b0 = mesg[0] & 0xFF;
                int b1 = -1;
                int b2 = -1;
                if (mesg.length > 1)
                    b1 = mesg[1] & 0xFF;
                if (mesg.length > 2)
                    b2 = mesg[2] & 0xFF;
                list.add(new Event(time, time, -1, b1, b2, -1, -1, 0, b0 & 0xF0, b0 & 0x0F, t));
            }
        }
    }
    for (int pitch = 0; pitch < 128; pitch++)
        for (int channel = 0; channel < 16; channel++)
            if (noteOns[pitch][channel] != null)
                System.err.println("Missing note off: n=" + noteOns[pitch][channel].midiPitch + " t="
                        + noteOns[pitch][channel].keyDown);
    return list;
}