start MIDI Sequencer - Java javax.sound.midi

Java examples for javax.sound.midi:Sequence

Description

start MIDI Sequencer

Demo Code

/*//from  w ww  . jav  a2  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[] outputMidiDeviceInfo;
    private static MidiDevice.Info[] inputMidiDeviceInfo;
    private static Receiver[] midiOutRcvr;
    public static void startSequencer(int myport) {
        Sequencer sequencer;
        MidiDevice outPort;
        Transmitter seqTrans;
        Receiver outRcvr;

        try {
            sequencer = MidiSystem.getSequencer();

            seqTrans = sequencer.getTransmitter();
            outPort = getOutputMidiDevice(myport);
            outRcvr = outPort.getReceiver();
            seqTrans.setReceiver(outRcvr);

            sequencer.open();
        } catch (MidiUnavailableException me) {
            ErrorMsg.reportWarning("MidiSystem Error",
                    "Can't access sequencer properly", me);
            return;
        }

        try {
            File myMidiFile = new File(AppConfig.getSequencePath());
            Sequence mySeq = MidiSystem.getSequence(myMidiFile);
            sequencer.setSequence(mySeq);
        } catch (Exception e) {
            ErrorMsg.reportWarning("MidiSystem Error",
                    "Can't access MIDI file for sequencer", e);
            return;
        }

        sequencer.start();
    }
    /**
     * 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 Output.
     *
     * @param port an index in an array returned by
     * <code>getOutputMidiDeviceInfo()</code>.
     * @return a <code>MidiDevice</code> object for MIDI output.  The
     * MidiDevice is already opened.
     * @see #getOutputMidiDeviceInfo()
     * @see #getReceiver
     * @see #send
     */
    private static MidiDevice getOutputMidiDevice(int port)
            throws MidiUnavailableException {
        if (outputMidiDeviceInfo.length == 0)
            return null;
        MidiDevice dev = MidiSystem
                .getMidiDevice(outputMidiDeviceInfo[port]);
        if (!dev.isOpen()) {
            ErrorMsg.reportStatus("open outport: "
                    + dev.getDeviceInfo().getName() + ", port: " + port);
            dev.open();
        }
        return dev;
    }
    /**
     * get a Receiver for Output.<p>
     *
     * Don't close Receiver returned since it may be shared with others.
     *
     * @param port an index in an array returned by
     * <code>getOutputMidiDeviceInfo()</code>.
     * @return a <code>Receiver</code> object for MIDI output.
     * @see #getOutputMidiDeviceInfo()
     * @see #send
     * @throws MidiUnavailableException
     */
    static Receiver getReceiver(int port) throws MidiUnavailableException {
        if (outputMidiDeviceInfo.length == 0)
            return null;

        if (midiOutRcvr[port] != null)
            return midiOutRcvr[port];

        MidiDevice dev = getOutputMidiDevice(port);
        Receiver r = dev.getReceiver();
        midiOutRcvr[port] = r;
        return r;
    }
    /**
     * 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