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

The following examples show how to use javax.sound.midi.MidiMessage#getLength() . 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: IOLoop.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean messagesEqual(MidiMessage m1, MidiMessage m2) {
    if (m1 == null || m2 == null) {
        return false;
    }
    if (m1.getLength() != m2.getLength()) {
        return false;
    }
    byte[] array1 = m1.getMessage();
    byte[] array2 = m2.getMessage();
    return messagesEqual(array1, array2);
}
 
Example 8
Source File: VolumeScalingReceiver.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
/** Determine if the given message is a channel volume change.
 *
 * @return Channel number for which volume is being changed, or -1 if not a
 * channel volume change command.
 */
private int getVolumeChangeChannel(MidiMessage message) {
    if (message.getLength() >= 3) {
        byte[] mBytes = message.getMessage();
        if ((byte) 0xb0 <= mBytes[0] && mBytes[0] < (byte) 0xc0 &&
            mBytes[1] == 7) {
            return mBytes[0] & 15;
        }
    }
    return -1;
}
 
Example 9
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 10
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 11
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;
}