javax.sound.midi.SysexMessage Java Examples

The following examples show how to use javax.sound.midi.SysexMessage. 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: DavidMusicModule.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
private static void sendSysexMessage(Receiver receiver, byte... message) {
 SysexMessage msg = new SysexMessage();
 try {
        msg.setMessage(message, message.length);
    } catch (InvalidMidiDataException ex) {
        throw new RuntimeException(ex);
    }
    receiver.send(msg, -1);
}
 
Example #2
Source File: FinnwMusicModule.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
private void addSysExMessage(int status, byte... data) {
    try {
        SysexMessage msg = new SysexMessage();
        msg.setMessage(status, data, data.length);
        messages.add(msg);
    } catch (InvalidMidiDataException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #3
Source File: StandardMidiFileReader.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
void readTrack(Track track) throws IOException, InvalidMidiDataException {
    try {
        // reset current tick to 0
        long tick = 0;

        // reset current status byte to 0 (invalid value).
        // this should cause us to throw an InvalidMidiDataException if we don't
        // get a valid status byte from the beginning of the track.
        int status = 0;
        boolean endOfTrackFound = false;

        while (!trackFinished() && !endOfTrackFound) {
            MidiMessage message;

            int data1 = -1;         // initialize to invalid value
            int data2 = 0;

            // each event has a tick delay and then the event data.

            // first read the delay (a variable-length int) and update our tick value
            tick += readVarInt();

            // check for new status
            int byteValue = readUnsigned();

            if (byteValue >= 0x80) {
                status = byteValue;
            } else {
                data1 = byteValue;
            }

            switch (status & 0xF0) {
            case 0x80:
            case 0x90:
            case 0xA0:
            case 0xB0:
            case 0xE0:
                // two data bytes
                if (data1 == -1) {
                    data1 = readUnsigned();
                }
                data2 = readUnsigned();
                message = new FastShortMessage(status | (data1 << 8) | (data2 << 16));
                break;
            case 0xC0:
            case 0xD0:
                // one data byte
                if (data1 == -1) {
                    data1 = readUnsigned();
                }
                message = new FastShortMessage(status | (data1 << 8));
                break;
            case 0xF0:
                // sys-ex or meta
                switch(status) {
                case 0xF0:
                case 0xF7:
                    // sys ex
                    int sysexLength = (int) readVarInt();
                    byte[] sysexData = new byte[sysexLength];
                    read(sysexData);

                    SysexMessage sysexMessage = new SysexMessage();
                    sysexMessage.setMessage(status, sysexData, sysexLength);
                    message = sysexMessage;
                    break;

                case 0xFF:
                    // meta
                    int metaType = readUnsigned();
                    int metaLength = (int) readVarInt();

                    byte[] metaData = new byte[metaLength];
                    read(metaData);

                    MetaMessage metaMessage = new MetaMessage();
                    metaMessage.setMessage(metaType, metaData, metaLength);
                    message = metaMessage;
                    if (metaType == 0x2F) {
                        // end of track means it!
                        endOfTrackFound = true;
                    }
                    break;
                default:
                    throw new InvalidMidiDataException("Invalid status byte: " + status);
                } // switch sys-ex or meta
                break;
            default:
                throw new InvalidMidiDataException("Invalid status byte: " + status);
            } // switch
            track.add(new MidiEvent(message, tick));
        } // while
    } catch (ArrayIndexOutOfBoundsException e) {
        if (DEBUG) e.printStackTrace();
        // fix for 4834374
        throw new EOFException("invalid MIDI file");
    }
}
 
Example #4
Source File: StandardMidiFileReader.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
void readTrack(Track track) throws IOException, InvalidMidiDataException {
    try {
        // reset current tick to 0
        long tick = 0;

        // reset current status byte to 0 (invalid value).
        // this should cause us to throw an InvalidMidiDataException if we don't
        // get a valid status byte from the beginning of the track.
        int status = 0;
        boolean endOfTrackFound = false;

        while (!trackFinished() && !endOfTrackFound) {
            MidiMessage message;

            int data1 = -1;         // initialize to invalid value
            int data2 = 0;

            // each event has a tick delay and then the event data.

            // first read the delay (a variable-length int) and update our tick value
            tick += readVarInt();

            // check for new status
            int byteValue = readUnsigned();

            if (byteValue >= 0x80) {
                status = byteValue;
            } else {
                data1 = byteValue;
            }

            switch (status & 0xF0) {
            case 0x80:
            case 0x90:
            case 0xA0:
            case 0xB0:
            case 0xE0:
                // two data bytes
                if (data1 == -1) {
                    data1 = readUnsigned();
                }
                data2 = readUnsigned();
                message = new FastShortMessage(status | (data1 << 8) | (data2 << 16));
                break;
            case 0xC0:
            case 0xD0:
                // one data byte
                if (data1 == -1) {
                    data1 = readUnsigned();
                }
                message = new FastShortMessage(status | (data1 << 8));
                break;
            case 0xF0:
                // sys-ex or meta
                switch(status) {
                case 0xF0:
                case 0xF7:
                    // sys ex
                    int sysexLength = (int) readVarInt();
                    byte[] sysexData = new byte[sysexLength];
                    read(sysexData);

                    SysexMessage sysexMessage = new SysexMessage();
                    sysexMessage.setMessage(status, sysexData, sysexLength);
                    message = sysexMessage;
                    break;

                case 0xFF:
                    // meta
                    int metaType = readUnsigned();
                    int metaLength = (int) readVarInt();

                    byte[] metaData = new byte[metaLength];
                    read(metaData);

                    MetaMessage metaMessage = new MetaMessage();
                    metaMessage.setMessage(metaType, metaData, metaLength);
                    message = metaMessage;
                    if (metaType == 0x2F) {
                        // end of track means it!
                        endOfTrackFound = true;
                    }
                    break;
                default:
                    throw new InvalidMidiDataException("Invalid status byte: " + status);
                } // switch sys-ex or meta
                break;
            default:
                throw new InvalidMidiDataException("Invalid status byte: " + status);
            } // switch
            track.add(new MidiEvent(message, tick));
        } // while
    } catch (ArrayIndexOutOfBoundsException e) {
        if (DEBUG) e.printStackTrace();
        // fix for 4834374
        throw new EOFException("invalid MIDI file");
    }
}
 
Example #5
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 #6
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;
}