Setup an input queue for MIDI System Exclusive Message input. - Java javax.sound.midi

Java examples for javax.sound.midi:MidiMessage

Description

Setup an input queue for MIDI System Exclusive Message input.

Demo Code

/*//  www  .  ja  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 MidiDevice.Info[] inputMidiDeviceInfo;
    private static MidiUtil.SysexInputQueue[] sysexInputQueue;
    /**
     * Setup an input queue for MIDI System Exclusive Message input.
     * The input queue is shared.  If the input queue is already
     * opened, nothing is done.
     * @see #clearSysexInputQueue
     */
    static void setSysexInputQueue(int port) {
        if (sysexInputQueue[port] != null) // already ready
            return;
        MidiUtil.SysexInputQueue rcvr = new MidiUtil.SysexInputQueue();
        Transmitter trns;
        try {
            trns = getInputMidiDevice(port).getTransmitter();
            trns.setReceiver(rcvr);
            sysexInputQueue[port] = rcvr;
        } catch (MidiUnavailableException e) {
            ErrorMsg.reportStatus(e);
        }
    }
    /**
     * get a Transmitter for Input.
     *
     * @param port an index in an array returned by
     * <code>getInputMidiDeviceInfo()</code>.
     * @return a <code>Transmitter</code> object for MIDI input.
     * @see #getInputMidiDeviceInfo()
     * @see #clearSysexInputQueue
     * @see #getMessage
     */
    static Transmitter getTransmitter(int port) {
        if (inputMidiDeviceInfo.length == 0)
            return null;

        // Transmitter cannot be shared.
        MidiDevice dev = getInputMidiDevice(port);
        try {
            return dev.getTransmitter();
        } catch (MidiUnavailableException e) {
            ErrorMsg.reportStatus(e);
        }
        return null;
    }
    /**
     * get MidiDevice for Input.
     *
     * @param port an index in an array returned by
     * <code>getInputMidiDeviceInfo()</code>.
     * @return a <code>MidiDevice</code> object for MIDI input.  The
     * MidiDevice is already opened.
     * @see #getInputMidiDeviceInfo()
     * @see #clearSysexInputQueue
     * @see #getMessage
     */
    private static MidiDevice getInputMidiDevice(int port) {
        MidiDevice dev = null;
        if (inputMidiDeviceInfo.length == 0)
            return null;
        try {
            dev = MidiSystem.getMidiDevice(inputMidiDeviceInfo[port]);
            if (!dev.isOpen()) {
                ErrorMsg.reportStatus("open inport: "
                        + dev.getDeviceInfo().getName() + ", port: " + port);
                dev.open();
            }
        } catch (MidiUnavailableException e) {
            ErrorMsg.reportStatus(e);
        }
        return dev;
    }
}

Related Tutorials