Convert an array of MIDI SysexMessage to a byte array. - Java javax.sound.midi

Java examples for javax.sound.midi:MidiMessage

Description

Convert an array of MIDI SysexMessage to a byte array.

Demo Code

/*//from www.  j  a  v  a 2 s .  c o 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
 */
import java.io.File;
import javax.sound.midi.*;
import java.util.*;

public class Main{
    private static MidiUtil.SysexInputQueue[] sysexInputQueue;
    /**
     * Convert an array of SysexMessage to a byte array.
     * @param msgs an array of SysexMessage.
     * @return byte array of System Exclusive data.
     */
    public static byte[] sysexMessagesToByteArray(SysexMessage[] msgs) {
        int totalSize = 0;
        for (int i = 0; i < msgs.length; i++)
            totalSize += msgs[i].getLength();
        byte[] sysex = new byte[totalSize];
        for (int size, ofst = 0, i = 0; i < msgs.length; ofst += size, i++) {
            size = msgs[i].getLength();
            byte[] d = msgs[i].getMessage();
            System.arraycopy(d, 0, sysex, ofst, size);
        }
        return sysex;
    }
    /**
     * get Sysex Message from MIDI input queue.
     * @see #getInputMidiDeviceInfo()
     * @see #clearSysexInputQueue
     */
    static MidiMessage getMessage(int port, long timeout)
            throws MidiUtil.TimeoutException, InvalidMidiDataException {
        return sysexInputQueue[port].getMessage(timeout);
    }
}

Related Tutorials