Converts a byte array into an array of MIDI SysexMessages. - Java javax.sound.midi

Java examples for javax.sound.midi:MidiMessage

Description

Converts a byte array into an array of MIDI SysexMessages.

Demo Code

/*/*ww  w.j  a  v  a2 s. co  m*/
 * Copyright 2004 Hiroo Hayashi
 *
 * This file is part of JSynthLib.
 *
 * JSynthLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or(at your option) any later version.
 *
 * JSynthLib is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JSynthLib; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
//package com.java2s;

import javax.sound.midi.*;
import java.util.*;

public class Main {
    /**
     * Converts a byte array into an array of SysexMessages.  Each
     * SysexMessage must be terminated by END_OF_EXCLUSIVE.<p>
     *
     * This method is provided to keep compatibility with the old MIDI
     * layer which handled MIDI data in byte array.  It is more
     * efficient to create SysexMessages directly because a synth
     * driver knows the start index and length of each Sysex data in
     * an array.
     */
    public static SysexMessage[] byteArrayToSysexMessages(byte[] d)
            throws InvalidMidiDataException {
        ArrayList list = new ArrayList();

        for (int i = 0; i < d.length; i++) {
            if ((d[i] & 0xFF) == SysexMessage.SYSTEM_EXCLUSIVE) {
                int j;
                // let cause exception if there is no END_OF_EXCLUSIVE
                for (j = i + 1; j < d.length
                        && (d[j] & 0xff) != ShortMessage.END_OF_EXCLUSIVE; j++)
                    ;
                if (j == d.length)
                    throw new InvalidMidiDataException("Missing EOX");
                // here d[j] is EOX.
                int l = j - i + 1;
                byte[] b = new byte[l];
                System.arraycopy(d, i, b, 0, l);
                SysexMessage m = new SysexMessage();
                m.setMessage(b, l);
                list.add(m);
                i = j;
            }
        }
        return (SysexMessage[]) list.toArray(new SysexMessage[0]);
    }
}

Related Tutorials