Java Code Examples for javax.sound.midi.MidiMessage#getStatus()

The following examples show how to use javax.sound.midi.MidiMessage#getStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MidiUtils.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/** parses this message for a META tempo message and returns
 * the tempo in MPQ, or -1 if this isn't a tempo message
 */
public static int getTempoMPQ(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 6
        || midiMsg.getStatus() != MetaMessage.META) {
        return -1;
    }
    byte[] msg = midiMsg.getMessage();
    if (((msg[1] & 0xFF) != META_TEMPO_TYPE) || (msg[2] != 3)) {
        return -1;
    }
    int tempo =    (msg[5] & 0xFF)
                | ((msg[4] & 0xFF) << 8)
                | ((msg[3] & 0xFF) << 16);
    return tempo;
}
 
Example 2
Source File: MidiUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** parses this message for a META tempo message and returns
 * the tempo in MPQ, or -1 if this isn't a tempo message
 */
public static int getTempoMPQ(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 6
        || midiMsg.getStatus() != MetaMessage.META) {
        return -1;
    }
    byte[] msg = midiMsg.getMessage();
    if (((msg[1] & 0xFF) != META_TEMPO_TYPE) || (msg[2] != 3)) {
        return -1;
    }
    int tempo =    (msg[5] & 0xFF)
                | ((msg[4] & 0xFF) << 8)
                | ((msg[3] & 0xFF) << 16);
    return tempo;
}
 
Example 3
Source File: MidiUtils.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** return true if the passed message is Meta End Of Track */
public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 3
        || midiMsg.getStatus() != MetaMessage.META) {
        return false;
    }
    // now get message and check for end of track
    byte[] msg = midiMsg.getMessage();
    return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0);
}
 
Example 4
Source File: MidiUtils.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** return if the given message is a meta tempo message */
public static boolean isMetaTempo(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 6
        || midiMsg.getStatus() != MetaMessage.META) {
        return false;
    }
    // now get message and check for tempo
    byte[] msg = midiMsg.getMessage();
    // meta type must be 0x51, and data length must be 3
    return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3);
}
 
Example 5
Source File: MidiUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** return true if the passed message is Meta End Of Track */
public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 3
        || midiMsg.getStatus() != MetaMessage.META) {
        return false;
    }
    // now get message and check for end of track
    byte[] msg = midiMsg.getMessage();
    return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0);
}
 
Example 6
Source File: MidiUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** return if the given message is a meta tempo message */
public static boolean isMetaTempo(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 6
        || midiMsg.getStatus() != MetaMessage.META) {
        return false;
    }
    // now get message and check for tempo
    byte[] msg = midiMsg.getMessage();
    // meta type must be 0x51, and data length must be 3
    return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3);
}
 
Example 7
Source File: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * chase all events from beginning of Track
 * and send note off for those events that are active
 * in noteOnCache array.
 * It is possible, of course, to catch notes from other tracks,
 * but better than more complicated logic to detect
 * which notes are really from this track
 */
private void sendNoteOffIfOn(Track track, long endTick) {
    int size = track.size();
    int done = 0;
    try {
        for (int i = 0; i < size; i++) {
            MidiEvent event = track.get(i);
            if (event.getTick() > endTick) break;
            MidiMessage msg = event.getMessage();
            int status = msg.getStatus();
            int len = msg.getLength();
            if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) {
                int note = -1;
                if (msg instanceof ShortMessage) {
                    ShortMessage smsg = (ShortMessage) msg;
                    if (smsg.getData2() > 0) {
                        // only consider Note On with velocity > 0
                        note = smsg.getData1();
                    }
                } else {
                    byte[] data = msg.getMessage();
                    if ((data[2] & 0x7F) > 0) {
                        // only consider Note On with velocity > 0
                        note = data[1] & 0x7F;
                    }
                }
                if (note >= 0) {
                    int bit = 1<<(status & 0x0F);
                    if ((noteOnCache[note] & bit) != 0) {
                        // the bit is set. Send Note Off
                        getTransmitterList().sendMessage(status | (note<<8), -1);
                        // clear the bit
                        noteOnCache[note] &= (0xFFFF ^ bit);
                        done++;
                    }
                }
            }
        }
    } catch (ArrayIndexOutOfBoundsException aioobe) {
        // this happens when messages are removed
        // from the track while this method executes
    }
}
 
Example 8
Source File: RealTimeSequencer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * chase all events from beginning of Track
 * and send note off for those events that are active
 * in noteOnCache array.
 * It is possible, of course, to catch notes from other tracks,
 * but better than more complicated logic to detect
 * which notes are really from this track
 */
