Example usage for javax.sound.midi MidiMessage getMessage

List of usage examples for javax.sound.midi MidiMessage getMessage

Introduction

In this page you can find the example usage for javax.sound.midi MidiMessage getMessage.

Prototype

public byte[] getMessage() 

Source Link

Document

Obtains the MIDI message data.

Usage

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

public static EventList readMidiFile(String fileName, int skipTrackFlag) {
    EventList list = new EventList();
    Sequence s;// ww  w .  java2 s.  c o  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;
}

From source file:de.ailis.midi4js.MessageReceiver.java

/**
 * @see javax.sound.midi.Receiver#send(javax.sound.midi.MidiMessage, long)
 *//*from  w  ww .j  a  v a 2s.  c om*/
@Override
public void send(final MidiMessage message, final long timeStamp) {
    try {
        final JSONStringer json = new JSONStringer();
        json.object();
        json.key("status").value(message.getStatus());
        json.key("message");
        json.array();
        final byte[] data = message.getMessage();
        final int max = Math.min(data.length, message.getLength());
        for (int i = 0; i < max; i++)
            json.value(data[i] & 0xff);
        json.endArray();
        if (message instanceof ShortMessage)
            processShortMessage((ShortMessage) message, json);
        else if (message instanceof MetaMessage)
            processMetaMessage((MetaMessage) message, json);
        else if (message instanceof SysexMessage)
            processSysexMessage((SysexMessage) message, json);
        json.endObject();
        this.applet.execJSMethod("messageReceived", System.identityHashCode(this), json.toString(), timeStamp);
    } catch (final JSONException e) {
        throw new RuntimeException(e.toString(), e);
    }
}

From source file:com.rockhoppertech.music.midi.js.MIDIEvent.java

/**
 * Creates a new <code>MIDIEvent</code> instance from JavaSound's MidiEvent.
 * /*from  w  w w .  j  av  a 2  s . c o m*/
 * @param e
 *            a <code>MidiEvent</code> object.
 */
public MIDIEvent(MidiEvent e) {
    tick = e.getTick();
    startBeat = (double) (tick) / (double) (division);
    MidiMessage mm = e.getMessage();
    byte[] ba = mm.getMessage();
    logger.debug("Original message: " + ba.length);
    for (byte element : ba) {
        logger.debug(Integer.toHexString(element & 0xf0) + " ");
    }

    if (mm instanceof ShortMessage) {
        logger.debug("Is short message");
        ShortMessage se = (ShortMessage) mm;
        bytes = new byte[2];
        bytes[0] = (byte) se.getData1();
        bytes[1] = (byte) se.getData2();
    } else if (mm instanceof SysexMessage) {
        logger.debug("is sysex message");
        SysexMessage sex = (SysexMessage) mm;
        bytes = sex.getData();
    } else if (mm instanceof MetaMessage) {
        logger.debug("is meta message");
        MetaMessage meta = (MetaMessage) mm;
        bytes = meta.getData();
    }

    status = mm.getStatus();
    logger.debug("Status: " + Integer.toHexString(status));
    if (mm instanceof MetaMessage) {
        metaMessageType = ((MetaMessage) mm).getType();
    }

    logger.debug("Constructed: " + toString());
}