Dump output MidiMessage msg on the MIDI Monitor Window with port number information. - Java javax.sound.midi

Java examples for javax.sound.midi:MidiMessage

Description

Dump output MidiMessage msg on the MIDI Monitor Window with port number information.

Demo Code

/*//from w w w .  j  av a  2  s.c  om
 * 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{
    /**
     * holds the state if a SysexMessage is completely displayed or
     * shorten to one hexdump line. (Complete Sysex Message)
     */
    private static boolean CSMstate = false;
    /**
     * Dump output MidiMessage <code>msg</code> on the MIDI Monitor
     * Window with port number information.
     *
     * @param port port number
     * @param msg MidiMessage
     */
    public static void logIn(int port, MidiMessage msg) {
        log("Port: " + port + " RECV ", msg);
    }
    public static void logIn(Transmitter trns, MidiMessage msg) {
        log(trns.toString() + " RECV ", msg);
    }
    /**
     * Dump input MIDI <code>sysex</code> byte array one the MIDI
     * Monitor Window with port number information.
     *
     * @param port port number
     * @param sysex byte array
     * @param length length of data to dump
     */
    public static void logIn(int port, byte[] sysex, int length) {
        log(port, " RECV ", sysex, length);
    }
    /**
     * Output string to MIDI Monitor Window.
     *
     * @param s string to be output
     */
    public static void log(String s) {
        Actions.midiMonitorLog(s);
    }
    public static void log(String str, MidiMessage msg) {
        try {
            log(str + midiMessageToString(msg) + "\n");
        } catch (InvalidMidiDataException e) {
            log("InvalidMidiDataException: " + msg + "\n");
        }
    }
    private static void log(int port, String dir, byte[] sysex, int length) {
        if (CSMstate) {
            log("Port: " + port + dir + length + " bytes :\n  "
                    + Utility.hexDump(sysex, 0, length, 16) + "\n");
        } else {
            log("Port: " + port + dir + length + " bytes :\n  "
                    + Utility.hexDumpOneLine(sysex, 0, length, 16) + "\n");
        }
    }
    /**
     * convert <code>MidiMessage</code> into a string.
     *
     * @param m a <code>MidiMessage</code> value
     * @return a <code>String</code> value
     */
    public static String midiMessageToString(MidiMessage m)
            throws InvalidMidiDataException {
        if (m instanceof ShortMessage)
            return (statusString(m) + "\n  " + shortMessageToString((ShortMessage) m));
        else if (m instanceof SysexMessage) {
            if (CSMstate) {
                return ("SysEX:length=" + m.getLength() + "\n  " + sysexMessageToString(
                        (SysexMessage) m, 16));
            } else {
                return ("SysEX:length=" + m.getLength() + "\n  " + sysexMessageToString((SysexMessage) m));
            }
        } else
            throw new InvalidMidiDataException();
    }
    /**
     * Return a <code>String</code> of the name of status byte of a
     * <code>MidiMessage</code>.
     *
     * @param m a <code>MidiMessage</code> value
     * @return a <code>String</code> value
     * @exception InvalidMidiDataException if an error occurs
     */
    public static String statusString(MidiMessage m)
            throws InvalidMidiDataException {
        int c = m.getStatus();
        switch (c < 0xf0 ? c & 0xf0 : c) {
        case 0x80:
            return "Note Off";
        case 0x90:
            return "Note On";
        case 0xa0:
            return "Poly Pressure";
        case 0xb0:
            return "Control Change";
        case 0xc0:
            return "Program Change";
        case 0xd0:
            return "Channel Pressure";
        case 0xe0:
            return "Pitch Bend";
        case 0xf0:
            return "System Exclusive";
        case 0xf1:
            return "MIDI Time Code";
        case 0xf2:
            return "Song Position Pointer";
        case 0xf3:
            return "Song Select";
        case 0xf4:
            return "Undefined";
        case 0xf5:
            return "Undefined";
        case 0xf6:
            return "Tune Request";
            //case 0xf7: return "End of System Exclusive";
        case 0xf7:
            return "Special System Exclusive";
        case 0xf8:
            return "Timing Clock";
        case 0xf9:
            return "Undefined";
        case 0xfa:
            return "Start";
        case 0xfb:
            return "Continue";
        case 0xfc:
            return "Stop";
        case 0xfd:
            return "Undefined";
        case 0xfe:
            return "Active Sensing";
        case 0xff:
            return "System Reset";
        default:
            throw new InvalidMidiDataException();
        }
    }
    /**
     * Convert <code>ShortMessage</code> into a hexa-dump string.
     *
     * @param m a <code>ShortMessage</code> value
     * @return a <code>String</code> value
     * @exception InvalidMidiDataException if an error occurs
     */
    public static String shortMessageToString(ShortMessage m)
            throws InvalidMidiDataException {
        int c = m.getStatus();
        switch (c < 0xf0 ? c & 0xf0 : c) {
        case 0x80:
        case 0x90:
        case 0xa0:
        case 0xb0:
        case 0xe0:
        case 0xf2:
            return (hex(c) + " " + hex(m.getData1()) + " " + hex(m
                    .getData2()));
        case 0xc0:
        case 0xd0:
        case 0xf1:
        case 0xf3:
            return (hex(c) + " " + hex(m.getData1()));
        case 0xf4:
        case 0xf5:
        case 0xf6:
        case 0xf7:
        case 0xf8:
        case 0xf9:
        case 0xfa:
        case 0xfb:
        case 0xfc:
        case 0xfd:
        case 0xfe:
        case 0xff:
            return (hex(c));
        default:
            throw new InvalidMidiDataException();
        }
    }
    /**
     * Convert <code>SysexMessage</code> into a hexa-dump string.
     *
     * @param m a <code>SysexMessage</code> value
     * @param bytes number of bytes per line.  If equal or less than
     * 0, no newlines are inserted.
     * @return a <code>String</code> value
     */
    public static String sysexMessageToString(SysexMessage m, int bytes) {
        byte[] d = m.getMessage();
        return Utility.hexDump(d, 0, -1, bytes);
    }
    /**
     * Convert <code>SysexMessage</code> into a hexa-dump string.  If
     * the length is longer than 16bytes, bytes of middle of the array
     * are not ignored.
     *
     * @param m a <code>SysexMessage</code> value
     * @return a <code>String</code> value
     * @exception InvalidMidiDataException if an error occurs
     */
    public static String sysexMessageToString(SysexMessage m)
            throws InvalidMidiDataException {
        byte[] d = m.getMessage();
        return Utility.hexDumpOneLine(d, 0, -1, 16);
    }
}

Related Tutorials