private void sendNoteOffIfOn(Track track, long endTick) {
    int size = track.size();
    int done = 0;
    try {
        for (int i = 0; i < size; i++) {
            MidiEvent event = track.get(i);
            if (event.getTick() > endTick) break;
            MidiMessage msg = event.getMessage();
            int status = msg.getStatus();
            int len = msg.getLength();
            if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) {
                int note = -1;
                if (msg instanceof ShortMessage) {
                    ShortMessage smsg = (ShortMessage) msg;
                    if (smsg.getData2() > 0) {
                        // only consider Note On with velocity > 0
                        note = smsg.getData1();
                    }
                } else {
                    byte[] data = msg.getMessage();
                    if ((data[2] & 0x7F) > 0) {
                        // only consider Note On with velocity > 0
                        note = data[1] & 0x7F;
                    }
                }
                if (note >= 0) {
                    int bit = 1<<(status & 0x0F);
                    if ((noteOnCache[note] & bit) != 0) {
                        // the bit is set. Send Note Off
                        getTransmitterList().sendMessage(status | (note<<8), -1);
                        // clear the bit
                        noteOnCache[note] &= (0xFFFF ^ bit);
                        done++;
                    }
                }
            }
        }
    } catch (ArrayIndexOutOfBoundsException aioobe) {
        // this happens when messages are removed
        // from the track while this method executes
    }
    if (DEBUG_PUMP) Printer.println("  sendNoteOffIfOn: sent "+done+" messages.");
}
 
Example 9
Source File: MidiCommunication.java    From jmg with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The method required by the recieve interface - It has a stupid name so we
 * pass data for short messages to handleMidiInput (a better name).
 */
// This method is based on test code in Plumbstone - OSX Core MIDI package.
public void send (MidiMessage message, long timeStamp) {
    // Get the message bytes
    byte [] m = message.getMessage ();
    int status = message.getStatus();
    // Is it a short message ?
    if (message instanceof ShortMessage) {
        // Get command and channel data
        int type = (m [0] & 0xFF) >> 4;
        int channel = m [0] & 0xF;
        int data1 = m[1];
        int data2 = -1;
        if (m.length > 2) {
            data2 = m[2];
        }
        
        // Is it a channel message?
        if (type != 15) {
            handleMidiInput(status - channel, channel, data1, data2);
        }
        else {
            // System message - some need special handling due to vast quanitity
            if (status == ShortMessage.TIMING_CLOCK) {
                System.out.print ("MIDI Clock message");
            }
            else if (status == ShortMessage.ACTIVE_SENSING) {
                System.out.print ("MIDI Active sensing message");
            }
            else {
                // Some other system message - just print status byte
                System.out.print ("A non-identified MIDI system message " + status);
            }
        }
    }
    
    // System exclusive message?
    else if (message instanceof SysexMessage) {
        // Print out sysex message
        System.out.println ();
        System.out.print ("Sysex MIDI message <<");
        for (int i=0; i<m.length; i++) {
            System.out.print (" " + m [i]);
        }
        System.out.println (">>");
    }
    
    // Meta message (probably shouldn't ever get this)
    else if (message instanceof MetaMessage) {
        // Print out meta message
        System.out.println ();
        System.out.print ("Meta MIDI Message {");
        for (int i=0; i<m.length; i++) {
            System.out.print (" " + m [i]);
        }
        System.out.println ("}");
    }
    
    // Uncast message
    else {
        System.out.println ("Unknown MIDI message [");
        for (int i=0; i<m.length; i++) {
            System.out.print (" " + m [i]);
        }
        System.out.println ("]");
    }
}
 
Example 10
Source File: IOLoop.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
private static boolean testMessage(MidiMessage message) {
    receivedMessage = null;
    baos = new ByteArrayOutputStream();
    expectedBytes = message.getLength();
    receivedBytes = 0;
    System.out.print("Sending message " + getMessageString(message.getMessage())+"...");
    receiver.send(message, -1);
    /* sending 3 bytes can roughly be done in 1 millisecond,
     * so this estimate waits at max 3 times longer than the message takes,
     * plus a little offset to allow the MIDI subsystem some processing time
     */
    int offset = 300; // standard offset 100 millis
    if (message instanceof SysexMessage) {
        // add a little processing time to sysex messages
        offset += 1000;
    }
    if (receivedBytes < expectedBytes) {
        sleep(expectedBytes + offset);
    }
    boolean equal;
    byte[] data = baos.toByteArray();
    if (data.length > 0) {
        equal = messagesEqual(message.getMessage(), data);
    } else {
        equal = messagesEqual(message, receivedMessage);
        if (receivedMessage != null) {
            data = receivedMessage.getMessage();
        } else {
            data = null;
        }
    }
    if (!equal) {
        if ((message.getStatus() & 0xF0) == ShortMessage.PITCH_BEND) {
            out("NOT failed (may expose a bug in ALSA)");
            equal = true;
            sleep(100);
        }
        if ((message.getStatus() == 0xF6) && (message.getLength() == 1)) {
            out("NOT failed (may expose an issue on Solaris)");
            equal = true;
            sleep(100);
        }
        else if ((message.getStatus()) == 0xF0 && message.getLength() < 4) {
            out("NOT failed (not a correct sys ex message)");
            equal = true;
            sleep(200);
        } else {
            out("FAILED:");
            out("  received as " + getMessageString(data));
        }
    } else {
        System.out.println("OK");
    }
    return equal;
}